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
3 // found in the LICENSE file.
7 #include "CCRenderSurface.h"
9 #include "CCAppendQuadsData.h"
10 #include "CCLayerImpl.h"
11 #include "CCRenderPassSink.h"
12 #include "CCSharedQuadState.h"
13 #include "CCSingleThreadProxy.h"
14 #include "cc/scoped_ptr_vector.h"
15 #include "cc/test/mock_quad_culler.h"
16 #include "testing/gmock/include/gmock/gmock.h"
17 #include "testing/gtest/include/gtest/gtest.h"
18 #include <public/WebTransformationMatrix.h>
21 using WebKit::WebTransformationMatrix
;
25 #define EXECUTE_AND_VERIFY_SURFACE_CHANGED(codeToTest) \
26 renderSurface->resetPropertyChangedFlag(); \
28 EXPECT_TRUE(renderSurface->surfacePropertyChanged())
30 #define EXECUTE_AND_VERIFY_SURFACE_DID_NOT_CHANGE(codeToTest) \
31 renderSurface->resetPropertyChangedFlag(); \
33 EXPECT_FALSE(renderSurface->surfacePropertyChanged())
35 TEST(CCRenderSurfaceTest
, verifySurfaceChangesAreTrackedProperly
)
38 // This test checks that surfacePropertyChanged() has the correct behavior.
41 // This will fake that we are on the correct thread for testing purposes.
42 DebugScopedSetImplThread setImplThread
;
44 scoped_ptr
<CCLayerImpl
> owningLayer
= CCLayerImpl::create(1);
45 owningLayer
->createRenderSurface();
46 ASSERT_TRUE(owningLayer
->renderSurface());
47 CCRenderSurface
* renderSurface
= owningLayer
->renderSurface();
48 IntRect testRect
= IntRect(IntPoint(3, 4), IntSize(5, 6));
49 owningLayer
->resetAllChangeTrackingForSubtree();
51 // Currently, the contentRect, clipRect, and owningLayer->layerPropertyChanged() are
52 // the only sources of change.
53 EXECUTE_AND_VERIFY_SURFACE_CHANGED(renderSurface
->setClipRect(testRect
));
54 EXECUTE_AND_VERIFY_SURFACE_CHANGED(renderSurface
->setContentRect(testRect
));
56 owningLayer
->setOpacity(0.5f
);
57 EXPECT_TRUE(renderSurface
->surfacePropertyChanged());
58 owningLayer
->resetAllChangeTrackingForSubtree();
60 // Setting the surface properties to the same values again should not be considered "change".
61 EXECUTE_AND_VERIFY_SURFACE_DID_NOT_CHANGE(renderSurface
->setClipRect(testRect
));
62 EXECUTE_AND_VERIFY_SURFACE_DID_NOT_CHANGE(renderSurface
->setContentRect(testRect
));
64 scoped_ptr
<CCLayerImpl
> dummyMask
= CCLayerImpl::create(1);
65 WebTransformationMatrix dummyMatrix
;
66 dummyMatrix
.translate(1.0, 2.0);
68 // The rest of the surface properties are either internal and should not cause change,
69 // or they are already accounted for by the owninglayer->layerPropertyChanged().
70 EXECUTE_AND_VERIFY_SURFACE_DID_NOT_CHANGE(renderSurface
->setDrawOpacity(0.5));
71 EXECUTE_AND_VERIFY_SURFACE_DID_NOT_CHANGE(renderSurface
->setDrawTransform(dummyMatrix
));
72 EXECUTE_AND_VERIFY_SURFACE_DID_NOT_CHANGE(renderSurface
->setReplicaDrawTransform(dummyMatrix
));
73 EXECUTE_AND_VERIFY_SURFACE_DID_NOT_CHANGE(renderSurface
->clearLayerLists());
76 TEST(CCRenderSurfaceTest
, sanityCheckSurfaceCreatesCorrectSharedQuadState
)
78 // This will fake that we are on the correct thread for testing purposes.
79 DebugScopedSetImplThread setImplThread
;
81 scoped_ptr
<CCLayerImpl
> rootLayer
= CCLayerImpl::create(1);
83 scoped_ptr
<CCLayerImpl
> owningLayer
= CCLayerImpl::create(2);
84 owningLayer
->createRenderSurface();
85 ASSERT_TRUE(owningLayer
->renderSurface());
86 owningLayer
->setRenderTarget(owningLayer
.get());
87 CCRenderSurface
* renderSurface
= owningLayer
->renderSurface();
89 rootLayer
->addChild(owningLayer
.Pass());
91 IntRect contentRect
= IntRect(IntPoint::zero(), IntSize(50, 50));
92 IntRect clipRect
= IntRect(IntPoint(5, 5), IntSize(40, 40));
93 WebTransformationMatrix origin
;
95 origin
.translate(30, 40);
97 renderSurface
->setDrawTransform(origin
);
98 renderSurface
->setContentRect(contentRect
);
99 renderSurface
->setClipRect(clipRect
);
100 renderSurface
->setDrawOpacity(1);
103 CCSharedQuadStateList sharedStateList
;
104 MockCCQuadCuller
mockQuadCuller(quadList
, sharedStateList
);
105 CCAppendQuadsData appendQuadsData
;
107 bool forReplica
= false;
108 renderSurface
->appendQuads(mockQuadCuller
, appendQuadsData
, forReplica
, CCRenderPass::Id(2, 0));
110 ASSERT_EQ(1u, sharedStateList
.size());
111 CCSharedQuadState
* sharedQuadState
= sharedStateList
[0];
113 EXPECT_EQ(30, sharedQuadState
->quadTransform
.m41());
114 EXPECT_EQ(40, sharedQuadState
->quadTransform
.m42());
115 EXPECT_EQ(contentRect
, IntRect(sharedQuadState
->visibleContentRect
));
116 EXPECT_EQ(1, sharedQuadState
->opacity
);
117 EXPECT_FALSE(sharedQuadState
->opaque
);
120 class TestCCRenderPassSink
: public CCRenderPassSink
{
122 virtual void appendRenderPass(scoped_ptr
<CCRenderPass
> renderPass
) OVERRIDE
{ m_renderPasses
.append(renderPass
.Pass()); }
124 const ScopedPtrVector
<CCRenderPass
>& renderPasses() const { return m_renderPasses
; }
127 ScopedPtrVector
<CCRenderPass
> m_renderPasses
;
130 TEST(CCRenderSurfaceTest
, sanityCheckSurfaceCreatesCorrectRenderPass
)
132 // This will fake that we are on the correct thread for testing purposes.
133 DebugScopedSetImplThread setImplThread
;
135 scoped_ptr
<CCLayerImpl
> rootLayer
= CCLayerImpl::create(1);
137 scoped_ptr
<CCLayerImpl
> owningLayer
= CCLayerImpl::create(2);
138 owningLayer
->createRenderSurface();
139 ASSERT_TRUE(owningLayer
->renderSurface());
140 owningLayer
->setRenderTarget(owningLayer
.get());
141 CCRenderSurface
* renderSurface
= owningLayer
->renderSurface();
143 rootLayer
->addChild(owningLayer
.Pass());
145 IntRect contentRect
= IntRect(IntPoint::zero(), IntSize(50, 50));
146 WebTransformationMatrix origin
;
147 origin
.translate(30, 40);
149 renderSurface
->setScreenSpaceTransform(origin
);
150 renderSurface
->setContentRect(contentRect
);
152 TestCCRenderPassSink passSink
;
154 renderSurface
->appendRenderPasses(passSink
);
156 ASSERT_EQ(1u, passSink
.renderPasses().size());
157 CCRenderPass
* pass
= passSink
.renderPasses()[0];
159 EXPECT_EQ(CCRenderPass::Id(2, 0), pass
->id());
160 EXPECT_EQ(contentRect
, pass
->outputRect());
161 EXPECT_EQ(origin
, pass
->transformToRootTarget());