https://bugs.webkit.org/show_bug.cgi?id=81617 fast/repaint/canvas-putImageData.html...
[WebKit-https.git] / Source / WebKit / chromium / tests / TiledLayerChromiumTest.cpp
1 /*
2  * Copyright (C) 2011 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 "TiledLayerChromium.h"
28
29 #include "CCLayerTreeTestCommon.h"
30 #include "FakeCCLayerTreeHostClient.h"
31 #include "LayerTextureUpdater.h"
32 #include "Region.h"
33 #include "TextureManager.h"
34 #include "WebCompositor.h"
35 #include "cc/CCSingleThreadProxy.h" // For DebugScopedSetImplThread
36 #include "cc/CCTextureUpdater.h"
37 #include "cc/CCTiledLayerImpl.h"
38 #include <gtest/gtest.h>
39
40 using namespace WebCore;
41 using namespace WTF;
42
43 #define EXPECT_EQ_RECT(a, b) \
44     EXPECT_EQ(a.x(), b.x()); \
45     EXPECT_EQ(a.y(), b.y()); \
46     EXPECT_EQ(a.width(), b.width()); \
47     EXPECT_EQ(a.height(), b.height());
48
49 namespace {
50
51 class TestCCOcclusionTracker : public CCOcclusionTracker {
52 public:
53     TestCCOcclusionTracker()
54         : CCOcclusionTracker(IntRect(0, 0, 1000, 1000))
55         , m_scissorRectInScreen(IntRect(0, 0, 1000, 1000))
56     {
57         // Pretend we have visited a render surface.
58         m_stack.append(StackObject());
59     }
60
61     void setOcclusion(const Region& occlusion) { m_stack.last().occlusionInScreen = occlusion; }
62
63 protected:
64     virtual IntRect layerScissorRectInTargetSurface(const LayerChromium* layer) const { return m_scissorRectInScreen; }
65
66 private:
67     IntRect m_scissorRectInScreen;
68 };
69
70 class FakeTextureAllocator : public TextureAllocator {
71 public:
72     virtual unsigned createTexture(const IntSize&, GC3Denum) { return 0; }
73     virtual void deleteTexture(unsigned, const IntSize&, GC3Denum) { }
74 };
75
76 class FakeTiledLayerChromium;
77
78 class FakeLayerTextureUpdater : public LayerTextureUpdater {
79 public:
80     class Texture : public LayerTextureUpdater::Texture {
81     public:
82         Texture(FakeLayerTextureUpdater* layer, PassOwnPtr<ManagedTexture> texture)
83             : LayerTextureUpdater::Texture(texture)
84             , m_layer(layer)
85         {
86         }
87         virtual ~Texture() { }
88
89         virtual void updateRect(GraphicsContext3D*, TextureAllocator*, const IntRect&, const IntRect&) { m_layer->updateRect(); }
90         virtual void prepareRect(const IntRect&) { m_layer->prepareRect(); }
91
92     private:
93         FakeLayerTextureUpdater* m_layer;
94     };
95
96     FakeLayerTextureUpdater()
97         : m_prepareCount(0)
98         , m_updateCount(0)
99         , m_prepareRectCount(0)
100     {
101     }
102     virtual ~FakeLayerTextureUpdater() { }
103
104     // Sets the rect to invalidate during the next call to prepareToUpdate(). After the next
105     // call to prepareToUpdate() the rect is reset.
106     void setRectToInvalidate(const IntRect&, FakeTiledLayerChromium*);
107
108     // Number of times prepareToUpdate has been invoked.
109     int prepareCount() const { return m_prepareCount; }
110     void clearPrepareCount() { m_prepareCount = 0; }
111
112     // Number of times updateRect has been invoked.
113     int updateCount() const { return m_updateCount; }
114     void clearUpdateCount() { m_updateCount = 0; }
115     void updateRect() { m_updateCount++; }
116
117     // Number of times prepareRect() has been invoked on a texture.
118     int prepareRectCount() const { return m_prepareRectCount; }
119     void clearPrepareRectCount() { m_prepareRectCount = 0; }
120     void prepareRect() { m_prepareRectCount++; }
121
122     void setOpaquePaintRect(const IntRect& opaquePaintRect) { m_opaquePaintRect = opaquePaintRect; }
123
124     // Last rect passed to prepareToUpdate().
125     const IntRect& lastUpdateRect()  const { return m_lastUpdateRect; }
126
127     virtual PassOwnPtr<LayerTextureUpdater::Texture> createTexture(TextureManager* manager) { return adoptPtr(new Texture(this, ManagedTexture::create(manager))); }
128     virtual SampledTexelFormat sampledTexelFormat(GC3Denum) { return SampledTexelFormatRGBA; }
129     virtual void prepareToUpdate(const IntRect& contentRect, const IntSize&, int, float, IntRect* resultingOpaqueRect);
130
131 private:
132     int m_prepareCount;
133     int m_updateCount;
134     int m_prepareRectCount;
135     IntRect m_rectToInvalidate;
136     IntRect m_lastUpdateRect;
137     IntRect m_opaquePaintRect;
138     RefPtr<FakeTiledLayerChromium> m_layer;
139 };
140
141 class FakeCCTiledLayerImpl : public CCTiledLayerImpl {
142 public:
143     explicit FakeCCTiledLayerImpl(int id)
144         : CCTiledLayerImpl(id) { }
145     virtual ~FakeCCTiledLayerImpl() { }
146
147     bool hasTileAt(int i, int j)
148     {
149         return CCTiledLayerImpl::hasTileAt(i, j);
150     }
151 };
152
153 class FakeTiledLayerChromium : public TiledLayerChromium {
154 public:
155     explicit FakeTiledLayerChromium(TextureManager* textureManager)
156         : TiledLayerChromium()
157         , m_fakeTextureUpdater(adoptRef(new FakeLayerTextureUpdater))
158         , m_textureManager(textureManager)
159     {
160         setTileSize(IntSize(100, 100));
161         setTextureFormat(GraphicsContext3D::RGBA);
162         setBorderTexelOption(CCLayerTilingData::NoBorderTexels);
163         setIsDrawable(true); // So that we don't get false positives if any of these tests expect to return false from drawsContent() for other reasons.
164     }
165     virtual ~FakeTiledLayerChromium() { }
166
167     void invalidateRect(const IntRect& rect)
168     {
169         TiledLayerChromium::invalidateRect(rect);
170     }
171
172     void prepareToUpdate(const IntRect& rect, const CCOcclusionTracker* occlusion)
173     {
174         TiledLayerChromium::prepareToUpdate(rect, occlusion);
175     }
176
177     void prepareToUpdateIdle(const IntRect& rect, const CCOcclusionTracker* occlusion)
178     {
179         TiledLayerChromium::prepareToUpdateIdle(rect, occlusion);
180     }
181
182     bool needsIdlePaint(const IntRect& rect)
183     {
184         return TiledLayerChromium::needsIdlePaint(rect);
185     }
186
187     bool skipsDraw() const
188     {
189         return TiledLayerChromium::skipsDraw();
190     }
191
192     virtual void setNeedsDisplayRect(const FloatRect& rect)
193     {
194         m_lastNeedsDisplayRect = rect;
195         TiledLayerChromium::setNeedsDisplayRect(rect);
196     }
197
198     const FloatRect& lastNeedsDisplayRect() const { return m_lastNeedsDisplayRect; }
199
200     FakeLayerTextureUpdater* fakeLayerTextureUpdater() { return m_fakeTextureUpdater.get(); }
201
202     virtual TextureManager* textureManager() const { return m_textureManager; }
203
204     virtual void paintContentsIfDirty(const CCOcclusionTracker* occlusion)
205     {
206         prepareToUpdate(visibleLayerRect(), occlusion);
207     }
208
209 private:
210     virtual LayerTextureUpdater* textureUpdater() const
211     {
212         return m_fakeTextureUpdater.get();
213     }
214
215     virtual void createTextureUpdaterIfNeeded() { }
216
217     RefPtr<FakeLayerTextureUpdater> m_fakeTextureUpdater;
218     TextureManager* m_textureManager;
219     FloatRect m_lastNeedsDisplayRect;
220 };
221
222 class FakeTiledLayerWithScaledBounds : public FakeTiledLayerChromium {
223 public:
224     explicit FakeTiledLayerWithScaledBounds(TextureManager* textureManager)
225         : FakeTiledLayerChromium(textureManager)
226     {
227     }
228
229     void setContentBounds(const IntSize& contentBounds) { m_forcedContentBounds = contentBounds; }
230     virtual IntSize contentBounds() const { return m_forcedContentBounds; }
231
232     FloatRect updateRect() { return m_updateRect; }
233
234 protected:
235     IntSize m_forcedContentBounds;
236 };
237
238 void FakeLayerTextureUpdater::setRectToInvalidate(const IntRect& rect, FakeTiledLayerChromium* layer)
239 {
240     m_rectToInvalidate = rect;
241     m_layer = layer;
242 }
243
244 void FakeLayerTextureUpdater::prepareToUpdate(const IntRect& contentRect, const IntSize&, int, float, IntRect* resultingOpaqueRect)
245 {
246     m_prepareCount++;
247     m_lastUpdateRect = contentRect;
248     if (!m_rectToInvalidate.isEmpty()) {
249         m_layer->invalidateRect(m_rectToInvalidate);
250         m_rectToInvalidate = IntRect();
251         m_layer = 0;
252     }
253     *resultingOpaqueRect = m_opaquePaintRect;
254 }
255
256 TEST(TiledLayerChromiumTest, pushDirtyTiles)
257 {
258     OwnPtr<TextureManager> textureManager = TextureManager::create(4*1024*1024, 2*1024*1024, 1024);
259     RefPtr<FakeTiledLayerChromium> layer = adoptRef(new FakeTiledLayerChromium(textureManager.get()));
260     DebugScopedSetImplThread implThread;
261     OwnPtr<FakeCCTiledLayerImpl> layerImpl(adoptPtr(new FakeCCTiledLayerImpl(0)));
262
263     FakeTextureAllocator textureAllocator;
264     CCTextureUpdater updater(&textureAllocator);
265
266     // The tile size is 100x100, so this invalidates and then paints two tiles.
267     layer->setBounds(IntSize(100, 200));
268     layer->invalidateRect(IntRect(0, 0, 100, 200));
269     layer->prepareToUpdate(IntRect(0, 0, 100, 200), 0);
270     layer->updateCompositorResources(0, updater);
271     layer->pushPropertiesTo(layerImpl.get());
272
273     // We should have both tiles on the impl side.
274     EXPECT_TRUE(layerImpl->hasTileAt(0, 0));
275     EXPECT_TRUE(layerImpl->hasTileAt(0, 1));
276
277     textureManager->unprotectAllTextures();
278
279     // Invalidates both tiles...
280     layer->invalidateRect(IntRect(0, 0, 100, 200));
281     // ....but then only update one of them.
282     layer->prepareToUpdate(IntRect(0, 0, 100, 100), 0);
283     layer->updateCompositorResources(0, updater);
284     layer->pushPropertiesTo(layerImpl.get());
285
286     // We should only have the first tile since the other tile was invalidated but not painted.
287     EXPECT_TRUE(layerImpl->hasTileAt(0, 0));
288     EXPECT_FALSE(layerImpl->hasTileAt(0, 1));
289 }
290
291 TEST(TiledLayerChromiumTest, pushOccludedDirtyTiles)
292 {
293     OwnPtr<TextureManager> textureManager = TextureManager::create(4*1024*1024, 2*1024*1024, 1024);
294     RefPtr<FakeTiledLayerChromium> layer = adoptRef(new FakeTiledLayerChromium(textureManager.get()));
295     DebugScopedSetImplThread implThread;
296     OwnPtr<FakeCCTiledLayerImpl> layerImpl(adoptPtr(new FakeCCTiledLayerImpl(0)));
297     TestCCOcclusionTracker occluded;
298
299     FakeTextureAllocator textureAllocator;
300     CCTextureUpdater updater(&textureAllocator);
301
302     // The tile size is 100x100, so this invalidates and then paints two tiles.
303     layer->setBounds(IntSize(100, 200));
304     layer->setDrawTransform(TransformationMatrix(1, 0, 0, 1, layer->bounds().width() / 2.0, layer->bounds().height() / 2.0));
305     layer->setVisibleLayerRect(IntRect(0, 0, 100, 200));
306     layer->invalidateRect(IntRect(0, 0, 100, 200));
307     layer->prepareToUpdate(IntRect(0, 0, 100, 200), &occluded);
308     layer->updateCompositorResources(0, updater);
309     layer->pushPropertiesTo(layerImpl.get());
310
311     EXPECT_NEAR(occluded.overdrawMetrics().pixelsDrawnOpaque(), 0, 1);
312     EXPECT_NEAR(occluded.overdrawMetrics().pixelsDrawnTranslucent(), 20000, 1);
313     EXPECT_NEAR(occluded.overdrawMetrics().pixelsCulled(), 0, 1);
314
315     // We should have both tiles on the impl side.
316     EXPECT_TRUE(layerImpl->hasTileAt(0, 0));
317     EXPECT_TRUE(layerImpl->hasTileAt(0, 1));
318
319     textureManager->unprotectAllTextures();
320
321     // Invalidates part of the top tile...
322     layer->invalidateRect(IntRect(0, 0, 50, 50));
323     // ....but the area is occluded.
324     occluded.setOcclusion(IntRect(0, 0, 50, 50));
325     layer->prepareToUpdate(IntRect(0, 0, 100, 100), &occluded);
326     layer->updateCompositorResources(0, updater);
327     layer->pushPropertiesTo(layerImpl.get());
328
329     EXPECT_NEAR(occluded.overdrawMetrics().pixelsDrawnOpaque(), 0, 1);
330     EXPECT_NEAR(occluded.overdrawMetrics().pixelsDrawnTranslucent(), 20000 + 2500, 1);
331     EXPECT_NEAR(occluded.overdrawMetrics().pixelsCulled(), 0, 1);
332
333     // We should still have both tiles, as part of the top tile is still unoccluded.
334     EXPECT_TRUE(layerImpl->hasTileAt(0, 0));
335     EXPECT_TRUE(layerImpl->hasTileAt(0, 1));
336 }
337
338 TEST(TiledLayerChromiumTest, pushDeletedTiles)
339 {
340     OwnPtr<TextureManager> textureManager = TextureManager::create(4*1024*1024, 2*1024*1024, 1024);
341     RefPtr<FakeTiledLayerChromium> layer = adoptRef(new FakeTiledLayerChromium(textureManager.get()));
342     DebugScopedSetImplThread implThread;
343     OwnPtr<FakeCCTiledLayerImpl> layerImpl(adoptPtr(new FakeCCTiledLayerImpl(0)));
344
345     FakeTextureAllocator textureAllocator;
346     CCTextureUpdater updater(&textureAllocator);
347
348     // The tile size is 100x100, so this invalidates and then paints two tiles.
349     layer->setBounds(IntSize(100, 200));
350     layer->invalidateRect(IntRect(0, 0, 100, 200));
351     layer->prepareToUpdate(IntRect(0, 0, 100, 200), 0);
352     layer->updateCompositorResources(0, updater);
353     layer->pushPropertiesTo(layerImpl.get());
354
355     // We should have both tiles on the impl side.
356     EXPECT_TRUE(layerImpl->hasTileAt(0, 0));
357     EXPECT_TRUE(layerImpl->hasTileAt(0, 1));
358
359     textureManager->evictAndDeleteAllTextures(&textureAllocator);
360     textureManager->setMaxMemoryLimitBytes(4*1024*1024);
361     textureManager->setPreferredMemoryLimitBytes(4*1024*1024);
362
363     // This should drop the tiles on the impl thread.
364     layer->pushPropertiesTo(layerImpl.get());
365
366     // We should now have no textures on the impl thread.
367     EXPECT_FALSE(layerImpl->hasTileAt(0, 0));
368     EXPECT_FALSE(layerImpl->hasTileAt(0, 1));
369
370     // This should recreate and update the deleted textures.
371     layer->prepareToUpdate(IntRect(0, 0, 100, 100), 0);
372     layer->updateCompositorResources(0, updater);
373     layer->pushPropertiesTo(layerImpl.get());
374
375     // We should only have the first tile since the other tile was invalidated but not painted.
376     EXPECT_TRUE(layerImpl->hasTileAt(0, 0));
377     EXPECT_FALSE(layerImpl->hasTileAt(0, 1));
378 }
379
380 TEST(TiledLayerChromiumTest, pushIdlePaintTiles)
381 {
382     OwnPtr<TextureManager> textureManager = TextureManager::create(4*1024*1024, 2*1024*1024, 1024);
383     RefPtr<FakeTiledLayerChromium> layer = adoptRef(new FakeTiledLayerChromium(textureManager.get()));
384     DebugScopedSetImplThread implThread;
385     OwnPtr<FakeCCTiledLayerImpl> layerImpl(adoptPtr(new FakeCCTiledLayerImpl(0)));
386
387     FakeTextureAllocator textureAllocator;
388     CCTextureUpdater updater(&textureAllocator);
389
390     // The tile size is 100x100. Setup 5x5 tiles with one visible tile in the center.
391     IntSize contentBounds(500, 500);
392     IntRect contentRect(IntPoint::zero(), contentBounds);
393     IntRect visibleRect(200, 200, 100, 100);
394
395     // This invalidates 25 tiles and then paints one visible tile.
396     layer->setBounds(contentBounds);
397     layer->setVisibleLayerRect(visibleRect);
398     layer->invalidateRect(contentRect);
399     layer->prepareToUpdate(visibleRect, 0);
400
401     // We should need idle-painting for 3x3 tiles in the center.
402     EXPECT_TRUE(layer->needsIdlePaint(visibleRect));
403
404     layer->updateCompositorResources(0, updater);
405     layer->pushPropertiesTo(layerImpl.get());
406
407     // We should have one tile on the impl side.
408     EXPECT_TRUE(layerImpl->hasTileAt(2, 2));
409
410     textureManager->unprotectAllTextures();
411
412     // For the next four updates, we should detect we still need idle painting.
413     for (int i = 0; i < 4; i++) {
414         layer->prepareToUpdate(visibleRect, 0);
415         EXPECT_TRUE(layer->needsIdlePaint(visibleRect));
416         layer->prepareToUpdateIdle(visibleRect, 0);
417         layer->updateCompositorResources(0, updater);
418         layer->pushPropertiesTo(layerImpl.get());
419         textureManager->unprotectAllTextures();
420     }
421
422     // After four passes of idle painting, we should be finished painting
423     EXPECT_FALSE(layer->needsIdlePaint(visibleRect));
424
425     // We should have one tile surrounding the visible tile on all sides, but no other tiles.
426     IntRect idlePaintTiles(1, 1, 3, 3);
427     for (int i = 0; i < 5; i++) {
428         for (int j = 0; j < 5; j++) {
429             if (idlePaintTiles.contains(i, j))
430                 EXPECT_TRUE(layerImpl->hasTileAt(i, j));
431             else
432                 EXPECT_FALSE(layerImpl->hasTileAt(i, j));
433         }
434     }
435 }
436
437 TEST(TiledLayerChromiumTest, pushTilesMarkedDirtyDuringPaint)
438 {
439     OwnPtr<TextureManager> textureManager = TextureManager::create(4*1024*1024, 2*1024*1024, 1024);
440     RefPtr<FakeTiledLayerChromium> layer = adoptRef(new FakeTiledLayerChromium(textureManager.get()));
441     DebugScopedSetImplThread implThread;
442     OwnPtr<FakeCCTiledLayerImpl> layerImpl(adoptPtr(new FakeCCTiledLayerImpl(0)));
443
444     FakeTextureAllocator textureAllocator;
445     CCTextureUpdater updater(&textureAllocator);
446
447     // The tile size is 100x100, so this invalidates and then paints two tiles.
448     // However, during the paint, we invalidate one of the tiles. This should
449     // not prevent the tile from being pushed.
450     layer->setBounds(IntSize(100, 200));
451     layer->invalidateRect(IntRect(0, 0, 100, 200));
452     layer->fakeLayerTextureUpdater()->setRectToInvalidate(IntRect(0, 50, 100, 50), layer.get());
453     layer->prepareToUpdate(IntRect(0, 0, 100, 200), 0);
454     layer->updateCompositorResources(0, updater);
455     layer->pushPropertiesTo(layerImpl.get());
456
457     // We should have both tiles on the impl side.
458     EXPECT_TRUE(layerImpl->hasTileAt(0, 0));
459     EXPECT_TRUE(layerImpl->hasTileAt(0, 1));
460 }
461
462 TEST(TiledLayerChromiumTest, pushTilesLayerMarkedDirtyDuringPaintOnNextLayer)
463 {
464     OwnPtr<TextureManager> textureManager = TextureManager::create(4*1024*1024, 2*1024*1024, 1024);
465     RefPtr<FakeTiledLayerChromium> layer1 = adoptRef(new FakeTiledLayerChromium(textureManager.get()));
466     RefPtr<FakeTiledLayerChromium> layer2 = adoptRef(new FakeTiledLayerChromium(textureManager.get()));
467     DebugScopedSetImplThread implThread;
468     OwnPtr<FakeCCTiledLayerImpl> layer1Impl(adoptPtr(new FakeCCTiledLayerImpl(0)));
469     OwnPtr<FakeCCTiledLayerImpl> layer2Impl(adoptPtr(new FakeCCTiledLayerImpl(0)));
470
471     FakeTextureAllocator textureAllocator;
472     CCTextureUpdater updater(&textureAllocator);
473
474     layer1->setBounds(IntSize(100, 200));
475     layer1->invalidateRect(IntRect(0, 0, 100, 200));
476     layer2->setBounds(IntSize(100, 200));
477     layer2->invalidateRect(IntRect(0, 0, 100, 200));
478
479     layer1->prepareToUpdate(IntRect(0, 0, 100, 200), 0);
480
481     // Invalidate a tile on layer1
482     layer2->fakeLayerTextureUpdater()->setRectToInvalidate(IntRect(0, 50, 100, 50), layer1.get());
483     layer2->prepareToUpdate(IntRect(0, 0, 100, 200), 0);
484
485     layer1->updateCompositorResources(0, updater);
486     layer2->updateCompositorResources(0, updater);
487
488     layer1->pushPropertiesTo(layer1Impl.get());
489     layer2->pushPropertiesTo(layer2Impl.get());
490
491     // We should have both tiles on the impl side for all layers.
492     EXPECT_TRUE(layer1Impl->hasTileAt(0, 0));
493     EXPECT_TRUE(layer1Impl->hasTileAt(0, 1));
494     EXPECT_TRUE(layer2Impl->hasTileAt(0, 0));
495     EXPECT_TRUE(layer2Impl->hasTileAt(0, 1));
496 }
497
498 TEST(TiledLayerChromiumTest, pushTilesLayerMarkedDirtyDuringPaintOnPreviousLayer)
499 {
500     OwnPtr<TextureManager> textureManager = TextureManager::create(4*1024*1024, 2*1024*1024, 1024);
501     RefPtr<FakeTiledLayerChromium> layer1 = adoptRef(new FakeTiledLayerChromium(textureManager.get()));
502     RefPtr<FakeTiledLayerChromium> layer2 = adoptRef(new FakeTiledLayerChromium(textureManager.get()));
503     DebugScopedSetImplThread implThread;
504     OwnPtr<FakeCCTiledLayerImpl> layer1Impl(adoptPtr(new FakeCCTiledLayerImpl(0)));
505     OwnPtr<FakeCCTiledLayerImpl> layer2Impl(adoptPtr(new FakeCCTiledLayerImpl(0)));
506
507     FakeTextureAllocator textureAllocator;
508     CCTextureUpdater updater(&textureAllocator);
509
510     layer1->setBounds(IntSize(100, 200));
511     layer1->invalidateRect(IntRect(0, 0, 100, 200));
512     layer2->setBounds(IntSize(100, 200));
513     layer2->invalidateRect(IntRect(0, 0, 100, 200));
514
515     // Invalidate a tile on layer2
516     layer1->fakeLayerTextureUpdater()->setRectToInvalidate(IntRect(0, 50, 100, 50), layer2.get());
517     layer1->prepareToUpdate(IntRect(0, 0, 100, 200), 0);
518
519     layer2->prepareToUpdate(IntRect(0, 0, 100, 200), 0);
520
521     layer1->updateCompositorResources(0, updater);
522     layer2->updateCompositorResources(0, updater);
523
524     layer1->pushPropertiesTo(layer1Impl.get());
525     layer2->pushPropertiesTo(layer2Impl.get());
526
527     // We should have both tiles on the impl side for all layers.
528     EXPECT_TRUE(layer1Impl->hasTileAt(0, 0));
529     EXPECT_TRUE(layer1Impl->hasTileAt(0, 1));
530     EXPECT_TRUE(layer2Impl->hasTileAt(0, 0));
531     EXPECT_TRUE(layer2Impl->hasTileAt(0, 1));
532 }
533
534 TEST(TiledLayerChromiumTest, idlePaintOutOfMemory)
535 {
536     // The tile size is 100x100. Setup 5x5 tiles with one 1x1 visible tile in the center.
537     IntSize contentBounds(300, 300);
538     IntRect contentRect(IntPoint::zero(), contentBounds);
539     IntRect visibleRect(100, 100, 100, 100);
540
541     // We have enough memory for only the visible rect, so we will run out of memory in first idle paint.
542     int memoryLimit = 4 * 100 * 100; // 2 tiles, 4 bytes per pixel.
543
544     OwnPtr<TextureManager> textureManager = TextureManager::create(memoryLimit, memoryLimit / 2, 1024);
545     RefPtr<FakeTiledLayerChromium> layer = adoptRef(new FakeTiledLayerChromium(textureManager.get()));
546     DebugScopedSetImplThread implThread;
547     OwnPtr<FakeCCTiledLayerImpl> layerImpl(adoptPtr(new FakeCCTiledLayerImpl(0)));
548
549     FakeTextureAllocator textureAllocator;
550     CCTextureUpdater updater(&textureAllocator);
551
552     // This invalidates 9 tiles and then paints one visible tile.
553     layer->setBounds(contentBounds);
554     layer->setVisibleLayerRect(visibleRect);
555     layer->invalidateRect(contentRect);
556     layer->prepareToUpdate(visibleRect, 0);
557
558     // We should need idle-painting for 3x3 tiles surounding visible tile.
559     EXPECT_TRUE(layer->needsIdlePaint(visibleRect));
560
561     layer->updateCompositorResources(0, updater);
562     layer->pushPropertiesTo(layerImpl.get());
563
564     // We should have one tile on the impl side.
565     EXPECT_TRUE(layerImpl->hasTileAt(1, 1));
566
567     textureManager->unprotectAllTextures();
568     layer->prepareToUpdate(visibleRect, 0);
569     layer->prepareToUpdateIdle(visibleRect, 0);
570
571     // We shouldn't signal we need another idle paint after we run out of memory.
572     EXPECT_FALSE(layer->needsIdlePaint(visibleRect));
573
574     layer->updateCompositorResources(0, updater);
575     layer->pushPropertiesTo(layerImpl.get());
576 }
577
578 TEST(TiledLayerChromiumTest, invalidateFromPrepare)
579 {
580     OwnPtr<TextureManager> textureManager = TextureManager::create(4*1024*1024, 2*1024*1024, 1024);
581     RefPtr<FakeTiledLayerChromium> layer = adoptRef(new FakeTiledLayerChromium(textureManager.get()));
582     DebugScopedSetImplThread implThread;
583     OwnPtr<FakeCCTiledLayerImpl> layerImpl(adoptPtr(new FakeCCTiledLayerImpl(0)));
584
585     FakeTextureAllocator textureAllocator;
586     CCTextureUpdater updater(&textureAllocator);
587
588     // The tile size is 100x100, so this invalidates and then paints two tiles.
589     layer->setBounds(IntSize(100, 200));
590     layer->invalidateRect(IntRect(0, 0, 100, 200));
591     layer->prepareToUpdate(IntRect(0, 0, 100, 200), 0);
592     layer->updateCompositorResources(0, updater);
593     layer->pushPropertiesTo(layerImpl.get());
594
595     // We should have both tiles on the impl side.
596     EXPECT_TRUE(layerImpl->hasTileAt(0, 0));
597     EXPECT_TRUE(layerImpl->hasTileAt(0, 1));
598
599     textureManager->unprotectAllTextures();
600
601     layer->fakeLayerTextureUpdater()->clearPrepareCount();
602     // Invoke prepareToUpdate again. As the layer is valid prepareToUpdate shouldn't be invoked on
603     // the LayerTextureUpdater.
604     layer->prepareToUpdate(IntRect(0, 0, 100, 200), 0);
605     EXPECT_EQ(0, layer->fakeLayerTextureUpdater()->prepareCount());
606
607     layer->invalidateRect(IntRect(0, 0, 50, 50));
608     // setRectToInvalidate triggers invalidateRect() being invoked from prepareToUpdate.
609     layer->fakeLayerTextureUpdater()->setRectToInvalidate(IntRect(25, 25, 50, 50), layer.get());
610     layer->fakeLayerTextureUpdater()->clearPrepareCount();
611     layer->prepareToUpdate(IntRect(0, 0, 100, 200), 0);
612     EXPECT_EQ(1, layer->fakeLayerTextureUpdater()->prepareCount());
613     layer->fakeLayerTextureUpdater()->clearPrepareCount();
614     // The layer should still be invalid as prepareToUpdate invoked invalidate.
615     layer->prepareToUpdate(IntRect(0, 0, 100, 200), 0);
616     EXPECT_EQ(1, layer->fakeLayerTextureUpdater()->prepareCount());
617 }
618
619 TEST(TiledLayerChromiumTest, verifyUpdateRectWhenContentBoundsAreScaled)
620 {
621     // The updateRect (that indicates what was actually painted) should be in
622     // layer space, not the content space.
623
624     OwnPtr<TextureManager> textureManager = TextureManager::create(4*1024*1024, 2*1024*1024, 1024);
625     RefPtr<FakeTiledLayerWithScaledBounds> layer = adoptRef(new FakeTiledLayerWithScaledBounds(textureManager.get()));
626
627     FakeTextureAllocator textureAllocator;
628     CCTextureUpdater updater(&textureAllocator);
629
630     IntRect layerBounds(0, 0, 300, 200);
631     IntRect contentBounds(0, 0, 200, 250);
632
633     layer->setBounds(layerBounds.size());
634     layer->setContentBounds(contentBounds.size());
635     layer->setVisibleLayerRect(contentBounds);
636
637     // On first update, the updateRect includes all tiles, even beyond the boundaries of the layer.
638     // However, it should still be in layer space, not content space.
639     layer->invalidateRect(contentBounds);
640     layer->prepareToUpdate(contentBounds, 0);
641     layer->updateCompositorResources(0, updater);
642     EXPECT_FLOAT_RECT_EQ(FloatRect(0, 0, 300, 300 * 0.8), layer->updateRect());
643
644     // After the tiles are updated once, another invalidate only needs to update the bounds of the layer.
645     layer->invalidateRect(contentBounds);
646     layer->prepareToUpdate(contentBounds, 0);
647     layer->updateCompositorResources(0, updater);
648     EXPECT_FLOAT_RECT_EQ(FloatRect(layerBounds), layer->updateRect());
649
650     // Partial re-paint should also be represented by the updateRect in layer space, not content space.
651     IntRect partialDamage(30, 100, 10, 10);
652     layer->invalidateRect(partialDamage);
653     layer->prepareToUpdate(contentBounds, 0);
654     layer->updateCompositorResources(0, updater);
655     EXPECT_FLOAT_RECT_EQ(FloatRect(45, 80, 15, 8), layer->updateRect());
656 }
657
658 TEST(TiledLayerChromiumTest, verifyInvalidationWhenContentsScaleChanges)
659 {
660     OwnPtr<TextureManager> textureManager = TextureManager::create(4*1024*1024, 2*1024*1024, 1024);
661     RefPtr<FakeTiledLayerChromium> layer = adoptRef(new FakeTiledLayerChromium(textureManager.get()));
662     DebugScopedSetImplThread implThread;
663     OwnPtr<FakeCCTiledLayerImpl> layerImpl(adoptPtr(new FakeCCTiledLayerImpl(0)));
664
665     FakeTextureAllocator textureAllocator;
666     CCTextureUpdater updater(&textureAllocator);
667
668     // Create a layer with one tile.
669     layer->setBounds(IntSize(100, 100));
670
671     // Invalidate the entire layer.
672     layer->setNeedsDisplay();
673     EXPECT_FLOAT_RECT_EQ(FloatRect(0, 0, 100, 100), layer->lastNeedsDisplayRect());
674
675     // Push the tiles to the impl side and check that there is exactly one.
676     layer->prepareToUpdate(IntRect(0, 0, 100, 100), 0);
677     layer->updateCompositorResources(0, updater);
678     layer->pushPropertiesTo(layerImpl.get());
679     EXPECT_TRUE(layerImpl->hasTileAt(0, 0));
680     EXPECT_FALSE(layerImpl->hasTileAt(0, 1));
681     EXPECT_FALSE(layerImpl->hasTileAt(1, 0));
682     EXPECT_FALSE(layerImpl->hasTileAt(1, 1));
683
684     // Change the contents scale and verify that the content rectangle requiring painting
685     // is not scaled.
686     layer->setContentsScale(2);
687     EXPECT_FLOAT_RECT_EQ(FloatRect(0, 0, 100, 100), layer->lastNeedsDisplayRect());
688
689     // The impl side should get 2x2 tiles now.
690     layer->prepareToUpdate(IntRect(0, 0, 200, 200), 0);
691     layer->updateCompositorResources(0, updater);
692     layer->pushPropertiesTo(layerImpl.get());
693     EXPECT_TRUE(layerImpl->hasTileAt(0, 0));
694     EXPECT_TRUE(layerImpl->hasTileAt(0, 1));
695     EXPECT_TRUE(layerImpl->hasTileAt(1, 0));
696     EXPECT_TRUE(layerImpl->hasTileAt(1, 1));
697
698     // Invalidate the entire layer again, but do not paint. All tiles should be gone now from the
699     // impl side.
700     layer->setNeedsDisplay();
701     layer->prepareToUpdate(IntRect(1, 0, 0, 1), 0);
702     layer->updateCompositorResources(0, updater);
703     layer->pushPropertiesTo(layerImpl.get());
704     EXPECT_FALSE(layerImpl->hasTileAt(0, 0));
705     EXPECT_FALSE(layerImpl->hasTileAt(0, 1));
706     EXPECT_FALSE(layerImpl->hasTileAt(1, 0));
707     EXPECT_FALSE(layerImpl->hasTileAt(1, 1));
708 }
709
710 TEST(TiledLayerChromiumTest, skipsDrawGetsReset)
711 {
712     // Initialize without threading support.
713     WebKit::WebCompositor::initialize(0);
714     FakeCCLayerTreeHostClient fakeCCLayerTreeHostClient;
715     RefPtr<CCLayerTreeHost> ccLayerTreeHost = CCLayerTreeHost::create(&fakeCCLayerTreeHostClient, CCSettings());
716
717     // Create two 300 x 300 tiled layers.
718     IntSize contentBounds(300, 300);
719     IntRect contentRect(IntPoint::zero(), contentBounds);
720
721     // We have enough memory for only one of the two layers.
722     int memoryLimit = 4 * 300 * 300; // 4 bytes per pixel.
723     OwnPtr<TextureManager> textureManager = TextureManager::create(memoryLimit, memoryLimit, memoryLimit);
724
725     RefPtr<FakeTiledLayerChromium> rootLayer = adoptRef(new FakeTiledLayerChromium(textureManager.get()));
726     RefPtr<FakeTiledLayerChromium> childLayer = adoptRef(new FakeTiledLayerChromium(textureManager.get()));
727     rootLayer->addChild(childLayer);
728
729     rootLayer->setBounds(contentBounds);
730     rootLayer->setPosition(FloatPoint(150, 150));
731     childLayer->setBounds(contentBounds);
732     childLayer->setPosition(FloatPoint(150, 150));
733     rootLayer->invalidateRect(contentRect);
734     childLayer->invalidateRect(contentRect);
735
736     FakeTextureAllocator textureAllocator;
737     CCTextureUpdater updater(&textureAllocator);
738
739     ccLayerTreeHost->setRootLayer(rootLayer);
740     ccLayerTreeHost->setViewportSize(IntSize(300, 300));
741     textureManager->setMaxMemoryLimitBytes(memoryLimit);
742     ccLayerTreeHost->updateLayers();
743     ccLayerTreeHost->updateCompositorResources(ccLayerTreeHost->context(), updater);
744
745     // We'll skip the root layer.
746     EXPECT_TRUE(rootLayer->skipsDraw());
747     EXPECT_FALSE(childLayer->skipsDraw());
748
749     ccLayerTreeHost->commitComplete();
750     textureManager->unprotectAllTextures(); // CCLayerTreeHost::commitComplete() normally does this, but since we're mocking out the manager we have to do it.
751
752     // Remove the child layer.
753     rootLayer->removeAllChildren();
754
755     ccLayerTreeHost->updateLayers();
756     EXPECT_FALSE(rootLayer->skipsDraw());
757
758     ccLayerTreeHost->setRootLayer(0);
759     ccLayerTreeHost.clear();
760     WebKit::WebCompositor::shutdown();
761 }
762
763 TEST(TiledLayerChromiumTest, resizeToSmaller)
764 {
765     OwnPtr<TextureManager> textureManager = TextureManager::create(60*1024*1024, 60*1024*1024, 1024);
766     RefPtr<FakeTiledLayerChromium> layer = adoptRef(new FakeTiledLayerChromium(textureManager.get()));
767
768     layer->setBounds(IntSize(700, 700));
769     layer->invalidateRect(IntRect(0, 0, 700, 700));
770     layer->prepareToUpdate(IntRect(0, 0, 700, 700), 0);
771
772     layer->setBounds(IntSize(200, 200));
773     layer->invalidateRect(IntRect(0, 0, 200, 200));
774 }
775
776 TEST(TiledLayerChromiumTest, partialUpdates)
777 {
778     CCSettings settings;
779     settings.maxPartialTextureUpdates = 4;
780     // Initialize without threading support.
781     WebKit::WebCompositor::initialize(0);
782     FakeCCLayerTreeHostClient fakeCCLayerTreeHostClient;
783     RefPtr<CCLayerTreeHost> ccLayerTreeHost = CCLayerTreeHost::create(&fakeCCLayerTreeHostClient, settings);
784
785     // Create one 500 x 300 tiled layer.
786     IntSize contentBounds(300, 200);
787     IntRect contentRect(IntPoint::zero(), contentBounds);
788
789     OwnPtr<TextureManager> textureManager = TextureManager::create(60*1024*1024, 60*1024*1024, 1024);
790     RefPtr<FakeTiledLayerChromium> layer = adoptRef(new FakeTiledLayerChromium(textureManager.get()));
791     layer->setBounds(contentBounds);
792     layer->setPosition(FloatPoint(150, 150));
793     layer->invalidateRect(contentRect);
794
795     FakeTextureAllocator textureAllocator;
796     CCTextureUpdater updater(&textureAllocator);
797
798     ccLayerTreeHost->setRootLayer(layer);
799     ccLayerTreeHost->setViewportSize(IntSize(300, 200));
800
801     // Full update of all 6 tiles.
802     ccLayerTreeHost->updateLayers();
803     ccLayerTreeHost->updateCompositorResources(ccLayerTreeHost->context(), updater);
804     updater.update(0, 4);
805     EXPECT_EQ(4, layer->fakeLayerTextureUpdater()->updateCount());
806     EXPECT_TRUE(updater.hasMoreUpdates());
807     layer->fakeLayerTextureUpdater()->clearUpdateCount();
808     updater.update(0, 4);
809     EXPECT_EQ(2, layer->fakeLayerTextureUpdater()->updateCount());
810     EXPECT_FALSE(updater.hasMoreUpdates());
811     layer->fakeLayerTextureUpdater()->clearUpdateCount();
812     ccLayerTreeHost->commitComplete();
813
814     // Full update of 3 tiles and partial update of 3 tiles.
815     layer->invalidateRect(IntRect(0, 0, 300, 150));
816     ccLayerTreeHost->updateLayers();
817     ccLayerTreeHost->updateCompositorResources(ccLayerTreeHost->context(), updater);
818     updater.update(0, 4);
819     EXPECT_EQ(3, layer->fakeLayerTextureUpdater()->updateCount());
820     EXPECT_TRUE(updater.hasMoreUpdates());
821     layer->fakeLayerTextureUpdater()->clearUpdateCount();
822     updater.update(0, 4);
823     EXPECT_EQ(3, layer->fakeLayerTextureUpdater()->updateCount());
824     EXPECT_FALSE(updater.hasMoreUpdates());
825     layer->fakeLayerTextureUpdater()->clearUpdateCount();
826     ccLayerTreeHost->commitComplete();
827
828     // Partial update of 6 tiles.
829     layer->invalidateRect(IntRect(50, 50, 200, 100));
830     ccLayerTreeHost->updateLayers();
831     ccLayerTreeHost->updateCompositorResources(ccLayerTreeHost->context(), updater);
832     updater.update(0, 4);
833     EXPECT_EQ(2, layer->fakeLayerTextureUpdater()->updateCount());
834     EXPECT_TRUE(updater.hasMoreUpdates());
835     layer->fakeLayerTextureUpdater()->clearUpdateCount();
836     updater.update(0, 4);
837     EXPECT_EQ(4, layer->fakeLayerTextureUpdater()->updateCount());
838     EXPECT_FALSE(updater.hasMoreUpdates());
839     layer->fakeLayerTextureUpdater()->clearUpdateCount();
840     ccLayerTreeHost->commitComplete();
841
842     ccLayerTreeHost->setRootLayer(0);
843     ccLayerTreeHost.clear();
844     WebKit::WebCompositor::shutdown();
845 }
846
847 TEST(TiledLayerChromiumTest, tilesPaintedWithoutOcclusion)
848 {
849     OwnPtr<TextureManager> textureManager = TextureManager::create(4*1024*1024, 2*1024*1024, 1024);
850     RefPtr<FakeTiledLayerChromium> layer = adoptRef(new FakeTiledLayerChromium(textureManager.get()));
851
852     // The tile size is 100x100, so this invalidates and then paints two tiles.
853     layer->setBounds(IntSize(100, 200));
854
855     layer->invalidateRect(IntRect(0, 0, 100, 200));
856     layer->prepareToUpdate(IntRect(0, 0, 100, 200), 0);
857     EXPECT_EQ(2, layer->fakeLayerTextureUpdater()->prepareRectCount());
858 }
859
860 TEST(TiledLayerChromiumTest, tilesPaintedWithOcclusion)
861 {
862     OwnPtr<TextureManager> textureManager = TextureManager::create(4*1024*1024, 2*1024*1024, 1024);
863     RefPtr<FakeTiledLayerChromium> layer = adoptRef(new FakeTiledLayerChromium(textureManager.get()));
864     TestCCOcclusionTracker occluded;
865
866     // The tile size is 100x100.
867
868     layer->setBounds(IntSize(600, 600));
869     layer->setDrawTransform(TransformationMatrix(1, 0, 0, 1, layer->bounds().width() / 2.0, layer->bounds().height() / 2.0));
870
871     occluded.setOcclusion(IntRect(200, 200, 300, 100));
872     layer->setVisibleLayerRect(IntRect(IntPoint(), layer->bounds()));
873     layer->invalidateRect(IntRect(0, 0, 600, 600));
874     layer->prepareToUpdate(IntRect(0, 0, 600, 600), &occluded);
875     EXPECT_EQ(36-3, layer->fakeLayerTextureUpdater()->prepareRectCount());
876
877     EXPECT_NEAR(occluded.overdrawMetrics().pixelsDrawnOpaque(), 0, 1);
878     EXPECT_NEAR(occluded.overdrawMetrics().pixelsDrawnTranslucent(), 330000, 1);
879     EXPECT_NEAR(occluded.overdrawMetrics().pixelsCulled(), 30000, 1);
880
881     layer->fakeLayerTextureUpdater()->clearPrepareRectCount();
882
883     occluded.setOcclusion(IntRect(250, 200, 300, 100));
884     layer->invalidateRect(IntRect(0, 0, 600, 600));
885     layer->prepareToUpdate(IntRect(0, 0, 600, 600), &occluded);
886     EXPECT_EQ(36-2, layer->fakeLayerTextureUpdater()->prepareRectCount());
887
888     EXPECT_NEAR(occluded.overdrawMetrics().pixelsDrawnOpaque(), 0, 1);
889     EXPECT_NEAR(occluded.overdrawMetrics().pixelsDrawnTranslucent(), 330000 + 340000, 1);
890     EXPECT_NEAR(occluded.overdrawMetrics().pixelsCulled(), 30000 + 20000, 1);
891
892     layer->fakeLayerTextureUpdater()->clearPrepareRectCount();
893
894     occluded.setOcclusion(IntRect(250, 250, 300, 100));
895     layer->invalidateRect(IntRect(0, 0, 600, 600));
896     layer->prepareToUpdate(IntRect(0, 0, 600, 600), &occluded);
897     EXPECT_EQ(36, layer->fakeLayerTextureUpdater()->prepareRectCount());
898
899     EXPECT_NEAR(occluded.overdrawMetrics().pixelsDrawnOpaque(), 0, 1);
900     EXPECT_NEAR(occluded.overdrawMetrics().pixelsDrawnTranslucent(), 330000 + 340000 + 360000, 1);
901     EXPECT_NEAR(occluded.overdrawMetrics().pixelsCulled(), 30000 + 20000, 1);
902 }
903
904 TEST(TiledLayerChromiumTest, tilesPaintedWithOcclusionAndVisiblityConstraints)
905 {
906     OwnPtr<TextureManager> textureManager = TextureManager::create(4*1024*1024, 2*1024*1024, 1024);
907     RefPtr<FakeTiledLayerChromium> layer = adoptRef(new FakeTiledLayerChromium(textureManager.get()));
908     TestCCOcclusionTracker occluded;
909
910     // The tile size is 100x100.
911
912     layer->setBounds(IntSize(600, 600));
913     layer->setDrawTransform(TransformationMatrix(1, 0, 0, 1, layer->bounds().width() / 2.0, layer->bounds().height() / 2.0));
914
915     // The partially occluded tiles (by the 150 occlusion height) are visible beyond the occlusion, so not culled.
916     occluded.setOcclusion(IntRect(200, 200, 300, 150));
917     layer->setVisibleLayerRect(IntRect(0, 0, 600, 360));
918     layer->invalidateRect(IntRect(0, 0, 600, 600));
919     layer->prepareToUpdate(IntRect(0, 0, 600, 600), &occluded);
920     EXPECT_EQ(24-3, layer->fakeLayerTextureUpdater()->prepareRectCount());
921
922     EXPECT_NEAR(occluded.overdrawMetrics().pixelsDrawnOpaque(), 0, 1);
923     EXPECT_NEAR(occluded.overdrawMetrics().pixelsDrawnTranslucent(), 210000, 1);
924     EXPECT_NEAR(occluded.overdrawMetrics().pixelsCulled(), 30000, 1);
925
926     layer->fakeLayerTextureUpdater()->clearPrepareRectCount();
927
928     // Now the visible region stops at the edge of the occlusion so the partly visible tiles become fully occluded.
929     occluded.setOcclusion(IntRect(200, 200, 300, 150));
930     layer->setVisibleLayerRect(IntRect(0, 0, 600, 350));
931     layer->invalidateRect(IntRect(0, 0, 600, 600));
932     layer->prepareToUpdate(IntRect(0, 0, 600, 600), &occluded);
933     EXPECT_EQ(24-6, layer->fakeLayerTextureUpdater()->prepareRectCount());
934
935     EXPECT_NEAR(occluded.overdrawMetrics().pixelsDrawnOpaque(), 0, 1);
936     EXPECT_NEAR(occluded.overdrawMetrics().pixelsDrawnTranslucent(), 210000 + 180000, 1);
937     EXPECT_NEAR(occluded.overdrawMetrics().pixelsCulled(), 30000 + 60000, 1);
938
939     layer->fakeLayerTextureUpdater()->clearPrepareRectCount();
940
941     // Now the visible region is even smaller than the occlusion, it should have the same result.
942     occluded.setOcclusion(IntRect(200, 200, 300, 150));
943     layer->setVisibleLayerRect(IntRect(0, 0, 600, 340));
944     layer->invalidateRect(IntRect(0, 0, 600, 600));
945     layer->prepareToUpdate(IntRect(0, 0, 600, 600), &occluded);
946     EXPECT_EQ(24-6, layer->fakeLayerTextureUpdater()->prepareRectCount());
947
948     EXPECT_NEAR(occluded.overdrawMetrics().pixelsDrawnOpaque(), 0, 1);
949     EXPECT_NEAR(occluded.overdrawMetrics().pixelsDrawnTranslucent(), 210000 + 180000 + 180000, 1);
950     EXPECT_NEAR(occluded.overdrawMetrics().pixelsCulled(), 30000 + 60000 + 60000, 1);
951
952 }
953
954 TEST(TiledLayerChromiumTest, tilesNotPaintedWithoutInvalidation)
955 {
956     OwnPtr<TextureManager> textureManager = TextureManager::create(4*1024*1024, 2*1024*1024, 1024);
957     RefPtr<FakeTiledLayerChromium> layer = adoptRef(new FakeTiledLayerChromium(textureManager.get()));
958     TestCCOcclusionTracker occluded;
959
960     // The tile size is 100x100.
961
962     layer->setBounds(IntSize(600, 600));
963     layer->setDrawTransform(TransformationMatrix(1, 0, 0, 1, layer->bounds().width() / 2.0, layer->bounds().height() / 2.0));
964
965     occluded.setOcclusion(IntRect(200, 200, 300, 100));
966     layer->setVisibleLayerRect(IntRect(0, 0, 600, 600));
967     layer->invalidateRect(IntRect(0, 0, 600, 600));
968     layer->prepareToUpdate(IntRect(0, 0, 600, 600), &occluded);
969     EXPECT_EQ(36-3, layer->fakeLayerTextureUpdater()->prepareRectCount());
970
971     EXPECT_NEAR(occluded.overdrawMetrics().pixelsDrawnOpaque(), 0, 1);
972     EXPECT_NEAR(occluded.overdrawMetrics().pixelsDrawnTranslucent(), 330000, 1);
973     EXPECT_NEAR(occluded.overdrawMetrics().pixelsCulled(), 30000, 1);
974
975     layer->fakeLayerTextureUpdater()->clearPrepareRectCount();
976
977     // Repaint without marking it dirty.
978     layer->prepareToUpdate(IntRect(0, 0, 600, 600), &occluded);
979     EXPECT_EQ(0, layer->fakeLayerTextureUpdater()->prepareRectCount());
980
981     EXPECT_NEAR(occluded.overdrawMetrics().pixelsDrawnOpaque(), 0, 1);
982     EXPECT_NEAR(occluded.overdrawMetrics().pixelsDrawnTranslucent(), 330000, 1);
983     EXPECT_NEAR(occluded.overdrawMetrics().pixelsCulled(), 30000, 1);
984 }
985
986 TEST(TiledLayerChromiumTest, tilesPaintedWithOcclusionAndTransforms)
987 {
988     OwnPtr<TextureManager> textureManager = TextureManager::create(4*1024*1024, 2*1024*1024, 1024);
989     RefPtr<FakeTiledLayerChromium> layer = adoptRef(new FakeTiledLayerChromium(textureManager.get()));
990     TestCCOcclusionTracker occluded;
991
992     // The tile size is 100x100.
993
994     // This makes sure the painting works when the occluded region (in screen space)
995     // is transformed differently than the layer.
996     layer->setBounds(IntSize(600, 600));
997     TransformationMatrix screenTransform;
998     screenTransform.scale(0.5);
999     layer->setScreenSpaceTransform(screenTransform);
1000     layer->setDrawTransform(screenTransform * TransformationMatrix(1, 0, 0, 1, layer->bounds().width() / 2.0, layer->bounds().height() / 2.0));
1001
1002     occluded.setOcclusion(IntRect(100, 100, 150, 50));
1003     layer->setVisibleLayerRect(IntRect(IntPoint(), layer->bounds()));
1004     layer->invalidateRect(IntRect(0, 0, 600, 600));
1005     layer->prepareToUpdate(IntRect(0, 0, 600, 600), &occluded);
1006     EXPECT_EQ(36-3, layer->fakeLayerTextureUpdater()->prepareRectCount());
1007
1008     EXPECT_NEAR(occluded.overdrawMetrics().pixelsDrawnOpaque(), 0, 1);
1009     EXPECT_NEAR(occluded.overdrawMetrics().pixelsDrawnTranslucent(), 330000, 1);
1010     EXPECT_NEAR(occluded.overdrawMetrics().pixelsCulled(), 30000, 1);
1011 }
1012
1013 TEST(TiledLayerChromiumTest, tilesPaintedWithOcclusionAndScaling)
1014 {
1015     OwnPtr<TextureManager> textureManager = TextureManager::create(4*1024*1024, 2*1024*1024, 1024);
1016     RefPtr<FakeTiledLayerChromium> layer = adoptRef(new FakeTiledLayerChromium(textureManager.get()));
1017     TestCCOcclusionTracker occluded;
1018
1019     // The tile size is 100x100.
1020
1021     // This makes sure the painting works when the content space is scaled to
1022     // a different layer space. In this case tiles are scaled to be 200x200
1023     // pixels, which means none should be occluded.
1024     layer->setContentsScale(0.5);
1025     layer->setBounds(IntSize(600, 600));
1026     layer->setDrawTransform(TransformationMatrix(1, 0, 0, 1, layer->bounds().width() / 2.0, layer->bounds().height() / 2.0));
1027
1028     occluded.setOcclusion(IntRect(200, 200, 300, 100));
1029     layer->setVisibleLayerRect(IntRect(IntPoint(), layer->bounds()));
1030     layer->invalidateRect(IntRect(0, 0, 600, 600));
1031     layer->prepareToUpdate(IntRect(0, 0, 600, 600), &occluded);
1032     // The content is half the size of the layer (so the number of tiles is fewer).
1033     // In this case, the content is 300x300, and since the tile size is 100, the
1034     // number of tiles 3x3.
1035     EXPECT_EQ(9, layer->fakeLayerTextureUpdater()->prepareRectCount());
1036
1037     EXPECT_NEAR(occluded.overdrawMetrics().pixelsDrawnOpaque(), 0, 1);
1038     EXPECT_NEAR(occluded.overdrawMetrics().pixelsDrawnTranslucent(), 90000, 1);
1039     EXPECT_NEAR(occluded.overdrawMetrics().pixelsCulled(), 0, 1);
1040
1041     layer->fakeLayerTextureUpdater()->clearPrepareRectCount();
1042
1043     // This makes sure the painting works when the content space is scaled to
1044     // a different layer space. In this case the occluded region catches the
1045     // blown up tiles.
1046     occluded.setOcclusion(IntRect(200, 200, 300, 200));
1047     layer->setVisibleLayerRect(IntRect(IntPoint(), layer->bounds()));
1048     layer->invalidateRect(IntRect(0, 0, 600, 600));
1049     layer->prepareToUpdate(IntRect(0, 0, 600, 600), &occluded);
1050     EXPECT_EQ(9-1, layer->fakeLayerTextureUpdater()->prepareRectCount());
1051
1052     EXPECT_NEAR(occluded.overdrawMetrics().pixelsDrawnOpaque(), 0, 1);
1053     EXPECT_NEAR(occluded.overdrawMetrics().pixelsDrawnTranslucent(), 90000 + 80000, 1);
1054     EXPECT_NEAR(occluded.overdrawMetrics().pixelsCulled(), 10000, 1);
1055
1056     layer->fakeLayerTextureUpdater()->clearPrepareRectCount();
1057
1058     // This makes sure content scaling and transforms work together.
1059     TransformationMatrix screenTransform;
1060     screenTransform.scale(0.5);
1061     layer->setScreenSpaceTransform(screenTransform);
1062     layer->setDrawTransform(screenTransform * TransformationMatrix(1, 0, 0, 1, layer->bounds().width() / 2.0, layer->bounds().height() / 2.0));
1063
1064     occluded.setOcclusion(IntRect(100, 100, 150, 100));
1065     layer->setVisibleLayerRect(IntRect(IntPoint(), layer->bounds()));
1066     layer->invalidateRect(IntRect(0, 0, 600, 600));
1067     layer->prepareToUpdate(IntRect(0, 0, 600, 600), &occluded);
1068     EXPECT_EQ(9-1, layer->fakeLayerTextureUpdater()->prepareRectCount());
1069
1070     EXPECT_NEAR(occluded.overdrawMetrics().pixelsDrawnOpaque(), 0, 1);
1071     EXPECT_NEAR(occluded.overdrawMetrics().pixelsDrawnTranslucent(), 90000 + 80000 + 80000, 1);
1072     EXPECT_NEAR(occluded.overdrawMetrics().pixelsCulled(), 10000 + 10000, 1);
1073 }
1074
1075 TEST(TiledLayerChromiumTest, opaqueContentsRegion)
1076 {
1077     OwnPtr<TextureManager> textureManager = TextureManager::create(4*1024*1024, 2*1024*1024, 1024);
1078     RefPtr<FakeTiledLayerChromium> layer = adoptRef(new FakeTiledLayerChromium(textureManager.get()));
1079     TestCCOcclusionTracker occluded;
1080
1081     // The tile size is 100x100, so this invalidates and then paints two tiles in various ways.
1082
1083     IntRect opaquePaintRect;
1084     Region opaqueContents;
1085
1086     IntRect contentBounds = IntRect(0, 0, 100, 200);
1087     IntRect visibleBounds = IntRect(0, 0, 100, 150);
1088
1089     layer->setBounds(contentBounds.size());
1090     layer->setDrawTransform(TransformationMatrix(1, 0, 0, 1, layer->bounds().width() / 2.0, layer->bounds().height() / 2.0));
1091     layer->setVisibleLayerRect(visibleBounds);
1092     layer->setDrawOpacity(1);
1093
1094     // If the layer doesn't paint opaque content, then the opaqueContentsRegion should be empty.
1095     layer->fakeLayerTextureUpdater()->setOpaquePaintRect(IntRect());
1096     layer->invalidateRect(contentBounds);
1097     layer->prepareToUpdate(contentBounds, &occluded);
1098     opaqueContents = layer->opaqueContentsRegion();
1099     EXPECT_TRUE(opaqueContents.isEmpty());
1100
1101     EXPECT_NEAR(occluded.overdrawMetrics().pixelsDrawnOpaque(), 0, 1);
1102     EXPECT_NEAR(occluded.overdrawMetrics().pixelsDrawnTranslucent(), 20000, 1);
1103     EXPECT_NEAR(occluded.overdrawMetrics().pixelsCulled(), 0, 1);
1104
1105     // opaqueContentsRegion should match the visible part of what is painted opaque.
1106     opaquePaintRect = IntRect(10, 10, 90, 190);
1107     layer->fakeLayerTextureUpdater()->setOpaquePaintRect(opaquePaintRect);
1108     layer->invalidateRect(contentBounds);
1109     layer->prepareToUpdate(contentBounds, &occluded);
1110     opaqueContents = layer->opaqueContentsRegion();
1111     EXPECT_EQ_RECT(intersection(opaquePaintRect, visibleBounds), opaqueContents.bounds());
1112     EXPECT_EQ(1u, opaqueContents.rects().size());
1113
1114     EXPECT_NEAR(occluded.overdrawMetrics().pixelsDrawnOpaque(), 17100, 1);
1115     EXPECT_NEAR(occluded.overdrawMetrics().pixelsDrawnTranslucent(), 20000 + 20000 - 17100, 1);
1116     EXPECT_NEAR(occluded.overdrawMetrics().pixelsCulled(), 0, 1);
1117
1118     // If we paint again without invalidating, the same stuff should be opaque.
1119     layer->fakeLayerTextureUpdater()->setOpaquePaintRect(IntRect());
1120     layer->prepareToUpdate(contentBounds, &occluded);
1121     opaqueContents = layer->opaqueContentsRegion();
1122     EXPECT_EQ_RECT(intersection(opaquePaintRect, visibleBounds), opaqueContents.bounds());
1123     EXPECT_EQ(1u, opaqueContents.rects().size());
1124
1125     EXPECT_NEAR(occluded.overdrawMetrics().pixelsDrawnOpaque(), 17100, 1);
1126     EXPECT_NEAR(occluded.overdrawMetrics().pixelsDrawnTranslucent(), 20000 + 20000 - 17100, 1);
1127     EXPECT_NEAR(occluded.overdrawMetrics().pixelsCulled(), 0, 1);
1128
1129     // If we repaint a non-opaque part of the tile, then it shouldn't lose its opaque-ness. And other tiles should
1130     // not be affected.
1131     layer->fakeLayerTextureUpdater()->setOpaquePaintRect(IntRect());
1132     layer->invalidateRect(IntRect(0, 0, 1, 1));
1133     layer->prepareToUpdate(contentBounds, &occluded);
1134     opaqueContents = layer->opaqueContentsRegion();
1135     EXPECT_EQ_RECT(intersection(opaquePaintRect, visibleBounds), opaqueContents.bounds());
1136     EXPECT_EQ(1u, opaqueContents.rects().size());
1137
1138     EXPECT_NEAR(occluded.overdrawMetrics().pixelsDrawnOpaque(), 17100, 1);
1139     EXPECT_NEAR(occluded.overdrawMetrics().pixelsDrawnTranslucent(), 20000 + 20000 - 17100 + 1, 1);
1140     EXPECT_NEAR(occluded.overdrawMetrics().pixelsCulled(), 0, 1);
1141
1142     // If we repaint an opaque part of the tile, then it should lose its opaque-ness. But other tiles should still
1143     // not be affected.
1144     layer->fakeLayerTextureUpdater()->setOpaquePaintRect(IntRect());
1145     layer->invalidateRect(IntRect(10, 10, 1, 1));
1146     layer->prepareToUpdate(contentBounds, &occluded);
1147     opaqueContents = layer->opaqueContentsRegion();
1148     EXPECT_EQ_RECT(intersection(IntRect(10, 100, 90, 100), visibleBounds), opaqueContents.bounds());
1149     EXPECT_EQ(1u, opaqueContents.rects().size());
1150
1151     EXPECT_NEAR(occluded.overdrawMetrics().pixelsDrawnOpaque(), 17100, 1);
1152     EXPECT_NEAR(occluded.overdrawMetrics().pixelsDrawnTranslucent(), 20000 + 20000 - 17100 + 1 + 1, 1);
1153     EXPECT_NEAR(occluded.overdrawMetrics().pixelsCulled(), 0, 1);
1154 }
1155
1156 } // namespace