26d52523be134c2efc978b34a0bd4c3320509cac
[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/CCLayerTreeHostCommon.h"
34
35 #include <gmock/gmock.h>
36 #include <gtest/gtest.h>
37
38 using namespace WebCore;
39
40 #define EXPECT_EQ_RECT(a, b) \
41     EXPECT_EQ(a.x(), b.x()); \
42     EXPECT_EQ(a.y(), b.y()); \
43     EXPECT_EQ(a.width(), b.width()); \
44     EXPECT_EQ(a.height(), b.height());
45
46 namespace {
47
48 class LayerChromiumWithForcedDrawsContent : public LayerChromium {
49 public:
50     LayerChromiumWithForcedDrawsContent()
51         : LayerChromium()
52     {
53     }
54
55     virtual bool drawsContent() const { return true; }
56     virtual Region opaqueContentsRegion() const
57     {
58         return intersection(m_opaquePaintRect, visibleLayerRect());
59     }
60
61     void setOpaquePaintRect(const IntRect& opaquePaintRect) { m_opaquePaintRect = opaquePaintRect; }
62
63 private:
64     IntRect m_opaquePaintRect;
65 };
66
67 void setLayerPropertiesForTesting(LayerChromium* layer, const TransformationMatrix& transform, const FloatPoint& position, const IntSize& bounds)
68 {
69     layer->setTransform(transform);
70     layer->setSublayerTransform(TransformationMatrix());
71     layer->setAnchorPoint(FloatPoint(0, 0));
72     layer->setPosition(position);
73     layer->setBounds(bounds);
74 }
75
76 void setLayerPropertiesForTesting(LayerChromiumWithForcedDrawsContent* layer, const TransformationMatrix& transform, const FloatPoint& position, const IntSize& bounds, bool opaque, bool opaqueLayers)
77 {
78     setLayerPropertiesForTesting(layer, transform, position, bounds);
79     if (opaqueLayers)
80         layer->setOpaque(opaque);
81     else {
82         layer->setOpaque(false);
83         if (opaque)
84             layer->setOpaquePaintRect(IntRect(IntPoint(), bounds));
85         else
86             layer->setOpaquePaintRect(IntRect());
87     }
88 }
89
90 // A subclass to expose the total current occlusion.
91 class TestCCOcclusionTracker : public CCOcclusionTracker {
92 public:
93     TestCCOcclusionTracker(IntRect screenScissorRect)
94         : CCOcclusionTracker(screenScissorRect)
95         , m_overrideLayerScissorRect(false)
96     {
97     }
98
99     TestCCOcclusionTracker(IntRect screenScissorRect, const CCOcclusionTrackerDamageClient* damageClient)
100         : CCOcclusionTracker(screenScissorRect, damageClient)
101         , m_overrideLayerScissorRect(false)
102     {
103     }
104
105     Region occlusionInScreenSpace() const { return CCOcclusionTracker::m_stack.last().occlusionInScreen; }
106     Region occlusionInTargetSurface() const { return CCOcclusionTracker::m_stack.last().occlusionInTarget; }
107
108     void setOcclusionInScreenSpace(const Region& region) { CCOcclusionTracker::m_stack.last().occlusionInScreen = region; }
109     void setOcclusionInTargetSurface(const Region& region) { CCOcclusionTracker::m_stack.last().occlusionInTarget = region; }
110
111     void setLayerScissorRect(const IntRect& rect) { m_overrideLayerScissorRect = true; m_layerScissorRect = rect;}
112     void useDefaultLayerScissorRect() { m_overrideLayerScissorRect = false; }
113
114 protected:
115     virtual IntRect layerScissorRectInTargetSurface(const LayerChromium* layer) const { return m_overrideLayerScissorRect ? m_layerScissorRect : CCOcclusionTracker::layerScissorRectInTargetSurface(layer); }
116
117 private:
118     bool m_overrideLayerScissorRect;
119     IntRect m_layerScissorRect;
120 };
121
122 class TestDamageClient : public CCOcclusionTrackerDamageClient {
123 public:
124     // The interface
125     virtual FloatRect damageRect(const RenderSurfaceChromium*) const { return m_damageRect; }
126
127     // Testing stuff
128     TestDamageClient(const FloatRect& damageRect) : m_damageRect(damageRect) { }
129     void setDamageRect(const FloatRect& damageRect) { m_damageRect = damageRect; }
130
131 private:
132     FloatRect m_damageRect;
133 };
134
135 #define TEST_OPAQUE_AND_PAINTED_OPAQUE(FULLTESTNAME, SHORTTESTNAME) \
136     TEST(FULLTESTNAME, opaqueLayer)                                 \
137     {                                                               \
138         SHORTTESTNAME(true);                                        \
139     }                                                               \
140     TEST(FULLTESTNAME, opaquePaintRect)                             \
141     {                                                               \
142         SHORTTESTNAME(false);                                       \
143     }
144
145 void layerAddedToOccludedRegion(bool opaqueLayers)
146 {
147     // This tests that the right transforms are being used.
148     const TransformationMatrix identityMatrix;
149     RefPtr<LayerChromium> parent = LayerChromium::create();
150     RefPtr<LayerChromiumWithForcedDrawsContent> layer = adoptRef(new LayerChromiumWithForcedDrawsContent());
151     parent->createRenderSurface();
152     parent->addChild(layer);
153
154     setLayerPropertiesForTesting(parent.get(), identityMatrix, FloatPoint(0, 0), IntSize(100, 100));
155     setLayerPropertiesForTesting(layer.get(), identityMatrix, FloatPoint(30, 30), IntSize(500, 500), true, opaqueLayers);
156
157     Vector<RefPtr<LayerChromium> > renderSurfaceLayerList;
158     Vector<RefPtr<LayerChromium> > dummyLayerList;
159     int dummyMaxTextureSize = 512;
160
161     parent->renderSurface()->setContentRect(IntRect(IntPoint::zero(), parent->bounds()));
162     parent->setClipRect(IntRect(IntPoint::zero(), parent->bounds()));
163     renderSurfaceLayerList.append(parent);
164
165     CCLayerTreeHostCommon::calculateDrawTransformsAndVisibility(parent.get(), parent.get(), identityMatrix, identityMatrix, renderSurfaceLayerList, dummyLayerList, dummyMaxTextureSize);
166
167     TestCCOcclusionTracker occlusion(IntRect(0, 0, 1000, 1000));
168     occlusion.setLayerScissorRect(IntRect(0, 0, 1000, 1000));
169
170     occlusion.enterTargetRenderSurface(parent->renderSurface());
171     occlusion.markOccludedBehindLayer(layer.get());
172     EXPECT_EQ_RECT(IntRect(30, 30, 70, 70), occlusion.occlusionInScreenSpace().bounds());
173     EXPECT_EQ(1u, occlusion.occlusionInScreenSpace().rects().size());
174     EXPECT_EQ_RECT(IntRect(30, 30, 70, 70), occlusion.occlusionInTargetSurface().bounds());
175     EXPECT_EQ(1u, occlusion.occlusionInTargetSurface().rects().size());
176
177     EXPECT_TRUE(occlusion.occluded(parent.get(), IntRect(30, 30, 70, 70)));
178     EXPECT_FALSE(occlusion.occluded(parent.get(), IntRect(29, 30, 70, 70)));
179     EXPECT_FALSE(occlusion.occluded(parent.get(), IntRect(30, 29, 70, 70)));
180     EXPECT_FALSE(occlusion.occluded(parent.get(), IntRect(31, 30, 70, 70)));
181     EXPECT_FALSE(occlusion.occluded(parent.get(), IntRect(30, 31, 70, 70)));
182
183     occlusion.useDefaultLayerScissorRect();
184     EXPECT_TRUE(occlusion.occluded(parent.get(), IntRect(30, 30, 70, 70)));
185     EXPECT_FALSE(occlusion.occluded(parent.get(), IntRect(29, 30, 70, 70)));
186     EXPECT_FALSE(occlusion.occluded(parent.get(), IntRect(30, 29, 70, 70)));
187     EXPECT_TRUE(occlusion.occluded(parent.get(), IntRect(31, 30, 70, 70)));
188     EXPECT_TRUE(occlusion.occluded(parent.get(), IntRect(30, 31, 70, 70)));
189     occlusion.setLayerScissorRect(IntRect(0, 0, 1000, 1000));
190
191     EXPECT_TRUE(occlusion.unoccludedContentRect(parent.get(), IntRect(30, 30, 70, 70)).isEmpty());
192     EXPECT_EQ_RECT(IntRect(29, 30, 1, 70), occlusion.unoccludedContentRect(parent.get(), IntRect(29, 30, 70, 70)));
193     EXPECT_EQ_RECT(IntRect(29, 29, 70, 70), occlusion.unoccludedContentRect(parent.get(), IntRect(29, 29, 70, 70)));
194     EXPECT_EQ_RECT(IntRect(30, 29, 70, 1), occlusion.unoccludedContentRect(parent.get(), IntRect(30, 29, 70, 70)));
195     EXPECT_EQ_RECT(IntRect(31, 29, 70, 70), occlusion.unoccludedContentRect(parent.get(), IntRect(31, 29, 70, 70)));
196     EXPECT_EQ_RECT(IntRect(100, 30, 1, 70), occlusion.unoccludedContentRect(parent.get(), IntRect(31, 30, 70, 70)));
197     EXPECT_EQ_RECT(IntRect(31, 31, 70, 70), occlusion.unoccludedContentRect(parent.get(), IntRect(31, 31, 70, 70)));
198     EXPECT_EQ_RECT(IntRect(30, 100, 70, 1), occlusion.unoccludedContentRect(parent.get(), IntRect(30, 31, 70, 70)));
199     EXPECT_EQ_RECT(IntRect(29, 31, 70, 70), occlusion.unoccludedContentRect(parent.get(), IntRect(29, 31, 70, 70)));
200 }
201
202 TEST_OPAQUE_AND_PAINTED_OPAQUE(CCOcclusionTrackerTest_layerAddedToOccludedRegion, layerAddedToOccludedRegion);
203
204 void layerAddedToOccludedRegionWithRotation(bool opaqueLayers)
205 {
206     // This tests that the right transforms are being used.
207     const TransformationMatrix identityMatrix;
208     RefPtr<LayerChromium> parent = LayerChromium::create();
209     RefPtr<LayerChromiumWithForcedDrawsContent> layer = adoptRef(new LayerChromiumWithForcedDrawsContent());
210     parent->createRenderSurface();
211     parent->addChild(layer);
212
213     TransformationMatrix layerTransform;
214     layerTransform.translate(250, 250);
215     layerTransform.rotate(90);
216     layerTransform.translate(-250, -250);
217
218     setLayerPropertiesForTesting(parent.get(), identityMatrix, FloatPoint(0, 0), IntSize(100, 100));
219     setLayerPropertiesForTesting(layer.get(), layerTransform, FloatPoint(30, 30), IntSize(500, 500), true, opaqueLayers);
220
221     Vector<RefPtr<LayerChromium> > renderSurfaceLayerList;
222     Vector<RefPtr<LayerChromium> > dummyLayerList;
223     int dummyMaxTextureSize = 512;
224
225     parent->renderSurface()->setContentRect(IntRect(IntPoint::zero(), parent->bounds()));
226     parent->setClipRect(IntRect(IntPoint::zero(), parent->bounds()));
227     renderSurfaceLayerList.append(parent);
228
229     CCLayerTreeHostCommon::calculateDrawTransformsAndVisibility(parent.get(), parent.get(), identityMatrix, identityMatrix, renderSurfaceLayerList, dummyLayerList, dummyMaxTextureSize);
230
231     TestCCOcclusionTracker occlusion(IntRect(0, 0, 1000, 1000));
232     occlusion.setLayerScissorRect(IntRect(0, 0, 1000, 1000));
233
234     occlusion.enterTargetRenderSurface(parent->renderSurface());
235     occlusion.markOccludedBehindLayer(layer.get());
236     EXPECT_EQ_RECT(IntRect(30, 30, 70, 70), occlusion.occlusionInScreenSpace().bounds());
237     EXPECT_EQ(1u, occlusion.occlusionInScreenSpace().rects().size());
238     EXPECT_EQ_RECT(IntRect(30, 30, 70, 70), occlusion.occlusionInTargetSurface().bounds());
239     EXPECT_EQ(1u, occlusion.occlusionInTargetSurface().rects().size());
240
241     EXPECT_TRUE(occlusion.occluded(parent.get(), IntRect(30, 30, 70, 70)));
242     EXPECT_FALSE(occlusion.occluded(parent.get(), IntRect(29, 30, 70, 70)));
243     EXPECT_FALSE(occlusion.occluded(parent.get(), IntRect(30, 29, 70, 70)));
244     EXPECT_FALSE(occlusion.occluded(parent.get(), IntRect(31, 30, 70, 70)));
245     EXPECT_FALSE(occlusion.occluded(parent.get(), IntRect(30, 31, 70, 70)));
246
247     occlusion.useDefaultLayerScissorRect();
248     EXPECT_TRUE(occlusion.occluded(parent.get(), IntRect(30, 30, 70, 70)));
249     EXPECT_FALSE(occlusion.occluded(parent.get(), IntRect(29, 30, 70, 70)));
250     EXPECT_FALSE(occlusion.occluded(parent.get(), IntRect(30, 29, 70, 70)));
251     EXPECT_TRUE(occlusion.occluded(parent.get(), IntRect(31, 30, 70, 70)));
252     EXPECT_TRUE(occlusion.occluded(parent.get(), IntRect(30, 31, 70, 70)));
253     occlusion.setLayerScissorRect(IntRect(0, 0, 1000, 1000));
254
255     EXPECT_TRUE(occlusion.unoccludedContentRect(parent.get(), IntRect(30, 30, 70, 70)).isEmpty());
256     EXPECT_EQ_RECT(IntRect(29, 30, 1, 70), occlusion.unoccludedContentRect(parent.get(), IntRect(29, 30, 70, 70)));
257     EXPECT_EQ_RECT(IntRect(29, 29, 70, 70), occlusion.unoccludedContentRect(parent.get(), IntRect(29, 29, 70, 70)));
258     EXPECT_EQ_RECT(IntRect(30, 29, 70, 1), occlusion.unoccludedContentRect(parent.get(), IntRect(30, 29, 70, 70)));
259     EXPECT_EQ_RECT(IntRect(31, 29, 70, 70), occlusion.unoccludedContentRect(parent.get(), IntRect(31, 29, 70, 70)));
260     EXPECT_EQ_RECT(IntRect(100, 30, 1, 70), occlusion.unoccludedContentRect(parent.get(), IntRect(31, 30, 70, 70)));
261     EXPECT_EQ_RECT(IntRect(31, 31, 70, 70), occlusion.unoccludedContentRect(parent.get(), IntRect(31, 31, 70, 70)));
262     EXPECT_EQ_RECT(IntRect(30, 100, 70, 1), occlusion.unoccludedContentRect(parent.get(), IntRect(30, 31, 70, 70)));
263     EXPECT_EQ_RECT(IntRect(29, 31, 70, 70), occlusion.unoccludedContentRect(parent.get(), IntRect(29, 31, 70, 70)));
264 }
265
266 TEST_OPAQUE_AND_PAINTED_OPAQUE(CCOcclusionTrackerTest_layerAddedToOccludedRegionWithRotation, layerAddedToOccludedRegionWithRotation);
267
268 void layerAddedToOccludedRegionWithTranslation(bool opaqueLayers)
269 {
270     // This tests that the right transforms are being used.
271     const TransformationMatrix identityMatrix;
272     RefPtr<LayerChromium> parent = LayerChromium::create();
273     RefPtr<LayerChromiumWithForcedDrawsContent> layer = adoptRef(new LayerChromiumWithForcedDrawsContent());
274     parent->createRenderSurface();
275     parent->addChild(layer);
276
277     TransformationMatrix layerTransform;
278     layerTransform.translate(20, 20);
279
280     setLayerPropertiesForTesting(parent.get(), identityMatrix, FloatPoint(0, 0), IntSize(100, 100));
281     setLayerPropertiesForTesting(layer.get(), layerTransform, FloatPoint(30, 30), IntSize(500, 500), true, opaqueLayers);
282
283     Vector<RefPtr<LayerChromium> > renderSurfaceLayerList;
284     Vector<RefPtr<LayerChromium> > dummyLayerList;
285     int dummyMaxTextureSize = 512;
286
287     parent->renderSurface()->setContentRect(IntRect(IntPoint::zero(), parent->bounds()));
288     parent->setClipRect(IntRect(IntPoint::zero(), parent->bounds()));
289     renderSurfaceLayerList.append(parent);
290
291     CCLayerTreeHostCommon::calculateDrawTransformsAndVisibility(parent.get(), parent.get(), identityMatrix, identityMatrix, renderSurfaceLayerList, dummyLayerList, dummyMaxTextureSize);
292
293     TestCCOcclusionTracker occlusion(IntRect(0, 0, 1000, 1000));
294     occlusion.setLayerScissorRect(IntRect(0, 0, 1000, 1000));
295
296     occlusion.enterTargetRenderSurface(parent->renderSurface());
297     occlusion.markOccludedBehindLayer(layer.get());
298     EXPECT_EQ_RECT(IntRect(50, 50, 50, 50), occlusion.occlusionInScreenSpace().bounds());
299     EXPECT_EQ(1u, occlusion.occlusionInScreenSpace().rects().size());
300     EXPECT_EQ_RECT(IntRect(50, 50, 50, 50), occlusion.occlusionInTargetSurface().bounds());
301     EXPECT_EQ(1u, occlusion.occlusionInTargetSurface().rects().size());
302
303     EXPECT_TRUE(occlusion.occluded(parent.get(), IntRect(50, 50, 50, 50)));
304     EXPECT_FALSE(occlusion.occluded(parent.get(), IntRect(49, 50, 50, 50)));
305     EXPECT_FALSE(occlusion.occluded(parent.get(), IntRect(50, 49, 50, 50)));
306     EXPECT_FALSE(occlusion.occluded(parent.get(), IntRect(51, 50, 50, 50)));
307     EXPECT_FALSE(occlusion.occluded(parent.get(), IntRect(50, 51, 50, 50)));
308
309     occlusion.useDefaultLayerScissorRect();
310     EXPECT_TRUE(occlusion.occluded(parent.get(), IntRect(50, 50, 50, 50)));
311     EXPECT_FALSE(occlusion.occluded(parent.get(), IntRect(49, 50, 50, 50)));
312     EXPECT_FALSE(occlusion.occluded(parent.get(), IntRect(50, 49, 50, 50)));
313     EXPECT_TRUE(occlusion.occluded(parent.get(), IntRect(51, 50, 50, 50)));
314     EXPECT_TRUE(occlusion.occluded(parent.get(), IntRect(50, 51, 50, 50)));
315     occlusion.setLayerScissorRect(IntRect(0, 0, 1000, 1000));
316
317     EXPECT_TRUE(occlusion.unoccludedContentRect(parent.get(), IntRect(50, 50, 50, 50)).isEmpty());
318     EXPECT_EQ_RECT(IntRect(49, 50, 1, 50), occlusion.unoccludedContentRect(parent.get(), IntRect(49, 50, 50, 50)));
319     EXPECT_EQ_RECT(IntRect(49, 49, 50, 50), occlusion.unoccludedContentRect(parent.get(), IntRect(49, 49, 50, 50)));
320     EXPECT_EQ_RECT(IntRect(50, 49, 50, 1), occlusion.unoccludedContentRect(parent.get(), IntRect(50, 49, 50, 50)));
321     EXPECT_EQ_RECT(IntRect(51, 49, 50, 50), occlusion.unoccludedContentRect(parent.get(), IntRect(51, 49, 50, 50)));
322     EXPECT_EQ_RECT(IntRect(100, 50, 1, 50), occlusion.unoccludedContentRect(parent.get(), IntRect(51, 50, 50, 50)));
323     EXPECT_EQ_RECT(IntRect(51, 51, 50, 50), occlusion.unoccludedContentRect(parent.get(), IntRect(51, 51, 50, 50)));
324     EXPECT_EQ_RECT(IntRect(50, 100, 50, 1), occlusion.unoccludedContentRect(parent.get(), IntRect(50, 51, 50, 50)));
325     EXPECT_EQ_RECT(IntRect(49, 51, 50, 50), occlusion.unoccludedContentRect(parent.get(), IntRect(49, 51, 50, 50)));
326
327     occlusion.useDefaultLayerScissorRect();
328     EXPECT_TRUE(occlusion.unoccludedContentRect(parent.get(), IntRect(50, 50, 50, 50)).isEmpty());
329     EXPECT_EQ_RECT(IntRect(49, 50, 1, 50), occlusion.unoccludedContentRect(parent.get(), IntRect(49, 50, 50, 50)));
330     EXPECT_EQ_RECT(IntRect(49, 49, 50, 50), occlusion.unoccludedContentRect(parent.get(), IntRect(49, 49, 50, 50)));
331     EXPECT_EQ_RECT(IntRect(50, 49, 50, 1), occlusion.unoccludedContentRect(parent.get(), IntRect(50, 49, 50, 50)));
332     EXPECT_EQ_RECT(IntRect(51, 49, 49, 1), occlusion.unoccludedContentRect(parent.get(), IntRect(51, 49, 50, 50)));
333     EXPECT_TRUE(occlusion.unoccludedContentRect(parent.get(), IntRect(51, 50, 50, 50)).isEmpty());
334     EXPECT_TRUE(occlusion.unoccludedContentRect(parent.get(), IntRect(51, 51, 50, 50)).isEmpty());
335     EXPECT_TRUE(occlusion.unoccludedContentRect(parent.get(), IntRect(50, 51, 50, 50)).isEmpty());
336     EXPECT_EQ_RECT(IntRect(49, 51, 1, 49), occlusion.unoccludedContentRect(parent.get(), IntRect(49, 51, 50, 50)));
337     occlusion.setLayerScissorRect(IntRect(0, 0, 1000, 1000));
338 }
339
340 TEST_OPAQUE_AND_PAINTED_OPAQUE(CCOcclusionTrackerTest_layerAddedToOccludedRegionWithTranslation, layerAddedToOccludedRegionWithTranslation);
341
342 void layerAddedToOccludedRegionWithRotatedSurface(bool opaqueLayers)
343 {
344     // This tests that the right transforms are being used.
345     const TransformationMatrix identityMatrix;
346     RefPtr<LayerChromium> parent = LayerChromium::create();
347     RefPtr<LayerChromium> child = LayerChromium::create();
348     RefPtr<LayerChromiumWithForcedDrawsContent> layer = adoptRef(new LayerChromiumWithForcedDrawsContent());
349     parent->createRenderSurface();
350     parent->addChild(child);
351     child->addChild(layer);
352
353     TransformationMatrix childTransform;
354     childTransform.translate(250, 250);
355     childTransform.rotate(90);
356     childTransform.translate(-250, -250);
357
358     setLayerPropertiesForTesting(parent.get(), identityMatrix, FloatPoint(0, 0), IntSize(100, 100));
359     setLayerPropertiesForTesting(child.get(), childTransform, FloatPoint(30, 30), IntSize(500, 500));
360     setLayerPropertiesForTesting(layer.get(), identityMatrix, FloatPoint(10, 10), IntSize(500, 500), true, opaqueLayers);
361
362     child->setMasksToBounds(true);
363
364     Vector<RefPtr<LayerChromium> > renderSurfaceLayerList;
365     Vector<RefPtr<LayerChromium> > dummyLayerList;
366     int dummyMaxTextureSize = 512;
367
368     parent->renderSurface()->setContentRect(IntRect(IntPoint::zero(), parent->bounds()));
369     parent->setClipRect(IntRect(IntPoint::zero(), parent->bounds()));
370     renderSurfaceLayerList.append(parent);
371
372     CCLayerTreeHostCommon::calculateDrawTransformsAndVisibility(parent.get(), parent.get(), identityMatrix, identityMatrix, renderSurfaceLayerList, dummyLayerList, dummyMaxTextureSize);
373
374     TestCCOcclusionTracker occlusion(IntRect(0, 0, 1000, 1000));
375     occlusion.setLayerScissorRect(IntRect(0, 0, 1000, 1000));
376
377     occlusion.enterTargetRenderSurface(child->renderSurface());
378     occlusion.markOccludedBehindLayer(layer.get());
379
380     EXPECT_EQ_RECT(IntRect(30, 40, 70, 60), occlusion.occlusionInScreenSpace().bounds());
381     EXPECT_EQ(1u, occlusion.occlusionInScreenSpace().rects().size());
382     EXPECT_EQ_RECT(IntRect(10, 430, 60, 70), occlusion.occlusionInTargetSurface().bounds());
383     EXPECT_EQ(1u, occlusion.occlusionInTargetSurface().rects().size());
384
385     EXPECT_TRUE(occlusion.occluded(child.get(), IntRect(10, 430, 60, 70)));
386     EXPECT_FALSE(occlusion.occluded(child.get(), IntRect(9, 430, 60, 70)));
387     EXPECT_FALSE(occlusion.occluded(child.get(), IntRect(10, 429, 60, 70)));
388     EXPECT_FALSE(occlusion.occluded(child.get(), IntRect(10, 430, 61, 70)));
389     EXPECT_FALSE(occlusion.occluded(child.get(), IntRect(10, 430, 60, 71)));
390
391     occlusion.useDefaultLayerScissorRect();
392     EXPECT_TRUE(occlusion.occluded(child.get(), IntRect(10, 430, 60, 70)));
393     EXPECT_TRUE(occlusion.occluded(child.get(), IntRect(9, 430, 60, 70)));
394     EXPECT_TRUE(occlusion.occluded(child.get(), IntRect(10, 429, 60, 70)));
395     EXPECT_TRUE(occlusion.occluded(child.get(), IntRect(10, 430, 61, 70)));
396     EXPECT_TRUE(occlusion.occluded(child.get(), IntRect(10, 430, 60, 71)));
397     occlusion.setLayerScissorRect(IntRect(0, 0, 1000, 1000));
398
399     occlusion.markOccludedBehindLayer(child.get());
400     occlusion.finishedTargetRenderSurface(child.get(), child->renderSurface());
401     occlusion.leaveToTargetRenderSurface(parent->renderSurface());
402
403     EXPECT_EQ_RECT(IntRect(30, 40, 70, 60), occlusion.occlusionInScreenSpace().bounds());
404     EXPECT_EQ(1u, occlusion.occlusionInScreenSpace().rects().size());
405     EXPECT_EQ_RECT(IntRect(30, 40, 70, 60), occlusion.occlusionInTargetSurface().bounds());
406     EXPECT_EQ(1u, occlusion.occlusionInTargetSurface().rects().size());
407
408     EXPECT_TRUE(occlusion.occluded(parent.get(), IntRect(30, 40, 70, 60)));
409     EXPECT_FALSE(occlusion.occluded(parent.get(), IntRect(29, 40, 70, 60)));
410     EXPECT_FALSE(occlusion.occluded(parent.get(), IntRect(30, 39, 70, 60)));
411     EXPECT_FALSE(occlusion.occluded(parent.get(), IntRect(31, 40, 70, 60)));
412     EXPECT_FALSE(occlusion.occluded(parent.get(), IntRect(30, 41, 70, 60)));
413
414     occlusion.useDefaultLayerScissorRect();
415     EXPECT_TRUE(occlusion.occluded(parent.get(), IntRect(30, 40, 70, 60)));
416     EXPECT_FALSE(occlusion.occluded(parent.get(), IntRect(29, 40, 70, 60)));
417     EXPECT_FALSE(occlusion.occluded(parent.get(), IntRect(30, 39, 70, 60)));
418     EXPECT_TRUE(occlusion.occluded(parent.get(), IntRect(31, 40, 70, 60)));
419     EXPECT_TRUE(occlusion.occluded(parent.get(), IntRect(30, 41, 70, 60)));
420     occlusion.setLayerScissorRect(IntRect(0, 0, 1000, 1000));
421
422
423     /* Justification for the above occlusion from |layer|:
424                100
425       +---------------------+                                      +---------------------+
426       |                     |                                      |                     |30  Visible region of |layer|: /////
427       |    30               |           rotate(90)                 |                     |
428       | 30 + ---------------------------------+                    |     +---------------------------------+
429   100 |    |  10            |                 |            ==>     |     |               |10               |
430       |    |10+---------------------------------+                  |  +---------------------------------+  |
431       |    |  |             |                 | |                  |  |  |///////////////|     420      |  |
432       |    |  |             |                 | |                  |  |  |///////////////|60            |  |
433       |    |  |             |                 | |                  |  |  |///////////////|              |  |
434       +----|--|-------------+                 | |                  +--|--|---------------+              |  |
435            |  |                               | |                   20|10|     70                       |  |
436            |  |                               | |                     |  |                              |  |
437            |  |                               | |500                  |  |                              |  |
438            |  |                               | |                     |  |                              |  |
439            |  |                               | |                     |  |                              |  |
440            |  |                               | |                     |  |                              |  |
441            |  |                               | |                     |  |                              |10|
442            +--|-------------------------------+ |                     |  +------------------------------|--+
443               |                                 |                     |                 490             |
444               +---------------------------------+                     +---------------------------------+
445                              500                                                     500
446      */
447 }
448
449 TEST_OPAQUE_AND_PAINTED_OPAQUE(CCOcclusionTrackerTest_layerAddedToOccludedRegionWithRotatedSurface, layerAddedToOccludedRegionWithRotatedSurface);
450
451 void layerAddedToOccludedRegionWithSurfaceAlreadyOnStack(bool opaqueLayers)
452 {
453     // This tests that the right transforms are being used.
454     const TransformationMatrix identityMatrix;
455     RefPtr<LayerChromium> parent = LayerChromium::create();
456     RefPtr<LayerChromium> child = LayerChromium::create();
457     RefPtr<LayerChromiumWithForcedDrawsContent> child2 = adoptRef(new LayerChromiumWithForcedDrawsContent());
458     RefPtr<LayerChromiumWithForcedDrawsContent> layer = adoptRef(new LayerChromiumWithForcedDrawsContent());
459     parent->createRenderSurface();
460     parent->addChild(child);
461     child->addChild(layer);
462     parent->addChild(child2);
463
464     TransformationMatrix childTransform;
465     childTransform.translate(250, 250);
466     childTransform.rotate(90);
467     childTransform.translate(-250, -250);
468
469     setLayerPropertiesForTesting(parent.get(), identityMatrix, FloatPoint(0, 0), IntSize(100, 100));
470     setLayerPropertiesForTesting(child.get(), childTransform, FloatPoint(30, 30), IntSize(500, 500));
471     setLayerPropertiesForTesting(layer.get(), identityMatrix, FloatPoint(10, 10), IntSize(500, 500), true, opaqueLayers);
472
473     // |child2| makes |parent|'s surface get considered by CCOcclusionTracker first, instead of |child|'s. This exercises different code in
474     // leaveToTargetRenderSurface, as the target surface has already been seen.
475     setLayerPropertiesForTesting(child2.get(), identityMatrix, FloatPoint(30, 30), IntSize(60, 20), true, opaqueLayers);
476
477     child->setMasksToBounds(true);
478
479     Vector<RefPtr<LayerChromium> > renderSurfaceLayerList;
480     Vector<RefPtr<LayerChromium> > dummyLayerList;
481     int dummyMaxTextureSize = 512;
482
483     parent->renderSurface()->setContentRect(IntRect(IntPoint::zero(), parent->bounds()));
484     parent->setClipRect(IntRect(IntPoint::zero(), parent->bounds()));
485     renderSurfaceLayerList.append(parent);
486
487     CCLayerTreeHostCommon::calculateDrawTransformsAndVisibility(parent.get(), parent.get(), identityMatrix, identityMatrix, renderSurfaceLayerList, dummyLayerList, dummyMaxTextureSize);
488
489     TestCCOcclusionTracker occlusion(IntRect(0, 0, 1000, 1000));
490     occlusion.setLayerScissorRect(IntRect(-10, -10, 1000, 1000));
491
492     occlusion.enterTargetRenderSurface(parent->renderSurface());
493     occlusion.markOccludedBehindLayer(child2.get());
494
495     EXPECT_EQ_RECT(IntRect(30, 30, 60, 20), occlusion.occlusionInScreenSpace().bounds());
496     EXPECT_EQ(1u, occlusion.occlusionInScreenSpace().rects().size());
497     EXPECT_EQ_RECT(IntRect(30, 30, 60, 20), occlusion.occlusionInTargetSurface().bounds());
498     EXPECT_EQ(1u, occlusion.occlusionInTargetSurface().rects().size());
499
500     occlusion.enterTargetRenderSurface(child->renderSurface());
501     occlusion.markOccludedBehindLayer(layer.get());
502
503     EXPECT_EQ_RECT(IntRect(30, 30, 70, 70), occlusion.occlusionInScreenSpace().bounds());
504     EXPECT_EQ(2u, occlusion.occlusionInScreenSpace().rects().size());
505     EXPECT_EQ_RECT(IntRect(10, 430, 60, 70), occlusion.occlusionInTargetSurface().bounds());
506     EXPECT_EQ(1u, occlusion.occlusionInTargetSurface().rects().size());
507
508     EXPECT_TRUE(occlusion.occluded(child.get(), IntRect(10, 430, 60, 70)));
509     EXPECT_FALSE(occlusion.occluded(child.get(), IntRect(9, 430, 60, 70)));
510     EXPECT_FALSE(occlusion.occluded(child.get(), IntRect(10, 429, 60, 70)));
511     EXPECT_FALSE(occlusion.occluded(child.get(), IntRect(11, 430, 60, 70)));
512     EXPECT_FALSE(occlusion.occluded(child.get(), IntRect(10, 431, 60, 70)));
513
514     occlusion.useDefaultLayerScissorRect();
515     EXPECT_TRUE(occlusion.occluded(child.get(), IntRect(10, 430, 60, 70)));
516     EXPECT_TRUE(occlusion.occluded(child.get(), IntRect(9, 430, 60, 70)));
517     EXPECT_TRUE(occlusion.occluded(child.get(), IntRect(10, 429, 60, 70)));
518     EXPECT_TRUE(occlusion.occluded(child.get(), IntRect(11, 430, 60, 70)));
519     EXPECT_TRUE(occlusion.occluded(child.get(), IntRect(10, 431, 60, 70)));
520     occlusion.setLayerScissorRect(IntRect(-10, -10, 1000, 1000));
521
522     EXPECT_TRUE(occlusion.unoccludedContentRect(child.get(), IntRect(10, 430, 60, 70)).isEmpty());
523     // This is the little piece not occluded by child2
524     EXPECT_EQ_RECT(IntRect(9, 430, 1, 10), occlusion.unoccludedContentRect(child.get(), IntRect(9, 430, 60, 70)));
525     // This extends past both sides of child2, so it will be the original rect.
526     EXPECT_EQ_RECT(IntRect(9, 430, 60, 80), occlusion.unoccludedContentRect(child.get(), IntRect(9, 430, 60, 80)));
527     // This extends past two adjacent sides of child2, and should included the unoccluded parts of each side.
528     // This also demonstrates that the rect can be arbitrary and does not get clipped to the layer's visibleLayerRect().
529     EXPECT_EQ_RECT(IntRect(-10, 430, 20, 70), occlusion.unoccludedContentRect(child.get(), IntRect(-10, 430, 60, 70)));
530     // This extends past three adjacent sides of child2, so it should contain the unoccluded parts of each side. The left
531     // and bottom edges are completely unoccluded for some row/column so we get back the original query rect.
532     EXPECT_EQ_RECT(IntRect(-10, 430, 60, 80), occlusion.unoccludedContentRect(child.get(), IntRect(-10, 430, 60, 80)));
533     EXPECT_EQ_RECT(IntRect(10, 429, 60, 1), occlusion.unoccludedContentRect(child.get(), IntRect(10, 429, 60, 70)));
534     EXPECT_EQ_RECT(IntRect(70, 430, 1, 70), occlusion.unoccludedContentRect(child.get(), IntRect(11, 430, 60, 70)));
535     EXPECT_EQ_RECT(IntRect(10, 500, 60, 1), occlusion.unoccludedContentRect(child.get(), IntRect(10, 431, 60, 70)));
536
537     occlusion.useDefaultLayerScissorRect();
538     EXPECT_TRUE(occlusion.unoccludedContentRect(child.get(), IntRect(10, 430, 60, 70)).isEmpty());
539     EXPECT_TRUE(occlusion.unoccludedContentRect(child.get(), IntRect(9, 430, 60, 70)).isEmpty());
540     EXPECT_TRUE(occlusion.unoccludedContentRect(child.get(), IntRect(9, 430, 60, 80)).isEmpty());
541     EXPECT_TRUE(occlusion.unoccludedContentRect(child.get(), IntRect(-10, 430, 60, 70)).isEmpty());
542     EXPECT_TRUE(occlusion.unoccludedContentRect(child.get(), IntRect(-10, 430, 60, 80)).isEmpty());
543     EXPECT_TRUE(occlusion.unoccludedContentRect(child.get(), IntRect(10, 429, 60, 70)).isEmpty());
544     EXPECT_TRUE(occlusion.unoccludedContentRect(child.get(), IntRect(11, 430, 60, 70)).isEmpty());
545     EXPECT_TRUE(occlusion.unoccludedContentRect(child.get(), IntRect(10, 431, 60, 70)).isEmpty());
546     occlusion.setLayerScissorRect(IntRect(0, 0, 1000, 1000));
547
548     occlusion.markOccludedBehindLayer(child.get());
549     // |child2| should get merged with the surface we are leaving now
550     occlusion.finishedTargetRenderSurface(child.get(), child->renderSurface());
551     occlusion.leaveToTargetRenderSurface(parent->renderSurface());
552
553     EXPECT_EQ_RECT(IntRect(30, 30, 70, 70), occlusion.occlusionInScreenSpace().bounds());
554     EXPECT_EQ(2u, occlusion.occlusionInScreenSpace().rects().size());
555     EXPECT_EQ_RECT(IntRect(30, 30, 70, 70), occlusion.occlusionInTargetSurface().bounds());
556     EXPECT_EQ(2u, occlusion.occlusionInTargetSurface().rects().size());
557
558     Vector<IntRect> screen = occlusion.occlusionInScreenSpace().rects();
559     Vector<IntRect> target = occlusion.occlusionInTargetSurface().rects();
560
561     EXPECT_FALSE(occlusion.occluded(parent.get(), IntRect(30, 30, 70, 70)));
562     EXPECT_EQ_RECT(IntRect(90, 30, 10, 10), occlusion.unoccludedContentRect(parent.get(), IntRect(30, 30, 70, 70)));
563
564     EXPECT_TRUE(occlusion.occluded(parent.get(), IntRect(30, 30, 60, 10)));
565     EXPECT_FALSE(occlusion.occluded(parent.get(), IntRect(29, 30, 60, 10)));
566     EXPECT_FALSE(occlusion.occluded(parent.get(), IntRect(30, 29, 60, 10)));
567     EXPECT_FALSE(occlusion.occluded(parent.get(), IntRect(31, 30, 60, 10)));
568     EXPECT_TRUE(occlusion.occluded(parent.get(), IntRect(30, 31, 60, 10)));
569
570     EXPECT_TRUE(occlusion.occluded(parent.get(), IntRect(30, 40, 70, 60)));
571     EXPECT_FALSE(occlusion.occluded(parent.get(), IntRect(29, 40, 70, 60)));
572     EXPECT_FALSE(occlusion.occluded(parent.get(), IntRect(30, 39, 70, 60)));
573
574     EXPECT_TRUE(occlusion.unoccludedContentRect(parent.get(), IntRect(30, 30, 60, 10)).isEmpty());
575     EXPECT_EQ_RECT(IntRect(29, 30, 1, 10), occlusion.unoccludedContentRect(parent.get(), IntRect(29, 30, 60, 10)));
576     EXPECT_EQ_RECT(IntRect(30, 29, 60, 1), occlusion.unoccludedContentRect(parent.get(), IntRect(30, 29, 60, 10)));
577     EXPECT_EQ_RECT(IntRect(90, 30, 1, 10), occlusion.unoccludedContentRect(parent.get(), IntRect(31, 30, 60, 10)));
578     EXPECT_TRUE(occlusion.unoccludedContentRect(parent.get(), IntRect(30, 31, 60, 10)).isEmpty());
579
580     EXPECT_TRUE(occlusion.unoccludedContentRect(parent.get(), IntRect(30, 40, 70, 60)).isEmpty());
581     EXPECT_EQ_RECT(IntRect(29, 40, 1, 60), occlusion.unoccludedContentRect(parent.get(), IntRect(29, 40, 70, 60)));
582     // This rect is mostly occluded by |child2|.
583     EXPECT_EQ_RECT(IntRect(90, 39, 10, 1), occlusion.unoccludedContentRect(parent.get(), IntRect(30, 39, 70, 60)));
584     // This rect extends past top/right ends of |child2|.
585     EXPECT_EQ_RECT(IntRect(30, 29, 70, 11), occlusion.unoccludedContentRect(parent.get(), IntRect(30, 29, 70, 70)));
586     // This rect extends past left/right ends of |child2|.
587     EXPECT_EQ_RECT(IntRect(20, 39, 80, 60), occlusion.unoccludedContentRect(parent.get(), IntRect(20, 39, 80, 60)));
588     EXPECT_EQ_RECT(IntRect(100, 40, 1, 60), occlusion.unoccludedContentRect(parent.get(), IntRect(31, 40, 70, 60)));
589     EXPECT_EQ_RECT(IntRect(30, 100, 70, 1), occlusion.unoccludedContentRect(parent.get(), IntRect(30, 41, 70, 60)));
590
591     /* Justification for the above occlusion from |layer|:
592                100
593       +---------------------+                                      +---------------------+
594       |                     |                                      |                     |30  Visible region of |layer|: /////
595       |    30               |           rotate(90)                 |     30   60         |    |child2|: \\\\\
596       | 30 + ------------+--------------------+                    |  30 +------------+--------------------+
597   100 |    |  10         |  |                 |            ==>     |     |\\\\\\\\\\\\|  |10               |
598       |    |10+----------|----------------------+                  |  +--|\\\\\\\\\\\\|-----------------+  |
599       |    + ------------+  |                 | |                  |  |  +------------+//|     420      |  |
600       |    |  |             |                 | |                  |  |  |///////////////|60            |  |
601       |    |  |             |                 | |                  |  |  |///////////////|              |  |
602       +----|--|-------------+                 | |                  +--|--|---------------+              |  |
603            |  |                               | |                   20|10|     70                       |  |
604            |  |                               | |                     |  |                              |  |
605            |  |                               | |500                  |  |                              |  |
606            |  |                               | |                     |  |                              |  |
607            |  |                               | |                     |  |                              |  |
608            |  |                               | |                     |  |                              |  |
609            |  |                               | |                     |  |                              |10|
610            +--|-------------------------------+ |                     |  +------------------------------|--+
611               |                                 |                     |                 490             |
612               +---------------------------------+                     +---------------------------------+
613                              500                                                     500
614      */
615 }
616
617 TEST_OPAQUE_AND_PAINTED_OPAQUE(CCOcclusionTrackerTest_layerAddedToOccludedRegionWithSurfaceAlreadyOnStack, layerAddedToOccludedRegionWithSurfaceAlreadyOnStack);
618
619 void layerAddedToOccludedRegionWithRotatedOffAxisSurface(bool opaqueLayers)
620 {
621     const TransformationMatrix identityMatrix;
622     RefPtr<LayerChromium> parent = LayerChromium::create();
623     RefPtr<LayerChromium> child = LayerChromium::create();
624     RefPtr<LayerChromiumWithForcedDrawsContent> layer = adoptRef(new LayerChromiumWithForcedDrawsContent());
625     parent->createRenderSurface();
626     parent->addChild(child);
627     child->addChild(layer);
628
629     // Now rotate the child a little more so it is not axis-aligned. The parent will no longer be occluded by |layer|, but
630     // the child's occlusion should be unchanged.
631
632     TransformationMatrix childTransform;
633     childTransform.translate(250, 250);
634     childTransform.rotate(95);
635     childTransform.translate(-250, -250);
636
637     TransformationMatrix layerTransform;
638     layerTransform.translate(10, 10);
639
640     setLayerPropertiesForTesting(parent.get(), identityMatrix, FloatPoint(0, 0), IntSize(100, 100));
641     setLayerPropertiesForTesting(child.get(), childTransform, FloatPoint(30, 30), IntSize(500, 500));
642     setLayerPropertiesForTesting(layer.get(), layerTransform, FloatPoint(0, 0), IntSize(500, 500), true, opaqueLayers);
643
644     child->setMasksToBounds(true);
645
646     Vector<RefPtr<LayerChromium> > renderSurfaceLayerList;
647     Vector<RefPtr<LayerChromium> > dummyLayerList;
648     int dummyMaxTextureSize = 512;
649
650     parent->renderSurface()->setContentRect(IntRect(IntPoint::zero(), parent->bounds()));
651     parent->setClipRect(IntRect(IntPoint::zero(), parent->bounds()));
652     renderSurfaceLayerList.append(parent);
653
654     CCLayerTreeHostCommon::calculateDrawTransformsAndVisibility(parent.get(), parent.get(), identityMatrix, identityMatrix, renderSurfaceLayerList, dummyLayerList, dummyMaxTextureSize);
655
656     TestCCOcclusionTracker occlusion(IntRect(0, 0, 1000, 1000));
657     occlusion.setLayerScissorRect(IntRect(0, 0, 1000, 1000));
658
659     IntRect clippedLayerInChild = layerTransform.mapRect(layer->visibleLayerRect());
660
661     occlusion.enterTargetRenderSurface(child->renderSurface());
662     occlusion.markOccludedBehindLayer(layer.get());
663
664     EXPECT_EQ_RECT(IntRect(), occlusion.occlusionInScreenSpace().bounds());
665     EXPECT_EQ(0u, occlusion.occlusionInScreenSpace().rects().size());
666     EXPECT_EQ_RECT(clippedLayerInChild, occlusion.occlusionInTargetSurface().bounds());
667     EXPECT_EQ(1u, occlusion.occlusionInTargetSurface().rects().size());
668
669     EXPECT_TRUE(occlusion.occluded(child.get(), clippedLayerInChild));
670     EXPECT_TRUE(occlusion.unoccludedContentRect(parent.get(), clippedLayerInChild).isEmpty());
671     clippedLayerInChild.move(-1, 0);
672     EXPECT_FALSE(occlusion.occluded(child.get(), clippedLayerInChild));
673     EXPECT_FALSE(occlusion.unoccludedContentRect(parent.get(), clippedLayerInChild).isEmpty());
674     clippedLayerInChild.move(1, 0);
675     clippedLayerInChild.move(1, 0);
676     EXPECT_FALSE(occlusion.occluded(child.get(), clippedLayerInChild));
677     EXPECT_FALSE(occlusion.unoccludedContentRect(parent.get(), clippedLayerInChild).isEmpty());
678     clippedLayerInChild.move(-1, 0);
679     clippedLayerInChild.move(0, -1);
680     EXPECT_FALSE(occlusion.occluded(child.get(), clippedLayerInChild));
681     EXPECT_FALSE(occlusion.unoccludedContentRect(parent.get(), clippedLayerInChild).isEmpty());
682     clippedLayerInChild.move(0, 1);
683     clippedLayerInChild.move(0, 1);
684     EXPECT_FALSE(occlusion.occluded(child.get(), clippedLayerInChild));
685     EXPECT_FALSE(occlusion.unoccludedContentRect(parent.get(), clippedLayerInChild).isEmpty());
686     clippedLayerInChild.move(0, -1);
687
688     occlusion.markOccludedBehindLayer(child.get());
689     occlusion.finishedTargetRenderSurface(child.get(), child->renderSurface());
690     occlusion.leaveToTargetRenderSurface(parent->renderSurface());
691
692     EXPECT_EQ_RECT(IntRect(), occlusion.occlusionInScreenSpace().bounds());
693     EXPECT_EQ(0u, occlusion.occlusionInScreenSpace().rects().size());
694     EXPECT_EQ_RECT(IntRect(), occlusion.occlusionInTargetSurface().bounds());
695     EXPECT_EQ(0u, occlusion.occlusionInTargetSurface().rects().size());
696
697     EXPECT_FALSE(occlusion.occluded(parent.get(), IntRect(75, 55, 1, 1)));
698     EXPECT_EQ_RECT(IntRect(75, 55, 1, 1), occlusion.unoccludedContentRect(parent.get(), IntRect(75, 55, 1, 1)));
699 }
700
701 TEST_OPAQUE_AND_PAINTED_OPAQUE(CCOcclusionTrackerTest_layerAddedToOccludedRegionWithRotatedOffAxisSurface, layerAddedToOccludedRegionWithRotatedOffAxisSurface);
702
703 void layerAddedToOccludedRegionWithMultipleOpaqueLayers(bool opaqueLayers)
704 {
705     // This is similar to the previous test but now we make a few opaque layers inside of |child| so that the occluded parts of child are not a simple rect.
706     const TransformationMatrix identityMatrix;
707     RefPtr<LayerChromium> parent = LayerChromium::create();
708     RefPtr<LayerChromium> child = LayerChromium::create();
709     RefPtr<LayerChromiumWithForcedDrawsContent> layer1 = adoptRef(new LayerChromiumWithForcedDrawsContent());
710     RefPtr<LayerChromiumWithForcedDrawsContent> layer2 = adoptRef(new LayerChromiumWithForcedDrawsContent());
711     parent->createRenderSurface();
712     parent->addChild(child);
713     child->addChild(layer1);
714     child->addChild(layer2);
715
716     TransformationMatrix childTransform;
717     childTransform.translate(250, 250);
718     childTransform.rotate(90);
719     childTransform.translate(-250, -250);
720
721     setLayerPropertiesForTesting(parent.get(), identityMatrix, FloatPoint(0, 0), IntSize(100, 100));
722     setLayerPropertiesForTesting(child.get(), childTransform, FloatPoint(30, 30), IntSize(500, 500));
723     setLayerPropertiesForTesting(layer1.get(), identityMatrix, FloatPoint(10, 10), IntSize(500, 440), true, opaqueLayers);
724     setLayerPropertiesForTesting(layer2.get(), identityMatrix, FloatPoint(10, 450), IntSize(500, 60), true, opaqueLayers);
725
726     child->setMasksToBounds(true);
727
728     Vector<RefPtr<LayerChromium> > renderSurfaceLayerList;
729     Vector<RefPtr<LayerChromium> > dummyLayerList;
730     int dummyMaxTextureSize = 512;
731
732     parent->renderSurface()->setContentRect(IntRect(IntPoint::zero(), parent->bounds()));
733     parent->setClipRect(IntRect(IntPoint::zero(), parent->bounds()));
734     renderSurfaceLayerList.append(parent);
735
736     CCLayerTreeHostCommon::calculateDrawTransformsAndVisibility(parent.get(), parent.get(), identityMatrix, identityMatrix, renderSurfaceLayerList, dummyLayerList, dummyMaxTextureSize);
737
738     TestCCOcclusionTracker occlusion(IntRect(0, 0, 1000, 1000));
739     occlusion.setLayerScissorRect(IntRect(0, 0, 1000, 1000));
740
741     occlusion.enterTargetRenderSurface(child->renderSurface());
742     occlusion.markOccludedBehindLayer(layer2.get());
743     occlusion.markOccludedBehindLayer(layer1.get());
744
745     EXPECT_EQ_RECT(IntRect(30, 40, 70, 60), occlusion.occlusionInScreenSpace().bounds());
746     EXPECT_EQ(1u, occlusion.occlusionInScreenSpace().rects().size());
747     EXPECT_EQ_RECT(IntRect(10, 430, 60, 70), occlusion.occlusionInTargetSurface().bounds());
748     EXPECT_EQ(1u, occlusion.occlusionInTargetSurface().rects().size());
749
750     EXPECT_TRUE(occlusion.occluded(child.get(), IntRect(10, 430, 60, 70)));
751     EXPECT_FALSE(occlusion.occluded(child.get(), IntRect(9, 430, 60, 70)));
752     EXPECT_FALSE(occlusion.occluded(child.get(), IntRect(10, 429, 60, 70)));
753     EXPECT_FALSE(occlusion.occluded(child.get(), IntRect(11, 430, 60, 70)));
754     EXPECT_FALSE(occlusion.occluded(child.get(), IntRect(10, 431, 60, 70)));
755
756     EXPECT_TRUE(occlusion.unoccludedContentRect(child.get(), IntRect(10, 430, 60, 70)).isEmpty());
757     EXPECT_EQ_RECT(IntRect(9, 430, 1, 70), occlusion.unoccludedContentRect(child.get(), IntRect(9, 430, 60, 70)));
758     EXPECT_EQ_RECT(IntRect(10, 429, 60, 1), occlusion.unoccludedContentRect(child.get(), IntRect(10, 429, 60, 70)));
759     EXPECT_EQ_RECT(IntRect(70, 430, 1, 70), occlusion.unoccludedContentRect(child.get(), IntRect(11, 430, 60, 70)));
760     EXPECT_EQ_RECT(IntRect(10, 500, 60, 1), occlusion.unoccludedContentRect(child.get(), IntRect(10, 431, 60, 70)));
761
762     occlusion.markOccludedBehindLayer(child.get());
763     occlusion.finishedTargetRenderSurface(child.get(), child->renderSurface());
764     occlusion.leaveToTargetRenderSurface(parent->renderSurface());
765
766     EXPECT_EQ_RECT(IntRect(30, 40, 70, 60), occlusion.occlusionInScreenSpace().bounds());
767     EXPECT_EQ(1u, occlusion.occlusionInScreenSpace().rects().size());
768     EXPECT_EQ_RECT(IntRect(30, 40, 70, 60), occlusion.occlusionInTargetSurface().bounds());
769     EXPECT_EQ(1u, occlusion.occlusionInTargetSurface().rects().size());
770
771     EXPECT_TRUE(occlusion.occluded(parent.get(), IntRect(30, 40, 70, 60)));
772     EXPECT_FALSE(occlusion.occluded(parent.get(), IntRect(29, 40, 70, 60)));
773     EXPECT_FALSE(occlusion.occluded(parent.get(), IntRect(30, 39, 70, 60)));
774
775     EXPECT_TRUE(occlusion.unoccludedContentRect(parent.get(), IntRect(30, 40, 70, 60)).isEmpty());
776     EXPECT_EQ_RECT(IntRect(29, 40, 1, 60), occlusion.unoccludedContentRect(parent.get(), IntRect(29, 40, 70, 60)));
777     EXPECT_EQ_RECT(IntRect(30, 39, 70, 1), occlusion.unoccludedContentRect(parent.get(), IntRect(30, 39, 70, 60)));
778     EXPECT_EQ_RECT(IntRect(100, 40, 1, 60), occlusion.unoccludedContentRect(parent.get(), IntRect(31, 40, 70, 60)));
779     EXPECT_EQ_RECT(IntRect(30, 100, 70, 1), occlusion.unoccludedContentRect(parent.get(), IntRect(30, 41, 70, 60)));
780
781     /* Justification for the above occlusion from |layer1| and |layer2|:
782
783        +---------------------+
784        |                     |30  Visible region of |layer1|: /////
785        |                     |    Visible region of |layer2|: \\\\\
786        |     +---------------------------------+
787        |     |               |10               |
788        |  +---------------+-----------------+  |
789        |  |  |\\\\\\\\\\\\|//|     420      |  |
790        |  |  |\\\\\\\\\\\\|//|60            |  |
791        |  |  |\\\\\\\\\\\\|//|              |  |
792        +--|--|------------|--+              |  |
793         20|10|     70     |                 |  |
794           |  |            |                 |  |
795           |  |            |                 |  |
796           |  |            |                 |  |
797           |  |            |                 |  |
798           |  |            |                 |  |
799           |  |            |                 |10|
800           |  +------------|-----------------|--+
801           |               | 490             |
802           +---------------+-----------------+
803                 60               440
804      */
805 }
806
807 TEST_OPAQUE_AND_PAINTED_OPAQUE(CCOcclusionTrackerTest_layerAddedToOccludedRegionWithMultipleOpaqueLayers, layerAddedToOccludedRegionWithMultipleOpaqueLayers);
808
809 void surfaceOcclusionWithOverlappingSiblingSurfaces(bool opaqueLayers)
810 {
811     // This tests that the right transforms are being used.
812     const TransformationMatrix identityMatrix;
813     RefPtr<LayerChromium> parent = LayerChromium::create();
814     RefPtr<LayerChromium> child1 = LayerChromium::create();
815     RefPtr<LayerChromium> child2 = LayerChromium::create();
816     RefPtr<LayerChromiumWithForcedDrawsContent> layer1 = adoptRef(new LayerChromiumWithForcedDrawsContent());
817     RefPtr<LayerChromiumWithForcedDrawsContent> layer2 = adoptRef(new LayerChromiumWithForcedDrawsContent());
818     parent->createRenderSurface();
819     parent->addChild(child1);
820     parent->addChild(child2);
821     child1->addChild(layer1);
822     child2->addChild(layer2);
823
824     TransformationMatrix childTransform;
825     childTransform.translate(250, 250);
826     childTransform.rotate(90);
827     childTransform.translate(-250, -250);
828
829     setLayerPropertiesForTesting(parent.get(), identityMatrix, FloatPoint(0, 0), IntSize(100, 100));
830     setLayerPropertiesForTesting(child1.get(), childTransform, FloatPoint(30, 30), IntSize(500, 500));
831     setLayerPropertiesForTesting(layer1.get(), identityMatrix, FloatPoint(0, 0), IntSize(500, 500), true, opaqueLayers);
832     setLayerPropertiesForTesting(child2.get(), childTransform, FloatPoint(20, 40), IntSize(500, 500));
833     setLayerPropertiesForTesting(layer2.get(), identityMatrix, FloatPoint(0, 0), IntSize(500, 500), true, opaqueLayers);
834
835     child1->setMasksToBounds(true);
836     child2->setMasksToBounds(true);
837
838     Vector<RefPtr<LayerChromium> > renderSurfaceLayerList;
839     Vector<RefPtr<LayerChromium> > dummyLayerList;
840     int dummyMaxTextureSize = 512;
841
842     parent->renderSurface()->setContentRect(IntRect(IntPoint::zero(), parent->bounds()));
843     parent->setClipRect(IntRect(IntPoint::zero(), parent->bounds()));
844     renderSurfaceLayerList.append(parent);
845
846     CCLayerTreeHostCommon::calculateDrawTransformsAndVisibility(parent.get(), parent.get(), identityMatrix, identityMatrix, renderSurfaceLayerList, dummyLayerList, dummyMaxTextureSize);
847
848     TestCCOcclusionTracker occlusion(IntRect(0, 0, 1000, 1000));
849     occlusion.setLayerScissorRect(IntRect(-10, -10, 1000, 1000));
850
851     occlusion.enterTargetRenderSurface(child2->renderSurface());
852     occlusion.markOccludedBehindLayer(layer2.get());
853
854     EXPECT_EQ_RECT(IntRect(20, 40, 80, 60), occlusion.occlusionInScreenSpace().bounds());
855     EXPECT_EQ(1u, occlusion.occlusionInScreenSpace().rects().size());
856     EXPECT_EQ_RECT(IntRect(0, 420, 60, 80), occlusion.occlusionInTargetSurface().bounds());
857     EXPECT_EQ(1u, occlusion.occlusionInTargetSurface().rects().size());
858
859     EXPECT_TRUE(occlusion.occluded(child2.get(), IntRect(0, 420, 60, 80)));
860     EXPECT_FALSE(occlusion.occluded(child2.get(), IntRect(-1, 420, 60, 80)));
861     EXPECT_FALSE(occlusion.occluded(child2.get(), IntRect(0, 419, 60, 80)));
862     EXPECT_FALSE(occlusion.occluded(child2.get(), IntRect(0, 420, 61, 80)));
863     EXPECT_FALSE(occlusion.occluded(child2.get(), IntRect(0, 420, 60, 81)));
864
865     occlusion.markOccludedBehindLayer(child2.get());
866     occlusion.finishedTargetRenderSurface(child2.get(), child2->renderSurface());
867     occlusion.leaveToTargetRenderSurface(parent->renderSurface());
868     occlusion.enterTargetRenderSurface(child1->renderSurface());
869     occlusion.markOccludedBehindLayer(layer1.get());
870
871     EXPECT_EQ_RECT(IntRect(20, 30, 80, 70), occlusion.occlusionInScreenSpace().bounds());
872     EXPECT_EQ(2u, occlusion.occlusionInScreenSpace().rects().size());
873     EXPECT_EQ_RECT(IntRect(0, 430, 70, 70), occlusion.occlusionInTargetSurface().bounds());
874     EXPECT_EQ(1u, occlusion.occlusionInTargetSurface().rects().size());
875
876     EXPECT_TRUE(occlusion.occluded(child1.get(), IntRect(0, 430, 70, 70)));
877     EXPECT_FALSE(occlusion.occluded(child1.get(), IntRect(-1, 430, 70, 70)));
878     EXPECT_FALSE(occlusion.occluded(child1.get(), IntRect(0, 429, 70, 70)));
879     EXPECT_FALSE(occlusion.occluded(child1.get(), IntRect(0, 430, 71, 70)));
880     EXPECT_FALSE(occlusion.occluded(child1.get(), IntRect(0, 430, 70, 71)));
881
882     occlusion.markOccludedBehindLayer(child1.get());
883     occlusion.finishedTargetRenderSurface(child1.get(), child1->renderSurface());
884     occlusion.leaveToTargetRenderSurface(parent->renderSurface());
885
886     EXPECT_EQ_RECT(IntRect(20, 30, 80, 70), occlusion.occlusionInScreenSpace().bounds());
887     EXPECT_EQ(2u, occlusion.occlusionInScreenSpace().rects().size());
888     EXPECT_EQ_RECT(IntRect(20, 30, 80, 70), occlusion.occlusionInTargetSurface().bounds());
889     EXPECT_EQ(2u, occlusion.occlusionInTargetSurface().rects().size());
890
891     EXPECT_FALSE(occlusion.occluded(parent.get(), IntRect(20, 30, 80, 70)));
892
893     EXPECT_TRUE(occlusion.occluded(parent.get(), IntRect(30, 30, 70, 70)));
894     EXPECT_FALSE(occlusion.occluded(parent.get(), IntRect(29, 30, 70, 70)));
895     EXPECT_FALSE(occlusion.occluded(parent.get(), IntRect(30, 29, 70, 70)));
896
897     EXPECT_TRUE(occlusion.occluded(parent.get(), IntRect(20, 40, 80, 60)));
898     EXPECT_FALSE(occlusion.occluded(parent.get(), IntRect(19, 40, 80, 60)));
899     EXPECT_FALSE(occlusion.occluded(parent.get(), IntRect(20, 39, 80, 60)));
900
901     /* Justification for the above occlusion:
902                100
903       +---------------------+
904       |                     |
905       |    30               |       child1
906       |  30+ ---------------------------------+
907   100 |  40|                |     child2      |
908       |20+----------------------------------+ |
909       |  | |                |               | |
910       |  | |                |               | |
911       |  | |                |               | |
912       +--|-|----------------+               | |
913          | |                                | | 500
914          | |                                | |
915          | |                                | |
916          | |                                | |
917          | |                                | |
918          | |                                | |
919          | |                                | |
920          | +--------------------------------|-+
921          |                                  |
922          +----------------------------------+
923                          500
924      */
925 }
926
927 TEST_OPAQUE_AND_PAINTED_OPAQUE(CCOcclusionTrackerTest_surfaceOcclusionWithOverlappingSiblingSurfaces, surfaceOcclusionWithOverlappingSiblingSurfaces);
928
929 void surfaceOcclusionInScreenSpace(bool opaqueLayers)
930 {
931     // This tests that the right transforms are being used.
932     const TransformationMatrix identityMatrix;
933     RefPtr<LayerChromium> parent = LayerChromium::create();
934     RefPtr<LayerChromium> child1 = LayerChromium::create();
935     RefPtr<LayerChromium> child2 = LayerChromium::create();
936     RefPtr<LayerChromiumWithForcedDrawsContent> layer1 = adoptRef(new LayerChromiumWithForcedDrawsContent());
937     RefPtr<LayerChromiumWithForcedDrawsContent> layer2 = adoptRef(new LayerChromiumWithForcedDrawsContent());
938     parent->createRenderSurface();
939     parent->addChild(child1);
940     parent->addChild(child2);
941     child1->addChild(layer1);
942     child2->addChild(layer2);
943
944     TransformationMatrix childTransform;
945     childTransform.translate(250, 250);
946     childTransform.rotate(90);
947     childTransform.translate(-250, -250);
948
949     // The owning layers have very different bounds from the surfaces that they own.
950     setLayerPropertiesForTesting(parent.get(), identityMatrix, FloatPoint(0, 0), IntSize(100, 100));
951     setLayerPropertiesForTesting(child1.get(), childTransform, FloatPoint(30, 30), IntSize(10, 10));
952     setLayerPropertiesForTesting(layer1.get(), identityMatrix, FloatPoint(-10, -10), IntSize(510, 510), true, opaqueLayers);
953     setLayerPropertiesForTesting(child2.get(), childTransform, FloatPoint(20, 40), IntSize(10, 10));
954     setLayerPropertiesForTesting(layer2.get(), identityMatrix, FloatPoint(-10, -10), IntSize(510, 510), true, opaqueLayers);
955
956     // Make them both render surfaces
957     FilterOperations filters;
958     filters.operations().append(BasicComponentTransferFilterOperation::create(0.5, FilterOperation::GRAYSCALE));
959     child1->setFilters(filters);
960     child2->setFilters(filters);
961
962     child1->setMasksToBounds(false);
963     child2->setMasksToBounds(false);
964
965     Vector<RefPtr<LayerChromium> > renderSurfaceLayerList;
966     Vector<RefPtr<LayerChromium> > dummyLayerList;
967     int dummyMaxTextureSize = 512;
968
969     parent->renderSurface()->setContentRect(IntRect(IntPoint::zero(), parent->bounds()));
970     parent->setClipRect(IntRect(IntPoint::zero(), parent->bounds()));
971     renderSurfaceLayerList.append(parent);
972
973     CCLayerTreeHostCommon::calculateDrawTransformsAndVisibility(parent.get(), parent.get(), identityMatrix, identityMatrix, renderSurfaceLayerList, dummyLayerList, dummyMaxTextureSize);
974
975     TestCCOcclusionTracker occlusion(IntRect(0, 0, 1000, 1000));
976     occlusion.setLayerScissorRect(IntRect(-20, -20, 1000, 1000));
977
978     occlusion.enterTargetRenderSurface(child2->renderSurface());
979     occlusion.markOccludedBehindLayer(layer2.get());
980
981     EXPECT_EQ_RECT(IntRect(20, 30, 80, 70), occlusion.occlusionInScreenSpace().bounds());
982     EXPECT_EQ(1u, occlusion.occlusionInScreenSpace().rects().size());
983     EXPECT_EQ_RECT(IntRect(-10, 420, 70, 80), occlusion.occlusionInTargetSurface().bounds());
984     EXPECT_EQ(1u, occlusion.occlusionInTargetSurface().rects().size());
985
986     EXPECT_TRUE(occlusion.occluded(child2.get(), IntRect(-10, 420, 70, 80)));
987     EXPECT_FALSE(occlusion.occluded(child2.get(), IntRect(-11, 420, 70, 80)));
988     EXPECT_FALSE(occlusion.occluded(child2.get(), IntRect(-10, 419, 70, 80)));
989     EXPECT_FALSE(occlusion.occluded(child2.get(), IntRect(-10, 420, 71, 80)));
990     EXPECT_FALSE(occlusion.occluded(child2.get(), IntRect(-10, 420, 70, 81)));
991
992     occlusion.useDefaultLayerScissorRect();
993     EXPECT_TRUE(occlusion.occluded(child2.get(), IntRect(-10, 420, 70, 80)));
994     EXPECT_TRUE(occlusion.occluded(child2.get(), IntRect(-11, 420, 70, 80)));
995     EXPECT_TRUE(occlusion.occluded(child2.get(), IntRect(-10, 419, 70, 80)));
996     EXPECT_TRUE(occlusion.occluded(child2.get(), IntRect(-10, 420, 71, 80)));
997     EXPECT_TRUE(occlusion.occluded(child2.get(), IntRect(-10, 420, 70, 81)));
998     occlusion.setLayerScissorRect(IntRect(-20, -20, 1000, 1000));
999
1000     occlusion.markOccludedBehindLayer(child2.get());
1001     occlusion.finishedTargetRenderSurface(child2.get(), child2->renderSurface());
1002     occlusion.leaveToTargetRenderSurface(parent->renderSurface());
1003     occlusion.enterTargetRenderSurface(child1->renderSurface());
1004     occlusion.markOccludedBehindLayer(layer1.get());
1005
1006     EXPECT_EQ_RECT(IntRect(20, 20, 80, 80), occlusion.occlusionInScreenSpace().bounds());
1007     EXPECT_EQ(2u, occlusion.occlusionInScreenSpace().rects().size());
1008     EXPECT_EQ_RECT(IntRect(-10, 430, 80, 70), occlusion.occlusionInTargetSurface().bounds());
1009     EXPECT_EQ(1u, occlusion.occlusionInTargetSurface().rects().size());
1010
1011     EXPECT_TRUE(occlusion.occluded(child1.get(), IntRect(-10, 430, 80, 70)));
1012     EXPECT_FALSE(occlusion.occluded(child1.get(), IntRect(-11, 430, 80, 70)));
1013     EXPECT_FALSE(occlusion.occluded(child1.get(), IntRect(-10, 429, 80, 70)));
1014     EXPECT_FALSE(occlusion.occluded(child1.get(), IntRect(-10, 430, 81, 70)));
1015     EXPECT_FALSE(occlusion.occluded(child1.get(), IntRect(-10, 430, 80, 71)));
1016
1017     occlusion.markOccludedBehindLayer(child1.get());
1018     occlusion.finishedTargetRenderSurface(child1.get(), child1->renderSurface());
1019     occlusion.leaveToTargetRenderSurface(parent->renderSurface());
1020
1021     EXPECT_EQ_RECT(IntRect(20, 20, 80, 80), occlusion.occlusionInScreenSpace().bounds());
1022     EXPECT_EQ(2u, occlusion.occlusionInScreenSpace().rects().size());
1023     EXPECT_EQ_RECT(IntRect(20, 20, 80, 80), occlusion.occlusionInTargetSurface().bounds());
1024     EXPECT_EQ(2u, occlusion.occlusionInTargetSurface().rects().size());
1025
1026     EXPECT_FALSE(occlusion.occluded(parent.get(), IntRect(20, 20, 80, 80)));
1027
1028     EXPECT_TRUE(occlusion.occluded(parent.get(), IntRect(30, 20, 70, 80)));
1029     EXPECT_FALSE(occlusion.occluded(parent.get(), IntRect(29, 20, 70, 80)));
1030     EXPECT_FALSE(occlusion.occluded(parent.get(), IntRect(30, 19, 70, 80)));
1031
1032     EXPECT_TRUE(occlusion.occluded(parent.get(), IntRect(20, 30, 80, 70)));
1033     EXPECT_FALSE(occlusion.occluded(parent.get(), IntRect(19, 30, 80, 70)));
1034     EXPECT_FALSE(occlusion.occluded(parent.get(), IntRect(20, 29, 80, 70)));
1035
1036     /* Justification for the above occlusion:
1037                100
1038       +---------------------+
1039       |    20               |       layer1
1040       |  30+ ---------------------------------+
1041   100 |  30|                |     layer2      |
1042       |20+----------------------------------+ |
1043       |  | |                |               | |
1044       |  | |                |               | |
1045       |  | |                |               | |
1046       +--|-|----------------+               | |
1047          | |                                | | 510
1048          | |                                | |
1049          | |                                | |
1050          | |                                | |
1051          | |                                | |
1052          | |                                | |
1053          | |                                | |
1054          | +--------------------------------|-+
1055          |                                  |
1056          +----------------------------------+
1057                          510
1058      */
1059 }
1060
1061 TEST_OPAQUE_AND_PAINTED_OPAQUE(CCOcclusionTrackerTest_surfaceOcclusionInScreenSpace, surfaceOcclusionInScreenSpace);
1062
1063 void surfaceOcclusionInScreenSpaceDifferentTransforms(bool opaqueLayers)
1064 {
1065     // This tests that the right transforms are being used.
1066     const TransformationMatrix identityMatrix;
1067     RefPtr<LayerChromium> parent = LayerChromium::create();
1068     RefPtr<LayerChromium> child1 = LayerChromium::create();
1069     RefPtr<LayerChromium> child2 = LayerChromium::create();
1070     RefPtr<LayerChromiumWithForcedDrawsContent> layer1 = adoptRef(new LayerChromiumWithForcedDrawsContent());
1071     RefPtr<LayerChromiumWithForcedDrawsContent> layer2 = adoptRef(new LayerChromiumWithForcedDrawsContent());
1072     parent->createRenderSurface();
1073     parent->addChild(child1);
1074     parent->addChild(child2);
1075     child1->addChild(layer1);
1076     child2->addChild(layer2);
1077
1078     TransformationMatrix child1Transform;
1079     child1Transform.translate(250, 250);
1080     child1Transform.rotate(-90);
1081     child1Transform.translate(-250, -250);
1082
1083     TransformationMatrix child2Transform;
1084     child2Transform.translate(250, 250);
1085     child2Transform.rotate(90);
1086     child2Transform.translate(-250, -250);
1087
1088     // The owning layers have very different bounds from the surfaces that they own.
1089     setLayerPropertiesForTesting(parent.get(), identityMatrix, FloatPoint(0, 0), IntSize(100, 100));
1090     setLayerPropertiesForTesting(child1.get(), child1Transform, FloatPoint(30, 20), IntSize(10, 10));
1091     setLayerPropertiesForTesting(layer1.get(), identityMatrix, FloatPoint(-10, -20), IntSize(510, 520), true, opaqueLayers);
1092     setLayerPropertiesForTesting(child2.get(), child2Transform, FloatPoint(20, 40), IntSize(10, 10));
1093     setLayerPropertiesForTesting(layer2.get(), identityMatrix, FloatPoint(-10, -10), IntSize(510, 510), true, opaqueLayers);
1094
1095     // Make them both render surfaces
1096     FilterOperations filters;
1097     filters.operations().append(BasicComponentTransferFilterOperation::create(0.5, FilterOperation::GRAYSCALE));
1098     child1->setFilters(filters);
1099     child2->setFilters(filters);
1100
1101     child1->setMasksToBounds(false);
1102     child2->setMasksToBounds(false);
1103
1104     Vector<RefPtr<LayerChromium> > renderSurfaceLayerList;
1105     Vector<RefPtr<LayerChromium> > dummyLayerList;
1106     int dummyMaxTextureSize = 512;
1107
1108     parent->renderSurface()->setContentRect(IntRect(IntPoint::zero(), parent->bounds()));
1109     parent->setClipRect(IntRect(IntPoint::zero(), parent->bounds()));
1110     renderSurfaceLayerList.append(parent);
1111
1112     CCLayerTreeHostCommon::calculateDrawTransformsAndVisibility(parent.get(), parent.get(), identityMatrix, identityMatrix, renderSurfaceLayerList, dummyLayerList, dummyMaxTextureSize);
1113
1114     TestCCOcclusionTracker occlusion(IntRect(0, 0, 1000, 1000));
1115     occlusion.setLayerScissorRect(IntRect(-30, -30, 1000, 1000));
1116
1117     occlusion.enterTargetRenderSurface(child2->renderSurface());
1118     occlusion.markOccludedBehindLayer(layer2.get());
1119
1120     EXPECT_EQ_RECT(IntRect(20, 30, 80, 70), occlusion.occlusionInScreenSpace().bounds());
1121     EXPECT_EQ(1u, occlusion.occlusionInScreenSpace().rects().size());
1122     EXPECT_EQ_RECT(IntRect(-10, 420, 70, 80), occlusion.occlusionInTargetSurface().bounds());
1123     EXPECT_EQ(1u, occlusion.occlusionInTargetSurface().rects().size());
1124
1125     EXPECT_TRUE(occlusion.occluded(child2.get(), IntRect(-10, 420, 70, 80)));
1126     EXPECT_FALSE(occlusion.occluded(child2.get(), IntRect(-11, 420, 70, 80)));
1127     EXPECT_FALSE(occlusion.occluded(child2.get(), IntRect(-10, 419, 70, 80)));
1128     EXPECT_FALSE(occlusion.occluded(child2.get(), IntRect(-10, 420, 71, 80)));
1129     EXPECT_FALSE(occlusion.occluded(child2.get(), IntRect(-10, 420, 70, 81)));
1130
1131     occlusion.markOccludedBehindLayer(child2.get());
1132     occlusion.finishedTargetRenderSurface(child2.get(), child2->renderSurface());
1133     occlusion.leaveToTargetRenderSurface(parent->renderSurface());
1134     occlusion.enterTargetRenderSurface(child1->renderSurface());
1135     occlusion.markOccludedBehindLayer(layer1.get());
1136
1137     EXPECT_EQ_RECT(IntRect(10, 20, 90, 80), occlusion.occlusionInScreenSpace().bounds());
1138     EXPECT_EQ(1u, occlusion.occlusionInScreenSpace().rects().size());
1139     EXPECT_EQ_RECT(IntRect(420, -20, 80, 90), occlusion.occlusionInTargetSurface().bounds());
1140     EXPECT_EQ(1u, occlusion.occlusionInTargetSurface().rects().size());
1141
1142     EXPECT_TRUE(occlusion.occluded(child1.get(), IntRect(420, -20, 80, 90)));
1143     EXPECT_FALSE(occlusion.occluded(child1.get(), IntRect(419, -20, 80, 90)));
1144     EXPECT_FALSE(occlusion.occluded(child1.get(), IntRect(420, -21, 80, 90)));
1145     EXPECT_FALSE(occlusion.occluded(child1.get(), IntRect(420, -19, 80, 90)));
1146     EXPECT_FALSE(occlusion.occluded(child1.get(), IntRect(421, -20, 80, 90)));
1147
1148     occlusion.markOccludedBehindLayer(child1.get());
1149     occlusion.finishedTargetRenderSurface(child1.get(), child1->renderSurface());
1150     occlusion.leaveToTargetRenderSurface(parent->renderSurface());
1151
1152     EXPECT_EQ_RECT(IntRect(10, 20, 90, 80), occlusion.occlusionInScreenSpace().bounds());
1153     EXPECT_EQ(1u, occlusion.occlusionInScreenSpace().rects().size());
1154     EXPECT_EQ_RECT(IntRect(10, 20, 90, 80), occlusion.occlusionInTargetSurface().bounds());
1155     EXPECT_EQ(1u, occlusion.occlusionInTargetSurface().rects().size());
1156
1157     EXPECT_TRUE(occlusion.occluded(parent.get(), IntRect(10, 20, 90, 80)));
1158     EXPECT_FALSE(occlusion.occluded(parent.get(), IntRect(9, 20, 90, 80)));
1159     EXPECT_FALSE(occlusion.occluded(parent.get(), IntRect(10, 19, 90, 80)));
1160     EXPECT_FALSE(occlusion.occluded(parent.get(), IntRect(11, 20, 90, 80)));
1161     EXPECT_FALSE(occlusion.occluded(parent.get(), IntRect(10, 21, 90, 80)));
1162
1163     /* Justification for the above occlusion:
1164                100
1165       +---------------------+
1166       |20                   |       layer1
1167      10+----------------------------------+
1168   100 || 30                 |     layer2  |
1169       |20+----------------------------------+
1170       || |                  |             | |
1171       || |                  |             | |
1172       || |                  |             | |
1173       +|-|------------------+             | |
1174        | |                                | | 510
1175        | |                            510 | |
1176        | |                                | |
1177        | |                                | |
1178        | |                                | |
1179        | |                                | |
1180        | |                520             | |
1181        +----------------------------------+ |
1182          |                                  |
1183          +----------------------------------+
1184                          510
1185      */
1186 }
1187
1188 TEST_OPAQUE_AND_PAINTED_OPAQUE(CCOcclusionTrackerTest_surfaceOcclusionInScreenSpaceDifferentTransforms, surfaceOcclusionInScreenSpaceDifferentTransforms);
1189
1190 void occlusionInteractionWithFilters(bool opaqueLayers)
1191 {
1192     // This tests that the right transforms are being used.
1193     const TransformationMatrix identityMatrix;
1194     RefPtr<LayerChromium> parent = LayerChromium::create();
1195     RefPtr<LayerChromiumWithForcedDrawsContent> blurLayer = adoptRef(new LayerChromiumWithForcedDrawsContent());
1196     RefPtr<LayerChromiumWithForcedDrawsContent> opacityLayer = adoptRef(new LayerChromiumWithForcedDrawsContent());
1197     RefPtr<LayerChromiumWithForcedDrawsContent> opaqueLayer = adoptRef(new LayerChromiumWithForcedDrawsContent());
1198     parent->createRenderSurface();
1199     parent->addChild(blurLayer);
1200     parent->addChild(opacityLayer);
1201     parent->addChild(opaqueLayer);
1202
1203     TransformationMatrix layerTransform;
1204     layerTransform.translate(250, 250);
1205     layerTransform.rotate(90);
1206     layerTransform.translate(-250, -250);
1207
1208     setLayerPropertiesForTesting(parent.get(), identityMatrix, FloatPoint(0, 0), IntSize(100, 100));
1209     setLayerPropertiesForTesting(blurLayer.get(), layerTransform, FloatPoint(30, 30), IntSize(500, 500), true, opaqueLayers);
1210     setLayerPropertiesForTesting(opaqueLayer.get(), layerTransform, FloatPoint(30, 30), IntSize(500, 500), true, opaqueLayers);
1211     setLayerPropertiesForTesting(opacityLayer.get(), layerTransform, FloatPoint(30, 30), IntSize(500, 500), true, opaqueLayers);
1212
1213     {
1214         FilterOperations filters;
1215         filters.operations().append(BlurFilterOperation::create(Length(10, WebCore::Percent), FilterOperation::BLUR));
1216         blurLayer->setFilters(filters);
1217     }
1218
1219     {
1220         FilterOperations filters;
1221         filters.operations().append(BasicComponentTransferFilterOperation::create(0.5, FilterOperation::GRAYSCALE));
1222         opaqueLayer->setFilters(filters);
1223     }
1224
1225     {
1226         FilterOperations filters;
1227         filters.operations().append(BasicComponentTransferFilterOperation::create(0.5, FilterOperation::OPACITY));
1228         opacityLayer->setFilters(filters);
1229     }
1230
1231
1232     Vector<RefPtr<LayerChromium> > renderSurfaceLayerList;
1233     Vector<RefPtr<LayerChromium> > dummyLayerList;
1234     int dummyMaxTextureSize = 512;
1235
1236     parent->renderSurface()->setContentRect(IntRect(IntPoint::zero(), parent->bounds()));
1237     parent->setClipRect(IntRect(IntPoint::zero(), parent->bounds()));
1238     renderSurfaceLayerList.append(parent);
1239
1240     CCLayerTreeHostCommon::calculateDrawTransformsAndVisibility(parent.get(), parent.get(), identityMatrix, identityMatrix, renderSurfaceLayerList, dummyLayerList, dummyMaxTextureSize);
1241
1242     TestCCOcclusionTracker occlusion(IntRect(0, 0, 1000, 1000));
1243     occlusion.setLayerScissorRect(IntRect(0, 0, 1000, 1000));
1244
1245     // Opacity layer won't contribute to occlusion.
1246     occlusion.enterTargetRenderSurface(opacityLayer->renderSurface());
1247     occlusion.markOccludedBehindLayer(opacityLayer.get());
1248     occlusion.finishedTargetRenderSurface(opacityLayer.get(), opacityLayer->renderSurface());
1249
1250     EXPECT_TRUE(occlusion.occlusionInScreenSpace().isEmpty());
1251     EXPECT_TRUE(occlusion.occlusionInTargetSurface().isEmpty());
1252
1253     // And has nothing to contribute to its parent surface.
1254     occlusion.leaveToTargetRenderSurface(parent->renderSurface());
1255     EXPECT_TRUE(occlusion.occlusionInScreenSpace().isEmpty());
1256     EXPECT_TRUE(occlusion.occlusionInTargetSurface().isEmpty());
1257
1258     // Opaque layer will contribute to occlusion.
1259     occlusion.enterTargetRenderSurface(opaqueLayer->renderSurface());
1260     occlusion.markOccludedBehindLayer(opaqueLayer.get());
1261     occlusion.finishedTargetRenderSurface(opaqueLayer.get(), opaqueLayer->renderSurface());
1262
1263     EXPECT_EQ_RECT(IntRect(30, 30, 70, 70), occlusion.occlusionInScreenSpace().bounds());
1264     EXPECT_EQ(1u, occlusion.occlusionInScreenSpace().rects().size());
1265     EXPECT_EQ_RECT(IntRect(0, 430, 70, 70), occlusion.occlusionInTargetSurface().bounds());
1266     EXPECT_EQ(1u, occlusion.occlusionInTargetSurface().rects().size());
1267
1268     // And it gets translated to the parent surface.
1269     occlusion.leaveToTargetRenderSurface(parent->renderSurface());
1270     EXPECT_EQ_RECT(IntRect(30, 30, 70, 70), occlusion.occlusionInScreenSpace().bounds());
1271     EXPECT_EQ(1u, occlusion.occlusionInScreenSpace().rects().size());
1272     EXPECT_EQ_RECT(IntRect(30, 30, 70, 70), occlusion.occlusionInTargetSurface().bounds());
1273     EXPECT_EQ(1u, occlusion.occlusionInTargetSurface().rects().size());
1274
1275     // The blur layer needs to throw away any occlusion from outside its subtree.
1276     occlusion.enterTargetRenderSurface(blurLayer->renderSurface());
1277     EXPECT_TRUE(occlusion.occlusionInScreenSpace().isEmpty());
1278     EXPECT_TRUE(occlusion.occlusionInTargetSurface().isEmpty());
1279
1280     // And it won't contribute to occlusion.
1281     occlusion.markOccludedBehindLayer(blurLayer.get());
1282     occlusion.finishedTargetRenderSurface(blurLayer.get(), blurLayer->renderSurface());
1283     EXPECT_TRUE(occlusion.occlusionInScreenSpace().isEmpty());
1284     EXPECT_TRUE(occlusion.occlusionInTargetSurface().isEmpty());
1285
1286     // But the opaque layer's occlusion is preserved on the parent.
1287     occlusion.leaveToTargetRenderSurface(parent->renderSurface());
1288     EXPECT_EQ_RECT(IntRect(30, 30, 70, 70), occlusion.occlusionInScreenSpace().bounds());
1289     EXPECT_EQ(1u, occlusion.occlusionInScreenSpace().rects().size());
1290     EXPECT_EQ_RECT(IntRect(30, 30, 70, 70), occlusion.occlusionInTargetSurface().bounds());
1291     EXPECT_EQ(1u, occlusion.occlusionInTargetSurface().rects().size());
1292 }
1293
1294 TEST_OPAQUE_AND_PAINTED_OPAQUE(CCOcclusionTrackerTest_occlusionInteractionWithFilters, occlusionInteractionWithFilters);
1295
1296 void layerScissorRectOverTile(bool opaqueLayers)
1297 {
1298     const TransformationMatrix identityMatrix;
1299     RefPtr<LayerChromium> parent = LayerChromium::create();
1300     RefPtr<LayerChromiumWithForcedDrawsContent> parentLayer = adoptRef(new LayerChromiumWithForcedDrawsContent());
1301     RefPtr<LayerChromiumWithForcedDrawsContent> layer = adoptRef(new LayerChromiumWithForcedDrawsContent());
1302     parent->createRenderSurface();
1303     parent->addChild(parentLayer);
1304     parent->addChild(layer);
1305
1306     FilterOperations filters;
1307     filters.operations().append(BasicComponentTransferFilterOperation::create(0.5, FilterOperation::GRAYSCALE));
1308     parentLayer->setFilters(filters);
1309     layer->setFilters(filters);
1310
1311     setLayerPropertiesForTesting(parent.get(), identityMatrix, FloatPoint(0, 0), IntSize(300, 300));
1312     setLayerPropertiesForTesting(parentLayer.get(), identityMatrix, FloatPoint(0, 0), IntSize(300, 300), false, opaqueLayers);
1313     setLayerPropertiesForTesting(layer.get(), identityMatrix, FloatPoint(0, 0), IntSize(200, 200), true, opaqueLayers);
1314
1315     Vector<RefPtr<LayerChromium> > renderSurfaceLayerList;
1316     Vector<RefPtr<LayerChromium> > dummyLayerList;
1317     int dummyMaxTextureSize = 512;
1318
1319     parent->renderSurface()->setContentRect(IntRect(IntPoint::zero(), parent->bounds()));
1320     parent->setClipRect(IntRect(IntPoint::zero(), parent->bounds()));
1321     renderSurfaceLayerList.append(parent);
1322
1323     CCLayerTreeHostCommon::calculateDrawTransformsAndVisibility(parent.get(), parent.get(), identityMatrix, identityMatrix, renderSurfaceLayerList, dummyLayerList, dummyMaxTextureSize);
1324
1325     TestCCOcclusionTracker occlusion(IntRect(0, 0, 1000, 1000));
1326     occlusion.setLayerScissorRect(IntRect(200, 100, 100, 100));
1327
1328     occlusion.enterTargetRenderSurface(layer->renderSurface());
1329
1330     EXPECT_TRUE(occlusion.occluded(layer.get(), IntRect(0, 0, 100, 100)));
1331     EXPECT_TRUE(occlusion.occluded(layer.get(), IntRect(100, 0, 100, 100)));
1332     EXPECT_TRUE(occlusion.occluded(layer.get(), IntRect(0, 100, 100, 100)));
1333     EXPECT_TRUE(occlusion.occluded(layer.get(), IntRect(100, 100, 100, 100)));
1334     EXPECT_FALSE(occlusion.occluded(layer.get(), IntRect(200, 100, 100, 100)));
1335
1336     occlusion.useDefaultLayerScissorRect();
1337     EXPECT_TRUE(occlusion.occluded(layer.get(), IntRect(200, 100, 100, 100)));
1338     occlusion.setLayerScissorRect(IntRect(200, 100, 100, 100));
1339
1340     occlusion.markOccludedBehindLayer(layer.get());
1341     occlusion.leaveToTargetRenderSurface(parent->renderSurface());
1342     occlusion.enterTargetRenderSurface(parentLayer->renderSurface());
1343
1344     EXPECT_TRUE(occlusion.occluded(parentLayer.get(), IntRect(0, 0, 100, 100)));
1345     EXPECT_TRUE(occlusion.occluded(parentLayer.get(), IntRect(0, 100, 100, 100)));
1346     EXPECT_TRUE(occlusion.occluded(parentLayer.get(), IntRect(100, 0, 100, 100)));
1347     EXPECT_TRUE(occlusion.occluded(parentLayer.get(), IntRect(0, 100, 100, 100)));
1348     EXPECT_FALSE(occlusion.occluded(parentLayer.get(), IntRect(200, 100, 100, 100)));
1349     EXPECT_TRUE(occlusion.occluded(parentLayer.get(), IntRect(200, 0, 100, 100)));
1350     EXPECT_TRUE(occlusion.occluded(parentLayer.get(), IntRect(0, 200, 100, 100)));
1351     EXPECT_TRUE(occlusion.occluded(parentLayer.get(), IntRect(100, 200, 100, 100)));
1352     EXPECT_TRUE(occlusion.occluded(parentLayer.get(), IntRect(200, 200, 100, 100)));
1353
1354     occlusion.leaveToTargetRenderSurface(parent->renderSurface());
1355
1356     EXPECT_EQ_RECT(IntRect(200, 100, 100, 100), occlusion.unoccludedContentRect(parent.get(), IntRect(0, 0, 300, 300)));
1357 }
1358
1359 TEST_OPAQUE_AND_PAINTED_OPAQUE(CCOcclusionTrackerTest_layerScissorRectOverTile, layerScissorRectOverTile);
1360
1361 void screenScissorRectOverTile(bool opaqueLayers)
1362 {
1363     const TransformationMatrix identityMatrix;
1364     RefPtr<LayerChromium> parent = LayerChromium::create();
1365     RefPtr<LayerChromiumWithForcedDrawsContent> parentLayer = adoptRef(new LayerChromiumWithForcedDrawsContent());
1366     RefPtr<LayerChromiumWithForcedDrawsContent> layer = adoptRef(new LayerChromiumWithForcedDrawsContent());
1367     parent->createRenderSurface();
1368     parent->addChild(parentLayer);
1369     parent->addChild(layer);
1370
1371     FilterOperations filters;
1372     filters.operations().append(BasicComponentTransferFilterOperation::create(0.5, FilterOperation::GRAYSCALE));
1373     parentLayer->setFilters(filters);
1374     layer->setFilters(filters);
1375
1376     setLayerPropertiesForTesting(parent.get(), identityMatrix, FloatPoint(0, 0), IntSize(300, 300));
1377     setLayerPropertiesForTesting(parentLayer.get(), identityMatrix, FloatPoint(0, 0), IntSize(300, 300), false, opaqueLayers);
1378     setLayerPropertiesForTesting(layer.get(), identityMatrix, FloatPoint(0, 0), IntSize(200, 200), true, opaqueLayers);
1379
1380     Vector<RefPtr<LayerChromium> > renderSurfaceLayerList;
1381     Vector<RefPtr<LayerChromium> > dummyLayerList;
1382     int dummyMaxTextureSize = 512;
1383
1384     parent->renderSurface()->setContentRect(IntRect(IntPoint::zero(), parent->bounds()));
1385     parent->setClipRect(IntRect(IntPoint::zero(), parent->bounds()));
1386     renderSurfaceLayerList.append(parent);
1387
1388     CCLayerTreeHostCommon::calculateDrawTransformsAndVisibility(parent.get(), parent.get(), identityMatrix, identityMatrix, renderSurfaceLayerList, dummyLayerList, dummyMaxTextureSize);
1389
1390     TestCCOcclusionTracker occlusion(IntRect(200, 100, 100, 100));
1391
1392     occlusion.enterTargetRenderSurface(layer->renderSurface());
1393
1394     EXPECT_TRUE(occlusion.occluded(layer.get(), IntRect(0, 0, 100, 100)));
1395     EXPECT_TRUE(occlusion.occluded(layer.get(), IntRect(100, 0, 100, 100)));
1396     EXPECT_TRUE(occlusion.occluded(layer.get(), IntRect(0, 100, 100, 100)));
1397     EXPECT_TRUE(occlusion.occluded(layer.get(), IntRect(100, 100, 100, 100)));
1398
1399     // Occluded since its outside the surface bounds.
1400     EXPECT_TRUE(occlusion.occluded(layer.get(), IntRect(200, 100, 100, 100)));
1401
1402     // Test without any scissors.
1403     occlusion.setLayerScissorRect(IntRect(0, 0, 1000, 1000));
1404     EXPECT_FALSE(occlusion.occluded(layer.get(), IntRect(200, 100, 100, 100)));
1405     occlusion.useDefaultLayerScissorRect();
1406
1407     occlusion.markOccludedBehindLayer(layer.get());
1408     occlusion.leaveToTargetRenderSurface(parent->renderSurface());
1409     occlusion.enterTargetRenderSurface(parentLayer->renderSurface());
1410
1411     EXPECT_TRUE(occlusion.occluded(parentLayer.get(), IntRect(0, 0, 100, 100)));
1412     EXPECT_TRUE(occlusion.occluded(parentLayer.get(), IntRect(0, 100, 100, 100)));
1413     EXPECT_TRUE(occlusion.occluded(parentLayer.get(), IntRect(100, 0, 100, 100)));
1414     EXPECT_TRUE(occlusion.occluded(parentLayer.get(), IntRect(0, 100, 100, 100)));
1415     EXPECT_FALSE(occlusion.occluded(parentLayer.get(), IntRect(200, 100, 100, 100)));
1416     EXPECT_TRUE(occlusion.occluded(parentLayer.get(), IntRect(200, 0, 100, 100)));
1417     EXPECT_TRUE(occlusion.occluded(parentLayer.get(), IntRect(0, 200, 100, 100)));
1418     EXPECT_TRUE(occlusion.occluded(parentLayer.get(), IntRect(100, 200, 100, 100)));
1419     EXPECT_TRUE(occlusion.occluded(parentLayer.get(), IntRect(200, 200, 100, 100)));
1420
1421     occlusion.leaveToTargetRenderSurface(parent->renderSurface());
1422
1423     EXPECT_EQ_RECT(IntRect(200, 100, 100, 100), occlusion.unoccludedContentRect(parent.get(), IntRect(0, 0, 300, 300)));
1424 }
1425
1426 TEST_OPAQUE_AND_PAINTED_OPAQUE(CCOcclusionTrackerTest_screenScissorRectOverTile, screenScissorRectOverTile);
1427
1428 void layerScissorRectOverCulledTile(bool opaqueLayers)
1429 {
1430     const TransformationMatrix identityMatrix;
1431     RefPtr<LayerChromium> parent = LayerChromium::create();
1432     RefPtr<LayerChromiumWithForcedDrawsContent> parentLayer = adoptRef(new LayerChromiumWithForcedDrawsContent());
1433     RefPtr<LayerChromiumWithForcedDrawsContent> layer = adoptRef(new LayerChromiumWithForcedDrawsContent());
1434     parent->createRenderSurface();
1435     parent->addChild(parentLayer);
1436     parent->addChild(layer);
1437
1438     FilterOperations filters;
1439     filters.operations().append(BasicComponentTransferFilterOperation::create(0.5, FilterOperation::GRAYSCALE));
1440     parentLayer->setFilters(filters);
1441     layer->setFilters(filters);
1442
1443     setLayerPropertiesForTesting(parent.get(), identityMatrix, FloatPoint(0, 0), IntSize(300, 300));
1444     setLayerPropertiesForTesting(parentLayer.get(), identityMatrix, FloatPoint(0, 0), IntSize(300, 300), false, opaqueLayers);
1445     setLayerPropertiesForTesting(layer.get(), identityMatrix, FloatPoint(0, 0), IntSize(200, 200), true, opaqueLayers);
1446
1447     Vector<RefPtr<LayerChromium> > renderSurfaceLayerList;
1448     Vector<RefPtr<LayerChromium> > dummyLayerList;
1449     int dummyMaxTextureSize = 512;
1450
1451     parent->renderSurface()->setContentRect(IntRect(IntPoint::zero(), parent->bounds()));
1452     parent->setClipRect(IntRect(IntPoint::zero(), parent->bounds()));
1453     renderSurfaceLayerList.append(parent);
1454
1455     CCLayerTreeHostCommon::calculateDrawTransformsAndVisibility(parent.get(), parent.get(), identityMatrix, identityMatrix, renderSurfaceLayerList, dummyLayerList, dummyMaxTextureSize);
1456
1457     TestCCOcclusionTracker occlusion(IntRect(0, 0, 1000, 1000));
1458     occlusion.setLayerScissorRect(IntRect(100, 100, 100, 100));
1459
1460     occlusion.enterTargetRenderSurface(layer->renderSurface());
1461
1462     EXPECT_TRUE(occlusion.occluded(layer.get(), IntRect(0, 0, 100, 100)));
1463     EXPECT_TRUE(occlusion.occluded(layer.get(), IntRect(0, 100, 100, 100)));
1464     EXPECT_TRUE(occlusion.occluded(layer.get(), IntRect(100, 0, 100, 100)));
1465     EXPECT_FALSE(occlusion.occluded(layer.get(), IntRect(100, 100, 100, 100)));
1466
1467     occlusion.markOccludedBehindLayer(layer.get());
1468     occlusion.leaveToTargetRenderSurface(parent->renderSurface());
1469     occlusion.enterTargetRenderSurface(parentLayer->renderSurface());
1470
1471     EXPECT_TRUE(occlusion.occluded(parentLayer.get(), IntRect(0, 0, 100, 100)));
1472     EXPECT_TRUE(occlusion.occluded(parentLayer.get(), IntRect(0, 100, 100, 100)));
1473     EXPECT_TRUE(occlusion.occluded(parentLayer.get(), IntRect(100, 0, 100, 100)));
1474     EXPECT_TRUE(occlusion.occluded(parentLayer.get(), IntRect(100, 100, 100, 100)));
1475     EXPECT_TRUE(occlusion.occluded(parentLayer.get(), IntRect(200, 100, 100, 100)));
1476     EXPECT_TRUE(occlusion.occluded(parentLayer.get(), IntRect(200, 0, 100, 100)));
1477     EXPECT_TRUE(occlusion.occluded(parentLayer.get(), IntRect(0, 200, 100, 100)));
1478     EXPECT_TRUE(occlusion.occluded(parentLayer.get(), IntRect(100, 200, 100, 100)));
1479     EXPECT_TRUE(occlusion.occluded(parentLayer.get(), IntRect(200, 200, 100, 100)));
1480
1481     occlusion.leaveToTargetRenderSurface(parent->renderSurface());
1482
1483     EXPECT_TRUE(occlusion.unoccludedContentRect(parent.get(), IntRect(0, 0, 300, 300)).isEmpty());
1484 }
1485
1486 TEST_OPAQUE_AND_PAINTED_OPAQUE(CCOcclusionTrackerTest_layerScissorRectOverCulledTile, layerScissorRectOverCulledTile);
1487
1488 void screenScissorRectOverCulledTile(bool opaqueLayers)
1489 {
1490     const TransformationMatrix identityMatrix;
1491     RefPtr<LayerChromium> parent = LayerChromium::create();
1492     RefPtr<LayerChromiumWithForcedDrawsContent> parentLayer = adoptRef(new LayerChromiumWithForcedDrawsContent());
1493     RefPtr<LayerChromiumWithForcedDrawsContent> layer = adoptRef(new LayerChromiumWithForcedDrawsContent());
1494     parent->createRenderSurface();
1495     parent->addChild(parentLayer);
1496     parent->addChild(layer);
1497
1498     FilterOperations filters;
1499     filters.operations().append(BasicComponentTransferFilterOperation::create(0.5, FilterOperation::GRAYSCALE));
1500     parentLayer->setFilters(filters);
1501     layer->setFilters(filters);
1502
1503     setLayerPropertiesForTesting(parent.get(), identityMatrix, FloatPoint(0, 0), IntSize(300, 300));
1504     setLayerPropertiesForTesting(parentLayer.get(), identityMatrix, FloatPoint(0, 0), IntSize(300, 300), false, opaqueLayers);
1505     setLayerPropertiesForTesting(layer.get(), identityMatrix, FloatPoint(0, 0), IntSize(200, 200), true, opaqueLayers);
1506
1507     Vector<RefPtr<LayerChromium> > renderSurfaceLayerList;
1508     Vector<RefPtr<LayerChromium> > dummyLayerList;
1509     int dummyMaxTextureSize = 512;
1510
1511     parent->renderSurface()->setContentRect(IntRect(IntPoint::zero(), parent->bounds()));
1512     parent->setClipRect(IntRect(IntPoint::zero(), parent->bounds()));
1513     renderSurfaceLayerList.append(parent);
1514
1515     CCLayerTreeHostCommon::calculateDrawTransformsAndVisibility(parent.get(), parent.get(), identityMatrix, identityMatrix, renderSurfaceLayerList, dummyLayerList, dummyMaxTextureSize);
1516
1517     TestCCOcclusionTracker occlusion(IntRect(100, 100, 100, 100));
1518
1519     occlusion.enterTargetRenderSurface(layer->renderSurface());
1520
1521     EXPECT_TRUE(occlusion.occluded(layer.get(), IntRect(0, 0, 100, 100)));
1522     EXPECT_TRUE(occlusion.occluded(layer.get(), IntRect(0, 100, 100, 100)));
1523     EXPECT_TRUE(occlusion.occluded(layer.get(), IntRect(100, 0, 100, 100)));
1524     EXPECT_FALSE(occlusion.occluded(layer.get(), IntRect(100, 100, 100, 100)));
1525
1526     occlusion.markOccludedBehindLayer(layer.get());
1527     occlusion.leaveToTargetRenderSurface(parent->renderSurface());
1528     occlusion.enterTargetRenderSurface(parentLayer->renderSurface());
1529
1530     EXPECT_TRUE(occlusion.occluded(parentLayer.get(), IntRect(0, 0, 100, 100)));
1531     EXPECT_TRUE(occlusion.occluded(parentLayer.get(), IntRect(0, 100, 100, 100)));
1532     EXPECT_TRUE(occlusion.occluded(parentLayer.get(), IntRect(100, 0, 100, 100)));
1533     EXPECT_TRUE(occlusion.occluded(parentLayer.get(), IntRect(100, 100, 100, 100)));
1534     EXPECT_TRUE(occlusion.occluded(parentLayer.get(), IntRect(200, 100, 100, 100)));
1535     EXPECT_TRUE(occlusion.occluded(parentLayer.get(), IntRect(200, 0, 100, 100)));
1536     EXPECT_TRUE(occlusion.occluded(parentLayer.get(), IntRect(0, 200, 100, 100)));
1537     EXPECT_TRUE(occlusion.occluded(parentLayer.get(), IntRect(100, 200, 100, 100)));
1538     EXPECT_TRUE(occlusion.occluded(parentLayer.get(), IntRect(200, 200, 100, 100)));
1539
1540     occlusion.leaveToTargetRenderSurface(parent->renderSurface());
1541
1542     EXPECT_TRUE(occlusion.unoccludedContentRect(parent.get(), IntRect(0, 0, 300, 300)).isEmpty());
1543 }
1544
1545 TEST_OPAQUE_AND_PAINTED_OPAQUE(CCOcclusionTrackerTest_screenScissorRectOverCulledTile, screenScissorRectOverCulledTile);
1546
1547 void layerScissorRectOverPartialTiles(bool opaqueLayers)
1548 {
1549     const TransformationMatrix identityMatrix;
1550     RefPtr<LayerChromium> parent = LayerChromium::create();
1551     RefPtr<LayerChromiumWithForcedDrawsContent> parentLayer = adoptRef(new LayerChromiumWithForcedDrawsContent());
1552     RefPtr<LayerChromiumWithForcedDrawsContent> layer = adoptRef(new LayerChromiumWithForcedDrawsContent());
1553     parent->createRenderSurface();
1554     parent->addChild(parentLayer);
1555     parent->addChild(layer);
1556
1557     FilterOperations filters;
1558     filters.operations().append(BasicComponentTransferFilterOperation::create(0.5, FilterOperation::GRAYSCALE));
1559     parentLayer->setFilters(filters);
1560     layer->setFilters(filters);
1561
1562     setLayerPropertiesForTesting(parent.get(), identityMatrix, FloatPoint(0, 0), IntSize(300, 300));
1563     setLayerPropertiesForTesting(parentLayer.get(), identityMatrix, FloatPoint(0, 0), IntSize(300, 300), false, opaqueLayers);
1564     setLayerPropertiesForTesting(layer.get(), identityMatrix, FloatPoint(0, 0), IntSize(200, 200), true, opaqueLayers);
1565
1566     Vector<RefPtr<LayerChromium> > renderSurfaceLayerList;
1567     Vector<RefPtr<LayerChromium> > dummyLayerList;
1568     int dummyMaxTextureSize = 512;
1569
1570     parent->renderSurface()->setContentRect(IntRect(IntPoint::zero(), parent->bounds()));
1571     parent->setClipRect(IntRect(IntPoint::zero(), parent->bounds()));
1572     renderSurfaceLayerList.append(parent);
1573
1574     CCLayerTreeHostCommon::calculateDrawTransformsAndVisibility(parent.get(), parent.get(), identityMatrix, identityMatrix, renderSurfaceLayerList, dummyLayerList, dummyMaxTextureSize);
1575
1576     TestCCOcclusionTracker occlusion(IntRect(0, 0, 1000, 1000));
1577     occlusion.setLayerScissorRect(IntRect(50, 50, 200, 200));
1578
1579     occlusion.enterTargetRenderSurface(layer->renderSurface());
1580
1581     EXPECT_FALSE(occlusion.occluded(layer.get(), IntRect(0, 0, 100, 100)));
1582     EXPECT_FALSE(occlusion.occluded(layer.get(), IntRect(0, 100, 100, 100)));
1583     EXPECT_FALSE(occlusion.occluded(layer.get(), IntRect(100, 0, 100, 100)));
1584     EXPECT_FALSE(occlusion.occluded(layer.get(), IntRect(100, 100, 100, 100)));
1585
1586     occlusion.markOccludedBehindLayer(layer.get());
1587     occlusion.leaveToTargetRenderSurface(parent->renderSurface());
1588     occlusion.enterTargetRenderSurface(parentLayer->renderSurface());
1589
1590     EXPECT_TRUE(occlusion.occluded(parentLayer.get(), IntRect(0, 0, 100, 100)));
1591     EXPECT_TRUE(occlusion.occluded(parentLayer.get(), IntRect(0, 100, 100, 100)));
1592     EXPECT_TRUE(occlusion.occluded(parentLayer.get(), IntRect(100, 0, 100, 100)));
1593     EXPECT_TRUE(occlusion.occluded(parentLayer.get(), IntRect(100, 100, 100, 100)));
1594     EXPECT_FALSE(occlusion.occluded(parentLayer.get(), IntRect(200, 100, 100, 100)));
1595     EXPECT_FALSE(occlusion.occluded(parentLayer.get(), IntRect(200, 0, 100, 100)));
1596     EXPECT_FALSE(occlusion.occluded(parentLayer.get(), IntRect(0, 200, 100, 100)));
1597     EXPECT_FALSE(occlusion.occluded(parentLayer.get(), IntRect(100, 200, 100, 100)));
1598     EXPECT_FALSE(occlusion.occluded(parentLayer.get(), IntRect(200, 200, 100, 100)));
1599
1600     occlusion.leaveToTargetRenderSurface(parent->renderSurface());
1601
1602     EXPECT_EQ_RECT(IntRect(50, 50, 200, 200), occlusion.unoccludedContentRect(parent.get(), IntRect(0, 0, 300, 300)));
1603     EXPECT_EQ_RECT(IntRect(200, 50, 50, 50), occlusion.unoccludedContentRect(parent.get(), IntRect(0, 0, 300, 100)));
1604     EXPECT_EQ_RECT(IntRect(200, 100, 50, 100), occlusion.unoccludedContentRect(parent.get(), IntRect(0, 100, 300, 100)));
1605     EXPECT_EQ_RECT(IntRect(200, 100, 50, 100), occlusion.unoccludedContentRect(parent.get(), IntRect(200, 100, 100, 100)));
1606     EXPECT_EQ_RECT(IntRect(100, 200, 100, 50), occlusion.unoccludedContentRect(parent.get(), IntRect(100, 200, 100, 100)));
1607 }
1608
1609 TEST_OPAQUE_AND_PAINTED_OPAQUE(CCOcclusionTrackerTest_layerScissorRectOverPartialTiles, layerScissorRectOverPartialTiles);
1610
1611 void screenScissorRectOverPartialTiles(bool opaqueLayers)
1612 {
1613     const TransformationMatrix identityMatrix;
1614     RefPtr<LayerChromium> parent = LayerChromium::create();
1615     RefPtr<LayerChromiumWithForcedDrawsContent> parentLayer = adoptRef(new LayerChromiumWithForcedDrawsContent());
1616     RefPtr<LayerChromiumWithForcedDrawsContent> layer = adoptRef(new LayerChromiumWithForcedDrawsContent());
1617     parent->createRenderSurface();
1618     parent->addChild(parentLayer);
1619     parent->addChild(layer);
1620
1621     FilterOperations filters;
1622     filters.operations().append(BasicComponentTransferFilterOperation::create(0.5, FilterOperation::GRAYSCALE));
1623     parentLayer->setFilters(filters);
1624     layer->setFilters(filters);
1625
1626     setLayerPropertiesForTesting(parent.get(), identityMatrix, FloatPoint(0, 0), IntSize(300, 300));
1627     setLayerPropertiesForTesting(parentLayer.get(), identityMatrix, FloatPoint(0, 0), IntSize(300, 300), false, opaqueLayers);
1628     setLayerPropertiesForTesting(layer.get(), identityMatrix, FloatPoint(0, 0), IntSize(200, 200), true, opaqueLayers);
1629
1630     Vector<RefPtr<LayerChromium> > renderSurfaceLayerList;
1631     Vector<RefPtr<LayerChromium> > dummyLayerList;
1632     int dummyMaxTextureSize = 512;
1633
1634     parent->renderSurface()->setContentRect(IntRect(IntPoint::zero(), parent->bounds()));
1635     parent->setClipRect(IntRect(IntPoint::zero(), parent->bounds()));
1636     renderSurfaceLayerList.append(parent);
1637
1638     CCLayerTreeHostCommon::calculateDrawTransformsAndVisibility(parent.get(), parent.get(), identityMatrix, identityMatrix, renderSurfaceLayerList, dummyLayerList, dummyMaxTextureSize);
1639
1640     TestCCOcclusionTracker occlusion(IntRect(50, 50, 200, 200));
1641
1642     occlusion.enterTargetRenderSurface(layer->renderSurface());
1643
1644     EXPECT_FALSE(occlusion.occluded(layer.get(), IntRect(0, 0, 100, 100)));
1645     EXPECT_FALSE(occlusion.occluded(layer.get(), IntRect(0, 100, 100, 100)));
1646     EXPECT_FALSE(occlusion.occluded(layer.get(), IntRect(100, 0, 100, 100)));
1647     EXPECT_FALSE(occlusion.occluded(layer.get(), IntRect(100, 100, 100, 100)));
1648
1649     occlusion.markOccludedBehindLayer(layer.get());
1650     occlusion.leaveToTargetRenderSurface(parent->renderSurface());
1651     occlusion.enterTargetRenderSurface(parentLayer->renderSurface());
1652
1653     EXPECT_TRUE(occlusion.occluded(parentLayer.get(), IntRect(0, 0, 100, 100)));
1654     EXPECT_TRUE(occlusion.occluded(parentLayer.get(), IntRect(0, 100, 100, 100)));
1655     EXPECT_TRUE(occlusion.occluded(parentLayer.get(), IntRect(100, 0, 100, 100)));
1656     EXPECT_TRUE(occlusion.occluded(parentLayer.get(), IntRect(100, 100, 100, 100)));
1657     EXPECT_FALSE(occlusion.occluded(parentLayer.get(), IntRect(200, 100, 100, 100)));
1658     EXPECT_FALSE(occlusion.occluded(parentLayer.get(), IntRect(200, 0, 100, 100)));
1659     EXPECT_FALSE(occlusion.occluded(parentLayer.get(), IntRect(0, 200, 100, 100)));
1660     EXPECT_FALSE(occlusion.occluded(parentLayer.get(), IntRect(100, 200, 100, 100)));
1661     EXPECT_FALSE(occlusion.occluded(parentLayer.get(), IntRect(200, 200, 100, 100)));
1662
1663     occlusion.leaveToTargetRenderSurface(parent->renderSurface());
1664
1665     EXPECT_EQ_RECT(IntRect(50, 50, 200, 200), occlusion.unoccludedContentRect(parent.get(), IntRect(0, 0, 300, 300)));
1666     EXPECT_EQ_RECT(IntRect(200, 50, 50, 50), occlusion.unoccludedContentRect(parent.get(), IntRect(0, 0, 300, 100)));
1667     EXPECT_EQ_RECT(IntRect(200, 100, 50, 100), occlusion.unoccludedContentRect(parent.get(), IntRect(0, 100, 300, 100)));
1668     EXPECT_EQ_RECT(IntRect(200, 100, 50, 100), occlusion.unoccludedContentRect(parent.get(), IntRect(200, 100, 100, 100)));
1669     EXPECT_EQ_RECT(IntRect(100, 200, 100, 50), occlusion.unoccludedContentRect(parent.get(), IntRect(100, 200, 100, 100)));
1670 }
1671
1672 TEST_OPAQUE_AND_PAINTED_OPAQUE(CCOcclusionTrackerTest_screenScissorRectOverPartialTiles, screenScissorRectOverPartialTiles);
1673
1674 void layerScissorRectOverNoTiles(bool opaqueLayers)
1675 {
1676     const TransformationMatrix identityMatrix;
1677     RefPtr<LayerChromium> parent = LayerChromium::create();
1678     RefPtr<LayerChromiumWithForcedDrawsContent> parentLayer = adoptRef(new LayerChromiumWithForcedDrawsContent());
1679     RefPtr<LayerChromiumWithForcedDrawsContent> layer = adoptRef(new LayerChromiumWithForcedDrawsContent());
1680     parent->createRenderSurface();
1681     parent->addChild(parentLayer);
1682     parent->addChild(layer);
1683
1684     FilterOperations filters;
1685     filters.operations().append(BasicComponentTransferFilterOperation::create(0.5, FilterOperation::GRAYSCALE));
1686     parentLayer->setFilters(filters);
1687     layer->setFilters(filters);
1688
1689     setLayerPropertiesForTesting(parent.get(), identityMatrix, FloatPoint(0, 0), IntSize(300, 300));
1690     setLayerPropertiesForTesting(parentLayer.get(), identityMatrix, FloatPoint(0, 0), IntSize(300, 300), false, opaqueLayers);
1691     setLayerPropertiesForTesting(layer.get(), identityMatrix, FloatPoint(0, 0), IntSize(200, 200), true, opaqueLayers);
1692
1693     Vector<RefPtr<LayerChromium> > renderSurfaceLayerList;
1694     Vector<RefPtr<LayerChromium> > dummyLayerList;
1695     int dummyMaxTextureSize = 512;
1696
1697     parent->renderSurface()->setContentRect(IntRect(IntPoint::zero(), parent->bounds()));
1698     parent->setClipRect(IntRect(IntPoint::zero(), parent->bounds()));
1699     renderSurfaceLayerList.append(parent);
1700
1701     CCLayerTreeHostCommon::calculateDrawTransformsAndVisibility(parent.get(), parent.get(), identityMatrix, identityMatrix, renderSurfaceLayerList, dummyLayerList, dummyMaxTextureSize);
1702
1703     TestCCOcclusionTracker occlusion(IntRect(0, 0, 1000, 1000));
1704     occlusion.setLayerScissorRect(IntRect(500, 500, 100, 100));
1705
1706     occlusion.enterTargetRenderSurface(layer->renderSurface());
1707
1708     EXPECT_TRUE(occlusion.occluded(layer.get(), IntRect(0, 0, 100, 100)));
1709     EXPECT_TRUE(occlusion.occluded(layer.get(), IntRect(0, 100, 100, 100)));
1710     EXPECT_TRUE(occlusion.occluded(layer.get(), IntRect(100, 0, 100, 100)));
1711     EXPECT_TRUE(occlusion.occluded(layer.get(), IntRect(100, 100, 100, 100)));
1712
1713     occlusion.markOccludedBehindLayer(layer.get());
1714     occlusion.leaveToTargetRenderSurface(parent->renderSurface());
1715     occlusion.enterTargetRenderSurface(parentLayer->renderSurface());
1716
1717     EXPECT_TRUE(occlusion.occluded(parentLayer.get(), IntRect(0, 0, 100, 100)));
1718     EXPECT_TRUE(occlusion.occluded(parentLayer.get(), IntRect(0, 100, 100, 100)));
1719     EXPECT_TRUE(occlusion.occluded(parentLayer.get(), IntRect(100, 0, 100, 100)));
1720     EXPECT_TRUE(occlusion.occluded(parentLayer.get(), IntRect(100, 100, 100, 100)));
1721     EXPECT_TRUE(occlusion.occluded(parentLayer.get(), IntRect(200, 100, 100, 100)));
1722     EXPECT_TRUE(occlusion.occluded(parentLayer.get(), IntRect(200, 0, 100, 100)));
1723     EXPECT_TRUE(occlusion.occluded(parentLayer.get(), IntRect(0, 200, 100, 100)));
1724     EXPECT_TRUE(occlusion.occluded(parentLayer.get(), IntRect(100, 200, 100, 100)));
1725     EXPECT_TRUE(occlusion.occluded(parentLayer.get(), IntRect(200, 200, 100, 100)));
1726
1727     occlusion.leaveToTargetRenderSurface(parent->renderSurface());
1728
1729     EXPECT_TRUE(occlusion.unoccludedContentRect(parent.get(), IntRect(0, 0, 300, 300)).isEmpty());
1730     EXPECT_TRUE(occlusion.unoccludedContentRect(parent.get(), IntRect(0, 0, 300, 100)).isEmpty());
1731     EXPECT_TRUE(occlusion.unoccludedContentRect(parent.get(), IntRect(0, 100, 300, 100)).isEmpty());
1732     EXPECT_TRUE(occlusion.unoccludedContentRect(parent.get(), IntRect(200, 100, 100, 100)).isEmpty());
1733     EXPECT_TRUE(occlusion.unoccludedContentRect(parent.get(), IntRect(100, 200, 100, 100)).isEmpty());
1734 }
1735
1736 TEST_OPAQUE_AND_PAINTED_OPAQUE(CCOcclusionTrackerTest_layerScissorRectOverNoTiles, layerScissorRectOverNoTiles);
1737
1738 void screenScissorRectOverNoTiles(bool opaqueLayers)
1739 {
1740     const TransformationMatrix identityMatrix;
1741     RefPtr<LayerChromium> parent = LayerChromium::create();
1742     RefPtr<LayerChromiumWithForcedDrawsContent> parentLayer = adoptRef(new LayerChromiumWithForcedDrawsContent());
1743     RefPtr<LayerChromiumWithForcedDrawsContent> layer = adoptRef(new LayerChromiumWithForcedDrawsContent());
1744     parent->createRenderSurface();
1745     parent->addChild(parentLayer);
1746     parent->addChild(layer);
1747
1748     FilterOperations filters;
1749     filters.operations().append(BasicComponentTransferFilterOperation::create(0.5, FilterOperation::GRAYSCALE));
1750     parentLayer->setFilters(filters);
1751     layer->setFilters(filters);
1752
1753     setLayerPropertiesForTesting(parent.get(), identityMatrix, FloatPoint(0, 0), IntSize(300, 300));
1754     setLayerPropertiesForTesting(parentLayer.get(), identityMatrix, FloatPoint(0, 0), IntSize(300, 300), false, opaqueLayers);
1755     setLayerPropertiesForTesting(layer.get(), identityMatrix, FloatPoint(0, 0), IntSize(200, 200), true, opaqueLayers);
1756
1757     Vector<RefPtr<LayerChromium> > renderSurfaceLayerList;
1758     Vector<RefPtr<LayerChromium> > dummyLayerList;
1759     int dummyMaxTextureSize = 512;
1760
1761     parent->renderSurface()->setContentRect(IntRect(IntPoint::zero(), parent->bounds()));
1762     parent->setClipRect(IntRect(IntPoint::zero(), parent->bounds()));
1763     renderSurfaceLayerList.append(parent);
1764
1765     CCLayerTreeHostCommon::calculateDrawTransformsAndVisibility(parent.get(), parent.get(), identityMatrix, identityMatrix, renderSurfaceLayerList, dummyLayerList, dummyMaxTextureSize);
1766
1767     TestCCOcclusionTracker occlusion(IntRect(500, 500, 100, 100));
1768
1769     occlusion.enterTargetRenderSurface(layer->renderSurface());
1770
1771     EXPECT_TRUE(occlusion.occluded(layer.get(), IntRect(0, 0, 100, 100)));
1772     EXPECT_TRUE(occlusion.occluded(layer.get(), IntRect(0, 100, 100, 100)));
1773     EXPECT_TRUE(occlusion.occluded(layer.get(), IntRect(100, 0, 100, 100)));
1774     EXPECT_TRUE(occlusion.occluded(layer.get(), IntRect(100, 100, 100, 100)));
1775
1776     occlusion.markOccludedBehindLayer(layer.get());
1777     occlusion.leaveToTargetRenderSurface(parent->renderSurface());
1778     occlusion.enterTargetRenderSurface(parentLayer->renderSurface());
1779
1780     EXPECT_TRUE(occlusion.occluded(parentLayer.get(), IntRect(0, 0, 100, 100)));
1781     EXPECT_TRUE(occlusion.occluded(parentLayer.get(), IntRect(0, 100, 100, 100)));
1782     EXPECT_TRUE(occlusion.occluded(parentLayer.get(), IntRect(100, 0, 100, 100)));
1783     EXPECT_TRUE(occlusion.occluded(parentLayer.get(), IntRect(100, 100, 100, 100)));
1784     EXPECT_TRUE(occlusion.occluded(parentLayer.get(), IntRect(200, 100, 100, 100)));
1785     EXPECT_TRUE(occlusion.occluded(parentLayer.get(), IntRect(200, 0, 100, 100)));
1786     EXPECT_TRUE(occlusion.occluded(parentLayer.get(), IntRect(0, 200, 100, 100)));
1787     EXPECT_TRUE(occlusion.occluded(parentLayer.get(), IntRect(100, 200, 100, 100)));
1788     EXPECT_TRUE(occlusion.occluded(parentLayer.get(), IntRect(200, 200, 100, 100)));
1789
1790     occlusion.leaveToTargetRenderSurface(parent->renderSurface());
1791
1792     EXPECT_TRUE(occlusion.unoccludedContentRect(parent.get(), IntRect(0, 0, 300, 300)).isEmpty());
1793     EXPECT_TRUE(occlusion.unoccludedContentRect(parent.get(), IntRect(0, 0, 300, 100)).isEmpty());
1794     EXPECT_TRUE(occlusion.unoccludedContentRect(parent.get(), IntRect(0, 100, 300, 100)).isEmpty());
1795     EXPECT_TRUE(occlusion.unoccludedContentRect(parent.get(), IntRect(200, 100, 100, 100)).isEmpty());
1796     EXPECT_TRUE(occlusion.unoccludedContentRect(parent.get(), IntRect(100, 200, 100, 100)).isEmpty());
1797 }
1798
1799 TEST_OPAQUE_AND_PAINTED_OPAQUE(CCOcclusionTrackerTest_screenScissorRectOverNoTiles, screenScissorRectOverNoTiles);
1800
1801 void layerScissorRectForLayerOffOrigin(bool opaqueLayers)
1802 {
1803     const TransformationMatrix identityMatrix;
1804     RefPtr<LayerChromium> parent = LayerChromium::create();
1805     RefPtr<LayerChromiumWithForcedDrawsContent> parentLayer = adoptRef(new LayerChromiumWithForcedDrawsContent());
1806     RefPtr<LayerChromiumWithForcedDrawsContent> layer = adoptRef(new LayerChromiumWithForcedDrawsContent());
1807     parent->createRenderSurface();
1808     parent->addChild(parentLayer);
1809     parent->addChild(layer);
1810
1811     FilterOperations filters;
1812     filters.operations().append(BasicComponentTransferFilterOperation::create(0.5, FilterOperation::GRAYSCALE));
1813     parentLayer->setFilters(filters);
1814     layer->setFilters(filters);
1815
1816     setLayerPropertiesForTesting(parent.get(), identityMatrix, FloatPoint(0, 0), IntSize(300, 300));
1817     setLayerPropertiesForTesting(parentLayer.get(), identityMatrix, FloatPoint(0, 0), IntSize(300, 300), false, opaqueLayers);
1818     setLayerPropertiesForTesting(layer.get(), identityMatrix, FloatPoint(100, 100), IntSize(200, 200), true, opaqueLayers);
1819
1820     Vector<RefPtr<LayerChromium> > renderSurfaceLayerList;
1821     Vector<RefPtr<LayerChromium> > dummyLayerList;
1822     int dummyMaxTextureSize = 512;
1823
1824     parent->renderSurface()->setContentRect(IntRect(IntPoint::zero(), parent->bounds()));
1825     parent->setClipRect(IntRect(IntPoint::zero(), parent->bounds()));
1826     renderSurfaceLayerList.append(parent);
1827
1828     CCLayerTreeHostCommon::calculateDrawTransformsAndVisibility(parent.get(), parent.get(), identityMatrix, identityMatrix, renderSurfaceLayerList, dummyLayerList, dummyMaxTextureSize);
1829
1830     TestCCOcclusionTracker occlusion(IntRect(0, 0, 1000, 1000));
1831
1832     occlusion.enterTargetRenderSurface(layer->renderSurface());
1833
1834     // This layer is translated when drawn into its target. So if the scissor rect given from the target surface
1835     // is not in that target space, then after translating these query rects into the target, they will fall outside
1836     // the scissor and be considered occluded.
1837     EXPECT_FALSE(occlusion.occluded(layer.get(), IntRect(0, 0, 100, 100)));
1838     EXPECT_FALSE(occlusion.occluded(layer.get(), IntRect(0, 100, 100, 100)));
1839     EXPECT_FALSE(occlusion.occluded(layer.get(), IntRect(100, 0, 100, 100)));
1840     EXPECT_FALSE(occlusion.occluded(layer.get(), IntRect(100, 100, 100, 100)));
1841 }
1842
1843 TEST_OPAQUE_AND_PAINTED_OPAQUE(CCOcclusionTrackerTest_layerScissorRectForLayerOffOrigin, layerScissorRectForLayerOffOrigin);
1844
1845 void damageRectOverTile(bool opaqueLayers)
1846 {
1847     const TransformationMatrix identityMatrix;
1848     RefPtr<LayerChromium> parent = LayerChromium::create();
1849     RefPtr<LayerChromiumWithForcedDrawsContent> parentLayer = adoptRef(new LayerChromiumWithForcedDrawsContent());
1850     RefPtr<LayerChromiumWithForcedDrawsContent> layer = adoptRef(new LayerChromiumWithForcedDrawsContent());
1851     parent->createRenderSurface();
1852     parent->addChild(parentLayer);
1853     parent->addChild(layer);
1854
1855     FilterOperations filters;
1856     filters.operations().append(BasicComponentTransferFilterOperation::create(0.5, FilterOperation::GRAYSCALE));
1857     parentLayer->setFilters(filters);
1858     layer->setFilters(filters);
1859
1860     setLayerPropertiesForTesting(parent.get(), identityMatrix, FloatPoint(0, 0), IntSize(300, 300));
1861     setLayerPropertiesForTesting(parentLayer.get(), identityMatrix, FloatPoint(0, 0), IntSize(300, 300), false, opaqueLayers);
1862     setLayerPropertiesForTesting(layer.get(), identityMatrix, FloatPoint(0, 0), IntSize(200, 200), true, opaqueLayers);
1863
1864     Vector<RefPtr<LayerChromium> > renderSurfaceLayerList;
1865     Vector<RefPtr<LayerChromium> > dummyLayerList;
1866     int dummyMaxTextureSize = 512;
1867
1868     parent->renderSurface()->setContentRect(IntRect(IntPoint::zero(), parent->bounds()));
1869     parent->setClipRect(IntRect(IntPoint::zero(), parent->bounds()));
1870     renderSurfaceLayerList.append(parent);
1871
1872     CCLayerTreeHostCommon::calculateDrawTransformsAndVisibility(parent.get(), parent.get(), identityMatrix, identityMatrix, renderSurfaceLayerList, dummyLayerList, dummyMaxTextureSize);
1873
1874     TestDamageClient damage(FloatRect(200, 100, 100, 100));
1875     TestCCOcclusionTracker occlusion(IntRect(0, 0, 1000, 1000), &damage);
1876
1877     occlusion.enterTargetRenderSurface(layer->renderSurface());
1878
1879     EXPECT_TRUE(occlusion.occluded(layer.get(), IntRect(0, 0, 100, 100)));
1880     EXPECT_TRUE(occlusion.occluded(layer.get(), IntRect(100, 0, 100, 100)));
1881     EXPECT_TRUE(occlusion.occluded(layer.get(), IntRect(0, 100, 100, 100)));
1882     EXPECT_TRUE(occlusion.occluded(layer.get(), IntRect(100, 100, 100, 100)));
1883
1884     // Outside the layer's clip rect.
1885     EXPECT_TRUE(occlusion.occluded(layer.get(), IntRect(200, 100, 100, 100)));
1886
1887     occlusion.markOccludedBehindLayer(layer.get());
1888     occlusion.leaveToTargetRenderSurface(parent->renderSurface());
1889     occlusion.enterTargetRenderSurface(parentLayer->renderSurface());
1890
1891     EXPECT_TRUE(occlusion.occluded(parentLayer.get(), IntRect(0, 0, 100, 100)));
1892     EXPECT_TRUE(occlusion.occluded(parentLayer.get(), IntRect(0, 100, 100, 100)));
1893     EXPECT_TRUE(occlusion.occluded(parentLayer.get(), IntRect(100, 0, 100, 100)));
1894     EXPECT_TRUE(occlusion.occluded(parentLayer.get(), IntRect(100, 100, 100, 100)));
1895     EXPECT_FALSE(occlusion.occluded(parentLayer.get(), IntRect(200, 100, 100, 100)));
1896     EXPECT_TRUE(occlusion.occluded(parentLayer.get(), IntRect(200, 0, 100, 100)));
1897     EXPECT_TRUE(occlusion.occluded(parentLayer.get(), IntRect(0, 200, 100, 100)));
1898     EXPECT_TRUE(occlusion.occluded(parentLayer.get(), IntRect(100, 200, 100, 100)));
1899     EXPECT_TRUE(occlusion.occluded(parentLayer.get(), IntRect(200, 200, 100, 100)));
1900
1901     occlusion.leaveToTargetRenderSurface(parent->renderSurface());
1902
1903     EXPECT_EQ_RECT(IntRect(200, 100, 100, 100), occlusion.unoccludedContentRect(parent.get(), IntRect(0, 0, 300, 300)));
1904 }
1905
1906 TEST_OPAQUE_AND_PAINTED_OPAQUE(CCOcclusionTrackerTest_damageRectOverTile, damageRectOverTile);
1907
1908 void damageRectOverCulledTile(bool opaqueLayers)
1909 {
1910     const TransformationMatrix identityMatrix;
1911     RefPtr<LayerChromium> parent = LayerChromium::create();
1912     RefPtr<LayerChromiumWithForcedDrawsContent> parentLayer = adoptRef(new LayerChromiumWithForcedDrawsContent());
1913     RefPtr<LayerChromiumWithForcedDrawsContent> layer = adoptRef(new LayerChromiumWithForcedDrawsContent());
1914     parent->createRenderSurface();
1915     parent->addChild(parentLayer);
1916     parent->addChild(layer);
1917
1918     FilterOperations filters;
1919     filters.operations().append(BasicComponentTransferFilterOperation::create(0.5, FilterOperation::GRAYSCALE));
1920     parentLayer->setFilters(filters);
1921     layer->setFilters(filters);
1922
1923     setLayerPropertiesForTesting(parent.get(), identityMatrix, FloatPoint(0, 0), IntSize(300, 300));
1924     setLayerPropertiesForTesting(parentLayer.get(), identityMatrix, FloatPoint(0, 0), IntSize(300, 300), false, opaqueLayers);
1925     setLayerPropertiesForTesting(layer.get(), identityMatrix, FloatPoint(0, 0), IntSize(200, 200), true, opaqueLayers);
1926
1927     Vector<RefPtr<LayerChromium> > renderSurfaceLayerList;
1928     Vector<RefPtr<LayerChromium> > dummyLayerList;
1929     int dummyMaxTextureSize = 512;
1930
1931     parent->renderSurface()->setContentRect(IntRect(IntPoint::zero(), parent->bounds()));
1932     parent->setClipRect(IntRect(IntPoint::zero(), parent->bounds()));
1933     renderSurfaceLayerList.append(parent);
1934
1935     CCLayerTreeHostCommon::calculateDrawTransformsAndVisibility(parent.get(), parent.get(), identityMatrix, identityMatrix, renderSurfaceLayerList, dummyLayerList, dummyMaxTextureSize);
1936
1937     TestDamageClient damage(FloatRect(100, 100, 100, 100));
1938     TestCCOcclusionTracker occlusion(IntRect(0, 0, 1000, 1000), &damage);
1939
1940     occlusion.enterTargetRenderSurface(layer->renderSurface());
1941
1942     EXPECT_TRUE(occlusion.occluded(layer.get(), IntRect(0, 0, 100, 100)));
1943     EXPECT_TRUE(occlusion.occluded(layer.get(), IntRect(0, 100, 100, 100)));
1944     EXPECT_TRUE(occlusion.occluded(layer.get(), IntRect(100, 0, 100, 100)));
1945     EXPECT_FALSE(occlusion.occluded(layer.get(), IntRect(100, 100, 100, 100)));
1946
1947     occlusion.markOccludedBehindLayer(layer.get());
1948     occlusion.leaveToTargetRenderSurface(parent->renderSurface());
1949     occlusion.enterTargetRenderSurface(parentLayer->renderSurface());
1950
1951     EXPECT_TRUE(occlusion.occluded(parentLayer.get(), IntRect(0, 0, 100, 100)));
1952     EXPECT_TRUE(occlusion.occluded(parentLayer.get(), IntRect(0, 100, 100, 100)));
1953     EXPECT_TRUE(occlusion.occluded(parentLayer.get(), IntRect(100, 0, 100, 100)));
1954     EXPECT_TRUE(occlusion.occluded(parentLayer.get(), IntRect(100, 100, 100, 100)));
1955     EXPECT_TRUE(occlusion.occluded(parentLayer.get(), IntRect(200, 100, 100, 100)));
1956     EXPECT_TRUE(occlusion.occluded(parentLayer.get(), IntRect(200, 0, 100, 100)));
1957     EXPECT_TRUE(occlusion.occluded(parentLayer.get(), IntRect(0, 200, 100, 100)));
1958     EXPECT_TRUE(occlusion.occluded(parentLayer.get(), IntRect(100, 200, 100, 100)));
1959     EXPECT_TRUE(occlusion.occluded(parentLayer.get(), IntRect(200, 200, 100, 100)));
1960
1961     occlusion.leaveToTargetRenderSurface(parent->renderSurface());
1962
1963     EXPECT_TRUE(occlusion.unoccludedContentRect(parent.get(), IntRect(0, 0, 300, 300)).isEmpty());
1964 }
1965
1966 TEST_OPAQUE_AND_PAINTED_OPAQUE(CCOcclusionTrackerTest_damageRectOverCulledTile, damageRectOverCulledTile);
1967
1968 void damageRectOverPartialTiles(bool opaqueLayers)
1969 {
1970     const TransformationMatrix identityMatrix;
1971     RefPtr<LayerChromium> parent = LayerChromium::create();
1972     RefPtr<LayerChromiumWithForcedDrawsContent> parentLayer = adoptRef(new LayerChromiumWithForcedDrawsContent());
1973     RefPtr<LayerChromiumWithForcedDrawsContent> layer = adoptRef(new LayerChromiumWithForcedDrawsContent());
1974     parent->createRenderSurface();
1975     parent->addChild(parentLayer);
1976     parent->addChild(layer);
1977
1978     FilterOperations filters;
1979     filters.operations().append(BasicComponentTransferFilterOperation::create(0.5, FilterOperation::GRAYSCALE));
1980     parentLayer->setFilters(filters);
1981     layer->setFilters(filters);
1982
1983     setLayerPropertiesForTesting(parent.get(), identityMatrix, FloatPoint(0, 0), IntSize(300, 300));
1984     setLayerPropertiesForTesting(parentLayer.get(), identityMatrix, FloatPoint(0, 0), IntSize(300, 300), false, opaqueLayers);
1985     setLayerPropertiesForTesting(layer.get(), identityMatrix, FloatPoint(0, 0), IntSize(200, 200), true, opaqueLayers);
1986
1987     Vector<RefPtr<LayerChromium> > renderSurfaceLayerList;
1988     Vector<RefPtr<LayerChromium> > dummyLayerList;
1989     int dummyMaxTextureSize = 512;
1990
1991     parent->renderSurface()->setContentRect(IntRect(IntPoint::zero(), parent->bounds()));
1992     parent->setClipRect(IntRect(IntPoint::zero(), parent->bounds()));
1993     renderSurfaceLayerList.append(parent);
1994
1995     CCLayerTreeHostCommon::calculateDrawTransformsAndVisibility(parent.get(), parent.get(), identityMatrix, identityMatrix, renderSurfaceLayerList, dummyLayerList, dummyMaxTextureSize);
1996
1997     TestDamageClient damage(FloatRect(50, 50, 200, 200));
1998     TestCCOcclusionTracker occlusion(IntRect(0, 0, 1000, 1000), &damage);
1999
2000     occlusion.enterTargetRenderSurface(layer->renderSurface());
2001
2002     EXPECT_FALSE(occlusion.occluded(layer.get(), IntRect(0, 0, 100, 100)));
2003     EXPECT_FALSE(occlusion.occluded(layer.get(), IntRect(0, 100, 100, 100)));
2004     EXPECT_FALSE(occlusion.occluded(layer.get(), IntRect(100, 0, 100, 100)));
2005     EXPECT_FALSE(occlusion.occluded(layer.get(), IntRect(100, 100, 100, 100)));
2006
2007     occlusion.markOccludedBehindLayer(layer.get());
2008     occlusion.leaveToTargetRenderSurface(parent->renderSurface());
2009     occlusion.enterTargetRenderSurface(parentLayer->renderSurface());
2010
2011     EXPECT_TRUE(occlusion.occluded(parentLayer.get(), IntRect(0, 0, 100, 100)));
2012     EXPECT_TRUE(occlusion.occluded(parentLayer.get(), IntRect(0, 100, 100, 100)));
2013     EXPECT_TRUE(occlusion.occluded(parentLayer.get(), IntRect(100, 0, 100, 100)));
2014     EXPECT_TRUE(occlusion.occluded(parentLayer.get(), IntRect(100, 100, 100, 100)));
2015     EXPECT_FALSE(occlusion.occluded(parentLayer.get(), IntRect(200, 100, 100, 100)));
2016     EXPECT_FALSE(occlusion.occluded(parentLayer.get(), IntRect(200, 0, 100, 100)));
2017     EXPECT_FALSE(occlusion.occluded(parentLayer.get(), IntRect(0, 200, 100, 100)));
2018     EXPECT_FALSE(occlusion.occluded(parentLayer.get(), IntRect(100, 200, 100, 100)));
2019     EXPECT_FALSE(occlusion.occluded(parentLayer.get(), IntRect(200, 200, 100, 100)));
2020
2021     occlusion.leaveToTargetRenderSurface(parent->renderSurface());
2022
2023     EXPECT_EQ_RECT(IntRect(50, 50, 200, 200), occlusion.unoccludedContentRect(parent.get(), IntRect(0, 0, 300, 300)));
2024     EXPECT_EQ_RECT(IntRect(200, 50, 50, 50), occlusion.unoccludedContentRect(parent.get(), IntRect(0, 0, 300, 100)));
2025     EXPECT_EQ_RECT(IntRect(200, 100, 50, 100), occlusion.unoccludedContentRect(parent.get(), IntRect(0, 100, 300, 100)));
2026     EXPECT_EQ_RECT(IntRect(200, 100, 50, 100), occlusion.unoccludedContentRect(parent.get(), IntRect(200, 100, 100, 100)));
2027     EXPECT_EQ_RECT(IntRect(100, 200, 100, 50), occlusion.unoccludedContentRect(parent.get(), IntRect(100, 200, 100, 100)));
2028 }
2029
2030 TEST_OPAQUE_AND_PAINTED_OPAQUE(CCOcclusionTrackerTest_damageRectOverPartialTiles, damageRectOverPartialTiles);
2031
2032 void damageRectOverNoTiles(bool opaqueLayers)
2033 {
2034     const TransformationMatrix identityMatrix;
2035     RefPtr<LayerChromium> parent = LayerChromium::create();
2036     RefPtr<LayerChromiumWithForcedDrawsContent> parentLayer = adoptRef(new LayerChromiumWithForcedDrawsContent());
2037     RefPtr<LayerChromiumWithForcedDrawsContent> layer = adoptRef(new LayerChromiumWithForcedDrawsContent());
2038     parent->createRenderSurface();
2039     parent->addChild(parentLayer);
2040     parent->addChild(layer);
2041
2042     FilterOperations filters;
2043     filters.operations().append(BasicComponentTransferFilterOperation::create(0.5, FilterOperation::GRAYSCALE));
2044     parentLayer->setFilters(filters);
2045     layer->setFilters(filters);
2046
2047     setLayerPropertiesForTesting(parent.get(), identityMatrix, FloatPoint(0, 0), IntSize(300, 300));
2048     setLayerPropertiesForTesting(parentLayer.get(), identityMatrix, FloatPoint(0, 0), IntSize(300, 300), false, opaqueLayers);
2049     setLayerPropertiesForTesting(layer.get(), identityMatrix, FloatPoint(0, 0), IntSize(200, 200), true, opaqueLayers);
2050
2051     Vector<RefPtr<LayerChromium> > renderSurfaceLayerList;
2052     Vector<RefPtr<LayerChromium> > dummyLayerList;
2053     int dummyMaxTextureSize = 512;
2054
2055     parent->renderSurface()->setContentRect(IntRect(IntPoint::zero(), parent->bounds()));
2056     parent->setClipRect(IntRect(IntPoint::zero(), parent->bounds()));
2057     renderSurfaceLayerList.append(parent);
2058
2059     CCLayerTreeHostCommon::calculateDrawTransformsAndVisibility(parent.get(), parent.get(), identityMatrix, identityMatrix, renderSurfaceLayerList, dummyLayerList, dummyMaxTextureSize);
2060
2061     TestDamageClient damage(FloatRect(500, 500, 100, 100));
2062     TestCCOcclusionTracker occlusion(IntRect(0, 0, 1000, 1000), &damage);
2063
2064     occlusion.enterTargetRenderSurface(layer->renderSurface());
2065
2066     EXPECT_TRUE(occlusion.occluded(layer.get(), IntRect(0, 0, 100, 100)));
2067     EXPECT_TRUE(occlusion.occluded(layer.get(), IntRect(0, 100, 100, 100)));
2068     EXPECT_TRUE(occlusion.occluded(layer.get(), IntRect(100, 0, 100, 100)));
2069     EXPECT_TRUE(occlusion.occluded(layer.get(), IntRect(100, 100, 100, 100)));
2070
2071     occlusion.markOccludedBehindLayer(layer.get());
2072     occlusion.leaveToTargetRenderSurface(parent->renderSurface());
2073     occlusion.enterTargetRenderSurface(parentLayer->renderSurface());
2074
2075     EXPECT_TRUE(occlusion.occluded(parentLayer.get(), IntRect(0, 0, 100, 100)));
2076     EXPECT_TRUE(occlusion.occluded(parentLayer.get(), IntRect(0, 100, 100, 100)));
2077     EXPECT_TRUE(occlusion.occluded(parentLayer.get(), IntRect(100, 0, 100, 100)));
2078     EXPECT_TRUE(occlusion.occluded(parentLayer.get(), IntRect(100, 100, 100, 100)));
2079     EXPECT_TRUE(occlusion.occluded(parentLayer.get(), IntRect(200, 100, 100, 100)));
2080     EXPECT_TRUE(occlusion.occluded(parentLayer.get(), IntRect(200, 0, 100, 100)));
2081     EXPECT_TRUE(occlusion.occluded(parentLayer.get(), IntRect(0, 200, 100, 100)));
2082     EXPECT_TRUE(occlusion.occluded(parentLayer.get(), IntRect(100, 200, 100, 100)));
2083     EXPECT_TRUE(occlusion.occluded(parentLayer.get(), IntRect(200, 200, 100, 100)));
2084
2085     occlusion.leaveToTargetRenderSurface(parent->renderSurface());
2086
2087     EXPECT_TRUE(occlusion.unoccludedContentRect(parent.get(), IntRect(0, 0, 300, 300)).isEmpty());
2088     EXPECT_TRUE(occlusion.unoccludedContentRect(parent.get(), IntRect(0, 0, 300, 100)).isEmpty());
2089     EXPECT_TRUE(occlusion.unoccludedContentRect(parent.get(), IntRect(0, 100, 300, 100)).isEmpty());
2090     EXPECT_TRUE(occlusion.unoccludedContentRect(parent.get(), IntRect(200, 100, 100, 100)).isEmpty());
2091     EXPECT_TRUE(occlusion.unoccludedContentRect(parent.get(), IntRect(100, 200, 100, 100)).isEmpty());
2092 }
2093
2094 TEST_OPAQUE_AND_PAINTED_OPAQUE(CCOcclusionTrackerTest_damageRectOverNoTiles, damageRectOverNoTiles);
2095
2096 TEST(CCOcclusionTrackerTest, opaqueContentsRegionEmpty)
2097 {
2098     const TransformationMatrix identityMatrix;
2099     RefPtr<LayerChromium> parent = LayerChromium::create();
2100     RefPtr<LayerChromiumWithForcedDrawsContent> layer = adoptRef(new LayerChromiumWithForcedDrawsContent());
2101     parent->createRenderSurface();
2102     parent->addChild(layer);
2103
2104     FilterOperations filters;
2105     filters.operations().append(BasicComponentTransferFilterOperation::create(0.5, FilterOperation::GRAYSCALE));
2106     layer->setFilters(filters);
2107
2108     setLayerPropertiesForTesting(parent.get(), identityMatrix, FloatPoint(0, 0), IntSize(300, 300));
2109     setLayerPropertiesForTesting(layer.get(), identityMatrix, FloatPoint(0, 0), IntSize(200, 200), false, false);
2110
2111     Vector<RefPtr<LayerChromium> > renderSurfaceLayerList;
2112     Vector<RefPtr<LayerChromium> > dummyLayerList;
2113     int dummyMaxTextureSize = 512;
2114
2115     parent->renderSurface()->setContentRect(IntRect(IntPoint::zero(), parent->bounds()));
2116     parent->setClipRect(IntRect(IntPoint::zero(), parent->bounds()));
2117     renderSurfaceLayerList.append(parent);
2118
2119     CCLayerTreeHostCommon::calculateDrawTransformsAndVisibility(parent.get(), parent.get(), identityMatrix, identityMatrix, renderSurfaceLayerList, dummyLayerList, dummyMaxTextureSize);
2120
2121     TestCCOcclusionTracker occlusion(IntRect(0, 0, 1000, 1000));
2122
2123     occlusion.enterTargetRenderSurface(layer->renderSurface());
2124
2125     EXPECT_FALSE(occlusion.occluded(layer.get(), IntRect(0, 0, 100, 100)));
2126     EXPECT_FALSE(occlusion.occluded(layer.get(), IntRect(100, 0, 100, 100)));
2127     EXPECT_FALSE(occlusion.occluded(layer.get(), IntRect(0, 100, 100, 100)));
2128     EXPECT_FALSE(occlusion.occluded(layer.get(), IntRect(100, 100, 100, 100)));
2129
2130     // Occluded since its outside the surface bounds.
2131     EXPECT_TRUE(occlusion.occluded(layer.get(), IntRect(200, 100, 100, 100)));
2132
2133     // Test without any scissors.
2134     occlusion.setLayerScissorRect(IntRect(0, 0, 1000, 1000));
2135     EXPECT_FALSE(occlusion.occluded(layer.get(), IntRect(200, 100, 100, 100)));
2136     occlusion.useDefaultLayerScissorRect();
2137
2138     occlusion.markOccludedBehindLayer(layer.get());
2139     occlusion.leaveToTargetRenderSurface(parent->renderSurface());
2140
2141     EXPECT_TRUE(occlusion.occlusionInScreenSpace().bounds().isEmpty());
2142     EXPECT_EQ(0u, occlusion.occlusionInScreenSpace().rects().size());
2143 }
2144
2145 TEST(CCOcclusionTrackerTest, opaqueContentsRegionNonEmpty)
2146 {
2147     const TransformationMatrix identityMatrix;
2148     RefPtr<LayerChromium> parent = LayerChromium::create();
2149     RefPtr<LayerChromiumWithForcedDrawsContent> layer = adoptRef(new LayerChromiumWithForcedDrawsContent());
2150     parent->createRenderSurface();
2151     parent->addChild(layer);
2152
2153     setLayerPropertiesForTesting(parent.get(), identityMatrix, FloatPoint(0, 0), IntSize(300, 300));
2154     setLayerPropertiesForTesting(layer.get(), identityMatrix, FloatPoint(100, 100), IntSize(200, 200), false, false);
2155
2156     Vector<RefPtr<LayerChromium> > renderSurfaceLayerList;
2157     Vector<RefPtr<LayerChromium> > dummyLayerList;
2158     int dummyMaxTextureSize = 512;
2159
2160     parent->renderSurface()->setContentRect(IntRect(IntPoint::zero(), parent->bounds()));
2161     parent->setClipRect(IntRect(IntPoint::zero(), parent->bounds()));
2162     renderSurfaceLayerList.append(parent);
2163
2164     CCLayerTreeHostCommon::calculateDrawTransformsAndVisibility(parent.get(), parent.get(), identityMatrix, identityMatrix, renderSurfaceLayerList, dummyLayerList, dummyMaxTextureSize);
2165
2166     {
2167         TestCCOcclusionTracker occlusion(IntRect(0, 0, 1000, 1000));
2168         layer->setOpaquePaintRect(IntRect(0, 0, 100, 100));
2169
2170         occlusion.enterTargetRenderSurface(parent->renderSurface());
2171         occlusion.markOccludedBehindLayer(layer.get());
2172
2173         EXPECT_EQ_RECT(IntRect(100, 100, 100, 100), occlusion.occlusionInScreenSpace().bounds());
2174         EXPECT_EQ(1u, occlusion.occlusionInScreenSpace().rects().size());
2175
2176         EXPECT_FALSE(occlusion.occluded(parent.get(), IntRect(0, 100, 100, 100)));
2177         EXPECT_TRUE(occlusion.occluded(parent.get(), IntRect(100, 100, 100, 100)));
2178         EXPECT_FALSE(occlusion.occluded(parent.get(), IntRect(200, 200, 100, 100)));
2179     }
2180
2181     {
2182         TestCCOcclusionTracker occlusion(IntRect(0, 0, 1000, 1000));
2183         layer->setOpaquePaintRect(IntRect(20, 20, 180, 180));
2184
2185         occlusion.enterTargetRenderSurface(parent->renderSurface());
2186         occlusion.markOccludedBehindLayer(layer.get());
2187
2188         EXPECT_EQ_RECT(IntRect(120, 120, 180, 180), occlusion.occlusionInScreenSpace().bounds());
2189         EXPECT_EQ(1u, occlusion.occlusionInScreenSpace().rects().size());
2190
2191         EXPECT_FALSE(occlusion.occluded(parent.get(), IntRect(0, 100, 100, 100)));
2192         EXPECT_FALSE(occlusion.occluded(parent.get(), IntRect(100, 100, 100, 100)));
2193         EXPECT_TRUE(occlusion.occluded(parent.get(), IntRect(200, 200, 100, 100)));
2194     }
2195
2196     {
2197         TestCCOcclusionTracker occlusion(IntRect(0, 0, 1000, 1000));
2198         layer->setOpaquePaintRect(IntRect(150, 150, 100, 100));
2199
2200         occlusion.enterTargetRenderSurface(parent->renderSurface());
2201         occlusion.markOccludedBehindLayer(layer.get());
2202
2203         EXPECT_EQ_RECT(IntRect(250, 250, 50, 50), occlusion.occlusionInScreenSpace().bounds());
2204         EXPECT_EQ(1u, occlusion.occlusionInScreenSpace().rects().size());
2205
2206         EXPECT_FALSE(occlusion.occluded(parent.get(), IntRect(0, 100, 100, 100)));
2207         EXPECT_FALSE(occlusion.occluded(parent.get(), IntRect(100, 100, 100, 100)));
2208         EXPECT_FALSE(occlusion.occluded(parent.get(), IntRect(200, 200, 100, 100)));
2209     }
2210 }
2211
2212 } // namespace