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