deca4acefbe66910ca51cb5e61d34eb8741ae829
[WebKit-https.git] / Source / WebKit / chromium / tests / CCOcclusionTrackerTest.cpp
1 /*
2  * Copyright (C) 2012 Google Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1.  Redistributions of source code must retain the above copyright
8  *     notice, this list of conditions and the following disclaimer.
9  * 2.  Redistributions in binary form must reproduce the above copyright
10  *     notice, this list of conditions and the following disclaimer in the
11  *     documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND ANY
14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
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 ANY
17  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
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 ON
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
22  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23  */
24
25 #include "config.h"
26
27 #include "cc/CCOcclusionTracker.h"
28
29 #include "FilterOperations.h"
30 #include "LayerChromium.h"
31 #include "Region.h"
32 #include "TransformationMatrix.h"
33 #include "cc/CCLayerImpl.h"
34 #include "cc/CCLayerTreeHostCommon.h"
35 #include "cc/CCSingleThreadProxy.h"
36
37 #include <gmock/gmock.h>
38 #include <gtest/gtest.h>
39
40 using namespace WebCore;
41
42 #define EXPECT_EQ_RECT(a, b) \
43     EXPECT_EQ(a.x(), b.x()); \
44     EXPECT_EQ(a.y(), b.y()); \
45     EXPECT_EQ(a.width(), b.width()); \
46     EXPECT_EQ(a.height(), b.height());
47
48 namespace {
49
50 class TestContentLayerChromium : public LayerChromium {
51 public:
52     TestContentLayerChromium() : LayerChromium() { }
53
54     virtual bool drawsContent() const { return true; }
55     virtual Region opaqueContentsRegion() const { return intersection(m_opaqueContentsRect, visibleLayerRect()); }
56     void setOpaqueContentsRect(const IntRect& opaqueContentsRect) { m_opaqueContentsRect = opaqueContentsRect; }
57
58 private:
59     IntRect m_opaqueContentsRect;
60 };
61
62 class TestContentLayerImpl : public CCLayerImpl {
63 public:
64     TestContentLayerImpl(int id) : CCLayerImpl(id) { setDrawsContent(true); }
65
66     virtual Region opaqueContentsRegion() const { return intersection(m_opaqueContentsRect, visibleLayerRect()); }
67     void setOpaqueContentsRect(const IntRect& opaqueContentsRect) { m_opaqueContentsRect = opaqueContentsRect; }
68 private:
69     IntRect m_opaqueContentsRect;
70 };
71
72 // A subclass to expose the total current occlusion.
73 template<typename LayerType, typename RenderSurfaceType>
74 class TestCCOcclusionTrackerBase : public CCOcclusionTrackerBase<LayerType, RenderSurfaceType> {
75 public:
76     TestCCOcclusionTrackerBase(IntRect screenScissorRect)
77         : CCOcclusionTrackerBase<LayerType, RenderSurfaceType>(screenScissorRect)
78         , m_overrideLayerScissorRect(false)
79     {
80     }
81
82     TestCCOcclusionTrackerBase(IntRect screenScissorRect, const CCOcclusionTrackerDamageClientBase<RenderSurfaceType>* damageClient)
83         : CCOcclusionTrackerBase<LayerType, RenderSurfaceType>(screenScissorRect, damageClient)
84         , m_overrideLayerScissorRect(false)
85     {
86     }
87
88     Region occlusionInScreenSpace() const { return CCOcclusionTrackerBase<LayerType, RenderSurfaceType>::m_stack.last().occlusionInScreen; }
89     Region occlusionInTargetSurface() const { return CCOcclusionTrackerBase<LayerType, RenderSurfaceType>::m_stack.last().occlusionInTarget; }
90
91     void setOcclusionInScreenSpace(const Region& region) { CCOcclusionTrackerBase<LayerType, RenderSurfaceType>::m_stack.last().occlusionInScreen = region; }
92     void setOcclusionInTargetSurface(const Region& region) { CCOcclusionTrackerBase<LayerType, RenderSurfaceType>::m_stack.last().occlusionInTarget = region; }
93
94     void setLayerScissorRect(const IntRect& rect) { m_overrideLayerScissorRect = true; m_layerScissorRect = rect;}
95     void useDefaultLayerScissorRect() { m_overrideLayerScissorRect = false; }
96
97 protected:
98     virtual IntRect layerScissorRectInTargetSurface(const LayerType* layer) const { return m_overrideLayerScissorRect ? m_layerScissorRect : CCOcclusionTrackerBase<LayerType, RenderSurfaceType>::layerScissorRectInTargetSurface(layer); }
99
100 private:
101     bool m_overrideLayerScissorRect;
102     IntRect m_layerScissorRect;
103 };
104
105 template<typename RenderSurfaceType>
106 class TestDamageClient : public CCOcclusionTrackerDamageClientBase<RenderSurfaceType> {
107 public:
108     // The interface
109     virtual FloatRect damageRect(const RenderSurfaceType*) const { return m_damageRect; }
110
111     // Testing stuff
112     TestDamageClient(const FloatRect& damageRect) : m_damageRect(damageRect) { }
113     void setDamageRect(const FloatRect& damageRect) { m_damageRect = damageRect; }
114
115 private:
116     FloatRect m_damageRect;
117 };
118
119 struct CCOcclusionTrackerTestMainThreadTypes {
120     typedef LayerChromium LayerType;
121     typedef RenderSurfaceChromium RenderSurfaceType;
122     typedef TestContentLayerChromium ContentLayerType;
123     typedef RefPtr<LayerChromium> LayerPtrType;
124     typedef PassRefPtr<LayerChromium> PassLayerPtrType;
125     typedef RefPtr<ContentLayerType> ContentLayerPtrType;
126     typedef PassRefPtr<ContentLayerType> PassContentLayerPtrType;
127
128     static PassLayerPtrType createLayer() { return LayerChromium::create(); }
129     static PassContentLayerPtrType createContentLayer() { return adoptRef(new ContentLayerType()); }
130 };
131
132 struct CCOcclusionTrackerTestImplThreadTypes {
133     typedef CCLayerImpl LayerType;
134     typedef CCRenderSurface RenderSurfaceType;
135     typedef TestContentLayerImpl ContentLayerType;
136     typedef OwnPtr<CCLayerImpl> LayerPtrType;
137     typedef PassOwnPtr<CCLayerImpl> PassLayerPtrType;
138     typedef OwnPtr<ContentLayerType> ContentLayerPtrType;
139     typedef PassOwnPtr<ContentLayerType> PassContentLayerPtrType;
140
141     static PassLayerPtrType createLayer() { return CCLayerImpl::create(nextCCLayerImplId++); }
142     static PassContentLayerPtrType createContentLayer() { return adoptPtr(new ContentLayerType(nextCCLayerImplId++)); }
143     static int nextCCLayerImplId;
144 };
145
146 int CCOcclusionTrackerTestImplThreadTypes::nextCCLayerImplId = 0;
147
148 template<typename Types, bool opaqueLayers>
149 class CCOcclusionTrackerTest : public testing::Test {
150 protected:
151     CCOcclusionTrackerTest() : testing::Test() { }
152
153     virtual void runMyTest() = 0;
154
155     virtual void TearDown()
156     {
157         m_root.clear();
158         m_renderSurfaceLayerListChromium.clear();
159         m_renderSurfaceLayerListImpl.clear();
160     }
161
162     typename Types::ContentLayerType* createRoot(const TransformationMatrix& transform, const FloatPoint& position, const IntSize& bounds)
163     {
164         typename Types::ContentLayerPtrType layer(Types::createContentLayer());
165         typename Types::ContentLayerType* layerPtr = layer.get();
166         setProperties(layerPtr, transform, position, bounds);
167
168         ASSERT(!m_root);
169         m_root = layer.release();
170         return layerPtr;
171     }
172
173     typename Types::LayerType* createLayer(typename Types::LayerType* parent, const TransformationMatrix& transform, const FloatPoint& position, const IntSize& bounds)
174     {
175         typename Types::LayerPtrType layer(Types::createLayer());
176         typename Types::LayerType* layerPtr = layer.get();
177         setProperties(layerPtr, transform, position, bounds);
178         parent->addChild(layer.release());
179         return layerPtr;
180     }
181
182     typename Types::LayerType* createSurface(typename Types::LayerType* parent, const TransformationMatrix& transform, const FloatPoint& position, const IntSize& bounds)
183     {
184         typename Types::LayerType* layer = createLayer(parent, transform, position, bounds);
185         FilterOperations filters;
186         filters.operations().append(BasicComponentTransferFilterOperation::create(0.5, FilterOperation::GRAYSCALE));
187         layer->setFilters(filters);
188         return layer;
189     }
190
191     typename Types::ContentLayerType* createDrawingLayer(typename Types::LayerType* parent, const TransformationMatrix& transform, const FloatPoint& position, const IntSize& bounds, bool opaque)
192     {
193         typename Types::ContentLayerPtrType layer(Types::createContentLayer());
194         typename Types::ContentLayerType* layerPtr = layer.get();
195         setProperties(layerPtr, transform, position, bounds);
196
197         if (opaqueLayers)
198             layerPtr->setOpaque(opaque);
199         else {
200             layerPtr->setOpaque(false);
201             if (opaque)
202                 layerPtr->setOpaqueContentsRect(IntRect(IntPoint(), bounds));
203             else
204                 layerPtr->setOpaqueContentsRect(IntRect());
205         }
206
207         parent->addChild(layer.release());
208         return layerPtr;
209     }
210
211     typename Types::ContentLayerType* createDrawingSurface(typename Types::LayerType* parent, const TransformationMatrix& transform, const FloatPoint& position, const IntSize& bounds, bool opaque)
212     {
213         typename Types::ContentLayerType* layer = createDrawingLayer(parent, transform, position, bounds, opaque);
214         FilterOperations filters;
215         filters.operations().append(BasicComponentTransferFilterOperation::create(0.5, FilterOperation::GRAYSCALE));
216         layer->setFilters(filters);
217         return layer;
218     }
219
220
221     TestContentLayerImpl* calcDrawEtc(TestContentLayerImpl* root)
222     {
223         ASSERT(root == m_root.get());
224         Vector<CCLayerImpl*> dummyLayerList;
225         int dummyMaxTextureSize = 512;
226
227         ASSERT(!root->renderSurface());
228         root->createRenderSurface();
229         root->renderSurface()->setContentRect(IntRect(IntPoint::zero(), root->bounds()));
230         root->setClipRect(IntRect(IntPoint::zero(), root->bounds()));
231         m_renderSurfaceLayerListImpl.append(m_root.get());
232
233         CCLayerTreeHostCommon::calculateDrawTransformsAndVisibility(root, root, identityMatrix, identityMatrix, m_renderSurfaceLayerListImpl, dummyLayerList, 0, dummyMaxTextureSize);
234         return root;
235     }
236
237     TestContentLayerChromium* calcDrawEtc(TestContentLayerChromium* root)
238     {
239         ASSERT(root == m_root.get());
240         Vector<RefPtr<LayerChromium> > dummyLayerList;
241         int dummyMaxTextureSize = 512;
242
243         ASSERT(!root->renderSurface());
244         root->createRenderSurface();
245         root->renderSurface()->setContentRect(IntRect(IntPoint::zero(), root->bounds()));
246         root->setClipRect(IntRect(IntPoint::zero(), root->bounds()));
247         m_renderSurfaceLayerListChromium.append(m_root);
248
249         CCLayerTreeHostCommon::calculateDrawTransformsAndVisibility(root, root, identityMatrix, identityMatrix, m_renderSurfaceLayerListChromium, dummyLayerList, dummyMaxTextureSize);
250         return root;
251     }
252
253     const TransformationMatrix identityMatrix;
254
255 private:
256     void setBaseProperties(typename Types::LayerType* layer, const TransformationMatrix& transform, const FloatPoint& position, const IntSize& bounds)
257     {
258         layer->setTransform(transform);
259         layer->setSublayerTransform(TransformationMatrix());
260         layer->setAnchorPoint(FloatPoint(0, 0));
261         layer->setPosition(position);
262         layer->setBounds(bounds);
263     }
264
265     void setProperties(LayerChromium* layer, const TransformationMatrix& transform, const FloatPoint& position, const IntSize& bounds)
266     {
267         setBaseProperties(layer, transform, position, bounds);
268     }
269
270     void setProperties(CCLayerImpl* layer, const TransformationMatrix& transform, const FloatPoint& position, const IntSize& bounds)
271     {
272         setBaseProperties(layer, transform, position, bounds);
273
274         layer->setContentBounds(layer->bounds());
275     }
276
277     // These hold ownership of the layers for the duration of the test.
278     typename Types::LayerPtrType m_root;
279     Vector<RefPtr<LayerChromium> > m_renderSurfaceLayerListChromium;
280     Vector<CCLayerImpl*> m_renderSurfaceLayerListImpl;
281 };
282
283 #define RUN_TEST_MAIN_THREAD_OPAQUE_LAYERS(ClassName) \
284     class ClassName##MainThreadOpaqueLayers : public ClassName<CCOcclusionTrackerTestMainThreadTypes, true> { \
285     public: \
286         ClassName##MainThreadOpaqueLayers() : ClassName<CCOcclusionTrackerTestMainThreadTypes, true>() { } \
287     }; \
288     TEST_F(ClassName##MainThreadOpaqueLayers, runTest) { runMyTest(); }
289 #define RUN_TEST_MAIN_THREAD_OPAQUE_PAINTS(ClassName) \
290     class ClassName##MainThreadOpaquePaints : public ClassName<CCOcclusionTrackerTestMainThreadTypes, false> { \
291     public: \
292         ClassName##MainThreadOpaquePaints() : ClassName<CCOcclusionTrackerTestMainThreadTypes, false>() { } \
293     }; \
294     TEST_F(ClassName##MainThreadOpaquePaints, runTest) { runMyTest(); }
295
296 #define RUN_TEST_IMPL_THREAD_OPAQUE_LAYERS(ClassName) \
297     class ClassName##ImplThreadOpaqueLayers : public ClassName<CCOcclusionTrackerTestImplThreadTypes, true> { \
298         DebugScopedSetImplThread impl; \
299     public: \
300         ClassName##ImplThreadOpaqueLayers() : ClassName<CCOcclusionTrackerTestImplThreadTypes, true>() { } \
301     }; \
302     TEST_F(ClassName##ImplThreadOpaqueLayers, runTest) { runMyTest(); }
303 #define RUN_TEST_IMPL_THREAD_OPAQUE_PAINTS(ClassName) \
304     class ClassName##ImplThreadOpaquePaints : public ClassName<CCOcclusionTrackerTestImplThreadTypes, false> { \
305         DebugScopedSetImplThread impl; \
306     public: \
307         ClassName##ImplThreadOpaquePaints() : ClassName<CCOcclusionTrackerTestImplThreadTypes, false>() { } \
308     }; \
309     TEST_F(ClassName##ImplThreadOpaquePaints, runTest) { runMyTest(); }
310
311 #define ALL_CCOCCLUSIONTRACKER_TEST(ClassName) \
312     RUN_TEST_MAIN_THREAD_OPAQUE_LAYERS(ClassName) \
313     RUN_TEST_MAIN_THREAD_OPAQUE_PAINTS(ClassName) \
314     RUN_TEST_IMPL_THREAD_OPAQUE_LAYERS(ClassName) \
315     RUN_TEST_IMPL_THREAD_OPAQUE_PAINTS(ClassName)
316
317 #define MAIN_THREAD_TEST(ClassName) \
318     RUN_TEST_MAIN_THREAD_OPAQUE_LAYERS(ClassName)
319
320 #define MAIN_AND_IMPL_THREAD_TEST(ClassName) \
321     RUN_TEST_MAIN_THREAD_OPAQUE_LAYERS(ClassName) \
322     RUN_TEST_IMPL_THREAD_OPAQUE_LAYERS(ClassName)
323
324 template<class Types, bool opaqueLayers>
325 class CCOcclusionTrackerTestIdentityTransforms : public CCOcclusionTrackerTest<Types, opaqueLayers> {
326 protected:
327     void runMyTest()
328     {
329         typename Types::ContentLayerType* parent = this->createRoot(this->identityMatrix, FloatPoint(0, 0), IntSize(100, 100));
330         typename Types::ContentLayerType* layer = this->createDrawingLayer(parent, this->identityMatrix, FloatPoint(30, 30), IntSize(500, 500), true);
331         this->calcDrawEtc(parent);
332
333         TestCCOcclusionTrackerBase<typename Types::LayerType, typename Types::RenderSurfaceType> occlusion(IntRect(0, 0, 1000, 1000));
334         occlusion.setLayerScissorRect(IntRect(0, 0, 1000, 1000));
335
336         occlusion.enterTargetRenderSurface(parent->renderSurface());
337         occlusion.markOccludedBehindLayer(layer);
338         EXPECT_EQ_RECT(IntRect(30, 30, 70, 70), occlusion.occlusionInScreenSpace().bounds());
339         EXPECT_EQ(1u, occlusion.occlusionInScreenSpace().rects().size());
340         EXPECT_EQ_RECT(IntRect(30, 30, 70, 70), occlusion.occlusionInTargetSurface().bounds());
341         EXPECT_EQ(1u, occlusion.occlusionInTargetSurface().rects().size());
342
343         EXPECT_TRUE(occlusion.occluded(parent, IntRect(30, 30, 70, 70)));
344         EXPECT_FALSE(occlusion.occluded(parent, IntRect(29, 30, 70, 70)));
345         EXPECT_FALSE(occlusion.occluded(parent, IntRect(30, 29, 70, 70)));
346         EXPECT_FALSE(occlusion.occluded(parent, IntRect(31, 30, 70, 70)));
347         EXPECT_FALSE(occlusion.occluded(parent, IntRect(30, 31, 70, 70)));
348
349         occlusion.useDefaultLayerScissorRect();
350         EXPECT_TRUE(occlusion.occluded(parent, IntRect(30, 30, 70, 70)));
351         EXPECT_FALSE(occlusion.occluded(parent, IntRect(29, 30, 70, 70)));
352         EXPECT_FALSE(occlusion.occluded(parent, IntRect(30, 29, 70, 70)));
353         EXPECT_TRUE(occlusion.occluded(parent, IntRect(31, 30, 70, 70)));
354         EXPECT_TRUE(occlusion.occluded(parent, IntRect(30, 31, 70, 70)));
355         occlusion.setLayerScissorRect(IntRect(0, 0, 1000, 1000));
356
357         EXPECT_TRUE(occlusion.unoccludedContentRect(parent, IntRect(30, 30, 70, 70)).isEmpty());
358         EXPECT_EQ_RECT(IntRect(29, 30, 1, 70), occlusion.unoccludedContentRect(parent, IntRect(29, 30, 70, 70)));
359         EXPECT_EQ_RECT(IntRect(29, 29, 70, 70), occlusion.unoccludedContentRect(parent, IntRect(29, 29, 70, 70)));
360         EXPECT_EQ_RECT(IntRect(30, 29, 70, 1), occlusion.unoccludedContentRect(parent, IntRect(30, 29, 70, 70)));
361         EXPECT_EQ_RECT(IntRect(31, 29, 70, 70), occlusion.unoccludedContentRect(parent, IntRect(31, 29, 70, 70)));
362         EXPECT_EQ_RECT(IntRect(100, 30, 1, 70), occlusion.unoccludedContentRect(parent, IntRect(31, 30, 70, 70)));
363         EXPECT_EQ_RECT(IntRect(31, 31, 70, 70), occlusion.unoccludedContentRect(parent, IntRect(31, 31, 70, 70)));
364         EXPECT_EQ_RECT(IntRect(30, 100, 70, 1), occlusion.unoccludedContentRect(parent, IntRect(30, 31, 70, 70)));
365         EXPECT_EQ_RECT(IntRect(29, 31, 70, 70), occlusion.unoccludedContentRect(parent, IntRect(29, 31, 70, 70)));
366     }
367 };
368
369 ALL_CCOCCLUSIONTRACKER_TEST(CCOcclusionTrackerTestIdentityTransforms);
370
371 template<class Types, bool opaqueLayers>
372 class CCOcclusionTrackerTestRotatedChild : public CCOcclusionTrackerTest<Types, opaqueLayers> {
373 protected:
374     void runMyTest()
375     {
376         TransformationMatrix layerTransform;
377         layerTransform.translate(250, 250);
378         layerTransform.rotate(90);
379         layerTransform.translate(-250, -250);
380
381         typename Types::ContentLayerType* parent = this->createRoot(this->identityMatrix, FloatPoint(0, 0), IntSize(100, 100));
382         typename Types::ContentLayerType* layer = this->createDrawingLayer(parent, layerTransform, FloatPoint(30, 30), IntSize(500, 500), true);
383         this->calcDrawEtc(parent);
384
385         TestCCOcclusionTrackerBase<typename Types::LayerType, typename Types::RenderSurfaceType> occlusion(IntRect(0, 0, 1000, 1000));
386         occlusion.setLayerScissorRect(IntRect(0, 0, 1000, 1000));
387
388         occlusion.enterTargetRenderSurface(parent->renderSurface());
389         occlusion.markOccludedBehindLayer(layer);
390         EXPECT_EQ_RECT(IntRect(30, 30, 70, 70), occlusion.occlusionInScreenSpace().bounds());
391         EXPECT_EQ(1u, occlusion.occlusionInScreenSpace().rects().size());
392         EXPECT_EQ_RECT(IntRect(30, 30, 70, 70), occlusion.occlusionInTargetSurface().bounds());
393         EXPECT_EQ(1u, occlusion.occlusionInTargetSurface().rects().size());
394
395         EXPECT_TRUE(occlusion.occluded(parent, IntRect(30, 30, 70, 70)));
396         EXPECT_FALSE(occlusion.occluded(parent, IntRect(29, 30, 70, 70)));
397         EXPECT_FALSE(occlusion.occluded(parent, IntRect(30, 29, 70, 70)));
398         EXPECT_FALSE(occlusion.occluded(parent, IntRect(31, 30, 70, 70)));
399         EXPECT_FALSE(occlusion.occluded(parent, IntRect(30, 31, 70, 70)));
400
401         occlusion.useDefaultLayerScissorRect();
402         EXPECT_TRUE(occlusion.occluded(parent, IntRect(30, 30, 70, 70)));
403         EXPECT_FALSE(occlusion.occluded(parent, IntRect(29, 30, 70, 70)));
404         EXPECT_FALSE(occlusion.occluded(parent, IntRect(30, 29, 70, 70)));
405         EXPECT_TRUE(occlusion.occluded(parent, IntRect(31, 30, 70, 70)));
406         EXPECT_TRUE(occlusion.occluded(parent, IntRect(30, 31, 70, 70)));
407         occlusion.setLayerScissorRect(IntRect(0, 0, 1000, 1000));
408
409         EXPECT_TRUE(occlusion.unoccludedContentRect(parent, IntRect(30, 30, 70, 70)).isEmpty());
410         EXPECT_EQ_RECT(IntRect(29, 30, 1, 70), occlusion.unoccludedContentRect(parent, IntRect(29, 30, 70, 70)));
411         EXPECT_EQ_RECT(IntRect(29, 29, 70, 70), occlusion.unoccludedContentRect(parent, IntRect(29, 29, 70, 70)));
412         EXPECT_EQ_RECT(IntRect(30, 29, 70, 1), occlusion.unoccludedContentRect(parent, IntRect(30, 29, 70, 70)));
413         EXPECT_EQ_RECT(IntRect(31, 29, 70, 70), occlusion.unoccludedContentRect(parent, IntRect(31, 29, 70, 70)));
414         EXPECT_EQ_RECT(IntRect(100, 30, 1, 70), occlusion.unoccludedContentRect(parent, IntRect(31, 30, 70, 70)));
415         EXPECT_EQ_RECT(IntRect(31, 31, 70, 70), occlusion.unoccludedContentRect(parent, IntRect(31, 31, 70, 70)));
416         EXPECT_EQ_RECT(IntRect(30, 100, 70, 1), occlusion.unoccludedContentRect(parent, IntRect(30, 31, 70, 70)));
417         EXPECT_EQ_RECT(IntRect(29, 31, 70, 70), occlusion.unoccludedContentRect(parent, IntRect(29, 31, 70, 70)));
418     }
419 };
420
421 ALL_CCOCCLUSIONTRACKER_TEST(CCOcclusionTrackerTestRotatedChild);
422
423 template<class Types, bool opaqueLayers>
424 class CCOcclusionTrackerTestTranslatedChild : public CCOcclusionTrackerTest<Types, opaqueLayers> {
425 protected:
426     void runMyTest()
427     {
428         TransformationMatrix layerTransform;
429         layerTransform.translate(20, 20);
430
431         typename Types::ContentLayerType* parent = this->createRoot(this->identityMatrix, FloatPoint(0, 0), IntSize(100, 100));
432         typename Types::ContentLayerType* layer = this->createDrawingLayer(parent, layerTransform, FloatPoint(30, 30), IntSize(500, 500), true);
433         this->calcDrawEtc(parent);
434
435         TestCCOcclusionTrackerBase<typename Types::LayerType, typename Types::RenderSurfaceType> occlusion(IntRect(0, 0, 1000, 1000));
436         occlusion.setLayerScissorRect(IntRect(0, 0, 1000, 1000));
437
438         occlusion.enterTargetRenderSurface(parent->renderSurface());
439         occlusion.markOccludedBehindLayer(layer);
440         EXPECT_EQ_RECT(IntRect(50, 50, 50, 50), occlusion.occlusionInScreenSpace().bounds());
441         EXPECT_EQ(1u, occlusion.occlusionInScreenSpace().rects().size());
442         EXPECT_EQ_RECT(IntRect(50, 50, 50, 50), occlusion.occlusionInTargetSurface().bounds());
443         EXPECT_EQ(1u, occlusion.occlusionInTargetSurface().rects().size());
444
445         EXPECT_TRUE(occlusion.occluded(parent, IntRect(50, 50, 50, 50)));
446         EXPECT_FALSE(occlusion.occluded(parent, IntRect(49, 50, 50, 50)));
447         EXPECT_FALSE(occlusion.occluded(parent, IntRect(50, 49, 50, 50)));
448         EXPECT_FALSE(occlusion.occluded(parent, IntRect(51, 50, 50, 50)));
449         EXPECT_FALSE(occlusion.occluded(parent, IntRect(50, 51, 50, 50)));
450
451         occlusion.useDefaultLayerScissorRect();
452         EXPECT_TRUE(occlusion.occluded(parent, IntRect(50, 50, 50, 50)));
453         EXPECT_FALSE(occlusion.occluded(parent, IntRect(49, 50, 50, 50)));
454         EXPECT_FALSE(occlusion.occluded(parent, IntRect(50, 49, 50, 50)));
455         EXPECT_TRUE(occlusion.occluded(parent, IntRect(51, 50, 50, 50)));
456         EXPECT_TRUE(occlusion.occluded(parent, IntRect(50, 51, 50, 50)));
457         occlusion.setLayerScissorRect(IntRect(0, 0, 1000, 1000));
458
459         EXPECT_TRUE(occlusion.unoccludedContentRect(parent, IntRect(50, 50, 50, 50)).isEmpty());
460         EXPECT_EQ_RECT(IntRect(49, 50, 1, 50), occlusion.unoccludedContentRect(parent, IntRect(49, 50, 50, 50)));
461         EXPECT_EQ_RECT(IntRect(49, 49, 50, 50), occlusion.unoccludedContentRect(parent, IntRect(49, 49, 50, 50)));
462         EXPECT_EQ_RECT(IntRect(50, 49, 50, 1), occlusion.unoccludedContentRect(parent, IntRect(50, 49, 50, 50)));
463         EXPECT_EQ_RECT(IntRect(51, 49, 50, 50), occlusion.unoccludedContentRect(parent, IntRect(51, 49, 50, 50)));
464         EXPECT_EQ_RECT(IntRect(100, 50, 1, 50), occlusion.unoccludedContentRect(parent, IntRect(51, 50, 50, 50)));
465         EXPECT_EQ_RECT(IntRect(51, 51, 50, 50), occlusion.unoccludedContentRect(parent, IntRect(51, 51, 50, 50)));
466         EXPECT_EQ_RECT(IntRect(50, 100, 50, 1), occlusion.unoccludedContentRect(parent, IntRect(50, 51, 50, 50)));
467         EXPECT_EQ_RECT(IntRect(49, 51, 50, 50), occlusion.unoccludedContentRect(parent, IntRect(49, 51, 50, 50)));
468
469         occlusion.useDefaultLayerScissorRect();
470         EXPECT_TRUE(occlusion.unoccludedContentRect(parent, IntRect(50, 50, 50, 50)).isEmpty());
471         EXPECT_EQ_RECT(IntRect(49, 50, 1, 50), occlusion.unoccludedContentRect(parent, IntRect(49, 50, 50, 50)));
472         EXPECT_EQ_RECT(IntRect(49, 49, 50, 50), occlusion.unoccludedContentRect(parent, IntRect(49, 49, 50, 50)));
473         EXPECT_EQ_RECT(IntRect(50, 49, 50, 1), occlusion.unoccludedContentRect(parent, IntRect(50, 49, 50, 50)));
474         EXPECT_EQ_RECT(IntRect(51, 49, 49, 1), occlusion.unoccludedContentRect(parent, IntRect(51, 49, 50, 50)));
475         EXPECT_TRUE(occlusion.unoccludedContentRect(parent, IntRect(51, 50, 50, 50)).isEmpty());
476         EXPECT_TRUE(occlusion.unoccludedContentRect(parent, IntRect(51, 51, 50, 50)).isEmpty());
477         EXPECT_TRUE(occlusion.unoccludedContentRect(parent, IntRect(50, 51, 50, 50)).isEmpty());
478         EXPECT_EQ_RECT(IntRect(49, 51, 1, 49), occlusion.unoccludedContentRect(parent, IntRect(49, 51, 50, 50)));
479         occlusion.setLayerScissorRect(IntRect(0, 0, 1000, 1000));
480     }
481 };
482
483 ALL_CCOCCLUSIONTRACKER_TEST(CCOcclusionTrackerTestTranslatedChild);
484
485 template<class Types, bool opaqueLayers>
486 class CCOcclusionTrackerTestChildInRotatedChild : public CCOcclusionTrackerTest<Types, opaqueLayers> {
487 protected:
488     void runMyTest()
489     {
490         TransformationMatrix childTransform;
491         childTransform.translate(250, 250);
492         childTransform.rotate(90);
493         childTransform.translate(-250, -250);
494
495         typename Types::ContentLayerType* parent = this->createRoot(this->identityMatrix, FloatPoint(0, 0), IntSize(100, 100));
496         typename Types::LayerType* child = this->createLayer(parent, childTransform, FloatPoint(30, 30), IntSize(500, 500));
497         child->setMasksToBounds(true);
498         typename Types::ContentLayerType* layer = this->createDrawingLayer(child, this->identityMatrix, FloatPoint(10, 10), IntSize(500, 500), true);
499         this->calcDrawEtc(parent);
500
501         TestCCOcclusionTrackerBase<typename Types::LayerType, typename Types::RenderSurfaceType> occlusion(IntRect(0, 0, 1000, 1000));
502         occlusion.setLayerScissorRect(IntRect(0, 0, 1000, 1000));
503
504         occlusion.enterTargetRenderSurface(child->renderSurface());
505         occlusion.markOccludedBehindLayer(layer);
506
507         EXPECT_EQ_RECT(IntRect(30, 40, 70, 60), occlusion.occlusionInScreenSpace().bounds());
508         EXPECT_EQ(1u, occlusion.occlusionInScreenSpace().rects().size());
509         EXPECT_EQ_RECT(IntRect(10, 430, 60, 70), occlusion.occlusionInTargetSurface().bounds());
510         EXPECT_EQ(1u, occlusion.occlusionInTargetSurface().rects().size());
511
512         EXPECT_TRUE(occlusion.occluded(child, IntRect(10, 430, 60, 70)));
513         EXPECT_FALSE(occlusion.occluded(child, IntRect(9, 430, 60, 70)));
514         EXPECT_FALSE(occlusion.occluded(child, IntRect(10, 429, 60, 70)));
515         EXPECT_FALSE(occlusion.occluded(child, IntRect(10, 430, 61, 70)));
516         EXPECT_FALSE(occlusion.occluded(child, IntRect(10, 430, 60, 71)));
517
518         occlusion.useDefaultLayerScissorRect();
519         EXPECT_TRUE(occlusion.occluded(child, IntRect(10, 430, 60, 70)));
520         EXPECT_TRUE(occlusion.occluded(child, IntRect(9, 430, 60, 70)));
521         EXPECT_TRUE(occlusion.occluded(child, IntRect(10, 429, 60, 70)));
522         EXPECT_TRUE(occlusion.occluded(child, IntRect(10, 430, 61, 70)));
523         EXPECT_TRUE(occlusion.occluded(child, IntRect(10, 430, 60, 71)));
524         occlusion.setLayerScissorRect(IntRect(0, 0, 1000, 1000));
525
526         occlusion.markOccludedBehindLayer(child);
527         occlusion.finishedTargetRenderSurface(child, child->renderSurface());
528         occlusion.leaveToTargetRenderSurface(parent->renderSurface());
529
530         EXPECT_EQ_RECT(IntRect(30, 40, 70, 60), occlusion.occlusionInScreenSpace().bounds());
531         EXPECT_EQ(1u, occlusion.occlusionInScreenSpace().rects().size());
532         EXPECT_EQ_RECT(IntRect(30, 40, 70, 60), occlusion.occlusionInTargetSurface().bounds());
533         EXPECT_EQ(1u, occlusion.occlusionInTargetSurface().rects().size());
534
535         EXPECT_TRUE(occlusion.occluded(parent, IntRect(30, 40, 70, 60)));
536         EXPECT_FALSE(occlusion.occluded(parent, IntRect(29, 40, 70, 60)));
537         EXPECT_FALSE(occlusion.occluded(parent, IntRect(30, 39, 70, 60)));
538         EXPECT_FALSE(occlusion.occluded(parent, IntRect(31, 40, 70, 60)));
539         EXPECT_FALSE(occlusion.occluded(parent, IntRect(30, 41, 70, 60)));
540
541         occlusion.useDefaultLayerScissorRect();
542         EXPECT_TRUE(occlusion.occluded(parent, IntRect(30, 40, 70, 60)));
543         EXPECT_FALSE(occlusion.occluded(parent, IntRect(29, 40, 70, 60)));
544         EXPECT_FALSE(occlusion.occluded(parent, IntRect(30, 39, 70, 60)));
545         EXPECT_TRUE(occlusion.occluded(parent, IntRect(31, 40, 70, 60)));
546         EXPECT_TRUE(occlusion.occluded(parent, IntRect(30, 41, 70, 60)));
547         occlusion.setLayerScissorRect(IntRect(0, 0, 1000, 1000));
548
549
550         /* Justification for the above occlusion from |layer|:
551                    100
552           +---------------------+                                      +---------------------+
553           |                     |                                      |                     |30  Visible region of |layer|: /////
554           |    30               |           rotate(90)                 |                     |
555           | 30 + ---------------------------------+                    |     +---------------------------------+
556       100 |    |  10            |                 |            ==>     |     |               |10               |
557           |    |10+---------------------------------+                  |  +---------------------------------+  |
558           |    |  |             |                 | |                  |  |  |///////////////|     420      |  |
559           |    |  |             |                 | |                  |  |  |///////////////|60            |  |
560           |    |  |             |                 | |                  |  |  |///////////////|              |  |
561           +----|--|-------------+                 | |                  +--|--|---------------+              |  |
562                |  |                               | |                   20|10|     70                       |  |
563                |  |                               | |                     |  |                              |  |
564                |  |                               | |500                  |  |                              |  |
565                |  |                               | |                     |  |                              |  |
566                |  |                               | |                     |  |                              |  |
567                |  |                               | |                     |  |                              |  |
568                |  |                               | |                     |  |                              |10|
569                +--|-------------------------------+ |                     |  +------------------------------|--+
570                   |                                 |                     |                 490             |
571                   +---------------------------------+                     +---------------------------------+
572                                  500                                                     500
573          */
574     }
575 };
576
577 ALL_CCOCCLUSIONTRACKER_TEST(CCOcclusionTrackerTestChildInRotatedChild);
578
579 template<class Types, bool opaqueLayers>
580 class CCOcclusionTrackerTestVisitTargetTwoTimes : public CCOcclusionTrackerTest<Types, opaqueLayers> {
581 protected:
582     void runMyTest()
583     {
584         TransformationMatrix childTransform;
585         childTransform.translate(250, 250);
586         childTransform.rotate(90);
587         childTransform.translate(-250, -250);
588
589         typename Types::ContentLayerType* parent = this->createRoot(this->identityMatrix, FloatPoint(0, 0), IntSize(100, 100));
590         typename Types::LayerType* child = this->createLayer(parent, childTransform, FloatPoint(30, 30), IntSize(500, 500));
591         child->setMasksToBounds(true);
592         typename Types::ContentLayerType* layer = this->createDrawingLayer(child, this->identityMatrix, FloatPoint(10, 10), IntSize(500, 500), true);
593         // |child2| makes |parent|'s surface get considered by CCOcclusionTracker first, instead of |child|'s. This exercises different code in
594         // leaveToTargetRenderSurface, as the target surface has already been seen.
595         typename Types::ContentLayerType* child2 = this->createDrawingLayer(parent, this->identityMatrix, FloatPoint(30, 30), IntSize(60, 20), true);
596         this->calcDrawEtc(parent);
597
598         TestCCOcclusionTrackerBase<typename Types::LayerType, typename Types::RenderSurfaceType> occlusion(IntRect(0, 0, 1000, 1000));
599         occlusion.setLayerScissorRect(IntRect(-10, -10, 1000, 1000));
600
601         occlusion.enterTargetRenderSurface(parent->renderSurface());
602         occlusion.markOccludedBehindLayer(child2);
603
604         EXPECT_EQ_RECT(IntRect(30, 30, 60, 20), occlusion.occlusionInScreenSpace().bounds());
605         EXPECT_EQ(1u, occlusion.occlusionInScreenSpace().rects().size());
606         EXPECT_EQ_RECT(IntRect(30, 30, 60, 20), occlusion.occlusionInTargetSurface().bounds());
607         EXPECT_EQ(1u, occlusion.occlusionInTargetSurface().rects().size());
608
609         occlusion.enterTargetRenderSurface(child->renderSurface());
610         occlusion.markOccludedBehindLayer(layer);
611
612         EXPECT_EQ_RECT(IntRect(30, 30, 70, 70), occlusion.occlusionInScreenSpace().bounds());
613         EXPECT_EQ(2u, occlusion.occlusionInScreenSpace().rects().size());
614         EXPECT_EQ_RECT(IntRect(10, 430, 60, 70), occlusion.occlusionInTargetSurface().bounds());
615         EXPECT_EQ(1u, occlusion.occlusionInTargetSurface().rects().size());
616
617         EXPECT_TRUE(occlusion.occluded(child, IntRect(10, 430, 60, 70)));
618         EXPECT_FALSE(occlusion.occluded(child, IntRect(9, 430, 60, 70)));
619         EXPECT_FALSE(occlusion.occluded(child, IntRect(10, 429, 60, 70)));
620         EXPECT_FALSE(occlusion.occluded(child, IntRect(11, 430, 60, 70)));
621         EXPECT_FALSE(occlusion.occluded(child, IntRect(10, 431, 60, 70)));
622
623         occlusion.useDefaultLayerScissorRect();
624         EXPECT_TRUE(occlusion.occluded(child, IntRect(10, 430, 60, 70)));
625         EXPECT_TRUE(occlusion.occluded(child, IntRect(9, 430, 60, 70)));
626         EXPECT_TRUE(occlusion.occluded(child, IntRect(10, 429, 60, 70)));
627         EXPECT_TRUE(occlusion.occluded(child, IntRect(11, 430, 60, 70)));
628         EXPECT_TRUE(occlusion.occluded(child, IntRect(10, 431, 60, 70)));
629         occlusion.setLayerScissorRect(IntRect(-10, -10, 1000, 1000));
630
631         EXPECT_TRUE(occlusion.unoccludedContentRect(child, IntRect(10, 430, 60, 70)).isEmpty());
632         // This is the little piece not occluded by child2
633         EXPECT_EQ_RECT(IntRect(9, 430, 1, 10), occlusion.unoccludedContentRect(child, IntRect(9, 430, 60, 70)));
634         // This extends past both sides of child2, so it will be the original rect.
635         EXPECT_EQ_RECT(IntRect(9, 430, 60, 80), occlusion.unoccludedContentRect(child, IntRect(9, 430, 60, 80)));
636         // This extends past two adjacent sides of child2, and should included the unoccluded parts of each side.
637         // This also demonstrates that the rect can be arbitrary and does not get clipped to the layer's visibleLayerRect().
638         EXPECT_EQ_RECT(IntRect(-10, 430, 20, 70), occlusion.unoccludedContentRect(child, IntRect(-10, 430, 60, 70)));
639         // This extends past three adjacent sides of child2, so it should contain the unoccluded parts of each side. The left
640         // and bottom edges are completely unoccluded for some row/column so we get back the original query rect.
641         EXPECT_EQ_RECT(IntRect(-10, 430, 60, 80), occlusion.unoccludedContentRect(child, IntRect(-10, 430, 60, 80)));
642         EXPECT_EQ_RECT(IntRect(10, 429, 60, 1), occlusion.unoccludedContentRect(child, IntRect(10, 429, 60, 70)));
643         EXPECT_EQ_RECT(IntRect(70, 430, 1, 70), occlusion.unoccludedContentRect(child, IntRect(11, 430, 60, 70)));
644         EXPECT_EQ_RECT(IntRect(10, 500, 60, 1), occlusion.unoccludedContentRect(child, IntRect(10, 431, 60, 70)));
645
646         occlusion.useDefaultLayerScissorRect();
647         EXPECT_TRUE(occlusion.unoccludedContentRect(child, IntRect(10, 430, 60, 70)).isEmpty());
648         EXPECT_TRUE(occlusion.unoccludedContentRect(child, IntRect(9, 430, 60, 70)).isEmpty());
649         EXPECT_TRUE(occlusion.unoccludedContentRect(child, IntRect(9, 430, 60, 80)).isEmpty());
650         EXPECT_TRUE(occlusion.unoccludedContentRect(child, IntRect(-10, 430, 60, 70)).isEmpty());
651         EXPECT_TRUE(occlusion.unoccludedContentRect(child, IntRect(-10, 430, 60, 80)).isEmpty());
652         EXPECT_TRUE(occlusion.unoccludedContentRect(child, IntRect(10, 429, 60, 70)).isEmpty());
653         EXPECT_TRUE(occlusion.unoccludedContentRect(child, IntRect(11, 430, 60, 70)).isEmpty());
654         EXPECT_TRUE(occlusion.unoccludedContentRect(child, IntRect(10, 431, 60, 70)).isEmpty());
655         occlusion.setLayerScissorRect(IntRect(0, 0, 1000, 1000));
656
657         occlusion.markOccludedBehindLayer(child);
658         // |child2| should get merged with the surface we are leaving now
659         occlusion.finishedTargetRenderSurface(child, child->renderSurface());
660         occlusion.leaveToTargetRenderSurface(parent->renderSurface());
661
662         EXPECT_EQ_RECT(IntRect(30, 30, 70, 70), occlusion.occlusionInScreenSpace().bounds());
663         EXPECT_EQ(2u, occlusion.occlusionInScreenSpace().rects().size());
664         EXPECT_EQ_RECT(IntRect(30, 30, 70, 70), occlusion.occlusionInTargetSurface().bounds());
665         EXPECT_EQ(2u, occlusion.occlusionInTargetSurface().rects().size());
666
667         EXPECT_FALSE(occlusion.occluded(parent, IntRect(30, 30, 70, 70)));
668         EXPECT_EQ_RECT(IntRect(90, 30, 10, 10), occlusion.unoccludedContentRect(parent, IntRect(30, 30, 70, 70)));
669
670         EXPECT_TRUE(occlusion.occluded(parent, IntRect(30, 30, 60, 10)));
671         EXPECT_FALSE(occlusion.occluded(parent, IntRect(29, 30, 60, 10)));
672         EXPECT_FALSE(occlusion.occluded(parent, IntRect(30, 29, 60, 10)));
673         EXPECT_FALSE(occlusion.occluded(parent, IntRect(31, 30, 60, 10)));
674         EXPECT_TRUE(occlusion.occluded(parent, IntRect(30, 31, 60, 10)));
675
676         EXPECT_TRUE(occlusion.occluded(parent, IntRect(30, 40, 70, 60)));
677         EXPECT_FALSE(occlusion.occluded(parent, IntRect(29, 40, 70, 60)));
678         EXPECT_FALSE(occlusion.occluded(parent, IntRect(30, 39, 70, 60)));
679
680         EXPECT_TRUE(occlusion.unoccludedContentRect(parent, IntRect(30, 30, 60, 10)).isEmpty());
681         EXPECT_EQ_RECT(IntRect(29, 30, 1, 10), occlusion.unoccludedContentRect(parent, IntRect(29, 30, 60, 10)));
682         EXPECT_EQ_RECT(IntRect(30, 29, 60, 1), occlusion.unoccludedContentRect(parent, IntRect(30, 29, 60, 10)));
683         EXPECT_EQ_RECT(IntRect(90, 30, 1, 10), occlusion.unoccludedContentRect(parent, IntRect(31, 30, 60, 10)));
684         EXPECT_TRUE(occlusion.unoccludedContentRect(parent, IntRect(30, 31, 60, 10)).isEmpty());
685
686         EXPECT_TRUE(occlusion.unoccludedContentRect(parent, IntRect(30, 40, 70, 60)).isEmpty());
687         EXPECT_EQ_RECT(IntRect(29, 40, 1, 60), occlusion.unoccludedContentRect(parent, IntRect(29, 40, 70, 60)));
688         // This rect is mostly occluded by |child2|.
689         EXPECT_EQ_RECT(IntRect(90, 39, 10, 1), occlusion.unoccludedContentRect(parent, IntRect(30, 39, 70, 60)));
690         // This rect extends past top/right ends of |child2|.
691         EXPECT_EQ_RECT(IntRect(30, 29, 70, 11), occlusion.unoccludedContentRect(parent, IntRect(30, 29, 70, 70)));
692         // This rect extends past left/right ends of |child2|.
693         EXPECT_EQ_RECT(IntRect(20, 39, 80, 60), occlusion.unoccludedContentRect(parent, IntRect(20, 39, 80, 60)));
694         EXPECT_EQ_RECT(IntRect(100, 40, 1, 60), occlusion.unoccludedContentRect(parent, IntRect(31, 40, 70, 60)));
695         EXPECT_EQ_RECT(IntRect(30, 100, 70, 1), occlusion.unoccludedContentRect(parent, IntRect(30, 41, 70, 60)));
696
697         /* Justification for the above occlusion from |layer|:
698                    100
699           +---------------------+                                      +---------------------+
700           |                     |                                      |                     |30  Visible region of |layer|: /////
701           |    30               |           rotate(90)                 |     30   60         |    |child2|: \\\\\
702           | 30 + ------------+--------------------+                    |  30 +------------+--------------------+
703       100 |    |  10         |  |                 |            ==>     |     |\\\\\\\\\\\\|  |10               |
704           |    |10+----------|----------------------+                  |  +--|\\\\\\\\\\\\|-----------------+  |
705           |    + ------------+  |                 | |                  |  |  +------------+//|     420      |  |
706           |    |  |             |                 | |                  |  |  |///////////////|60            |  |
707           |    |  |             |                 | |                  |  |  |///////////////|              |  |
708           +----|--|-------------+                 | |                  +--|--|---------------+              |  |
709                |  |                               | |                   20|10|     70                       |  |
710                |  |                               | |                     |  |                              |  |
711                |  |                               | |500                  |  |                              |  |
712                |  |                               | |                     |  |                              |  |
713                |  |                               | |                     |  |                              |  |
714                |  |                               | |                     |  |                              |  |
715                |  |                               | |                     |  |                              |10|
716                +--|-------------------------------+ |                     |  +------------------------------|--+
717                   |                                 |                     |                 490             |
718                   +---------------------------------+                     +---------------------------------+
719                                  500                                                     500
720          */
721     }
722 };
723
724 ALL_CCOCCLUSIONTRACKER_TEST(CCOcclusionTrackerTestVisitTargetTwoTimes);
725
726 template<class Types, bool opaqueLayers>
727 class CCOcclusionTrackerTestSurfaceRotatedOffAxis : public CCOcclusionTrackerTest<Types, opaqueLayers> {
728 protected:
729     void runMyTest()
730     {
731         TransformationMatrix childTransform;
732         childTransform.translate(250, 250);
733         childTransform.rotate(95);
734         childTransform.translate(-250, -250);
735
736         TransformationMatrix layerTransform;
737         layerTransform.translate(10, 10);
738
739         typename Types::ContentLayerType* parent = this->createRoot(this->identityMatrix, FloatPoint(0, 0), IntSize(100, 100));
740         typename Types::LayerType* child = this->createLayer(parent, childTransform, FloatPoint(30, 30), IntSize(500, 500));
741         child->setMasksToBounds(true);
742         typename Types::ContentLayerType* layer = this->createDrawingLayer(child, layerTransform, FloatPoint(0, 0), IntSize(500, 500), true);
743         this->calcDrawEtc(parent);
744
745         TestCCOcclusionTrackerBase<typename Types::LayerType, typename Types::RenderSurfaceType> occlusion(IntRect(0, 0, 1000, 1000));
746         occlusion.setLayerScissorRect(IntRect(0, 0, 1000, 1000));
747
748         IntRect clippedLayerInChild = layerTransform.mapRect(layer->visibleLayerRect());
749
750         occlusion.enterTargetRenderSurface(child->renderSurface());
751         occlusion.markOccludedBehindLayer(layer);
752
753         EXPECT_EQ_RECT(IntRect(), occlusion.occlusionInScreenSpace().bounds());
754         EXPECT_EQ(0u, occlusion.occlusionInScreenSpace().rects().size());
755         EXPECT_EQ_RECT(clippedLayerInChild, occlusion.occlusionInTargetSurface().bounds());
756         EXPECT_EQ(1u, occlusion.occlusionInTargetSurface().rects().size());
757
758         EXPECT_TRUE(occlusion.occluded(child, clippedLayerInChild));
759         EXPECT_TRUE(occlusion.unoccludedContentRect(child, clippedLayerInChild).isEmpty());
760         clippedLayerInChild.move(-1, 0);
761         EXPECT_FALSE(occlusion.occluded(child, clippedLayerInChild));
762         EXPECT_FALSE(occlusion.unoccludedContentRect(child, clippedLayerInChild).isEmpty());
763         clippedLayerInChild.move(1, 0);
764         clippedLayerInChild.move(1, 0);
765         EXPECT_FALSE(occlusion.occluded(child, clippedLayerInChild));
766         EXPECT_FALSE(occlusion.unoccludedContentRect(child, clippedLayerInChild).isEmpty());
767         clippedLayerInChild.move(-1, 0);
768         clippedLayerInChild.move(0, -1);
769         EXPECT_FALSE(occlusion.occluded(child, clippedLayerInChild));
770         EXPECT_FALSE(occlusion.unoccludedContentRect(child, clippedLayerInChild).isEmpty());
771         clippedLayerInChild.move(0, 1);
772         clippedLayerInChild.move(0, 1);
773         EXPECT_FALSE(occlusion.occluded(child, clippedLayerInChild));
774         EXPECT_FALSE(occlusion.unoccludedContentRect(child, clippedLayerInChild).isEmpty());
775         clippedLayerInChild.move(0, -1);
776
777         occlusion.markOccludedBehindLayer(child);
778         occlusion.finishedTargetRenderSurface(child, child->renderSurface());
779         occlusion.leaveToTargetRenderSurface(parent->renderSurface());
780
781         EXPECT_EQ_RECT(IntRect(), occlusion.occlusionInScreenSpace().bounds());
782         EXPECT_EQ(0u, occlusion.occlusionInScreenSpace().rects().size());
783         EXPECT_EQ_RECT(IntRect(), occlusion.occlusionInTargetSurface().bounds());
784         EXPECT_EQ(0u, occlusion.occlusionInTargetSurface().rects().size());
785
786         EXPECT_FALSE(occlusion.occluded(parent, IntRect(75, 55, 1, 1)));
787         EXPECT_EQ_RECT(IntRect(75, 55, 1, 1), occlusion.unoccludedContentRect(parent, IntRect(75, 55, 1, 1)));
788     }
789 };
790
791 ALL_CCOCCLUSIONTRACKER_TEST(CCOcclusionTrackerTestSurfaceRotatedOffAxis);
792
793 template<class Types, bool opaqueLayers>
794 class CCOcclusionTrackerTestSurfaceWithTwoOpaqueChildren : public CCOcclusionTrackerTest<Types, opaqueLayers> {
795 protected:
796     void runMyTest()
797     {
798         TransformationMatrix childTransform;
799         childTransform.translate(250, 250);
800         childTransform.rotate(90);
801         childTransform.translate(-250, -250);
802
803         typename Types::ContentLayerType* parent = this->createRoot(this->identityMatrix, FloatPoint(0, 0), IntSize(100, 100));
804         typename Types::LayerType* child = this->createLayer(parent, childTransform, FloatPoint(30, 30), IntSize(500, 500));
805         child->setMasksToBounds(true);
806         typename Types::ContentLayerType* layer1 = this->createDrawingLayer(child, this->identityMatrix, FloatPoint(10, 10), IntSize(500, 500), true);
807         typename Types::ContentLayerType* layer2 = this->createDrawingLayer(child, this->identityMatrix, FloatPoint(10, 450), IntSize(500, 60), true);
808         this->calcDrawEtc(parent);
809
810         TestCCOcclusionTrackerBase<typename Types::LayerType, typename Types::RenderSurfaceType> occlusion(IntRect(0, 0, 1000, 1000));
811         occlusion.setLayerScissorRect(IntRect(0, 0, 1000, 1000));
812
813         occlusion.enterTargetRenderSurface(child->renderSurface());
814         occlusion.markOccludedBehindLayer(layer2);
815         occlusion.markOccludedBehindLayer(layer1);
816
817         EXPECT_EQ_RECT(IntRect(30, 40, 70, 60), occlusion.occlusionInScreenSpace().bounds());
818         EXPECT_EQ(1u, occlusion.occlusionInScreenSpace().rects().size());
819         EXPECT_EQ_RECT(IntRect(10, 430, 60, 70), occlusion.occlusionInTargetSurface().bounds());
820         EXPECT_EQ(1u, occlusion.occlusionInTargetSurface().rects().size());
821
822         EXPECT_TRUE(occlusion.occluded(child, IntRect(10, 430, 60, 70)));
823         EXPECT_FALSE(occlusion.occluded(child, IntRect(9, 430, 60, 70)));
824         EXPECT_FALSE(occlusion.occluded(child, IntRect(10, 429, 60, 70)));
825         EXPECT_FALSE(occlusion.occluded(child, IntRect(11, 430, 60, 70)));
826         EXPECT_FALSE(occlusion.occluded(child, IntRect(10, 431, 60, 70)));
827
828         EXPECT_TRUE(occlusion.unoccludedContentRect(child, IntRect(10, 430, 60, 70)).isEmpty());
829         EXPECT_EQ_RECT(IntRect(9, 430, 1, 70), occlusion.unoccludedContentRect(child, IntRect(9, 430, 60, 70)));
830         EXPECT_EQ_RECT(IntRect(10, 429, 60, 1), occlusion.unoccludedContentRect(child, IntRect(10, 429, 60, 70)));
831         EXPECT_EQ_RECT(IntRect(70, 430, 1, 70), occlusion.unoccludedContentRect(child, IntRect(11, 430, 60, 70)));
832         EXPECT_EQ_RECT(IntRect(10, 500, 60, 1), occlusion.unoccludedContentRect(child, IntRect(10, 431, 60, 70)));
833
834         occlusion.markOccludedBehindLayer(child);
835         occlusion.finishedTargetRenderSurface(child, child->renderSurface());
836         occlusion.leaveToTargetRenderSurface(parent->renderSurface());
837
838         EXPECT_EQ_RECT(IntRect(30, 40, 70, 60), occlusion.occlusionInScreenSpace().bounds());
839         EXPECT_EQ(1u, occlusion.occlusionInScreenSpace().rects().size());
840         EXPECT_EQ_RECT(IntRect(30, 40, 70, 60), occlusion.occlusionInTargetSurface().bounds());
841         EXPECT_EQ(1u, occlusion.occlusionInTargetSurface().rects().size());
842
843         EXPECT_TRUE(occlusion.occluded(parent, IntRect(30, 40, 70, 60)));
844         EXPECT_FALSE(occlusion.occluded(parent, IntRect(29, 40, 70, 60)));
845         EXPECT_FALSE(occlusion.occluded(parent, IntRect(30, 39, 70, 60)));
846
847         EXPECT_TRUE(occlusion.unoccludedContentRect(parent, IntRect(30, 40, 70, 60)).isEmpty());
848         EXPECT_EQ_RECT(IntRect(29, 40, 1, 60), occlusion.unoccludedContentRect(parent, IntRect(29, 40, 70, 60)));
849         EXPECT_EQ_RECT(IntRect(30, 39, 70, 1), occlusion.unoccludedContentRect(parent, IntRect(30, 39, 70, 60)));
850         EXPECT_EQ_RECT(IntRect(100, 40, 1, 60), occlusion.unoccludedContentRect(parent, IntRect(31, 40, 70, 60)));
851         EXPECT_EQ_RECT(IntRect(30, 100, 70, 1), occlusion.unoccludedContentRect(parent, IntRect(30, 41, 70, 60)));
852
853         /* Justification for the above occlusion from |layer1| and |layer2|:
854
855            +---------------------+
856            |                     |30  Visible region of |layer1|: /////
857            |                     |    Visible region of |layer2|: \\\\\
858            |     +---------------------------------+
859            |     |               |10               |
860            |  +---------------+-----------------+  |
861            |  |  |\\\\\\\\\\\\|//|     420      |  |
862            |  |  |\\\\\\\\\\\\|//|60            |  |
863            |  |  |\\\\\\\\\\\\|//|              |  |
864            +--|--|------------|--+              |  |
865             20|10|     70     |                 |  |
866               |  |            |                 |  |
867               |  |            |                 |  |
868               |  |            |                 |  |
869               |  |            |                 |  |
870               |  |            |                 |  |
871               |  |            |                 |10|
872               |  +------------|-----------------|--+
873               |               | 490             |
874               +---------------+-----------------+
875                      60               440
876          */
877     }
878 };
879
880 ALL_CCOCCLUSIONTRACKER_TEST(CCOcclusionTrackerTestSurfaceWithTwoOpaqueChildren);
881
882 template<class Types, bool opaqueLayers>
883 class CCOcclusionTrackerTestOverlappingSurfaceSiblings : public CCOcclusionTrackerTest<Types, opaqueLayers> {
884 protected:
885     void runMyTest()
886     {
887         TransformationMatrix childTransform;
888         childTransform.translate(250, 250);
889         childTransform.rotate(90);
890         childTransform.translate(-250, -250);
891
892         typename Types::ContentLayerType* parent = this->createRoot(this->identityMatrix, FloatPoint(0, 0), IntSize(100, 100));
893         typename Types::LayerType* child1 = this->createSurface(parent, childTransform, FloatPoint(30, 30), IntSize(10, 10));
894         typename Types::LayerType* child2 = this->createSurface(parent, childTransform, FloatPoint(20, 40), IntSize(10, 10));
895         typename Types::ContentLayerType* layer1 = this->createDrawingLayer(child1, this->identityMatrix, FloatPoint(-10, -10), IntSize(510, 510), true);
896         typename Types::ContentLayerType* layer2 = this->createDrawingLayer(child2, this->identityMatrix, FloatPoint(-10, -10), IntSize(510, 510), true);
897         this->calcDrawEtc(parent);
898
899         TestCCOcclusionTrackerBase<typename Types::LayerType, typename Types::RenderSurfaceType> occlusion(IntRect(0, 0, 1000, 1000));
900         occlusion.setLayerScissorRect(IntRect(-20, -20, 1000, 1000));
901
902         occlusion.enterTargetRenderSurface(child2->renderSurface());
903         occlusion.markOccludedBehindLayer(layer2);
904
905         EXPECT_EQ_RECT(IntRect(20, 30, 80, 70), occlusion.occlusionInScreenSpace().bounds());
906         EXPECT_EQ(1u, occlusion.occlusionInScreenSpace().rects().size());
907         EXPECT_EQ_RECT(IntRect(-10, 420, 70, 80), occlusion.occlusionInTargetSurface().bounds());
908         EXPECT_EQ(1u, occlusion.occlusionInTargetSurface().rects().size());
909
910         EXPECT_TRUE(occlusion.occluded(child2, IntRect(-10, 420, 70, 80)));
911         EXPECT_FALSE(occlusion.occluded(child2, IntRect(-11, 420, 70, 80)));
912         EXPECT_FALSE(occlusion.occluded(child2, IntRect(-10, 419, 70, 80)));
913         EXPECT_FALSE(occlusion.occluded(child2, IntRect(-10, 420, 71, 80)));
914         EXPECT_FALSE(occlusion.occluded(child2, IntRect(-10, 420, 70, 81)));
915
916         occlusion.useDefaultLayerScissorRect();
917         EXPECT_TRUE(occlusion.occluded(child2, IntRect(-10, 420, 70, 80)));
918         EXPECT_TRUE(occlusion.occluded(child2, IntRect(-11, 420, 70, 80)));
919         EXPECT_TRUE(occlusion.occluded(child2, IntRect(-10, 419, 70, 80)));
920         EXPECT_TRUE(occlusion.occluded(child2, IntRect(-10, 420, 71, 80)));
921         EXPECT_TRUE(occlusion.occluded(child2, IntRect(-10, 420, 70, 81)));
922         occlusion.setLayerScissorRect(IntRect(-20, -20, 1000, 1000));
923
924         occlusion.markOccludedBehindLayer(child2);
925         occlusion.finishedTargetRenderSurface(child2, child2->renderSurface());
926         occlusion.leaveToTargetRenderSurface(parent->renderSurface());
927         occlusion.enterTargetRenderSurface(child1->renderSurface());
928         occlusion.markOccludedBehindLayer(layer1);
929
930         EXPECT_EQ_RECT(IntRect(20, 20, 80, 80), occlusion.occlusionInScreenSpace().bounds());
931         EXPECT_EQ(2u, occlusion.occlusionInScreenSpace().rects().size());
932         EXPECT_EQ_RECT(IntRect(-10, 430, 80, 70), occlusion.occlusionInTargetSurface().bounds());
933         EXPECT_EQ(1u, occlusion.occlusionInTargetSurface().rects().size());
934
935         EXPECT_TRUE(occlusion.occluded(child1, IntRect(-10, 430, 80, 70)));
936         EXPECT_FALSE(occlusion.occluded(child1, IntRect(-11, 430, 80, 70)));
937         EXPECT_FALSE(occlusion.occluded(child1, IntRect(-10, 429, 80, 70)));
938         EXPECT_FALSE(occlusion.occluded(child1, IntRect(-10, 430, 81, 70)));
939         EXPECT_FALSE(occlusion.occluded(child1, IntRect(-10, 430, 80, 71)));
940
941         occlusion.markOccludedBehindLayer(child1);
942         occlusion.finishedTargetRenderSurface(child1, child1->renderSurface());
943         occlusion.leaveToTargetRenderSurface(parent->renderSurface());
944
945         EXPECT_EQ_RECT(IntRect(20, 20, 80, 80), occlusion.occlusionInScreenSpace().bounds());
946         EXPECT_EQ(2u, occlusion.occlusionInScreenSpace().rects().size());
947         EXPECT_EQ_RECT(IntRect(20, 20, 80, 80), occlusion.occlusionInTargetSurface().bounds());
948         EXPECT_EQ(2u, occlusion.occlusionInTargetSurface().rects().size());
949
950         EXPECT_FALSE(occlusion.occluded(parent, IntRect(20, 20, 80, 80)));
951
952         EXPECT_TRUE(occlusion.occluded(parent, IntRect(30, 20, 70, 80)));
953         EXPECT_FALSE(occlusion.occluded(parent, IntRect(29, 20, 70, 80)));
954         EXPECT_FALSE(occlusion.occluded(parent, IntRect(30, 19, 70, 80)));
955
956         EXPECT_TRUE(occlusion.occluded(parent, IntRect(20, 30, 80, 70)));
957         EXPECT_FALSE(occlusion.occluded(parent, IntRect(19, 30, 80, 70)));
958         EXPECT_FALSE(occlusion.occluded(parent, IntRect(20, 29, 80, 70)));
959
960         /* Justification for the above occlusion:
961                    100
962           +---------------------+
963           |    20               |       layer1
964           |  30+ ---------------------------------+
965       100 |  30|                |     layer2      |
966           |20+----------------------------------+ |
967           |  | |                |               | |
968           |  | |                |               | |
969           |  | |                |               | |
970           +--|-|----------------+               | |
971              | |                                | | 510
972              | |                                | |
973              | |                                | |
974              | |                                | |
975              | |                                | |
976              | |                                | |
977              | |                                | |
978              | +--------------------------------|-+
979              |                                  |
980              +----------------------------------+
981                              510
982          */
983     }
984 };
985
986 ALL_CCOCCLUSIONTRACKER_TEST(CCOcclusionTrackerTestOverlappingSurfaceSiblings);
987
988 template<class Types, bool opaqueLayers>
989 class CCOcclusionTrackerTestOverlappingSurfaceSiblingsWithTwoTransforms : public CCOcclusionTrackerTest<Types, opaqueLayers> {
990 protected:
991     void runMyTest()
992     {
993         TransformationMatrix child1Transform;
994         child1Transform.translate(250, 250);
995         child1Transform.rotate(-90);
996         child1Transform.translate(-250, -250);
997
998         TransformationMatrix child2Transform;
999         child2Transform.translate(250, 250);
1000         child2Transform.rotate(90);
1001         child2Transform.translate(-250, -250);
1002
1003         typename Types::ContentLayerType* parent = this->createRoot(this->identityMatrix, FloatPoint(0, 0), IntSize(100, 100));
1004         typename Types::LayerType* child1 = this->createSurface(parent, child1Transform, FloatPoint(30, 20), IntSize(10, 10));
1005         typename Types::LayerType* child2 = this->createSurface(parent, child2Transform, FloatPoint(20, 40), IntSize(10, 10));
1006         typename Types::ContentLayerType* layer1 = this->createDrawingLayer(child1, this->identityMatrix, FloatPoint(-10, -20), IntSize(510, 510), true);
1007         typename Types::ContentLayerType* layer2 = this->createDrawingLayer(child2, this->identityMatrix, FloatPoint(-10, -10), IntSize(510, 510), true);
1008         this->calcDrawEtc(parent);
1009
1010         TestCCOcclusionTrackerBase<typename Types::LayerType, typename Types::RenderSurfaceType> occlusion(IntRect(0, 0, 1000, 1000));
1011         occlusion.setLayerScissorRect(IntRect(-30, -30, 1000, 1000));
1012
1013         occlusion.enterTargetRenderSurface(child2->renderSurface());
1014         occlusion.markOccludedBehindLayer(layer2);
1015
1016         EXPECT_EQ_RECT(IntRect(20, 30, 80, 70), occlusion.occlusionInScreenSpace().bounds());
1017         EXPECT_EQ(1u, occlusion.occlusionInScreenSpace().rects().size());
1018         EXPECT_EQ_RECT(IntRect(-10, 420, 70, 80), occlusion.occlusionInTargetSurface().bounds());
1019         EXPECT_EQ(1u, occlusion.occlusionInTargetSurface().rects().size());
1020
1021         EXPECT_TRUE(occlusion.occluded(child2, IntRect(-10, 420, 70, 80)));
1022         EXPECT_FALSE(occlusion.occluded(child2, IntRect(-11, 420, 70, 80)));
1023         EXPECT_FALSE(occlusion.occluded(child2, IntRect(-10, 419, 70, 80)));
1024         EXPECT_FALSE(occlusion.occluded(child2, IntRect(-10, 420, 71, 80)));
1025         EXPECT_FALSE(occlusion.occluded(child2, IntRect(-10, 420, 70, 81)));
1026
1027         occlusion.markOccludedBehindLayer(child2);
1028         occlusion.finishedTargetRenderSurface(child2, child2->renderSurface());
1029         occlusion.leaveToTargetRenderSurface(parent->renderSurface());
1030         occlusion.enterTargetRenderSurface(child1->renderSurface());
1031         occlusion.markOccludedBehindLayer(layer1);
1032
1033         EXPECT_EQ_RECT(IntRect(10, 20, 90, 80), occlusion.occlusionInScreenSpace().bounds());
1034         EXPECT_EQ(1u, occlusion.occlusionInScreenSpace().rects().size());
1035         EXPECT_EQ_RECT(IntRect(420, -20, 80, 90), occlusion.occlusionInTargetSurface().bounds());
1036         EXPECT_EQ(1u, occlusion.occlusionInTargetSurface().rects().size());
1037
1038         EXPECT_TRUE(occlusion.occluded(child1, IntRect(420, -20, 80, 90)));
1039         EXPECT_FALSE(occlusion.occluded(child1, IntRect(419, -20, 80, 90)));
1040         EXPECT_FALSE(occlusion.occluded(child1, IntRect(420, -21, 80, 90)));
1041         EXPECT_FALSE(occlusion.occluded(child1, IntRect(420, -19, 80, 90)));
1042         EXPECT_FALSE(occlusion.occluded(child1, IntRect(421, -20, 80, 90)));
1043
1044         occlusion.markOccludedBehindLayer(child1);
1045         occlusion.finishedTargetRenderSurface(child1, child1->renderSurface());
1046         occlusion.leaveToTargetRenderSurface(parent->renderSurface());
1047
1048         EXPECT_EQ_RECT(IntRect(10, 20, 90, 80), occlusion.occlusionInScreenSpace().bounds());
1049         EXPECT_EQ(1u, occlusion.occlusionInScreenSpace().rects().size());
1050         EXPECT_EQ_RECT(IntRect(10, 20, 90, 80), occlusion.occlusionInTargetSurface().bounds());
1051         EXPECT_EQ(1u, occlusion.occlusionInTargetSurface().rects().size());
1052
1053         EXPECT_TRUE(occlusion.occluded(parent, IntRect(10, 20, 90, 80)));
1054         EXPECT_FALSE(occlusion.occluded(parent, IntRect(9, 20, 90, 80)));
1055         EXPECT_FALSE(occlusion.occluded(parent, IntRect(10, 19, 90, 80)));
1056         EXPECT_FALSE(occlusion.occluded(parent, IntRect(11, 20, 90, 80)));
1057         EXPECT_FALSE(occlusion.occluded(parent, IntRect(10, 21, 90, 80)));
1058
1059         /* Justification for the above occlusion:
1060                       100
1061             +---------------------+
1062             |20                   |       layer1
1063            10+----------------------------------+
1064         100 || 30                 |     layer2  |
1065             |20+----------------------------------+
1066             || |                  |             | |
1067             || |                  |             | |
1068             || |                  |             | |
1069             +|-|------------------+             | |
1070              | |                                | | 510
1071              | |                            510 | |
1072              | |                                | |
1073              | |                                | |
1074              | |                                | |
1075              | |                                | |
1076              | |                520             | |
1077              +----------------------------------+ |
1078                |                                  |
1079                +----------------------------------+
1080                                510
1081          */
1082     }
1083 };
1084
1085 ALL_CCOCCLUSIONTRACKER_TEST(CCOcclusionTrackerTestOverlappingSurfaceSiblingsWithTwoTransforms);
1086
1087 template<class Types, bool opaqueLayers>
1088 class CCOcclusionTrackerTestFilters : public CCOcclusionTrackerTest<Types, opaqueLayers> {
1089 protected:
1090     void runMyTest()
1091     {
1092         TransformationMatrix layerTransform;
1093         layerTransform.translate(250, 250);
1094         layerTransform.rotate(90);
1095         layerTransform.translate(-250, -250);
1096
1097         typename Types::ContentLayerType* parent = this->createRoot(this->identityMatrix, FloatPoint(0, 0), IntSize(100, 100));
1098         typename Types::ContentLayerType* blurLayer = this->createDrawingLayer(parent, layerTransform, FloatPoint(30, 30), IntSize(500, 500), true);
1099         typename Types::ContentLayerType* opacityLayer = this->createDrawingLayer(parent, layerTransform, FloatPoint(30, 30), IntSize(500, 500), true);
1100         typename Types::ContentLayerType* opaqueLayer = this->createDrawingLayer(parent, layerTransform, FloatPoint(30, 30), IntSize(500, 500), true);
1101
1102         FilterOperations filters;
1103         filters.operations().append(BlurFilterOperation::create(Length(10, WebCore::Percent), FilterOperation::BLUR));
1104         blurLayer->setFilters(filters);
1105
1106         filters.operations().clear();
1107         filters.operations().append(BasicComponentTransferFilterOperation::create(0.5, FilterOperation::GRAYSCALE));
1108         opaqueLayer->setFilters(filters);
1109
1110         filters.operations().clear();
1111         filters.operations().append(BasicComponentTransferFilterOperation::create(0.5, FilterOperation::OPACITY));
1112         opacityLayer->setFilters(filters);
1113
1114         this->calcDrawEtc(parent);
1115
1116         TestCCOcclusionTrackerBase<typename Types::LayerType, typename Types::RenderSurfaceType> occlusion(IntRect(0, 0, 1000, 1000));
1117         occlusion.setLayerScissorRect(IntRect(0, 0, 1000, 1000));
1118
1119         // Opacity layer won't contribute to occlusion.
1120         occlusion.enterTargetRenderSurface(opacityLayer->renderSurface());
1121         occlusion.markOccludedBehindLayer(opacityLayer);
1122         occlusion.finishedTargetRenderSurface(opacityLayer, opacityLayer->renderSurface());
1123
1124         EXPECT_TRUE(occlusion.occlusionInScreenSpace().isEmpty());
1125         EXPECT_TRUE(occlusion.occlusionInTargetSurface().isEmpty());
1126
1127         // And has nothing to contribute to its parent surface.
1128         occlusion.leaveToTargetRenderSurface(parent->renderSurface());
1129         EXPECT_TRUE(occlusion.occlusionInScreenSpace().isEmpty());
1130         EXPECT_TRUE(occlusion.occlusionInTargetSurface().isEmpty());
1131
1132         // Opaque layer will contribute to occlusion.
1133         occlusion.enterTargetRenderSurface(opaqueLayer->renderSurface());
1134         occlusion.markOccludedBehindLayer(opaqueLayer);
1135         occlusion.finishedTargetRenderSurface(opaqueLayer, opaqueLayer->renderSurface());
1136
1137         EXPECT_EQ_RECT(IntRect(30, 30, 70, 70), occlusion.occlusionInScreenSpace().bounds());
1138         EXPECT_EQ(1u, occlusion.occlusionInScreenSpace().rects().size());
1139         EXPECT_EQ_RECT(IntRect(0, 430, 70, 70), occlusion.occlusionInTargetSurface().bounds());
1140         EXPECT_EQ(1u, occlusion.occlusionInTargetSurface().rects().size());
1141
1142         // And it gets translated to the parent surface.
1143         occlusion.leaveToTargetRenderSurface(parent->renderSurface());
1144         EXPECT_EQ_RECT(IntRect(30, 30, 70, 70), occlusion.occlusionInScreenSpace().bounds());
1145         EXPECT_EQ(1u, occlusion.occlusionInScreenSpace().rects().size());
1146         EXPECT_EQ_RECT(IntRect(30, 30, 70, 70), occlusion.occlusionInTargetSurface().bounds());
1147         EXPECT_EQ(1u, occlusion.occlusionInTargetSurface().rects().size());
1148
1149         // The blur layer needs to throw away any occlusion from outside its subtree.
1150         occlusion.enterTargetRenderSurface(blurLayer->renderSurface());
1151         EXPECT_TRUE(occlusion.occlusionInScreenSpace().isEmpty());
1152         EXPECT_TRUE(occlusion.occlusionInTargetSurface().isEmpty());
1153
1154         // And it won't contribute to occlusion.
1155         occlusion.markOccludedBehindLayer(blurLayer);
1156         occlusion.finishedTargetRenderSurface(blurLayer, blurLayer->renderSurface());
1157         EXPECT_TRUE(occlusion.occlusionInScreenSpace().isEmpty());
1158         EXPECT_TRUE(occlusion.occlusionInTargetSurface().isEmpty());
1159
1160         // But the opaque layer's occlusion is preserved on the parent.
1161         occlusion.leaveToTargetRenderSurface(parent->renderSurface());
1162         EXPECT_EQ_RECT(IntRect(30, 30, 70, 70), occlusion.occlusionInScreenSpace().bounds());
1163         EXPECT_EQ(1u, occlusion.occlusionInScreenSpace().rects().size());
1164         EXPECT_EQ_RECT(IntRect(30, 30, 70, 70), occlusion.occlusionInTargetSurface().bounds());
1165         EXPECT_EQ(1u, occlusion.occlusionInTargetSurface().rects().size());
1166     }
1167 };
1168
1169 ALL_CCOCCLUSIONTRACKER_TEST(CCOcclusionTrackerTestFilters);
1170
1171 template<class Types, bool opaqueLayers>
1172 class CCOcclusionTrackerTestLayerScissorRectOutsideChild : public CCOcclusionTrackerTest<Types, opaqueLayers> {
1173 protected:
1174     void runMyTest()
1175     {
1176         typename Types::ContentLayerType* parent = this->createRoot(this->identityMatrix, FloatPoint(0, 0), IntSize(300, 300));
1177         typename Types::ContentLayerType* layer = this->createDrawingSurface(parent, this->identityMatrix, FloatPoint(0, 0), IntSize(200, 200), true);
1178         this->calcDrawEtc(parent);
1179
1180         TestCCOcclusionTrackerBase<typename Types::LayerType, typename Types::RenderSurfaceType> occlusion(IntRect(0, 0, 1000, 1000));
1181         occlusion.setLayerScissorRect(IntRect(200, 100, 100, 100));
1182
1183         occlusion.enterTargetRenderSurface(layer->renderSurface());
1184
1185         EXPECT_TRUE(occlusion.occluded(layer, IntRect(0, 0, 100, 100)));
1186         EXPECT_TRUE(occlusion.occluded(layer, IntRect(100, 0, 100, 100)));
1187         EXPECT_TRUE(occlusion.occluded(layer, IntRect(0, 100, 100, 100)));
1188         EXPECT_TRUE(occlusion.occluded(layer, IntRect(100, 100, 100, 100)));
1189         EXPECT_FALSE(occlusion.occluded(layer, IntRect(200, 100, 100, 100)));
1190
1191         occlusion.useDefaultLayerScissorRect();
1192         EXPECT_TRUE(occlusion.occluded(layer, IntRect(200, 100, 100, 100)));
1193         occlusion.setLayerScissorRect(IntRect(200, 100, 100, 100));
1194
1195         occlusion.markOccludedBehindLayer(layer);
1196         occlusion.leaveToTargetRenderSurface(parent->renderSurface());
1197
1198         EXPECT_TRUE(occlusion.occluded(parent, IntRect(0, 0, 100, 100)));
1199         EXPECT_TRUE(occlusion.occluded(parent, IntRect(0, 100, 100, 100)));
1200         EXPECT_TRUE(occlusion.occluded(parent, IntRect(100, 0, 100, 100)));
1201         EXPECT_TRUE(occlusion.occluded(parent, IntRect(0, 100, 100, 100)));
1202         EXPECT_FALSE(occlusion.occluded(parent, IntRect(200, 100, 100, 100)));
1203         EXPECT_TRUE(occlusion.occluded(parent, IntRect(200, 0, 100, 100)));
1204         EXPECT_TRUE(occlusion.occluded(parent, IntRect(0, 200, 100, 100)));
1205         EXPECT_TRUE(occlusion.occluded(parent, IntRect(100, 200, 100, 100)));
1206         EXPECT_TRUE(occlusion.occluded(parent, IntRect(200, 200, 100, 100)));
1207
1208         EXPECT_EQ_RECT(IntRect(200, 100, 100, 100), occlusion.unoccludedContentRect(parent, IntRect(0, 0, 300, 300)));
1209     }
1210 };
1211
1212 ALL_CCOCCLUSIONTRACKER_TEST(CCOcclusionTrackerTestLayerScissorRectOutsideChild);
1213
1214 template<class Types, bool opaqueLayers>
1215 class CCOcclusionTrackerTestScreenScissorRectOutsideChild : public CCOcclusionTrackerTest<Types, opaqueLayers> {
1216 protected:
1217     void runMyTest()
1218     {
1219         typename Types::ContentLayerType* parent = this->createRoot(this->identityMatrix, FloatPoint(0, 0), IntSize(300, 300));
1220         typename Types::ContentLayerType* layer = this->createDrawingSurface(parent, this->identityMatrix, FloatPoint(0, 0), IntSize(200, 200), true);
1221         this->calcDrawEtc(parent);
1222
1223         TestCCOcclusionTrackerBase<typename Types::LayerType, typename Types::RenderSurfaceType> occlusion(IntRect(200, 100, 100, 100));
1224         occlusion.setLayerScissorRect(IntRect(0, 0, 1000, 1000));
1225
1226         occlusion.enterTargetRenderSurface(layer->renderSurface());
1227
1228         EXPECT_TRUE(occlusion.occluded(layer, IntRect(0, 0, 100, 100)));
1229         EXPECT_TRUE(occlusion.occluded(layer, IntRect(100, 0, 100, 100)));
1230         EXPECT_TRUE(occlusion.occluded(layer, IntRect(0, 100, 100, 100)));
1231         EXPECT_TRUE(occlusion.occluded(layer, IntRect(100, 100, 100, 100)));
1232         EXPECT_FALSE(occlusion.occluded(layer, IntRect(200, 100, 100, 100)));
1233
1234         occlusion.useDefaultLayerScissorRect();
1235         EXPECT_TRUE(occlusion.occluded(layer, IntRect(200, 100, 100, 100)));
1236         occlusion.setLayerScissorRect(IntRect(0, 0, 1000, 1000));
1237
1238         occlusion.markOccludedBehindLayer(layer);
1239         occlusion.leaveToTargetRenderSurface(parent->renderSurface());
1240
1241         EXPECT_TRUE(occlusion.occluded(parent, IntRect(0, 0, 100, 100)));
1242         EXPECT_TRUE(occlusion.occluded(parent, IntRect(0, 100, 100, 100)));
1243         EXPECT_TRUE(occlusion.occluded(parent, IntRect(100, 0, 100, 100)));
1244         EXPECT_TRUE(occlusion.occluded(parent, IntRect(0, 100, 100, 100)));
1245         EXPECT_FALSE(occlusion.occluded(parent, IntRect(200, 100, 100, 100)));
1246         EXPECT_TRUE(occlusion.occluded(parent, IntRect(200, 0, 100, 100)));
1247         EXPECT_TRUE(occlusion.occluded(parent, IntRect(0, 200, 100, 100)));
1248         EXPECT_TRUE(occlusion.occluded(parent, IntRect(100, 200, 100, 100)));
1249         EXPECT_TRUE(occlusion.occluded(parent, IntRect(200, 200, 100, 100)));
1250
1251         EXPECT_EQ_RECT(IntRect(200, 100, 100, 100), occlusion.unoccludedContentRect(parent, IntRect(0, 0, 300, 300)));
1252     }
1253 };
1254
1255 ALL_CCOCCLUSIONTRACKER_TEST(CCOcclusionTrackerTestScreenScissorRectOutsideChild);
1256
1257 template<class Types, bool opaqueLayers>
1258 class CCOcclusionTrackerTestDamageRectOutsideChild : public CCOcclusionTrackerTest<Types, opaqueLayers> {
1259 protected:
1260     void runMyTest()
1261     {
1262         typename Types::ContentLayerType* parent = this->createRoot(this->identityMatrix, FloatPoint(0, 0), IntSize(300, 300));
1263         typename Types::ContentLayerType* layer = this->createDrawingSurface(parent, this->identityMatrix, FloatPoint(0, 0), IntSize(200, 200), true);
1264         this->calcDrawEtc(parent);
1265
1266         TestDamageClient<typename Types::RenderSurfaceType> damage(FloatRect(200, 100, 100, 100));
1267         TestCCOcclusionTrackerBase<typename Types::LayerType, typename Types::RenderSurfaceType> occlusion(IntRect(0, 0, 1000, 1000), &damage);
1268
1269         occlusion.enterTargetRenderSurface(layer->renderSurface());
1270
1271         EXPECT_TRUE(occlusion.unoccludedContentRect(layer, IntRect(0, 0, 200, 200)).isEmpty());
1272
1273         occlusion.markOccludedBehindLayer(layer);
1274         occlusion.leaveToTargetRenderSurface(parent->renderSurface());
1275
1276         EXPECT_EQ_RECT(IntRect(200, 100, 100, 100), occlusion.unoccludedContentRect(parent, IntRect(0, 0, 300, 300)));
1277     }
1278 };
1279
1280 ALL_CCOCCLUSIONTRACKER_TEST(CCOcclusionTrackerTestDamageRectOutsideChild);
1281
1282 template<class Types, bool opaqueLayers>
1283 class CCOcclusionTrackerTestLayerScissorRectOverChild : public CCOcclusionTrackerTest<Types, opaqueLayers> {
1284 protected:
1285     void runMyTest()
1286     {
1287         typename Types::ContentLayerType* parent = this->createRoot(this->identityMatrix, FloatPoint(0, 0), IntSize(300, 300));
1288         typename Types::ContentLayerType* layer = this->createDrawingSurface(parent, this->identityMatrix, FloatPoint(0, 0), IntSize(200, 200), true);
1289         this->calcDrawEtc(parent);
1290
1291         TestCCOcclusionTrackerBase<typename Types::LayerType, typename Types::RenderSurfaceType> occlusion(IntRect(0, 0, 1000, 1000));
1292         occlusion.setLayerScissorRect(IntRect(100, 100, 100, 100));
1293
1294         occlusion.enterTargetRenderSurface(layer->renderSurface());
1295
1296         EXPECT_TRUE(occlusion.occluded(layer, IntRect(0, 0, 100, 100)));
1297         EXPECT_TRUE(occlusion.occluded(layer, IntRect(0, 100, 100, 100)));
1298         EXPECT_TRUE(occlusion.occluded(layer, IntRect(100, 0, 100, 100)));
1299         EXPECT_FALSE(occlusion.occluded(layer, IntRect(100, 100, 100, 100)));
1300
1301         occlusion.markOccludedBehindLayer(layer);
1302         occlusion.leaveToTargetRenderSurface(parent->renderSurface());
1303
1304         EXPECT_TRUE(occlusion.occluded(parent, IntRect(0, 0, 100, 100)));
1305         EXPECT_TRUE(occlusion.occluded(parent, IntRect(0, 100, 100, 100)));
1306         EXPECT_TRUE(occlusion.occluded(parent, IntRect(100, 0, 100, 100)));
1307         EXPECT_TRUE(occlusion.occluded(parent, IntRect(100, 100, 100, 100)));
1308         EXPECT_TRUE(occlusion.occluded(parent, IntRect(200, 100, 100, 100)));
1309         EXPECT_TRUE(occlusion.occluded(parent, IntRect(200, 0, 100, 100)));
1310         EXPECT_TRUE(occlusion.occluded(parent, IntRect(0, 200, 100, 100)));
1311         EXPECT_TRUE(occlusion.occluded(parent, IntRect(100, 200, 100, 100)));
1312         EXPECT_TRUE(occlusion.occluded(parent, IntRect(200, 200, 100, 100)));
1313
1314         EXPECT_TRUE(occlusion.unoccludedContentRect(parent, IntRect(0, 0, 300, 300)).isEmpty());
1315     }
1316 };
1317
1318 ALL_CCOCCLUSIONTRACKER_TEST(CCOcclusionTrackerTestLayerScissorRectOverChild);
1319
1320 template<class Types, bool opaqueLayers>
1321 class CCOcclusionTrackerTestScreenScissorRectOverChild : public CCOcclusionTrackerTest<Types, opaqueLayers> {
1322 protected:
1323     void runMyTest()
1324     {
1325         typename Types::ContentLayerType* parent = this->createRoot(this->identityMatrix, FloatPoint(0, 0), IntSize(300, 300));
1326         typename Types::ContentLayerType* layer = this->createDrawingSurface(parent, this->identityMatrix, FloatPoint(0, 0), IntSize(200, 200), true);
1327         this->calcDrawEtc(parent);
1328
1329         TestCCOcclusionTrackerBase<typename Types::LayerType, typename Types::RenderSurfaceType> occlusion(IntRect(100, 100, 100, 100));
1330         occlusion.setLayerScissorRect(IntRect(0, 0, 1000, 1000));
1331
1332         occlusion.enterTargetRenderSurface(layer->renderSurface());
1333
1334         EXPECT_TRUE(occlusion.occluded(layer, IntRect(0, 0, 100, 100)));
1335         EXPECT_TRUE(occlusion.occluded(layer, IntRect(0, 100, 100, 100)));
1336         EXPECT_TRUE(occlusion.occluded(layer, IntRect(100, 0, 100, 100)));
1337         EXPECT_FALSE(occlusion.occluded(layer, IntRect(100, 100, 100, 100)));
1338
1339         occlusion.markOccludedBehindLayer(layer);
1340         occlusion.leaveToTargetRenderSurface(parent->renderSurface());
1341
1342         EXPECT_TRUE(occlusion.occluded(parent, IntRect(0, 0, 100, 100)));
1343         EXPECT_TRUE(occlusion.occluded(parent, IntRect(0, 100, 100, 100)));
1344         EXPECT_TRUE(occlusion.occluded(parent, IntRect(100, 0, 100, 100)));
1345         EXPECT_TRUE(occlusion.occluded(parent, IntRect(100, 100, 100, 100)));
1346         EXPECT_TRUE(occlusion.occluded(parent, IntRect(200, 100, 100, 100)));
1347         EXPECT_TRUE(occlusion.occluded(parent, IntRect(200, 0, 100, 100)));
1348         EXPECT_TRUE(occlusion.occluded(parent, IntRect(0, 200, 100, 100)));
1349         EXPECT_TRUE(occlusion.occluded(parent, IntRect(100, 200, 100, 100)));
1350         EXPECT_TRUE(occlusion.occluded(parent, IntRect(200, 200, 100, 100)));
1351
1352         EXPECT_TRUE(occlusion.unoccludedContentRect(parent, IntRect(0, 0, 300, 300)).isEmpty());
1353     }
1354 };
1355
1356 ALL_CCOCCLUSIONTRACKER_TEST(CCOcclusionTrackerTestScreenScissorRectOverChild);
1357
1358 template<class Types, bool opaqueLayers>
1359 class CCOcclusionTrackerTestDamageRectOverChild : public CCOcclusionTrackerTest<Types, opaqueLayers> {
1360 protected:
1361     void runMyTest()
1362     {
1363         typename Types::ContentLayerType* parent = this->createRoot(this->identityMatrix, FloatPoint(0, 0), IntSize(300, 300));
1364         typename Types::ContentLayerType* layer = this->createDrawingSurface(parent, this->identityMatrix, FloatPoint(0, 0), IntSize(200, 200), true);
1365         this->calcDrawEtc(parent);
1366
1367         TestDamageClient<typename Types::RenderSurfaceType> damage(FloatRect(100, 100, 100, 100));
1368         TestCCOcclusionTrackerBase<typename Types::LayerType, typename Types::RenderSurfaceType> occlusion(IntRect(0, 0, 1000, 1000), &damage);
1369
1370         occlusion.enterTargetRenderSurface(layer->renderSurface());
1371
1372         EXPECT_EQ_RECT(IntRect(100, 100, 100, 100), occlusion.unoccludedContentRect(layer, IntRect(0, 0, 200, 200)));
1373
1374         occlusion.markOccludedBehindLayer(layer);
1375         occlusion.leaveToTargetRenderSurface(parent->renderSurface());
1376
1377         EXPECT_TRUE(occlusion.unoccludedContentRect(parent, IntRect(0, 0, 300, 300)).isEmpty());
1378     }
1379 };
1380
1381 ALL_CCOCCLUSIONTRACKER_TEST(CCOcclusionTrackerTestDamageRectOverChild);
1382
1383 template<class Types, bool opaqueLayers>
1384 class CCOcclusionTrackerTestLayerScissorRectPartlyOverChild : public CCOcclusionTrackerTest<Types, opaqueLayers> {
1385 protected:
1386     void runMyTest()
1387     {
1388         typename Types::ContentLayerType* parent = this->createRoot(this->identityMatrix, FloatPoint(0, 0), IntSize(300, 300));
1389         typename Types::ContentLayerType* layer = this->createDrawingSurface(parent, this->identityMatrix, FloatPoint(0, 0), IntSize(200, 200), true);
1390         this->calcDrawEtc(parent);
1391
1392         TestCCOcclusionTrackerBase<typename Types::LayerType, typename Types::RenderSurfaceType> occlusion(IntRect(0, 0, 1000, 1000));
1393         occlusion.setLayerScissorRect(IntRect(50, 50, 200, 200));
1394
1395         occlusion.enterTargetRenderSurface(layer->renderSurface());
1396
1397         EXPECT_FALSE(occlusion.occluded(layer, IntRect(0, 0, 100, 100)));
1398         EXPECT_FALSE(occlusion.occluded(layer, IntRect(0, 100, 100, 100)));
1399         EXPECT_FALSE(occlusion.occluded(layer, IntRect(100, 0, 100, 100)));
1400         EXPECT_FALSE(occlusion.occluded(layer, IntRect(100, 100, 100, 100)));
1401
1402         occlusion.markOccludedBehindLayer(layer);
1403         occlusion.leaveToTargetRenderSurface(parent->renderSurface());
1404
1405         EXPECT_TRUE(occlusion.occluded(parent, IntRect(0, 0, 100, 100)));
1406         EXPECT_TRUE(occlusion.occluded(parent, IntRect(0, 100, 100, 100)));
1407         EXPECT_TRUE(occlusion.occluded(parent, IntRect(100, 0, 100, 100)));
1408         EXPECT_TRUE(occlusion.occluded(parent, IntRect(100, 100, 100, 100)));
1409         EXPECT_FALSE(occlusion.occluded(parent, IntRect(200, 100, 100, 100)));
1410         EXPECT_FALSE(occlusion.occluded(parent, IntRect(200, 0, 100, 100)));
1411         EXPECT_FALSE(occlusion.occluded(parent, IntRect(0, 200, 100, 100)));
1412         EXPECT_FALSE(occlusion.occluded(parent, IntRect(100, 200, 100, 100)));
1413         EXPECT_FALSE(occlusion.occluded(parent, IntRect(200, 200, 100, 100)));
1414
1415         EXPECT_EQ_RECT(IntRect(50, 50, 200, 200), occlusion.unoccludedContentRect(parent, IntRect(0, 0, 300, 300)));
1416         EXPECT_EQ_RECT(IntRect(200, 50, 50, 50), occlusion.unoccludedContentRect(parent, IntRect(0, 0, 300, 100)));
1417         EXPECT_EQ_RECT(IntRect(200, 100, 50, 100), occlusion.unoccludedContentRect(parent, IntRect(0, 100, 300, 100)));
1418         EXPECT_EQ_RECT(IntRect(200, 100, 50, 100), occlusion.unoccludedContentRect(parent, IntRect(200, 100, 100, 100)));
1419         EXPECT_EQ_RECT(IntRect(100, 200, 100, 50), occlusion.unoccludedContentRect(parent, IntRect(100, 200, 100, 100)));
1420     }
1421 };
1422
1423 ALL_CCOCCLUSIONTRACKER_TEST(CCOcclusionTrackerTestLayerScissorRectPartlyOverChild);
1424
1425 template<class Types, bool opaqueLayers>
1426 class CCOcclusionTrackerTestScreenScissorRectPartlyOverChild : public CCOcclusionTrackerTest<Types, opaqueLayers> {
1427 protected:
1428     void runMyTest()
1429     {
1430         typename Types::ContentLayerType* parent = this->createRoot(this->identityMatrix, FloatPoint(0, 0), IntSize(300, 300));
1431         typename Types::ContentLayerType* layer = this->createDrawingSurface(parent, this->identityMatrix, FloatPoint(0, 0), IntSize(200, 200), true);
1432         this->calcDrawEtc(parent);
1433
1434         TestCCOcclusionTrackerBase<typename Types::LayerType, typename Types::RenderSurfaceType> occlusion(IntRect(50, 50, 200, 200));
1435         occlusion.setLayerScissorRect(IntRect(0, 0, 1000, 1000));
1436
1437         occlusion.enterTargetRenderSurface(layer->renderSurface());
1438
1439         EXPECT_FALSE(occlusion.occluded(layer, IntRect(0, 0, 100, 100)));
1440         EXPECT_FALSE(occlusion.occluded(layer, IntRect(0, 100, 100, 100)));
1441         EXPECT_FALSE(occlusion.occluded(layer, IntRect(100, 0, 100, 100)));
1442         EXPECT_FALSE(occlusion.occluded(layer, IntRect(100, 100, 100, 100)));
1443
1444         occlusion.markOccludedBehindLayer(layer);
1445         occlusion.leaveToTargetRenderSurface(parent->renderSurface());
1446
1447         EXPECT_TRUE(occlusion.occluded(parent, IntRect(0, 0, 100, 100)));
1448         EXPECT_TRUE(occlusion.occluded(parent, IntRect(0, 100, 100, 100)));
1449         EXPECT_TRUE(occlusion.occluded(parent, IntRect(100, 0, 100, 100)));
1450         EXPECT_TRUE(occlusion.occluded(parent, IntRect(100, 100, 100, 100)));
1451         EXPECT_FALSE(occlusion.occluded(parent, IntRect(200, 100, 100, 100)));
1452         EXPECT_FALSE(occlusion.occluded(parent, IntRect(200, 0, 100, 100)));
1453         EXPECT_FALSE(occlusion.occluded(parent, IntRect(0, 200, 100, 100)));
1454         EXPECT_FALSE(occlusion.occluded(parent, IntRect(100, 200, 100, 100)));
1455         EXPECT_FALSE(occlusion.occluded(parent, IntRect(200, 200, 100, 100)));
1456
1457         EXPECT_EQ_RECT(IntRect(50, 50, 200, 200), occlusion.unoccludedContentRect(parent, IntRect(0, 0, 300, 300)));
1458         EXPECT_EQ_RECT(IntRect(200, 50, 50, 50), occlusion.unoccludedContentRect(parent, IntRect(0, 0, 300, 100)));
1459         EXPECT_EQ_RECT(IntRect(200, 100, 50, 100), occlusion.unoccludedContentRect(parent, IntRect(0, 100, 300, 100)));
1460         EXPECT_EQ_RECT(IntRect(200, 100, 50, 100), occlusion.unoccludedContentRect(parent, IntRect(200, 100, 100, 100)));
1461         EXPECT_EQ_RECT(IntRect(100, 200, 100, 50), occlusion.unoccludedContentRect(parent, IntRect(100, 200, 100, 100)));
1462     }
1463 };
1464
1465 ALL_CCOCCLUSIONTRACKER_TEST(CCOcclusionTrackerTestScreenScissorRectPartlyOverChild);
1466
1467 template<class Types, bool opaqueLayers>
1468 class CCOcclusionTrackerTestDamageRectPartlyOverChild : public CCOcclusionTrackerTest<Types, opaqueLayers> {
1469 protected:
1470     void runMyTest()
1471     {
1472         typename Types::ContentLayerType* parent = this->createRoot(this->identityMatrix, FloatPoint(0, 0), IntSize(300, 300));
1473         typename Types::ContentLayerType* layer = this->createDrawingSurface(parent, this->identityMatrix, FloatPoint(0, 0), IntSize(200, 200), true);
1474         this->calcDrawEtc(parent);
1475
1476         TestDamageClient<typename Types::RenderSurfaceType> damage(FloatRect(50, 50, 200, 200));
1477         TestCCOcclusionTrackerBase<typename Types::LayerType, typename Types::RenderSurfaceType> occlusion(IntRect(0, 0, 1000, 1000), &damage);
1478
1479         occlusion.enterTargetRenderSurface(layer->renderSurface());
1480
1481         EXPECT_EQ_RECT(IntRect(50, 50, 150, 150), occlusion.unoccludedContentRect(layer, IntRect(0, 0, 200, 200)));
1482
1483         occlusion.markOccludedBehindLayer(layer);
1484         occlusion.leaveToTargetRenderSurface(parent->renderSurface());
1485
1486         EXPECT_EQ_RECT(IntRect(50, 50, 200, 200), occlusion.unoccludedContentRect(parent, IntRect(0, 0, 300, 300)));
1487     }
1488 };
1489
1490 ALL_CCOCCLUSIONTRACKER_TEST(CCOcclusionTrackerTestDamageRectPartlyOverChild);
1491
1492 template<class Types, bool opaqueLayers>
1493 class CCOcclusionTrackerTestLayerScissorRectOverNothing : public CCOcclusionTrackerTest<Types, opaqueLayers> {
1494 protected:
1495     void runMyTest()
1496     {
1497         typename Types::ContentLayerType* parent = this->createRoot(this->identityMatrix, FloatPoint(0, 0), IntSize(300, 300));
1498         typename Types::ContentLayerType* layer = this->createDrawingSurface(parent, this->identityMatrix, FloatPoint(0, 0), IntSize(200, 200), true);
1499         this->calcDrawEtc(parent);
1500
1501         TestCCOcclusionTrackerBase<typename Types::LayerType, typename Types::RenderSurfaceType> occlusion(IntRect(0, 0, 1000, 1000));
1502         occlusion.setLayerScissorRect(IntRect(500, 500, 100, 100));
1503
1504         occlusion.enterTargetRenderSurface(layer->renderSurface());
1505
1506         EXPECT_TRUE(occlusion.occluded(layer, IntRect(0, 0, 100, 100)));
1507         EXPECT_TRUE(occlusion.occluded(layer, IntRect(0, 100, 100, 100)));
1508         EXPECT_TRUE(occlusion.occluded(layer, IntRect(100, 0, 100, 100)));
1509         EXPECT_TRUE(occlusion.occluded(layer, IntRect(100, 100, 100, 100)));
1510
1511         occlusion.markOccludedBehindLayer(layer);
1512         occlusion.leaveToTargetRenderSurface(parent->renderSurface());
1513
1514         EXPECT_TRUE(occlusion.occluded(parent, IntRect(0, 0, 100, 100)));
1515         EXPECT_TRUE(occlusion.occluded(parent, IntRect(0, 100, 100, 100)));
1516         EXPECT_TRUE(occlusion.occluded(parent, IntRect(100, 0, 100, 100)));
1517         EXPECT_TRUE(occlusion.occluded(parent, IntRect(100, 100, 100, 100)));
1518         EXPECT_TRUE(occlusion.occluded(parent, IntRect(200, 100, 100, 100)));
1519         EXPECT_TRUE(occlusion.occluded(parent, IntRect(200, 0, 100, 100)));
1520         EXPECT_TRUE(occlusion.occluded(parent, IntRect(0, 200, 100, 100)));
1521         EXPECT_TRUE(occlusion.occluded(parent, IntRect(100, 200, 100, 100)));
1522         EXPECT_TRUE(occlusion.occluded(parent, IntRect(200, 200, 100, 100)));
1523
1524         EXPECT_TRUE(occlusion.unoccludedContentRect(parent, IntRect(0, 0, 300, 300)).isEmpty());
1525         EXPECT_TRUE(occlusion.unoccludedContentRect(parent, IntRect(0, 0, 300, 100)).isEmpty());
1526         EXPECT_TRUE(occlusion.unoccludedContentRect(parent, IntRect(0, 100, 300, 100)).isEmpty());
1527         EXPECT_TRUE(occlusion.unoccludedContentRect(parent, IntRect(200, 100, 100, 100)).isEmpty());
1528         EXPECT_TRUE(occlusion.unoccludedContentRect(parent, IntRect(100, 200, 100, 100)).isEmpty());
1529     }
1530 };
1531
1532 ALL_CCOCCLUSIONTRACKER_TEST(CCOcclusionTrackerTestLayerScissorRectOverNothing);
1533
1534 template<class Types, bool opaqueLayers>
1535 class CCOcclusionTrackerTestScreenScissorRectOverNothing : public CCOcclusionTrackerTest<Types, opaqueLayers> {
1536 protected:
1537     void runMyTest()
1538     {
1539         typename Types::ContentLayerType* parent = this->createRoot(this->identityMatrix, FloatPoint(0, 0), IntSize(300, 300));
1540         typename Types::ContentLayerType* layer = this->createDrawingSurface(parent, this->identityMatrix, FloatPoint(0, 0), IntSize(200, 200), true);
1541         this->calcDrawEtc(parent);
1542
1543         TestCCOcclusionTrackerBase<typename Types::LayerType, typename Types::RenderSurfaceType> occlusion(IntRect(500, 500, 100, 100));
1544         occlusion.setLayerScissorRect(IntRect(0, 0, 1000, 1000));
1545
1546         occlusion.enterTargetRenderSurface(layer->renderSurface());
1547
1548         EXPECT_TRUE(occlusion.occluded(layer, IntRect(0, 0, 100, 100)));
1549         EXPECT_TRUE(occlusion.occluded(layer, IntRect(0, 100, 100, 100)));
1550         EXPECT_TRUE(occlusion.occluded(layer, IntRect(100, 0, 100, 100)));
1551         EXPECT_TRUE(occlusion.occluded(layer, IntRect(100, 100, 100, 100)));
1552
1553         occlusion.markOccludedBehindLayer(layer);
1554         occlusion.leaveToTargetRenderSurface(parent->renderSurface());
1555
1556         EXPECT_TRUE(occlusion.occluded(parent, IntRect(0, 0, 100, 100)));
1557         EXPECT_TRUE(occlusion.occluded(parent, IntRect(0, 100, 100, 100)));
1558         EXPECT_TRUE(occlusion.occluded(parent, IntRect(100, 0, 100, 100)));
1559         EXPECT_TRUE(occlusion.occluded(parent, IntRect(100, 100, 100, 100)));
1560         EXPECT_TRUE(occlusion.occluded(parent, IntRect(200, 100, 100, 100)));
1561         EXPECT_TRUE(occlusion.occluded(parent, IntRect(200, 0, 100, 100)));
1562         EXPECT_TRUE(occlusion.occluded(parent, IntRect(0, 200, 100, 100)));
1563         EXPECT_TRUE(occlusion.occluded(parent, IntRect(100, 200, 100, 100)));
1564         EXPECT_TRUE(occlusion.occluded(parent, IntRect(200, 200, 100, 100)));
1565
1566         EXPECT_TRUE(occlusion.unoccludedContentRect(parent, IntRect(0, 0, 300, 300)).isEmpty());
1567         EXPECT_TRUE(occlusion.unoccludedContentRect(parent, IntRect(0, 0, 300, 100)).isEmpty());
1568         EXPECT_TRUE(occlusion.unoccludedContentRect(parent, IntRect(0, 100, 300, 100)).isEmpty());
1569         EXPECT_TRUE(occlusion.unoccludedContentRect(parent, IntRect(200, 100, 100, 100)).isEmpty());
1570         EXPECT_TRUE(occlusion.unoccludedContentRect(parent, IntRect(100, 200, 100, 100)).isEmpty());
1571     }
1572 };
1573
1574 ALL_CCOCCLUSIONTRACKER_TEST(CCOcclusionTrackerTestScreenScissorRectOverNothing);
1575
1576 template<class Types, bool opaqueLayers>
1577 class CCOcclusionTrackerTestDamageRectOverNothing : public CCOcclusionTrackerTest<Types, opaqueLayers> {
1578 protected:
1579     void runMyTest()
1580     {
1581         typename Types::ContentLayerType* parent = this->createRoot(this->identityMatrix, FloatPoint(0, 0), IntSize(300, 300));
1582         typename Types::ContentLayerType* layer = this->createDrawingSurface(parent, this->identityMatrix, FloatPoint(0, 0), IntSize(200, 200), true);
1583         this->calcDrawEtc(parent);
1584
1585         TestDamageClient<typename Types::RenderSurfaceType> damage(FloatRect(500, 500, 100, 100));
1586         TestCCOcclusionTrackerBase<typename Types::LayerType, typename Types::RenderSurfaceType> occlusion(IntRect(0, 0, 1000, 1000), &damage);
1587
1588         occlusion.enterTargetRenderSurface(layer->renderSurface());
1589
1590         EXPECT_TRUE(occlusion.unoccludedContentRect(layer, IntRect(0, 0, 200, 200)).isEmpty());
1591
1592         occlusion.markOccludedBehindLayer(layer);
1593         occlusion.leaveToTargetRenderSurface(parent->renderSurface());
1594
1595         EXPECT_TRUE(occlusion.unoccludedContentRect(parent, IntRect(0, 0, 300, 300)).isEmpty());
1596     }
1597 };
1598
1599 ALL_CCOCCLUSIONTRACKER_TEST(CCOcclusionTrackerTestDamageRectOverNothing);
1600
1601 template<class Types, bool opaqueLayers>
1602 class CCOcclusionTrackerTestLayerScissorRectForLayerOffOrigin : public CCOcclusionTrackerTest<Types, opaqueLayers> {
1603 protected:
1604     void runMyTest()
1605     {
1606         typename Types::ContentLayerType* parent = this->createRoot(this->identityMatrix, FloatPoint(0, 0), IntSize(300, 300));
1607         typename Types::ContentLayerType* layer = this->createDrawingSurface(parent, this->identityMatrix, FloatPoint(0, 0), IntSize(200, 200), true);
1608         this->calcDrawEtc(parent);
1609
1610         TestCCOcclusionTrackerBase<typename Types::LayerType, typename Types::RenderSurfaceType> occlusion(IntRect(0, 0, 1000, 1000));
1611         occlusion.enterTargetRenderSurface(layer->renderSurface());
1612
1613         // This layer is translated when drawn into its target. So if the scissor rect given from the target surface
1614         // is not in that target space, then after translating these query rects into the target, they will fall outside
1615         // the scissor and be considered occluded.
1616         EXPECT_FALSE(occlusion.occluded(layer, IntRect(0, 0, 100, 100)));
1617         EXPECT_FALSE(occlusion.occluded(layer, IntRect(0, 100, 100, 100)));
1618         EXPECT_FALSE(occlusion.occluded(layer, IntRect(100, 0, 100, 100)));
1619         EXPECT_FALSE(occlusion.occluded(layer, IntRect(100, 100, 100, 100)));
1620     }
1621 };
1622
1623 ALL_CCOCCLUSIONTRACKER_TEST(CCOcclusionTrackerTestLayerScissorRectForLayerOffOrigin);
1624
1625 template<class Types, bool opaqueLayers>
1626 class CCOcclusionTrackerTestOpaqueContentsRegionEmpty : public CCOcclusionTrackerTest<Types, opaqueLayers> {
1627 protected:
1628     void runMyTest()
1629     {
1630         typename Types::ContentLayerType* parent = this->createRoot(this->identityMatrix, FloatPoint(0, 0), IntSize(300, 300));
1631         typename Types::ContentLayerType* layer = this->createDrawingSurface(parent, this->identityMatrix, FloatPoint(0, 0), IntSize(200, 200), false);
1632         this->calcDrawEtc(parent);
1633
1634         TestCCOcclusionTrackerBase<typename Types::LayerType, typename Types::RenderSurfaceType> occlusion(IntRect(0, 0, 1000, 1000));
1635         occlusion.enterTargetRenderSurface(layer->renderSurface());
1636
1637         EXPECT_FALSE(occlusion.occluded(layer, IntRect(0, 0, 100, 100)));
1638         EXPECT_FALSE(occlusion.occluded(layer, IntRect(100, 0, 100, 100)));
1639         EXPECT_FALSE(occlusion.occluded(layer, IntRect(0, 100, 100, 100)));
1640         EXPECT_FALSE(occlusion.occluded(layer, IntRect(100, 100, 100, 100)));
1641
1642         // Occluded since its outside the surface bounds.
1643         EXPECT_TRUE(occlusion.occluded(layer, IntRect(200, 100, 100, 100)));
1644
1645         // Test without any scissors.
1646         occlusion.setLayerScissorRect(IntRect(0, 0, 1000, 1000));
1647         EXPECT_FALSE(occlusion.occluded(layer, IntRect(200, 100, 100, 100)));
1648         occlusion.useDefaultLayerScissorRect();
1649
1650         occlusion.markOccludedBehindLayer(layer);
1651         occlusion.leaveToTargetRenderSurface(parent->renderSurface());
1652
1653         EXPECT_TRUE(occlusion.occlusionInScreenSpace().bounds().isEmpty());
1654         EXPECT_EQ(0u, occlusion.occlusionInScreenSpace().rects().size());
1655     }
1656 };
1657
1658 MAIN_AND_IMPL_THREAD_TEST(CCOcclusionTrackerTestOpaqueContentsRegionEmpty);
1659
1660 template<class Types, bool opaqueLayers>
1661 class CCOcclusionTrackerTestOpaqueContentsRegionNonEmpty : public CCOcclusionTrackerTest<Types, opaqueLayers> {
1662 protected:
1663     void runMyTest()
1664     {
1665         typename Types::ContentLayerType* parent = this->createRoot(this->identityMatrix, FloatPoint(0, 0), IntSize(300, 300));
1666         typename Types::ContentLayerType* layer = this->createDrawingLayer(parent, this->identityMatrix, FloatPoint(100, 100), IntSize(200, 200), false);
1667         this->calcDrawEtc(parent);
1668
1669         {
1670             TestCCOcclusionTrackerBase<typename Types::LayerType, typename Types::RenderSurfaceType> occlusion(IntRect(0, 0, 1000, 1000));
1671             layer->setOpaqueContentsRect(IntRect(0, 0, 100, 100));
1672
1673             occlusion.enterTargetRenderSurface(parent->renderSurface());
1674             occlusion.markOccludedBehindLayer(layer);
1675
1676             EXPECT_EQ_RECT(IntRect(100, 100, 100, 100), occlusion.occlusionInScreenSpace().bounds());
1677             EXPECT_EQ(1u, occlusion.occlusionInScreenSpace().rects().size());
1678
1679             EXPECT_FALSE(occlusion.occluded(parent, IntRect(0, 100, 100, 100)));
1680             EXPECT_TRUE(occlusion.occluded(parent, IntRect(100, 100, 100, 100)));
1681             EXPECT_FALSE(occlusion.occluded(parent, IntRect(200, 200, 100, 100)));
1682         }
1683
1684         {
1685             TestCCOcclusionTrackerBase<typename Types::LayerType, typename Types::RenderSurfaceType> occlusion(IntRect(0, 0, 1000, 1000));
1686             layer->setOpaqueContentsRect(IntRect(20, 20, 180, 180));
1687
1688             occlusion.enterTargetRenderSurface(parent->renderSurface());
1689             occlusion.markOccludedBehindLayer(layer);
1690
1691             EXPECT_EQ_RECT(IntRect(120, 120, 180, 180), occlusion.occlusionInScreenSpace().bounds());
1692             EXPECT_EQ(1u, occlusion.occlusionInScreenSpace().rects().size());
1693
1694             EXPECT_FALSE(occlusion.occluded(parent, IntRect(0, 100, 100, 100)));
1695             EXPECT_FALSE(occlusion.occluded(parent, IntRect(100, 100, 100, 100)));
1696             EXPECT_TRUE(occlusion.occluded(parent, IntRect(200, 200, 100, 100)));
1697         }
1698
1699         {
1700             TestCCOcclusionTrackerBase<typename Types::LayerType, typename Types::RenderSurfaceType> occlusion(IntRect(0, 0, 1000, 1000));
1701             layer->setOpaqueContentsRect(IntRect(150, 150, 100, 100));
1702
1703             occlusion.enterTargetRenderSurface(parent->renderSurface());
1704             occlusion.markOccludedBehindLayer(layer);
1705
1706             EXPECT_EQ_RECT(IntRect(250, 250, 50, 50), occlusion.occlusionInScreenSpace().bounds());
1707             EXPECT_EQ(1u, occlusion.occlusionInScreenSpace().rects().size());
1708
1709             EXPECT_FALSE(occlusion.occluded(parent, IntRect(0, 100, 100, 100)));
1710             EXPECT_FALSE(occlusion.occluded(parent, IntRect(100, 100, 100, 100)));
1711             EXPECT_FALSE(occlusion.occluded(parent, IntRect(200, 200, 100, 100)));
1712         }
1713     }
1714 };
1715
1716 MAIN_AND_IMPL_THREAD_TEST(CCOcclusionTrackerTestOpaqueContentsRegionNonEmpty);
1717
1718 template<class Types, bool opaqueLayers>
1719 class CCOcclusionTrackerTest3dTransform : public CCOcclusionTrackerTest<Types, opaqueLayers> {
1720 protected:
1721     void runMyTest()
1722     {
1723         TransformationMatrix transform;
1724         transform.rotate3d(0, 30, 0);
1725
1726         typename Types::ContentLayerType* parent = this->createRoot(this->identityMatrix, FloatPoint(0, 0), IntSize(300, 300));
1727         typename Types::LayerType* container = this->createLayer(parent, this->identityMatrix, FloatPoint(0, 0), IntSize(300, 300));
1728         typename Types::ContentLayerType* layer = this->createDrawingLayer(container, transform, FloatPoint(100, 100), IntSize(200, 200), true);
1729         this->calcDrawEtc(parent);
1730
1731         TestCCOcclusionTrackerBase<typename Types::LayerType, typename Types::RenderSurfaceType> occlusion(IntRect(0, 0, 1000, 1000));
1732         occlusion.enterTargetRenderSurface(parent->renderSurface());
1733
1734         EXPECT_EQ_RECT(IntRect(0, 0, 200, 200), occlusion.unoccludedContentRect(layer, IntRect(0, 0, 200, 200)));
1735     }
1736 };
1737
1738 MAIN_AND_IMPL_THREAD_TEST(CCOcclusionTrackerTest3dTransform);
1739
1740 template<class Types, bool opaqueLayers>
1741 class CCOcclusionTrackerTestPerspectiveTransform : public CCOcclusionTrackerTest<Types, opaqueLayers> {
1742 protected:
1743     void runMyTest()
1744     {
1745         TransformationMatrix transform;
1746         transform.translate(150, 150);
1747         transform.applyPerspective(400);
1748         transform.rotate3d(1, 0, 0, -30);
1749         transform.translate(-150, -150);
1750
1751         typename Types::ContentLayerType* parent = this->createRoot(this->identityMatrix, FloatPoint(0, 0), IntSize(300, 300));
1752         typename Types::LayerType* container = this->createLayer(parent, this->identityMatrix, FloatPoint(0, 0), IntSize(300, 300));
1753         typename Types::ContentLayerType* layer = this->createDrawingLayer(container, transform, FloatPoint(100, 100), IntSize(200, 200), true);
1754         container->setPreserves3D(true);
1755         this->calcDrawEtc(parent);
1756
1757         TestCCOcclusionTrackerBase<typename Types::LayerType, typename Types::RenderSurfaceType> occlusion(IntRect(0, 0, 1000, 1000));
1758         occlusion.enterTargetRenderSurface(parent->renderSurface());
1759
1760         EXPECT_EQ_RECT(IntRect(0, 0, 200, 200), occlusion.unoccludedContentRect(layer, IntRect(0, 0, 200, 200)));
1761     }
1762 };
1763
1764 MAIN_THREAD_TEST(CCOcclusionTrackerTestPerspectiveTransform);
1765
1766 template<class Types, bool opaqueLayers>
1767 class CCOcclusionTrackerTestPerspectiveTransformBehindCamera : public CCOcclusionTrackerTest<Types, opaqueLayers> {
1768 protected:
1769     void runMyTest()
1770     {
1771         // This test is based on the platform/chromium/compositing/3d-corners.html layout test.
1772         TransformationMatrix transform;
1773         transform.translate(250, 50);
1774         transform.applyPerspective(10);
1775         transform.translate(-250, -50);
1776         transform.translate(250, 50);
1777         transform.rotate3d(1, 0, 0, -167);
1778         transform.translate(-250, -50);
1779
1780         typename Types::ContentLayerType* parent = this->createRoot(this->identityMatrix, FloatPoint(0, 0), IntSize(500, 100));
1781         typename Types::LayerType* container = this->createLayer(parent, this->identityMatrix, FloatPoint(0, 0), IntSize(500, 500));
1782         typename Types::ContentLayerType* layer = this->createDrawingLayer(container, transform, FloatPoint(0, 0), IntSize(500, 500), true);
1783         container->setPreserves3D(true);
1784         this->calcDrawEtc(parent);
1785
1786         TestCCOcclusionTrackerBase<typename Types::LayerType, typename Types::RenderSurfaceType> occlusion(IntRect(0, 0, 1000, 1000));
1787         occlusion.enterTargetRenderSurface(parent->renderSurface());
1788
1789         // The bottom 11 pixel rows of this layer remain visible inside the container, after translation to the target surface. When translated back,
1790         // this will include many more pixels but must include at least the bottom 11 rows.
1791         EXPECT_TRUE(occlusion.unoccludedContentRect(layer, IntRect(0, 0, 500, 500)).contains(IntRect(0, 489, 500, 11)));
1792     }
1793 };
1794
1795 MAIN_THREAD_TEST(CCOcclusionTrackerTestPerspectiveTransformBehindCamera);
1796
1797 } // namespace