[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 1 | // Copyright 2012 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. |
| 4 | |
[email protected] | d50c686 | 2012-10-23 02:08:31 | [diff] [blame] | 5 | #include "cc/layer_iterator.h" |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 6 | |
[email protected] | a8461d8 | 2012-10-16 21:11:14 | [diff] [blame] | 7 | #include "cc/layer.h" |
[email protected] | d50c686 | 2012-10-23 02:08:31 | [diff] [blame] | 8 | #include "cc/layer_tree_host_common.h" |
[email protected] | 7f0c53db | 2012-10-02 00:23:18 | [diff] [blame] | 9 | #include "testing/gmock/include/gmock/gmock.h" |
| 10 | #include "testing/gtest/include/gtest/gtest.h" |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 11 | #include <public/WebTransformationMatrix.h> |
| 12 | |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 13 | using WebKit::WebTransformationMatrix; |
| 14 | using ::testing::Mock; |
| 15 | using ::testing::_; |
| 16 | using ::testing::AtLeast; |
| 17 | using ::testing::AnyNumber; |
| 18 | |
[email protected] | ba56574 | 2012-11-10 09:29:48 | [diff] [blame^] | 19 | namespace cc { |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 20 | namespace { |
| 21 | |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 22 | class TestLayer : public Layer { |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 23 | public: |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 24 | static scoped_refptr<TestLayer> create() { return make_scoped_refptr(new TestLayer()); } |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 25 | |
| 26 | int m_countRepresentingTargetSurface; |
| 27 | int m_countRepresentingContributingSurface; |
| 28 | int m_countRepresentingItself; |
| 29 | |
| 30 | virtual bool drawsContent() const OVERRIDE { return m_drawsContent; } |
| 31 | void setDrawsContent(bool drawsContent) { m_drawsContent = drawsContent; } |
| 32 | |
| 33 | private: |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 34 | TestLayer() |
| 35 | : Layer() |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 36 | , m_drawsContent(true) |
| 37 | { |
[email protected] | aad0a007 | 2012-11-01 18:15:58 | [diff] [blame] | 38 | setBounds(gfx::Size(100, 100)); |
| 39 | setPosition(gfx::Point()); |
| 40 | setAnchorPoint(gfx::Point()); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 41 | } |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 42 | virtual ~TestLayer() |
[email protected] | d58499a | 2012-10-09 22:27:47 | [diff] [blame] | 43 | { |
| 44 | } |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 45 | |
| 46 | bool m_drawsContent; |
| 47 | }; |
| 48 | |
| 49 | #define EXPECT_COUNT(layer, target, contrib, itself) \ |
| 50 | EXPECT_EQ(target, layer->m_countRepresentingTargetSurface); \ |
| 51 | EXPECT_EQ(contrib, layer->m_countRepresentingContributingSurface); \ |
| 52 | EXPECT_EQ(itself, layer->m_countRepresentingItself); |
| 53 | |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 54 | typedef LayerIterator<Layer, std::vector<scoped_refptr<Layer> >, RenderSurface, LayerIteratorActions::FrontToBack> FrontToBack; |
| 55 | typedef LayerIterator<Layer, std::vector<scoped_refptr<Layer> >, RenderSurface, LayerIteratorActions::BackToFront> BackToFront; |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 56 | |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 57 | void resetCounts(std::vector<scoped_refptr<Layer> >& renderSurfaceLayerList) |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 58 | { |
| 59 | for (unsigned surfaceIndex = 0; surfaceIndex < renderSurfaceLayerList.size(); ++surfaceIndex) { |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 60 | TestLayer* renderSurfaceLayer = static_cast<TestLayer*>(renderSurfaceLayerList[surfaceIndex].get()); |
| 61 | RenderSurface* renderSurface = renderSurfaceLayer->renderSurface(); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 62 | |
| 63 | renderSurfaceLayer->m_countRepresentingTargetSurface = -1; |
| 64 | renderSurfaceLayer->m_countRepresentingContributingSurface = -1; |
| 65 | renderSurfaceLayer->m_countRepresentingItself = -1; |
| 66 | |
| 67 | for (unsigned layerIndex = 0; layerIndex < renderSurface->layerList().size(); ++layerIndex) { |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 68 | TestLayer* layer = static_cast<TestLayer*>(renderSurface->layerList()[layerIndex].get()); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 69 | |
| 70 | layer->m_countRepresentingTargetSurface = -1; |
| 71 | layer->m_countRepresentingContributingSurface = -1; |
| 72 | layer->m_countRepresentingItself = -1; |
| 73 | } |
| 74 | } |
| 75 | } |
| 76 | |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 77 | void iterateFrontToBack(std::vector<scoped_refptr<Layer> >* renderSurfaceLayerList) |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 78 | { |
| 79 | resetCounts(*renderSurfaceLayerList); |
| 80 | int count = 0; |
| 81 | for (FrontToBack it = FrontToBack::begin(renderSurfaceLayerList); it != FrontToBack::end(renderSurfaceLayerList); ++it, ++count) { |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 82 | TestLayer* layer = static_cast<TestLayer*>(*it); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 83 | if (it.representsTargetRenderSurface()) |
| 84 | layer->m_countRepresentingTargetSurface = count; |
| 85 | if (it.representsContributingRenderSurface()) |
| 86 | layer->m_countRepresentingContributingSurface = count; |
| 87 | if (it.representsItself()) |
| 88 | layer->m_countRepresentingItself = count; |
| 89 | } |
| 90 | } |
| 91 | |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 92 | void iterateBackToFront(std::vector<scoped_refptr<Layer> >* renderSurfaceLayerList) |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 93 | { |
| 94 | resetCounts(*renderSurfaceLayerList); |
| 95 | int count = 0; |
| 96 | for (BackToFront it = BackToFront::begin(renderSurfaceLayerList); it != BackToFront::end(renderSurfaceLayerList); ++it, ++count) { |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 97 | TestLayer* layer = static_cast<TestLayer*>(*it); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 98 | if (it.representsTargetRenderSurface()) |
| 99 | layer->m_countRepresentingTargetSurface = count; |
| 100 | if (it.representsContributingRenderSurface()) |
| 101 | layer->m_countRepresentingContributingSurface = count; |
| 102 | if (it.representsItself()) |
| 103 | layer->m_countRepresentingItself = count; |
| 104 | } |
| 105 | } |
| 106 | |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 107 | TEST(LayerIteratorTest, emptyTree) |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 108 | { |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 109 | std::vector<scoped_refptr<Layer> > renderSurfaceLayerList; |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 110 | |
| 111 | iterateBackToFront(&renderSurfaceLayerList); |
| 112 | iterateFrontToBack(&renderSurfaceLayerList); |
| 113 | } |
| 114 | |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 115 | TEST(LayerIteratorTest, simpleTree) |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 116 | { |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 117 | scoped_refptr<TestLayer> rootLayer = TestLayer::create(); |
| 118 | scoped_refptr<TestLayer> first = TestLayer::create(); |
| 119 | scoped_refptr<TestLayer> second = TestLayer::create(); |
| 120 | scoped_refptr<TestLayer> third = TestLayer::create(); |
| 121 | scoped_refptr<TestLayer> fourth = TestLayer::create(); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 122 | |
| 123 | rootLayer->createRenderSurface(); |
| 124 | |
| 125 | rootLayer->addChild(first); |
| 126 | rootLayer->addChild(second); |
| 127 | rootLayer->addChild(third); |
| 128 | rootLayer->addChild(fourth); |
| 129 | |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 130 | std::vector<scoped_refptr<Layer> > renderSurfaceLayerList; |
[email protected] | 518ee58 | 2012-10-24 18:29:44 | [diff] [blame] | 131 | LayerTreeHostCommon::calculateDrawTransforms(rootLayer.get(), rootLayer->bounds(), 1, 1, 256, renderSurfaceLayerList); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 132 | |
| 133 | iterateBackToFront(&renderSurfaceLayerList); |
| 134 | EXPECT_COUNT(rootLayer, 0, -1, 1); |
| 135 | EXPECT_COUNT(first, -1, -1, 2); |
| 136 | EXPECT_COUNT(second, -1, -1, 3); |
| 137 | EXPECT_COUNT(third, -1, -1, 4); |
| 138 | EXPECT_COUNT(fourth, -1, -1, 5); |
| 139 | |
| 140 | iterateFrontToBack(&renderSurfaceLayerList); |
| 141 | EXPECT_COUNT(rootLayer, 5, -1, 4); |
| 142 | EXPECT_COUNT(first, -1, -1, 3); |
| 143 | EXPECT_COUNT(second, -1, -1, 2); |
| 144 | EXPECT_COUNT(third, -1, -1, 1); |
| 145 | EXPECT_COUNT(fourth, -1, -1, 0); |
| 146 | |
| 147 | } |
| 148 | |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 149 | TEST(LayerIteratorTest, complexTree) |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 150 | { |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 151 | scoped_refptr<TestLayer> rootLayer = TestLayer::create(); |
| 152 | scoped_refptr<TestLayer> root1 = TestLayer::create(); |
| 153 | scoped_refptr<TestLayer> root2 = TestLayer::create(); |
| 154 | scoped_refptr<TestLayer> root3 = TestLayer::create(); |
| 155 | scoped_refptr<TestLayer> root21 = TestLayer::create(); |
| 156 | scoped_refptr<TestLayer> root22 = TestLayer::create(); |
| 157 | scoped_refptr<TestLayer> root23 = TestLayer::create(); |
| 158 | scoped_refptr<TestLayer> root221 = TestLayer::create(); |
| 159 | scoped_refptr<TestLayer> root231 = TestLayer::create(); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 160 | |
| 161 | rootLayer->createRenderSurface(); |
| 162 | |
| 163 | rootLayer->addChild(root1); |
| 164 | rootLayer->addChild(root2); |
| 165 | rootLayer->addChild(root3); |
| 166 | root2->addChild(root21); |
| 167 | root2->addChild(root22); |
| 168 | root2->addChild(root23); |
| 169 | root22->addChild(root221); |
| 170 | root23->addChild(root231); |
| 171 | |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 172 | std::vector<scoped_refptr<Layer> > renderSurfaceLayerList; |
[email protected] | 518ee58 | 2012-10-24 18:29:44 | [diff] [blame] | 173 | LayerTreeHostCommon::calculateDrawTransforms(rootLayer.get(), rootLayer->bounds(), 1, 1, 256, renderSurfaceLayerList); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 174 | |
| 175 | iterateBackToFront(&renderSurfaceLayerList); |
| 176 | EXPECT_COUNT(rootLayer, 0, -1, 1); |
| 177 | EXPECT_COUNT(root1, -1, -1, 2); |
| 178 | EXPECT_COUNT(root2, -1, -1, 3); |
| 179 | EXPECT_COUNT(root21, -1, -1, 4); |
| 180 | EXPECT_COUNT(root22, -1, -1, 5); |
| 181 | EXPECT_COUNT(root221, -1, -1, 6); |
| 182 | EXPECT_COUNT(root23, -1, -1, 7); |
| 183 | EXPECT_COUNT(root231, -1, -1, 8); |
| 184 | EXPECT_COUNT(root3, -1, -1, 9); |
| 185 | |
| 186 | iterateFrontToBack(&renderSurfaceLayerList); |
| 187 | EXPECT_COUNT(rootLayer, 9, -1, 8); |
| 188 | EXPECT_COUNT(root1, -1, -1, 7); |
| 189 | EXPECT_COUNT(root2, -1, -1, 6); |
| 190 | EXPECT_COUNT(root21, -1, -1, 5); |
| 191 | EXPECT_COUNT(root22, -1, -1, 4); |
| 192 | EXPECT_COUNT(root221, -1, -1, 3); |
| 193 | EXPECT_COUNT(root23, -1, -1, 2); |
| 194 | EXPECT_COUNT(root231, -1, -1, 1); |
| 195 | EXPECT_COUNT(root3, -1, -1, 0); |
| 196 | |
| 197 | } |
| 198 | |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 199 | TEST(LayerIteratorTest, complexTreeMultiSurface) |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 200 | { |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 201 | scoped_refptr<TestLayer> rootLayer = TestLayer::create(); |
| 202 | scoped_refptr<TestLayer> root1 = TestLayer::create(); |
| 203 | scoped_refptr<TestLayer> root2 = TestLayer::create(); |
| 204 | scoped_refptr<TestLayer> root3 = TestLayer::create(); |
| 205 | scoped_refptr<TestLayer> root21 = TestLayer::create(); |
| 206 | scoped_refptr<TestLayer> root22 = TestLayer::create(); |
| 207 | scoped_refptr<TestLayer> root23 = TestLayer::create(); |
| 208 | scoped_refptr<TestLayer> root221 = TestLayer::create(); |
| 209 | scoped_refptr<TestLayer> root231 = TestLayer::create(); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 210 | |
| 211 | rootLayer->createRenderSurface(); |
[email protected] | aad0a007 | 2012-11-01 18:15:58 | [diff] [blame] | 212 | rootLayer->renderSurface()->setContentRect(gfx::Rect(gfx::Point(), rootLayer->bounds())); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 213 | |
| 214 | rootLayer->addChild(root1); |
| 215 | rootLayer->addChild(root2); |
| 216 | rootLayer->addChild(root3); |
| 217 | root2->setDrawsContent(false); |
| 218 | root2->setOpacity(0.5); // Force the layer to own a new surface. |
| 219 | root2->addChild(root21); |
| 220 | root2->addChild(root22); |
| 221 | root2->addChild(root23); |
| 222 | root22->setOpacity(0.5); |
| 223 | root22->addChild(root221); |
| 224 | root23->setOpacity(0.5); |
| 225 | root23->addChild(root231); |
| 226 | |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 227 | std::vector<scoped_refptr<Layer> > renderSurfaceLayerList; |
[email protected] | 518ee58 | 2012-10-24 18:29:44 | [diff] [blame] | 228 | LayerTreeHostCommon::calculateDrawTransforms(rootLayer.get(), rootLayer->bounds(), 1, 1, 256, renderSurfaceLayerList); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 229 | |
| 230 | iterateBackToFront(&renderSurfaceLayerList); |
| 231 | EXPECT_COUNT(rootLayer, 0, -1, 1); |
| 232 | EXPECT_COUNT(root1, -1, -1, 2); |
| 233 | EXPECT_COUNT(root2, 4, 3, -1); |
| 234 | EXPECT_COUNT(root21, -1, -1, 5); |
| 235 | EXPECT_COUNT(root22, 7, 6, 8); |
| 236 | EXPECT_COUNT(root221, -1, -1, 9); |
| 237 | EXPECT_COUNT(root23, 11, 10, 12); |
| 238 | EXPECT_COUNT(root231, -1, -1, 13); |
| 239 | EXPECT_COUNT(root3, -1, -1, 14); |
| 240 | |
| 241 | iterateFrontToBack(&renderSurfaceLayerList); |
| 242 | EXPECT_COUNT(rootLayer, 14, -1, 13); |
| 243 | EXPECT_COUNT(root1, -1, -1, 12); |
| 244 | EXPECT_COUNT(root2, 10, 11, -1); |
| 245 | EXPECT_COUNT(root21, -1, -1, 9); |
| 246 | EXPECT_COUNT(root22, 7, 8, 6); |
| 247 | EXPECT_COUNT(root221, -1, -1, 5); |
| 248 | EXPECT_COUNT(root23, 3, 4, 2); |
| 249 | EXPECT_COUNT(root231, -1, -1, 1); |
| 250 | EXPECT_COUNT(root3, -1, -1, 0); |
| 251 | } |
| 252 | |
[email protected] | ba56574 | 2012-11-10 09:29:48 | [diff] [blame^] | 253 | } // namespace |
| 254 | } // namespace cc |