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