#include "config.h"
#include "CCDamageTracker.h"
#include "CCGeometryTestUtils.h"
#include "CCLayerImpl.h"
#include "CCLayerSorter.h"
#include "CCLayerTreeHostCommon.h"
#include "CCMathUtil.h"
#include "CCSingleThreadProxy.h"
#include <gtest/gtest.h>
#include <public/WebFilterOperation.h>
#include <public/WebFilterOperations.h>
using namespace cc;
using namespace WebKit;
using namespace WTF;
using namespace WebKitTests;
namespace {
void executeCalculateDrawTransformsAndVisibility(CCLayerImpl* root, Vector<CCLayerImpl*>& renderSurfaceLayerList)
{
CCLayerSorter layerSorter;
int dummyMaxTextureSize = 512;
ASSERT_TRUE(root->renderSurface());
ASSERT_FALSE(renderSurfaceLayerList.size());
CCLayerTreeHostCommon::calculateDrawTransforms(root, root->bounds(), 1, &layerSorter, dummyMaxTextureSize, renderSurfaceLayerList);
CCLayerTreeHostCommon::calculateVisibleRects(renderSurfaceLayerList);
}
void clearDamageForAllSurfaces(CCLayerImpl* layer)
{
if (layer->renderSurface())
layer->renderSurface()->damageTracker()->didDrawDamagedArea();
for (size_t i = 0; i < layer->children().size(); ++i)
clearDamageForAllSurfaces(layer->children()[i]);
}
void emulateDrawingOneFrame(CCLayerImpl* root)
{
Vector<CCLayerImpl*> renderSurfaceLayerList;
executeCalculateDrawTransformsAndVisibility(root, renderSurfaceLayerList);
for (int i = renderSurfaceLayerList.size() - 1; i >= 0; --i) {
CCRenderSurface* targetSurface = renderSurfaceLayerList[i]->renderSurface();
targetSurface->damageTracker()->updateDamageTrackingState(targetSurface->layerList(), targetSurface->owningLayerId(), targetSurface->surfacePropertyChangedOnlyFromDescendant(), targetSurface->contentRect(), renderSurfaceLayerList[i]->maskLayer(), renderSurfaceLayerList[i]->filters());
}
root->resetAllChangeTrackingForSubtree();
}
PassOwnPtr<CCLayerImpl> createTestTreeWithOneSurface()
{
OwnPtr<CCLayerImpl> root = CCLayerImpl::create(1);
OwnPtr<CCLayerImpl> child = CCLayerImpl::create(2);
root->setPosition(FloatPoint::zero());
root->setAnchorPoint(FloatPoint::zero());
root->setBounds(IntSize(500, 500));
root->setContentBounds(IntSize(500, 500));
root->setDrawsContent(true);
root->createRenderSurface();
root->renderSurface()->setContentRect(IntRect(IntPoint(), IntSize(500, 500)));
child->setPosition(FloatPoint(100, 100));
child->setAnchorPoint(FloatPoint::zero());
child->setBounds(IntSize(30, 30));
child->setContentBounds(IntSize(30, 30));
child->setDrawsContent(true);
root->addChild(child.release());
return root.release();
}
PassOwnPtr<CCLayerImpl> createTestTreeWithTwoSurfaces()
{
OwnPtr<CCLayerImpl> root = CCLayerImpl::create(1);
OwnPtr<CCLayerImpl> child1 = CCLayerImpl::create(2);
OwnPtr<CCLayerImpl> child2 = CCLayerImpl::create(3);
OwnPtr<CCLayerImpl> grandChild1 = CCLayerImpl::create(4);
OwnPtr<CCLayerImpl> grandChild2 = CCLayerImpl::create(5);
root->setPosition(FloatPoint::zero());
root->setAnchorPoint(FloatPoint::zero());
root->setBounds(IntSize(500, 500));
root->setContentBounds(IntSize(500, 500));
root->setDrawsContent(true);
root->createRenderSurface();
root->renderSurface()->setContentRect(IntRect(IntPoint(), IntSize(500, 500)));
child1->setPosition(FloatPoint(100, 100));
child1->setAnchorPoint(FloatPoint::zero());
child1->setBounds(IntSize(30, 30));
child1->setContentBounds(IntSize(30, 30));
child1->setOpacity(0.5);
child1->setDrawsContent(false);
child2->setPosition(FloatPoint(11, 11));
child2->setAnchorPoint(FloatPoint::zero());
child2->setBounds(IntSize(18, 18));
child2->setContentBounds(IntSize(18, 18));
child2->setDrawsContent(true);
grandChild1->setPosition(FloatPoint(200, 200));
grandChild1->setAnchorPoint(FloatPoint::zero());
grandChild1->setBounds(IntSize(6, 8));
grandChild1->setContentBounds(IntSize(6, 8));
grandChild1->setDrawsContent(true);
grandChild2->setPosition(FloatPoint(190, 190));
grandChild2->setAnchorPoint(FloatPoint::zero());
grandChild2->setBounds(IntSize(6, 8));
grandChild2->setContentBounds(IntSize(6, 8));
grandChild2->setDrawsContent(true);
child1->addChild(grandChild1.release());
child1->addChild(grandChild2.release());
root->addChild(child1.release());
root->addChild(child2.release());
return root.release();
}
PassOwnPtr<CCLayerImpl> createAndSetUpTestTreeWithOneSurface()
{
OwnPtr<CCLayerImpl> root = createTestTreeWithOneSurface();
emulateDrawingOneFrame(root.get());
return root.release();
}
PassOwnPtr<CCLayerImpl> createAndSetUpTestTreeWithTwoSurfaces()
{
OwnPtr<CCLayerImpl> root = createTestTreeWithTwoSurfaces();
emulateDrawingOneFrame(root.get());
return root.release();
}
class CCDamageTrackerTest : public testing::Test {
private:
DebugScopedSetImplThread setImplThread;
};
TEST_F(CCDamageTrackerTest, sanityCheckTestTreeWithOneSurface)
{
OwnPtr<CCLayerImpl> root = createAndSetUpTestTreeWithOneSurface();
EXPECT_EQ(2u, root->renderSurface()->layerList().size());
EXPECT_EQ(1, root->renderSurface()->layerList()[0]->id());
EXPECT_EQ(2, root->renderSurface()->layerList()[1]->id());
FloatRect rootDamageRect = root->renderSurface()->damageTracker()->currentDamageRect();
EXPECT_FLOAT_RECT_EQ(FloatRect(0, 0, 500, 500), rootDamageRect);
}
TEST_F(CCDamageTrackerTest, sanityCheckTestTreeWithTwoSurfaces)
{
OwnPtr<CCLayerImpl> root = createAndSetUpTestTreeWithTwoSurfaces();
CCLayerImpl* child1 = root->children()[0];
CCLayerImpl* child2 = root->children()[1];
FloatRect childDamageRect = child1->renderSurface()->damageTracker()->currentDamageRect();
FloatRect rootDamageRect = root->renderSurface()->damageTracker()->currentDamageRect();
ASSERT_TRUE(child1->renderSurface());
EXPECT_FALSE(child2->renderSurface());
EXPECT_EQ(3u, root->renderSurface()->layerList().size());
EXPECT_EQ(2u, child1->renderSurface()->layerList().size());
EXPECT_FLOAT_RECT_EQ(FloatRect(190, 190, 16, 18), childDamageRect);
EXPECT_FLOAT_RECT_EQ(FloatRect(0, 0, 500, 500), rootDamageRect);
}
TEST_F(CCDamageTrackerTest, verifyDamageForUpdateRects)
{
OwnPtr<CCLayerImpl> root = createAndSetUpTestTreeWithOneSurface();
CCLayerImpl* child = root->children()[0];
clearDamageForAllSurfaces(root.get());
child->setUpdateRect(FloatRect(10, 11, 12, 13));
emulateDrawingOneFrame(root.get());
FloatRect rootDamageRect = root->renderSurface()->damageTracker()->currentDamageRect();
EXPECT_FLOAT_RECT_EQ(FloatRect(110, 111, 12, 13), rootDamageRect);
clearDamageForAllSurfaces(root.get());
child->setUpdateRect(FloatRect(10, 11, 12, 13));
emulateDrawingOneFrame(root.get());
rootDamageRect = root->renderSurface()->damageTracker()->currentDamageRect();
EXPECT_FLOAT_RECT_EQ(FloatRect(110, 111, 12, 13), rootDamageRect);
clearDamageForAllSurfaces(root.get());
child->setUpdateRect(FloatRect(20, 25, 1, 2));
emulateDrawingOneFrame(root.get());
rootDamageRect = root->renderSurface()->damageTracker()->currentDamageRect();
EXPECT_FLOAT_RECT_EQ(FloatRect(120, 125, 1, 2), rootDamageRect);
}
TEST_F(CCDamageTrackerTest, verifyDamageForPropertyChanges)
{
OwnPtr<CCLayerImpl> root = createAndSetUpTestTreeWithOneSurface();
CCLayerImpl* child = root->children()[0];
clearDamageForAllSurfaces(root.get());
child->setUpdateRect(FloatRect(10, 11, 12, 13));
child->setOpacity(0.5);
emulateDrawingOneFrame(root.get());
ASSERT_FALSE(child->renderSurface());
ASSERT_EQ(2u, root->renderSurface()->layerList().size());
FloatRect expectedRect = FloatRect(100, 100, 30, 30);
FloatRect rootDamageRect = root->renderSurface()->damageTracker()->currentDamageRect();
EXPECT_FLOAT_RECT_EQ(expectedRect, rootDamageRect);
clearDamageForAllSurfaces(root.get());
emulateDrawingOneFrame(root.get());
EXPECT_TRUE(root->renderSurface()->damageTracker()->currentDamageRect().isEmpty());
clearDamageForAllSurfaces(root.get());
child->setPosition(FloatPoint(200, 230));
emulateDrawingOneFrame(root.get());
expectedRect.uniteIfNonZero(FloatRect(200, 230, 30, 30));
rootDamageRect = root->renderSurface()->damageTracker()->currentDamageRect();
EXPECT_FLOAT_RECT_EQ(expectedRect, rootDamageRect);
}
TEST_F(CCDamageTrackerTest, verifyDamageForTransformedLayer)
{
OwnPtr<CCLayerImpl> root = createAndSetUpTestTreeWithOneSurface();
CCLayerImpl* child = root->children()[0];
WebTransformationMatrix rotation;
rotation.rotate(45);
clearDamageForAllSurfaces(root.get());
child->setAnchorPoint(FloatPoint(0.5, 0.5));
child->setPosition(FloatPoint(85, 85));
emulateDrawingOneFrame(root.get());
FloatRect rootDamageRect = root->renderSurface()->damageTracker()->currentDamageRect();
EXPECT_FLOAT_RECT_EQ(FloatRect(85, 85, 45, 45), rootDamageRect);
clearDamageForAllSurfaces(root.get());
child->setTransform(rotation);
emulateDrawingOneFrame(root.get());
double expectedWidth = 30 * sqrt(2.0);
double expectedPosition = 100 - 0.5 * expectedWidth;
FloatRect expectedRect(expectedPosition, expectedPosition, expectedWidth, expectedWidth);
rootDamageRect = root->renderSurface()->damageTracker()->currentDamageRect();
EXPECT_FLOAT_RECT_EQ(expectedRect, rootDamageRect);
}
TEST_F(CCDamageTrackerTest, verifyDamageForPerspectiveClippedLayer)
{
OwnPtr<CCLayerImpl> root = createAndSetUpTestTreeWithOneSurface();
CCLayerImpl* child = root->children()[0];
WebTransformationMatrix transform;
transform.translate3d(500, 500, 0);
transform.applyPerspective(1);
transform.rotate3d(0, 45, 0);
transform.translate3d(-50, -50, 0);
child->setPosition(FloatPoint(0, 0));
child->setBounds(IntSize(100, 100));
child->setContentBounds(IntSize(100, 100));
child->setTransform(transform);
emulateDrawingOneFrame(root.get());
FloatQuad testQuad(FloatRect(FloatPoint::zero(), FloatSize(100, 100)));
bool clipped = false;
CCMathUtil::mapQuad(transform, testQuad, clipped);
EXPECT_TRUE(clipped);
clearDamageForAllSurfaces(root.get());
child->setOpacity(0.5);
emulateDrawingOneFrame(root.get());
FloatRect rootDamageRect = root->renderSurface()->damageTracker()->currentDamageRect();
FloatRect damageWeCareAbout = FloatRect(FloatPoint::zero(), FloatSize(500, 500));
EXPECT_TRUE(rootDamageRect.contains(damageWeCareAbout));
}
TEST_F(CCDamageTrackerTest, verifyDamageForBlurredSurface)
{
OwnPtr<CCLayerImpl> root = createAndSetUpTestTreeWithOneSurface();
CCLayerImpl* child = root->children()[0];
WebFilterOperations filters;
filters.append(WebFilterOperation::createBlurFilter(5));
int outsetTop, outsetRight, outsetBottom, outsetLeft;
filters.getOutsets(outsetTop, outsetRight, outsetBottom, outsetLeft);
clearDamageForAllSurfaces(root.get());
root->setFilters(filters);
emulateDrawingOneFrame(root.get());
clearDamageForAllSurfaces(root.get());
child->setUpdateRect(FloatRect(10, 11, 12, 13));
emulateDrawingOneFrame(root.get());
FloatRect rootDamageRect = root->renderSurface()->damageTracker()->currentDamageRect();
FloatRect expectedDamageRect = FloatRect(110, 111, 12, 13);
expectedDamageRect.move(-outsetLeft, -outsetTop);
expectedDamageRect.expand(outsetLeft + outsetRight, outsetTop + outsetBottom);
EXPECT_FLOAT_RECT_EQ(expectedDamageRect, rootDamageRect);
}
TEST_F(CCDamageTrackerTest, verifyDamageForBackgroundBlurredChild)
{
OwnPtr<CCLayerImpl> root = createAndSetUpTestTreeWithTwoSurfaces();
CCLayerImpl* child1 = root->children()[0];
CCLayerImpl* child2 = root->children()[1];
child1->setDrawsContent(true);
WebFilterOperations filters;
filters.append(WebFilterOperation::createBlurFilter(2));
int outsetTop, outsetRight, outsetBottom, outsetLeft;
filters.getOutsets(outsetTop, outsetRight, outsetBottom, outsetLeft);
clearDamageForAllSurfaces(root.get());
child1->setBackgroundFilters(filters);
emulateDrawingOneFrame(root.get());
clearDamageForAllSurfaces(root.get());
root->setUpdateRect(FloatRect(297, 297, 2, 2));
emulateDrawingOneFrame(root.get());
FloatRect rootDamageRect = root->renderSurface()->damageTracker()->currentDamageRect();
FloatRect expectedDamageRect = FloatRect(297, 297, 2, 2);
expectedDamageRect.move(-outsetLeft, -outsetTop);
expectedDamageRect.expand(outsetLeft + outsetRight, outsetTop + outsetBottom);
EXPECT_FLOAT_RECT_EQ(expectedDamageRect, rootDamageRect);
clearDamageForAllSurfaces(root.get());
root->setUpdateRect(FloatRect(297, 297, 30, 30));
emulateDrawingOneFrame(root.get());
rootDamageRect = root->renderSurface()->damageTracker()->currentDamageRect();
expectedDamageRect = FloatRect(297, 297, 30, 30);
expectedDamageRect.move(-outsetLeft, -outsetTop);
expectedDamageRect.expand(outsetLeft, outsetTop);
EXPECT_FLOAT_RECT_EQ(expectedDamageRect, rootDamageRect);
clearDamageForAllSurfaces(root.get());
root->setUpdateRect(FloatRect(30, 30, 2, 2));
emulateDrawingOneFrame(root.get());
rootDamageRect = root->renderSurface()->damageTracker()->currentDamageRect();
expectedDamageRect = FloatRect(30, 30, 2, 2);
EXPECT_FLOAT_RECT_EQ(expectedDamageRect, rootDamageRect);
clearDamageForAllSurfaces(root.get());
root->setUpdateRect(FloatRect(99, 99, 1, 1));
emulateDrawingOneFrame(root.get());
rootDamageRect = root->renderSurface()->damageTracker()->currentDamageRect();
expectedDamageRect = FloatRect(99, 99, 1, 1);
expectedDamageRect.move(-outsetLeft + 1, -outsetTop + 1);
expectedDamageRect.expand(outsetLeft + outsetRight - 1, outsetTop + outsetBottom - 1);
EXPECT_FLOAT_RECT_EQ(expectedDamageRect, rootDamageRect);
clearDamageForAllSurfaces(root.get());
child2->setUpdateRect(FloatRect(0, 0, 1, 1));
emulateDrawingOneFrame(root.get());
rootDamageRect = root->renderSurface()->damageTracker()->currentDamageRect();
expectedDamageRect = FloatRect(11, 11, 1, 1);
EXPECT_FLOAT_RECT_EQ(expectedDamageRect, rootDamageRect);
clearDamageForAllSurfaces(root.get());
child1->setUpdateRect(FloatRect(0, 0, 1, 1));
emulateDrawingOneFrame(root.get());
rootDamageRect = root->renderSurface()->damageTracker()->currentDamageRect();
expectedDamageRect = FloatRect(100, 100, 1, 1);
expectedDamageRect.move(-outsetLeft, -outsetTop);
expectedDamageRect.expand(outsetLeft + outsetRight, outsetTop + outsetBottom);
EXPECT_FLOAT_RECT_EQ(expectedDamageRect, rootDamageRect);
}
TEST_F(CCDamageTrackerTest, verifyDamageForAddingAndRemovingLayer)
{
OwnPtr<CCLayerImpl> root = createAndSetUpTestTreeWithOneSurface();
CCLayerImpl* child1 = root->children()[0];
clearDamageForAllSurfaces(root.get());
{
OwnPtr<CCLayerImpl> child2 = CCLayerImpl::create(3);
child2->setPosition(FloatPoint(400, 380));
child2->setAnchorPoint(FloatPoint::zero());
child2->setBounds(IntSize(6, 8));
child2->setContentBounds(IntSize(6, 8));
child2->setDrawsContent(true);
root->addChild(child2.release());
}
emulateDrawingOneFrame(root.get());
ASSERT_EQ(3u, root->renderSurface()->layerList().size());
FloatRect rootDamageRect = root->renderSurface()->damageTracker()->currentDamageRect();
EXPECT_FLOAT_RECT_EQ(FloatRect(400, 380, 6, 8), rootDamageRect);
clearDamageForAllSurfaces(root.get());
emulateDrawingOneFrame(root.get());
EXPECT_TRUE(root->renderSurface()->damageTracker()->currentDamageRect().isEmpty());
child1->removeFromParent();
emulateDrawingOneFrame(root.get());
rootDamageRect = root->renderSurface()->damageTracker()->currentDamageRect();
EXPECT_FLOAT_RECT_EQ(FloatRect(100, 100, 30, 30), rootDamageRect);
}
TEST_F(CCDamageTrackerTest, verifyDamageForNewUnchangedLayer)
{
OwnPtr<CCLayerImpl> root = createAndSetUpTestTreeWithOneSurface();
clearDamageForAllSurfaces(root.get());
{
OwnPtr<CCLayerImpl> child2 = CCLayerImpl::create(3);
child2->setPosition(FloatPoint(400, 380));
child2->setAnchorPoint(FloatPoint::zero());
child2->setBounds(IntSize(6, 8));
child2->setContentBounds(IntSize(6, 8));
child2->setDrawsContent(true);
child2->resetAllChangeTrackingForSubtree();
ASSERT_FALSE(child2->layerPropertyChanged());
ASSERT_TRUE(child2->updateRect().isEmpty());
root->addChild(child2.release());
}
emulateDrawingOneFrame(root.get());
ASSERT_EQ(3u, root->renderSurface()->layerList().size());
FloatRect rootDamageRect = root->renderSurface()->damageTracker()->currentDamageRect();
EXPECT_FLOAT_RECT_EQ(FloatRect(400, 380, 6, 8), rootDamageRect);
}
TEST_F(CCDamageTrackerTest, verifyDamageForMultipleLayers)
{
OwnPtr<CCLayerImpl> root = createAndSetUpTestTreeWithOneSurface();
CCLayerImpl* child1 = root->children()[0];
clearDamageForAllSurfaces(root.get());
{
OwnPtr<CCLayerImpl> child2 = CCLayerImpl::create(3);
child2->setPosition(FloatPoint(400, 380));
child2->setAnchorPoint(FloatPoint::zero());
child2->setBounds(IntSize(6, 8));
child2->setContentBounds(IntSize(6, 8));
child2->setDrawsContent(true);
root->addChild(child2.release());
}
CCLayerImpl* child2 = root->children()[1];
emulateDrawingOneFrame(root.get());
clearDamageForAllSurfaces(root.get());
child1->setUpdateRect(FloatRect(0, 0, 1, 2));
child2->setUpdateRect(FloatRect(0, 0, 3, 4));
emulateDrawingOneFrame(root.get());
FloatRect rootDamageRect = root->renderSurface()->damageTracker()->currentDamageRect();
EXPECT_FLOAT_RECT_EQ(FloatRect(100, 100, 303, 284), rootDamageRect);
}
TEST_F(CCDamageTrackerTest, verifyDamageForNestedSurfaces)
{
OwnPtr<CCLayerImpl> root = createAndSetUpTestTreeWithTwoSurfaces();
CCLayerImpl* child1 = root->children()[0];
CCLayerImpl* child2 = root->children()[1];
CCLayerImpl* grandChild1 = root->children()[0]->children()[0];
FloatRect childDamageRect;
FloatRect rootDamageRect;
clearDamageForAllSurfaces(root.get());
grandChild1->setOpacity(0.5);
emulateDrawingOneFrame(root.get());
childDamageRect = child1->renderSurface()->damageTracker()->currentDamageRect();
rootDamageRect = root->renderSurface()->damageTracker()->currentDamageRect();
EXPECT_FLOAT_RECT_EQ(FloatRect(200, 200, 6, 8), childDamageRect);
EXPECT_FLOAT_RECT_EQ(FloatRect(300, 300, 6, 8), rootDamageRect);
clearDamageForAllSurfaces(root.get());
grandChild1->setOpacity(0.7f);
child2->setOpacity(0.7f);
emulateDrawingOneFrame(root.get());
childDamageRect = child1->renderSurface()->damageTracker()->currentDamageRect();
rootDamageRect = root->renderSurface()->damageTracker()->currentDamageRect();
EXPECT_FLOAT_RECT_EQ(FloatRect(200, 200, 6, 8), childDamageRect);
EXPECT_FLOAT_RECT_EQ(FloatRect(11, 11, 295, 297), rootDamageRect);
}
TEST_F(CCDamageTrackerTest, verifyDamageForSurfaceChangeFromDescendantLayer)
{
OwnPtr<CCLayerImpl> root = createAndSetUpTestTreeWithTwoSurfaces();
CCLayerImpl* child1 = root->children()[0];
CCLayerImpl* grandChild1 = root->children()[0]->children()[0];
FloatRect childDamageRect;
FloatRect rootDamageRect;
clearDamageForAllSurfaces(root.get());
grandChild1->setPosition(FloatPoint(195, 205));
emulateDrawingOneFrame(root.get());
childDamageRect = child1->renderSurface()->damageTracker()->currentDamageRect();
rootDamageRect = root->renderSurface()->damageTracker()->currentDamageRect();
EXPECT_FLOAT_RECT_EQ(FloatRect(190, 190, 11, 23), childDamageRect);
EXPECT_FLOAT_RECT_EQ(FloatRect(290, 290, 16, 23), rootDamageRect);
}
TEST_F(CCDamageTrackerTest, verifyDamageForSurfaceChangeFromAncestorLayer)
{
OwnPtr<CCLayerImpl> root = createAndSetUpTestTreeWithTwoSurfaces();
CCLayerImpl* child1 = root->children()[0];
FloatRect childDamageRect;
FloatRect rootDamageRect;
clearDamageForAllSurfaces(root.get());
child1->setPosition(FloatPoint(50, 50));
emulateDrawingOneFrame(root.get());
childDamageRect = child1->renderSurface()->damageTracker()->currentDamageRect();
rootDamageRect = root->renderSurface()->damageTracker()->currentDamageRect();
EXPECT_FLOAT_RECT_EQ(FloatRect(190, 190, 16, 18), childDamageRect);
EXPECT_FLOAT_RECT_EQ(FloatRect(240, 240, 66, 68), rootDamageRect);
}
TEST_F(CCDamageTrackerTest, verifyDamageForAddingAndRemovingRenderSurfaces)
{
OwnPtr<CCLayerImpl> root = createAndSetUpTestTreeWithTwoSurfaces();
CCLayerImpl* child1 = root->children()[0];
FloatRect childDamageRect;
FloatRect rootDamageRect;
clearDamageForAllSurfaces(root.get());
child1->setOpacity(1);
emulateDrawingOneFrame(root.get());
ASSERT_FALSE(child1->renderSurface());
ASSERT_EQ(4u, root->renderSurface()->layerList().size());
rootDamageRect = root->renderSurface()->damageTracker()->currentDamageRect();
EXPECT_FLOAT_RECT_EQ(FloatRect(290, 290, 16, 18), rootDamageRect);
clearDamageForAllSurfaces(root.get());
emulateDrawingOneFrame(root.get());
rootDamageRect = root->renderSurface()->damageTracker()->currentDamageRect();
EXPECT_TRUE(rootDamageRect.isEmpty());
clearDamageForAllSurfaces(root.get());
child1->setOpacity(0.5);
emulateDrawingOneFrame(root.get());
ASSERT_TRUE(child1->renderSurface());
EXPECT_EQ(3u, root->renderSurface()->layerList().size());
EXPECT_EQ(2u, child1->renderSurface()->layerList().size());
childDamageRect = child1->renderSurface()->damageTracker()->currentDamageRect();
rootDamageRect = root->renderSurface()->damageTracker()->currentDamageRect();
EXPECT_FLOAT_RECT_EQ(FloatRect(190, 190, 16, 18), childDamageRect);
EXPECT_FLOAT_RECT_EQ(FloatRect(290, 290, 16, 18), rootDamageRect);
}
TEST_F(CCDamageTrackerTest, verifyNoDamageWhenNothingChanged)
{
OwnPtr<CCLayerImpl> root = createAndSetUpTestTreeWithTwoSurfaces();
CCLayerImpl* child1 = root->children()[0];
FloatRect childDamageRect;
FloatRect rootDamageRect;
clearDamageForAllSurfaces(root.get());
emulateDrawingOneFrame(root.get());
childDamageRect = child1->renderSurface()->damageTracker()->currentDamageRect();
rootDamageRect = root->renderSurface()->damageTracker()->currentDamageRect();
EXPECT_TRUE(childDamageRect.isEmpty());
EXPECT_TRUE(rootDamageRect.isEmpty());
clearDamageForAllSurfaces(root.get());
emulateDrawingOneFrame(root.get());
childDamageRect = child1->renderSurface()->damageTracker()->currentDamageRect();
rootDamageRect = root->renderSurface()->damageTracker()->currentDamageRect();
EXPECT_TRUE(childDamageRect.isEmpty());
EXPECT_TRUE(rootDamageRect.isEmpty());
}
TEST_F(CCDamageTrackerTest, verifyNoDamageForUpdateRectThatDoesNotDrawContent)
{
OwnPtr<CCLayerImpl> root = createAndSetUpTestTreeWithTwoSurfaces();
CCLayerImpl* child1 = root->children()[0];
FloatRect childDamageRect;
FloatRect rootDamageRect;
clearDamageForAllSurfaces(root.get());
child1->setUpdateRect(FloatRect(0, 0, 1, 2));
emulateDrawingOneFrame(root.get());
childDamageRect = child1->renderSurface()->damageTracker()->currentDamageRect();
rootDamageRect = root->renderSurface()->damageTracker()->currentDamageRect();
EXPECT_TRUE(childDamageRect.isEmpty());
EXPECT_TRUE(rootDamageRect.isEmpty());
}
TEST_F(CCDamageTrackerTest, verifyDamageForReplica)
{
OwnPtr<CCLayerImpl> root = createAndSetUpTestTreeWithTwoSurfaces();
CCLayerImpl* child1 = root->children()[0];
CCLayerImpl* grandChild1 = child1->children()[0];
CCLayerImpl* grandChild2 = child1->children()[1];
grandChild2->setPosition(FloatPoint(180, 180));
{
OwnPtr<CCLayerImpl> grandChild3 = CCLayerImpl::create(6);
grandChild3->setPosition(FloatPoint(240, 240));
grandChild3->setAnchorPoint(FloatPoint::zero());
grandChild3->setBounds(IntSize(10, 10));
grandChild3->setContentBounds(IntSize(10, 10));
grandChild3->setDrawsContent(true);
child1->addChild(grandChild3.release());
}
child1->setOpacity(0.5);
emulateDrawingOneFrame(root.get());
clearDamageForAllSurfaces(root.get());
{
OwnPtr<CCLayerImpl> grandChild1Replica = CCLayerImpl::create(7);
grandChild1Replica->setPosition(FloatPoint::zero());
grandChild1Replica->setAnchorPoint(FloatPoint::zero());
WebTransformationMatrix reflection;
reflection.scale3d(-1, 1, 1);
grandChild1Replica->setTransform(reflection);
grandChild1->setReplicaLayer(grandChild1Replica.release());
}
emulateDrawingOneFrame(root.get());
FloatRect grandChildDamageRect = grandChild1->renderSurface()->damageTracker()->currentDamageRect();
FloatRect childDamageRect = child1->renderSurface()->damageTracker()->currentDamageRect();
FloatRect rootDamageRect = root->renderSurface()->damageTracker()->currentDamageRect();
EXPECT_FLOAT_RECT_EQ(FloatRect(0, 0, 6, 8), grandChildDamageRect);
EXPECT_FLOAT_RECT_EQ(FloatRect(194, 200, 12, 8), childDamageRect);
EXPECT_FLOAT_RECT_EQ(FloatRect(294, 300, 12, 8), rootDamageRect);
clearDamageForAllSurfaces(root.get());
IntRect oldContentRect = child1->renderSurface()->contentRect();
grandChild1->setPosition(FloatPoint(195, 205));
emulateDrawingOneFrame(root.get());
ASSERT_EQ(oldContentRect.width(), child1->renderSurface()->contentRect().width());
ASSERT_EQ(oldContentRect.height(), child1->renderSurface()->contentRect().height());
grandChildDamageRect = grandChild1->renderSurface()->damageTracker()->currentDamageRect();
childDamageRect = child1->renderSurface()->damageTracker()->currentDamageRect();
rootDamageRect = root->renderSurface()->damageTracker()->currentDamageRect();
EXPECT_FLOAT_RECT_EQ(FloatRect(0, 0, 6, 8), grandChildDamageRect);
EXPECT_FLOAT_RECT_EQ(FloatRect(189, 200, 17, 13), childDamageRect);
EXPECT_FLOAT_RECT_EQ(FloatRect(289, 300, 17, 13), rootDamageRect);
clearDamageForAllSurfaces(root.get());
grandChild1->setReplicaLayer(nullptr);
emulateDrawingOneFrame(root.get());
ASSERT_EQ(oldContentRect.width(), child1->renderSurface()->contentRect().width());
ASSERT_EQ(oldContentRect.height(), child1->renderSurface()->contentRect().height());
EXPECT_FALSE(grandChild1->renderSurface());
childDamageRect = child1->renderSurface()->damageTracker()->currentDamageRect();
rootDamageRect = root->renderSurface()->damageTracker()->currentDamageRect();
EXPECT_FLOAT_RECT_EQ(FloatRect(189, 205, 12, 8), childDamageRect);
EXPECT_FLOAT_RECT_EQ(FloatRect(289, 305, 12, 8), rootDamageRect);
}
TEST_F(CCDamageTrackerTest, verifyDamageForMask)
{
OwnPtr<CCLayerImpl> root = createAndSetUpTestTreeWithOneSurface();
CCLayerImpl* child = root->children()[0];
clearDamageForAllSurfaces(root.get());
{
OwnPtr<CCLayerImpl> maskLayer = CCLayerImpl::create(3);
maskLayer->setPosition(child->position());
maskLayer->setAnchorPoint(FloatPoint::zero());
maskLayer->setBounds(child->bounds());
maskLayer->setContentBounds(child->bounds());
child->setMaskLayer(maskLayer.release());
}
CCLayerImpl* maskLayer = child->maskLayer();
child->setOpacity(0.5);
{
OwnPtr<CCLayerImpl> grandChild = CCLayerImpl::create(4);
grandChild->setPosition(FloatPoint(2, 2));
grandChild->setAnchorPoint(FloatPoint::zero());
grandChild->setBounds(IntSize(2, 2));
grandChild->setContentBounds(IntSize(2, 2));
grandChild->setDrawsContent(true);
child->addChild(grandChild.release());
}
emulateDrawingOneFrame(root.get());
ASSERT_TRUE(child->renderSurface());
clearDamageForAllSurfaces(root.get());
maskLayer->setUpdateRect(FloatRect(1, 2, 3, 4));
emulateDrawingOneFrame(root.get());
FloatRect childDamageRect = child->renderSurface()->damageTracker()->currentDamageRect();
EXPECT_FLOAT_RECT_EQ(FloatRect(0, 0, 30, 30), childDamageRect);
clearDamageForAllSurfaces(root.get());
emulateDrawingOneFrame(root.get());
childDamageRect = child->renderSurface()->damageTracker()->currentDamageRect();
EXPECT_TRUE(childDamageRect.isEmpty());
clearDamageForAllSurfaces(root.get());
maskLayer->setStackingOrderChanged(true);
emulateDrawingOneFrame(root.get());
childDamageRect = child->renderSurface()->damageTracker()->currentDamageRect();
EXPECT_FLOAT_RECT_EQ(FloatRect(0, 0, 30, 30), childDamageRect);
clearDamageForAllSurfaces(root.get());
emulateDrawingOneFrame(root.get());
childDamageRect = child->renderSurface()->damageTracker()->currentDamageRect();
EXPECT_TRUE(childDamageRect.isEmpty());
clearDamageForAllSurfaces(root.get());
child->setMaskLayer(nullptr);
ASSERT_TRUE(child->layerPropertyChanged());
emulateDrawingOneFrame(root.get());
ASSERT_TRUE(child->renderSurface());
childDamageRect = child->renderSurface()->damageTracker()->currentDamageRect();
EXPECT_FLOAT_RECT_EQ(FloatRect(0, 0, 30, 30), childDamageRect);
}
TEST_F(CCDamageTrackerTest, verifyDamageForReplicaMask)
{
OwnPtr<CCLayerImpl> root = createAndSetUpTestTreeWithTwoSurfaces();
CCLayerImpl* child1 = root->children()[0];
CCLayerImpl* grandChild1 = child1->children()[0];
clearDamageForAllSurfaces(root.get());
{
OwnPtr<CCLayerImpl> grandChild1Replica = CCLayerImpl::create(6);
grandChild1Replica->setPosition(FloatPoint::zero());
grandChild1Replica->setAnchorPoint(FloatPoint::zero());
WebTransformationMatrix reflection;
reflection.scale3d(-1, 1, 1);
grandChild1Replica->setTransform(reflection);
grandChild1->setReplicaLayer(grandChild1Replica.release());
}
CCLayerImpl* grandChild1Replica = grandChild1->replicaLayer();
{
OwnPtr<CCLayerImpl> replicaMaskLayer = CCLayerImpl::create(7);
replicaMaskLayer->setPosition(FloatPoint::zero());
replicaMaskLayer->setAnchorPoint(FloatPoint::zero());
replicaMaskLayer->setBounds(grandChild1->bounds());
replicaMaskLayer->setContentBounds(grandChild1->bounds());
grandChild1Replica->setMaskLayer(replicaMaskLayer.release());
}
CCLayerImpl* replicaMaskLayer = grandChild1Replica->maskLayer();
emulateDrawingOneFrame(root.get());
ASSERT_TRUE(grandChild1->renderSurface());
clearDamageForAllSurfaces(root.get());
replicaMaskLayer->setStackingOrderChanged(true);
emulateDrawingOneFrame(root.get());
FloatRect grandChildDamageRect = grandChild1->renderSurface()->damageTracker()->currentDamageRect();
FloatRect childDamageRect = child1->renderSurface()->damageTracker()->currentDamageRect();
EXPECT_TRUE(grandChildDamageRect.isEmpty());
EXPECT_FLOAT_RECT_EQ(FloatRect(194, 200, 6, 8), childDamageRect);
clearDamageForAllSurfaces(root.get());
grandChild1Replica->setMaskLayer(nullptr);
emulateDrawingOneFrame(root.get());
grandChildDamageRect = grandChild1->renderSurface()->damageTracker()->currentDamageRect();
childDamageRect = child1->renderSurface()->damageTracker()->currentDamageRect();
EXPECT_TRUE(grandChildDamageRect.isEmpty());
EXPECT_FLOAT_RECT_EQ(FloatRect(194, 200, 6, 8), childDamageRect);
}
TEST_F(CCDamageTrackerTest, verifyDamageForReplicaMaskWithAnchor)
{
OwnPtr<CCLayerImpl> root = createAndSetUpTestTreeWithTwoSurfaces();
CCLayerImpl* child1 = root->children()[0];
CCLayerImpl* grandChild1 = child1->children()[0];
clearDamageForAllSurfaces(root.get());
grandChild1->setAnchorPoint(FloatPoint(1, 0));
{
OwnPtr<CCLayerImpl> grandChild1Replica = CCLayerImpl::create(6);
grandChild1Replica->setPosition(FloatPoint::zero());
grandChild1Replica->setAnchorPoint(FloatPoint(1, 0));
WebTransformationMatrix reflection;
reflection.scale3d(-1, 1, 1);
grandChild1Replica->setTransform(reflection);
grandChild1->setReplicaLayer(grandChild1Replica.release());
}
CCLayerImpl* grandChild1Replica = grandChild1->replicaLayer();
{
OwnPtr<CCLayerImpl> replicaMaskLayer = CCLayerImpl::create(7);
replicaMaskLayer->setPosition(FloatPoint::zero());
replicaMaskLayer->setAnchorPoint(FloatPoint::zero());
replicaMaskLayer->setBounds(grandChild1->bounds());
replicaMaskLayer->setContentBounds(grandChild1->bounds());
grandChild1Replica->setMaskLayer(replicaMaskLayer.release());
}
CCLayerImpl* replicaMaskLayer = grandChild1Replica->maskLayer();
emulateDrawingOneFrame(root.get());
ASSERT_TRUE(grandChild1->renderSurface());
clearDamageForAllSurfaces(root.get());
replicaMaskLayer->setStackingOrderChanged(true);
emulateDrawingOneFrame(root.get());
FloatRect childDamageRect = child1->renderSurface()->damageTracker()->currentDamageRect();
EXPECT_FLOAT_RECT_EQ(FloatRect(206, 200, 6, 8), childDamageRect);
}
TEST_F(CCDamageTrackerTest, verifyDamageWhenForcedFullDamage)
{
OwnPtr<CCLayerImpl> root = createAndSetUpTestTreeWithOneSurface();
CCLayerImpl* child = root->children()[0];
clearDamageForAllSurfaces(root.get());
child->setUpdateRect(FloatRect(10, 11, 12, 13));
root->renderSurface()->damageTracker()->forceFullDamageNextUpdate();
emulateDrawingOneFrame(root.get());
FloatRect rootDamageRect = root->renderSurface()->damageTracker()->currentDamageRect();
EXPECT_FLOAT_RECT_EQ(FloatRect(0, 0, 500, 500), rootDamageRect);
clearDamageForAllSurfaces(root.get());
root->renderSurface()->damageTracker()->forceFullDamageNextUpdate();
emulateDrawingOneFrame(root.get());
rootDamageRect = root->renderSurface()->damageTracker()->currentDamageRect();
EXPECT_FLOAT_RECT_EQ(FloatRect(0, 0, 500, 500), rootDamageRect);
}
TEST_F(CCDamageTrackerTest, verifyDamageForEmptyLayerList)
{
OwnPtr<CCLayerImpl> root = CCLayerImpl::create(1);
root->createRenderSurface();
ASSERT_TRUE(root == root->renderTarget());
CCRenderSurface* targetSurface = root->renderSurface();
targetSurface->clearLayerLists();
targetSurface->damageTracker()->updateDamageTrackingState(targetSurface->layerList(), targetSurface->owningLayerId(), false, IntRect(), 0, WebFilterOperations());
FloatRect damageRect = targetSurface->damageTracker()->currentDamageRect();
EXPECT_TRUE(damageRect.isEmpty());
}
TEST_F(CCDamageTrackerTest, verifyDamageAccumulatesUntilReset)
{
OwnPtr<CCLayerImpl> root = createAndSetUpTestTreeWithOneSurface();
CCLayerImpl* child = root->children()[0];
clearDamageForAllSurfaces(root.get());
child->setUpdateRect(FloatRect(10, 11, 1, 2));
emulateDrawingOneFrame(root.get());
FloatRect rootDamageRect = root->renderSurface()->damageTracker()->currentDamageRect();
EXPECT_FLOAT_RECT_EQ(FloatRect(110, 111, 1, 2), rootDamageRect);
child->setUpdateRect(FloatRect(20, 25, 1, 2));
emulateDrawingOneFrame(root.get());
rootDamageRect = root->renderSurface()->damageTracker()->currentDamageRect();
EXPECT_FLOAT_RECT_EQ(FloatRect(110, 111, 11, 16), rootDamageRect);
root->renderSurface()->damageTracker()->didDrawDamagedArea();
rootDamageRect = root->renderSurface()->damageTracker()->currentDamageRect();
EXPECT_TRUE(rootDamageRect.isEmpty());
emulateDrawingOneFrame(root.get());
rootDamageRect = root->renderSurface()->damageTracker()->currentDamageRect();
EXPECT_TRUE(rootDamageRect.isEmpty());
}
}