[chromium] Add overdraw metrics for texture uploads
[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, pushIdlePaintTiles)
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. Setup 5x5 tiles with one visible tile in the center.
349     IntSize contentBounds(500, 500);
350     IntRect contentRect(IntPoint::zero(), contentBounds);
351     IntRect visibleRect(200, 200, 100, 100);
352
353     // This invalidates 25 tiles and then paints one visible tile.
354     layer->setBounds(contentBounds);
355     layer->setVisibleLayerRect(visibleRect);
356     layer->invalidateRect(contentRect);
357     layer->prepareToUpdate(visibleRect, 0);
358
359     // We should need idle-painting for 3x3 tiles in the center.
360     EXPECT_TRUE(layer->needsIdlePaint(visibleRect));
361
362     layer->updateCompositorResources(0, updater);
363     layer->pushPropertiesTo(layerImpl.get());
364
365     // We should have one tile on the impl side.
366     EXPECT_TRUE(layerImpl->hasTileAt(2, 2));
367
368     textureManager->unprotectAllTextures();
369
370     // For the next four updates, we should detect we still need idle painting.
371     for (int i = 0; i < 4; i++) {
372         layer->prepareToUpdate(visibleRect, 0);
373         EXPECT_TRUE(layer->needsIdlePaint(visibleRect));
374         layer->prepareToUpdateIdle(visibleRect, 0);
375         layer->updateCompositorResources(0, updater);
376         layer->pushPropertiesTo(layerImpl.get());
377         textureManager->unprotectAllTextures();
378     }
379
380     // After four passes of idle painting, we should be finished painting
381     EXPECT_FALSE(layer->needsIdlePaint(visibleRect));
382
383     // We should have one tile surrounding the visible tile on all sides, but no other tiles.
384     IntRect idlePaintTiles(1, 1, 3, 3);
385     for (int i = 0; i < 5; i++) {
386         for (int j = 0; j < 5; j++) {
387             if (idlePaintTiles.contains(i, j))
388                 EXPECT_TRUE(layerImpl->hasTileAt(i, j));
389             else
390                 EXPECT_FALSE(layerImpl->hasTileAt(i, j));
391         }
392     }
393 }
394
395 TEST(TiledLayerChromiumTest, pushTilesMarkedDirtyDuringPaint)
396 {
397     OwnPtr<TextureManager> textureManager = TextureManager::create(4*1024*1024, 2*1024*1024, 1024);
398     RefPtr<FakeTiledLayerChromium> layer = adoptRef(new FakeTiledLayerChromium(textureManager.get()));
399     DebugScopedSetImplThread implThread;
400     OwnPtr<FakeCCTiledLayerImpl> layerImpl(adoptPtr(new FakeCCTiledLayerImpl(0)));
401
402     FakeTextureAllocator textureAllocator;
403     CCTextureUpdater updater(&textureAllocator);
404
405     // The tile size is 100x100, so this invalidates and then paints two tiles.
406     // However, during the paint, we invalidate one of the tiles. This should
407     // not prevent the tile from being pushed.
408     layer->setBounds(IntSize(100, 200));
409     layer->invalidateRect(IntRect(0, 0, 100, 200));
410     layer->fakeLayerTextureUpdater()->setRectToInvalidate(IntRect(0, 50, 100, 50), layer.get());
411     layer->prepareToUpdate(IntRect(0, 0, 100, 200), 0);
412     layer->updateCompositorResources(0, updater);
413     layer->pushPropertiesTo(layerImpl.get());
414
415     // We should have both tiles on the impl side.
416     EXPECT_TRUE(layerImpl->hasTileAt(0, 0));
417     EXPECT_TRUE(layerImpl->hasTileAt(0, 1));
418 }
419
420 TEST(TiledLayerChromiumTest, pushTilesLayerMarkedDirtyDuringPaintOnNextLayer)
421 {
422     OwnPtr<TextureManager> textureManager = TextureManager::create(4*1024*1024, 2*1024*1024, 1024);
423     RefPtr<FakeTiledLayerChromium> layer1 = adoptRef(new FakeTiledLayerChromium(textureManager.get()));
424     RefPtr<FakeTiledLayerChromium> layer2 = adoptRef(new FakeTiledLayerChromium(textureManager.get()));
425     DebugScopedSetImplThread implThread;
426     OwnPtr<FakeCCTiledLayerImpl> layer1Impl(adoptPtr(new FakeCCTiledLayerImpl(0)));
427     OwnPtr<FakeCCTiledLayerImpl> layer2Impl(adoptPtr(new FakeCCTiledLayerImpl(0)));
428
429     FakeTextureAllocator textureAllocator;
430     CCTextureUpdater updater(&textureAllocator);
431
432     layer1->setBounds(IntSize(100, 200));
433     layer1->invalidateRect(IntRect(0, 0, 100, 200));
434     layer2->setBounds(IntSize(100, 200));
435     layer2->invalidateRect(IntRect(0, 0, 100, 200));
436
437     layer1->prepareToUpdate(IntRect(0, 0, 100, 200), 0);
438
439     // Invalidate a tile on layer1
440     layer2->fakeLayerTextureUpdater()->setRectToInvalidate(IntRect(0, 50, 100, 50), layer1.get());
441     layer2->prepareToUpdate(IntRect(0, 0, 100, 200), 0);
442
443     layer1->updateCompositorResources(0, updater);
444     layer2->updateCompositorResources(0, updater);
445
446     layer1->pushPropertiesTo(layer1Impl.get());
447     layer2->pushPropertiesTo(layer2Impl.get());
448
449     // We should have both tiles on the impl side for all layers.
450     EXPECT_TRUE(layer1Impl->hasTileAt(0, 0));
451     EXPECT_TRUE(layer1Impl->hasTileAt(0, 1));
452     EXPECT_TRUE(layer2Impl->hasTileAt(0, 0));
453     EXPECT_TRUE(layer2Impl->hasTileAt(0, 1));
454 }
455
456 TEST(TiledLayerChromiumTest, pushTilesLayerMarkedDirtyDuringPaintOnPreviousLayer)
457 {
458     OwnPtr<TextureManager> textureManager = TextureManager::create(4*1024*1024, 2*1024*1024, 1024);
459     RefPtr<FakeTiledLayerChromium> layer1 = adoptRef(new FakeTiledLayerChromium(textureManager.get()));
460     RefPtr<FakeTiledLayerChromium> layer2 = adoptRef(new FakeTiledLayerChromium(textureManager.get()));
461     DebugScopedSetImplThread implThread;
462     OwnPtr<FakeCCTiledLayerImpl> layer1Impl(adoptPtr(new FakeCCTiledLayerImpl(0)));
463     OwnPtr<FakeCCTiledLayerImpl> layer2Impl(adoptPtr(new FakeCCTiledLayerImpl(0)));
464
465     FakeTextureAllocator textureAllocator;
466     CCTextureUpdater updater(&textureAllocator);
467
468     layer1->setBounds(IntSize(100, 200));
469     layer1->invalidateRect(IntRect(0, 0, 100, 200));
470     layer2->setBounds(IntSize(100, 200));
471     layer2->invalidateRect(IntRect(0, 0, 100, 200));
472
473     // Invalidate a tile on layer2
474     layer1->fakeLayerTextureUpdater()->setRectToInvalidate(IntRect(0, 50, 100, 50), layer2.get());
475     layer1->prepareToUpdate(IntRect(0, 0, 100, 200), 0);
476
477     layer2->prepareToUpdate(IntRect(0, 0, 100, 200), 0);
478
479     layer1->updateCompositorResources(0, updater);
480     layer2->updateCompositorResources(0, updater);
481
482     layer1->pushPropertiesTo(layer1Impl.get());
483     layer2->pushPropertiesTo(layer2Impl.get());
484
485     // We should have both tiles on the impl side for all layers.
486     EXPECT_TRUE(layer1Impl->hasTileAt(0, 0));
487     EXPECT_TRUE(layer1Impl->hasTileAt(0, 1));
488     EXPECT_TRUE(layer2Impl->hasTileAt(0, 0));
489     EXPECT_TRUE(layer2Impl->hasTileAt(0, 1));
490 }
491
492 TEST(TiledLayerChromiumTest, idlePaintOutOfMemory)
493 {
494     // The tile size is 100x100. Setup 5x5 tiles with one 1x1 visible tile in the center.
495     IntSize contentBounds(300, 300);
496     IntRect contentRect(IntPoint::zero(), contentBounds);
497     IntRect visibleRect(100, 100, 100, 100);
498
499     // We have enough memory for only the visible rect, so we will run out of memory in first idle paint.
500     int memoryLimit = 4 * 100 * 100; // 2 tiles, 4 bytes per pixel.
501
502     OwnPtr<TextureManager> textureManager = TextureManager::create(memoryLimit, memoryLimit / 2, 1024);
503     RefPtr<FakeTiledLayerChromium> layer = adoptRef(new FakeTiledLayerChromium(textureManager.get()));
504     DebugScopedSetImplThread implThread;
505     OwnPtr<FakeCCTiledLayerImpl> layerImpl(adoptPtr(new FakeCCTiledLayerImpl(0)));
506
507     FakeTextureAllocator textureAllocator;
508     CCTextureUpdater updater(&textureAllocator);
509
510     // This invalidates 9 tiles and then paints one visible tile.
511     layer->setBounds(contentBounds);
512     layer->setVisibleLayerRect(visibleRect);
513     layer->invalidateRect(contentRect);
514     layer->prepareToUpdate(visibleRect, 0);
515
516     // We should need idle-painting for 3x3 tiles surounding visible tile.
517     EXPECT_TRUE(layer->needsIdlePaint(visibleRect));
518
519     layer->updateCompositorResources(0, updater);
520     layer->pushPropertiesTo(layerImpl.get());
521
522     // We should have one tile on the impl side.
523     EXPECT_TRUE(layerImpl->hasTileAt(1, 1));
524
525     textureManager->unprotectAllTextures();
526     layer->prepareToUpdate(visibleRect, 0);
527     layer->prepareToUpdateIdle(visibleRect, 0);
528
529     // We shouldn't signal we need another idle paint after we run out of memory.
530     EXPECT_FALSE(layer->needsIdlePaint(visibleRect));
531
532     layer->updateCompositorResources(0, updater);
533     layer->pushPropertiesTo(layerImpl.get());
534 }
535
536 TEST(TiledLayerChromiumTest, invalidateFromPrepare)
537 {
538     OwnPtr<TextureManager> textureManager = TextureManager::create(4*1024*1024, 2*1024*1024, 1024);
539     RefPtr<FakeTiledLayerChromium> layer = adoptRef(new FakeTiledLayerChromium(textureManager.get()));
540     DebugScopedSetImplThread implThread;
541     OwnPtr<FakeCCTiledLayerImpl> layerImpl(adoptPtr(new FakeCCTiledLayerImpl(0)));
542
543     FakeTextureAllocator textureAllocator;
544     CCTextureUpdater updater(&textureAllocator);
545
546     // The tile size is 100x100, so this invalidates and then paints two tiles.
547     layer->setBounds(IntSize(100, 200));
548     layer->invalidateRect(IntRect(0, 0, 100, 200));
549     layer->prepareToUpdate(IntRect(0, 0, 100, 200), 0);
550     layer->updateCompositorResources(0, updater);
551     layer->pushPropertiesTo(layerImpl.get());
552
553     // We should have both tiles on the impl side.
554     EXPECT_TRUE(layerImpl->hasTileAt(0, 0));
555     EXPECT_TRUE(layerImpl->hasTileAt(0, 1));
556
557     textureManager->unprotectAllTextures();
558
559     layer->fakeLayerTextureUpdater()->clearPrepareCount();
560     // Invoke prepareToUpdate again. As the layer is valid prepareToUpdate shouldn't be invoked on
561     // the LayerTextureUpdater.
562     layer->prepareToUpdate(IntRect(0, 0, 100, 200), 0);
563     EXPECT_EQ(0, layer->fakeLayerTextureUpdater()->prepareCount());
564
565     layer->invalidateRect(IntRect(0, 0, 50, 50));
566     // setRectToInvalidate triggers invalidateRect() being invoked from prepareToUpdate.
567     layer->fakeLayerTextureUpdater()->setRectToInvalidate(IntRect(25, 25, 50, 50), layer.get());
568     layer->fakeLayerTextureUpdater()->clearPrepareCount();
569     layer->prepareToUpdate(IntRect(0, 0, 100, 200), 0);
570     EXPECT_EQ(1, layer->fakeLayerTextureUpdater()->prepareCount());
571     layer->fakeLayerTextureUpdater()->clearPrepareCount();
572     // The layer should still be invalid as prepareToUpdate invoked invalidate.
573     layer->prepareToUpdate(IntRect(0, 0, 100, 200), 0);
574     EXPECT_EQ(1, layer->fakeLayerTextureUpdater()->prepareCount());
575 }
576
577 TEST(TiledLayerChromiumTest, verifyUpdateRectWhenContentBoundsAreScaled)
578 {
579     // The updateRect (that indicates what was actually painted) should be in
580     // layer space, not the content space.
581
582     OwnPtr<TextureManager> textureManager = TextureManager::create(4*1024*1024, 2*1024*1024, 1024);
583     RefPtr<FakeTiledLayerWithScaledBounds> layer = adoptRef(new FakeTiledLayerWithScaledBounds(textureManager.get()));
584
585     FakeTextureAllocator textureAllocator;
586     CCTextureUpdater updater(&textureAllocator);
587
588     IntRect layerBounds(0, 0, 300, 200);
589     IntRect contentBounds(0, 0, 200, 250);
590
591     layer->setBounds(layerBounds.size());
592     layer->setContentBounds(contentBounds.size());
593     layer->setVisibleLayerRect(contentBounds);
594
595     // On first update, the updateRect includes all tiles, even beyond the boundaries of the layer.
596     // However, it should still be in layer space, not content space.
597     layer->invalidateRect(contentBounds);
598     layer->prepareToUpdate(contentBounds, 0);
599     layer->updateCompositorResources(0, updater);
600     EXPECT_FLOAT_RECT_EQ(FloatRect(0, 0, 300, 300 * 0.8), layer->updateRect());
601
602     // After the tiles are updated once, another invalidate only needs to update the bounds of the layer.
603     layer->invalidateRect(contentBounds);
604     layer->prepareToUpdate(contentBounds, 0);
605     layer->updateCompositorResources(0, updater);
606     EXPECT_FLOAT_RECT_EQ(FloatRect(layerBounds), layer->updateRect());
607
608     // Partial re-paint should also be represented by the updateRect in layer space, not content space.
609     IntRect partialDamage(30, 100, 10, 10);
610     layer->invalidateRect(partialDamage);
611     layer->prepareToUpdate(contentBounds, 0);
612     layer->updateCompositorResources(0, updater);
613     EXPECT_FLOAT_RECT_EQ(FloatRect(45, 80, 15, 8), layer->updateRect());
614 }
615
616 TEST(TiledLayerChromiumTest, verifyInvalidationWhenContentsScaleChanges)
617 {
618     OwnPtr<TextureManager> textureManager = TextureManager::create(4*1024*1024, 2*1024*1024, 1024);
619     RefPtr<FakeTiledLayerChromium> layer = adoptRef(new FakeTiledLayerChromium(textureManager.get()));
620     DebugScopedSetImplThread implThread;
621     OwnPtr<FakeCCTiledLayerImpl> layerImpl(adoptPtr(new FakeCCTiledLayerImpl(0)));
622
623     FakeTextureAllocator textureAllocator;
624     CCTextureUpdater updater(&textureAllocator);
625
626     // Create a layer with one tile.
627     layer->setBounds(IntSize(100, 100));
628
629     // Invalidate the entire layer.
630     layer->setNeedsDisplay();
631     EXPECT_FLOAT_RECT_EQ(FloatRect(0, 0, 100, 100), layer->lastNeedsDisplayRect());
632
633     // Push the tiles to the impl side and check that there is exactly one.
634     layer->prepareToUpdate(IntRect(0, 0, 100, 100), 0);
635     layer->updateCompositorResources(0, updater);
636     layer->pushPropertiesTo(layerImpl.get());
637     EXPECT_TRUE(layerImpl->hasTileAt(0, 0));
638     EXPECT_FALSE(layerImpl->hasTileAt(0, 1));
639     EXPECT_FALSE(layerImpl->hasTileAt(1, 0));
640     EXPECT_FALSE(layerImpl->hasTileAt(1, 1));
641
642     // Change the contents scale and verify that the content rectangle requiring painting
643     // is not scaled.
644     layer->setContentsScale(2);
645     EXPECT_FLOAT_RECT_EQ(FloatRect(0, 0, 100, 100), layer->lastNeedsDisplayRect());
646
647     // The impl side should get 2x2 tiles now.
648     layer->prepareToUpdate(IntRect(0, 0, 200, 200), 0);
649     layer->updateCompositorResources(0, updater);
650     layer->pushPropertiesTo(layerImpl.get());
651     EXPECT_TRUE(layerImpl->hasTileAt(0, 0));
652     EXPECT_TRUE(layerImpl->hasTileAt(0, 1));
653     EXPECT_TRUE(layerImpl->hasTileAt(1, 0));
654     EXPECT_TRUE(layerImpl->hasTileAt(1, 1));
655
656     // Invalidate the entire layer again, but do not paint. All tiles should be gone now from the
657     // impl side.
658     layer->setNeedsDisplay();
659     layer->prepareToUpdate(IntRect(1, 0, 0, 1), 0);
660     layer->updateCompositorResources(0, updater);
661     layer->pushPropertiesTo(layerImpl.get());
662     EXPECT_FALSE(layerImpl->hasTileAt(0, 0));
663     EXPECT_FALSE(layerImpl->hasTileAt(0, 1));
664     EXPECT_FALSE(layerImpl->hasTileAt(1, 0));
665     EXPECT_FALSE(layerImpl->hasTileAt(1, 1));
666 }
667
668 TEST(TiledLayerChromiumTest, skipsDrawGetsReset)
669 {
670     // Initialize without threading support.
671     WebKit::WebCompositor::initialize(0);
672     FakeCCLayerTreeHostClient fakeCCLayerTreeHostClient;
673     RefPtr<CCLayerTreeHost> ccLayerTreeHost = CCLayerTreeHost::create(&fakeCCLayerTreeHostClient, CCSettings());
674
675     // Create two 300 x 300 tiled layers.
676     IntSize contentBounds(300, 300);
677     IntRect contentRect(IntPoint::zero(), contentBounds);
678
679     // We have enough memory for only one of the two layers.
680     int memoryLimit = 4 * 300 * 300; // 4 bytes per pixel.
681     OwnPtr<TextureManager> textureManager = TextureManager::create(memoryLimit, memoryLimit, memoryLimit);
682
683     RefPtr<FakeTiledLayerChromium> rootLayer = adoptRef(new FakeTiledLayerChromium(textureManager.get()));
684     RefPtr<FakeTiledLayerChromium> childLayer = adoptRef(new FakeTiledLayerChromium(textureManager.get()));
685     rootLayer->addChild(childLayer);
686
687     rootLayer->setBounds(contentBounds);
688     rootLayer->setPosition(FloatPoint(150, 150));
689     childLayer->setBounds(contentBounds);
690     childLayer->setPosition(FloatPoint(150, 150));
691     rootLayer->invalidateRect(contentRect);
692     childLayer->invalidateRect(contentRect);
693
694     FakeTextureAllocator textureAllocator;
695     CCTextureUpdater updater(&textureAllocator);
696
697     ccLayerTreeHost->setRootLayer(rootLayer);
698     ccLayerTreeHost->setViewportSize(IntSize(300, 300));
699     textureManager->setMaxMemoryLimitBytes(memoryLimit);
700     ccLayerTreeHost->updateLayers();
701     ccLayerTreeHost->updateCompositorResources(ccLayerTreeHost->context(), updater);
702
703     // We'll skip the root layer.
704     EXPECT_TRUE(rootLayer->skipsDraw());
705     EXPECT_FALSE(childLayer->skipsDraw());
706
707     ccLayerTreeHost->commitComplete();
708     textureManager->unprotectAllTextures(); // CCLayerTreeHost::commitComplete() normally does this, but since we're mocking out the manager we have to do it.
709
710     // Remove the child layer.
711     rootLayer->removeAllChildren();
712
713     ccLayerTreeHost->updateLayers();
714     EXPECT_FALSE(rootLayer->skipsDraw());
715
716     ccLayerTreeHost->setRootLayer(0);
717     ccLayerTreeHost.clear();
718     WebKit::WebCompositor::shutdown();
719 }
720
721 TEST(TiledLayerChromiumTest, resizeToSmaller)
722 {
723     OwnPtr<TextureManager> textureManager = TextureManager::create(60*1024*1024, 60*1024*1024, 1024);
724     RefPtr<FakeTiledLayerChromium> layer = adoptRef(new FakeTiledLayerChromium(textureManager.get()));
725
726     layer->setBounds(IntSize(700, 700));
727     layer->invalidateRect(IntRect(0, 0, 700, 700));
728     layer->prepareToUpdate(IntRect(0, 0, 700, 700), 0);
729
730     layer->setBounds(IntSize(200, 200));
731     layer->invalidateRect(IntRect(0, 0, 200, 200));
732 }
733
734 TEST(TiledLayerChromiumTest, partialUpdates)
735 {
736     CCSettings settings;
737     settings.maxPartialTextureUpdates = 4;
738     // Initialize without threading support.
739     WebKit::WebCompositor::initialize(0);
740     FakeCCLayerTreeHostClient fakeCCLayerTreeHostClient;
741     RefPtr<CCLayerTreeHost> ccLayerTreeHost = CCLayerTreeHost::create(&fakeCCLayerTreeHostClient, settings);
742
743     // Create one 500 x 300 tiled layer.
744     IntSize contentBounds(300, 200);
745     IntRect contentRect(IntPoint::zero(), contentBounds);
746
747     OwnPtr<TextureManager> textureManager = TextureManager::create(60*1024*1024, 60*1024*1024, 1024);
748     RefPtr<FakeTiledLayerChromium> layer = adoptRef(new FakeTiledLayerChromium(textureManager.get()));
749     layer->setBounds(contentBounds);
750     layer->setPosition(FloatPoint(150, 150));
751     layer->invalidateRect(contentRect);
752
753     FakeTextureAllocator textureAllocator;
754     CCTextureUpdater updater(&textureAllocator);
755
756     ccLayerTreeHost->setRootLayer(layer);
757     ccLayerTreeHost->setViewportSize(IntSize(300, 200));
758
759     // Full update of all 6 tiles.
760     ccLayerTreeHost->updateLayers();
761     ccLayerTreeHost->updateCompositorResources(ccLayerTreeHost->context(), updater);
762     updater.update(0, 4);
763     EXPECT_EQ(4, layer->fakeLayerTextureUpdater()->updateCount());
764     EXPECT_TRUE(updater.hasMoreUpdates());
765     layer->fakeLayerTextureUpdater()->clearUpdateCount();
766     updater.update(0, 4);
767     EXPECT_EQ(2, layer->fakeLayerTextureUpdater()->updateCount());
768     EXPECT_FALSE(updater.hasMoreUpdates());
769     layer->fakeLayerTextureUpdater()->clearUpdateCount();
770     ccLayerTreeHost->commitComplete();
771
772     // Full update of 3 tiles and partial update of 3 tiles.
773     layer->invalidateRect(IntRect(0, 0, 300, 150));
774     ccLayerTreeHost->updateLayers();
775     ccLayerTreeHost->updateCompositorResources(ccLayerTreeHost->context(), updater);
776     updater.update(0, 4);
777     EXPECT_EQ(3, layer->fakeLayerTextureUpdater()->updateCount());
778     EXPECT_TRUE(updater.hasMoreUpdates());
779     layer->fakeLayerTextureUpdater()->clearUpdateCount();
780     updater.update(0, 4);
781     EXPECT_EQ(3, layer->fakeLayerTextureUpdater()->updateCount());
782     EXPECT_FALSE(updater.hasMoreUpdates());
783     layer->fakeLayerTextureUpdater()->clearUpdateCount();
784     ccLayerTreeHost->commitComplete();
785
786     // Partial update of 6 tiles.
787     layer->invalidateRect(IntRect(50, 50, 200, 100));
788     ccLayerTreeHost->updateLayers();
789     ccLayerTreeHost->updateCompositorResources(ccLayerTreeHost->context(), updater);
790     updater.update(0, 4);
791     EXPECT_EQ(2, layer->fakeLayerTextureUpdater()->updateCount());
792     EXPECT_TRUE(updater.hasMoreUpdates());
793     layer->fakeLayerTextureUpdater()->clearUpdateCount();
794     updater.update(0, 4);
795     EXPECT_EQ(4, layer->fakeLayerTextureUpdater()->updateCount());
796     EXPECT_FALSE(updater.hasMoreUpdates());
797     layer->fakeLayerTextureUpdater()->clearUpdateCount();
798     ccLayerTreeHost->commitComplete();
799
800     ccLayerTreeHost->setRootLayer(0);
801     ccLayerTreeHost.clear();
802     WebKit::WebCompositor::shutdown();
803 }
804
805 TEST(TiledLayerChromiumTest, tilesPaintedWithoutOcclusion)
806 {
807     OwnPtr<TextureManager> textureManager = TextureManager::create(4*1024*1024, 2*1024*1024, 1024);
808     RefPtr<FakeTiledLayerChromium> layer = adoptRef(new FakeTiledLayerChromium(textureManager.get()));
809
810     // The tile size is 100x100, so this invalidates and then paints two tiles.
811     layer->setBounds(IntSize(100, 200));
812
813     layer->invalidateRect(IntRect(0, 0, 100, 200));
814     layer->prepareToUpdate(IntRect(0, 0, 100, 200), 0);
815     EXPECT_EQ(2, layer->fakeLayerTextureUpdater()->prepareRectCount());
816 }
817
818 TEST(TiledLayerChromiumTest, tilesPaintedWithOcclusion)
819 {
820     OwnPtr<TextureManager> textureManager = TextureManager::create(4*1024*1024, 2*1024*1024, 1024);
821     RefPtr<FakeTiledLayerChromium> layer = adoptRef(new FakeTiledLayerChromium(textureManager.get()));
822     TestCCOcclusionTracker occluded;
823
824     // The tile size is 100x100.
825
826     layer->setBounds(IntSize(600, 600));
827     layer->setDrawTransform(TransformationMatrix(1, 0, 0, 1, layer->bounds().width() / 2.0, layer->bounds().height() / 2.0));
828
829     occluded.setOcclusion(IntRect(200, 200, 300, 100));
830     layer->setVisibleLayerRect(IntRect(IntPoint(), layer->bounds()));
831     layer->invalidateRect(IntRect(0, 0, 600, 600));
832     layer->prepareToUpdate(IntRect(0, 0, 600, 600), &occluded);
833     EXPECT_EQ(36-3, layer->fakeLayerTextureUpdater()->prepareRectCount());
834
835     EXPECT_NEAR(occluded.overdrawMetrics().pixelsDrawnOpaque(), 0, 1);
836     EXPECT_NEAR(occluded.overdrawMetrics().pixelsDrawnTranslucent(), 330000, 1);
837     EXPECT_NEAR(occluded.overdrawMetrics().pixelsCulled(), 30000, 1);
838
839     layer->fakeLayerTextureUpdater()->clearPrepareRectCount();
840
841     occluded.setOcclusion(IntRect(250, 200, 300, 100));
842     layer->invalidateRect(IntRect(0, 0, 600, 600));
843     layer->prepareToUpdate(IntRect(0, 0, 600, 600), &occluded);
844     EXPECT_EQ(36-2, layer->fakeLayerTextureUpdater()->prepareRectCount());
845
846     EXPECT_NEAR(occluded.overdrawMetrics().pixelsDrawnOpaque(), 0, 1);
847     EXPECT_NEAR(occluded.overdrawMetrics().pixelsDrawnTranslucent(), 330000 + 340000, 1);
848     EXPECT_NEAR(occluded.overdrawMetrics().pixelsCulled(), 30000 + 20000, 1);
849
850     layer->fakeLayerTextureUpdater()->clearPrepareRectCount();
851
852     occluded.setOcclusion(IntRect(250, 250, 300, 100));
853     layer->invalidateRect(IntRect(0, 0, 600, 600));
854     layer->prepareToUpdate(IntRect(0, 0, 600, 600), &occluded);
855     EXPECT_EQ(36, layer->fakeLayerTextureUpdater()->prepareRectCount());
856
857     EXPECT_NEAR(occluded.overdrawMetrics().pixelsDrawnOpaque(), 0, 1);
858     EXPECT_NEAR(occluded.overdrawMetrics().pixelsDrawnTranslucent(), 330000 + 340000 + 360000, 1);
859     EXPECT_NEAR(occluded.overdrawMetrics().pixelsCulled(), 30000 + 20000, 1);
860 }
861
862 TEST(TiledLayerChromiumTest, tilesPaintedWithOcclusionAndVisiblityConstraints)
863 {
864     OwnPtr<TextureManager> textureManager = TextureManager::create(4*1024*1024, 2*1024*1024, 1024);
865     RefPtr<FakeTiledLayerChromium> layer = adoptRef(new FakeTiledLayerChromium(textureManager.get()));
866     TestCCOcclusionTracker occluded;
867
868     // The tile size is 100x100.
869
870     layer->setBounds(IntSize(600, 600));
871     layer->setDrawTransform(TransformationMatrix(1, 0, 0, 1, layer->bounds().width() / 2.0, layer->bounds().height() / 2.0));
872
873     // The partially occluded tiles (by the 150 occlusion height) are visible beyond the occlusion, so not culled.
874     occluded.setOcclusion(IntRect(200, 200, 300, 150));
875     layer->setVisibleLayerRect(IntRect(0, 0, 600, 360));
876     layer->invalidateRect(IntRect(0, 0, 600, 600));
877     layer->prepareToUpdate(IntRect(0, 0, 600, 600), &occluded);
878     EXPECT_EQ(24-3, layer->fakeLayerTextureUpdater()->prepareRectCount());
879
880     EXPECT_NEAR(occluded.overdrawMetrics().pixelsDrawnOpaque(), 0, 1);
881     EXPECT_NEAR(occluded.overdrawMetrics().pixelsDrawnTranslucent(), 210000, 1);
882     EXPECT_NEAR(occluded.overdrawMetrics().pixelsCulled(), 30000, 1);
883
884     layer->fakeLayerTextureUpdater()->clearPrepareRectCount();
885
886     // Now the visible region stops at the edge of the occlusion so the partly visible tiles become fully occluded.
887     occluded.setOcclusion(IntRect(200, 200, 300, 150));
888     layer->setVisibleLayerRect(IntRect(0, 0, 600, 350));
889     layer->invalidateRect(IntRect(0, 0, 600, 600));
890     layer->prepareToUpdate(IntRect(0, 0, 600, 600), &occluded);
891     EXPECT_EQ(24-6, layer->fakeLayerTextureUpdater()->prepareRectCount());
892
893     EXPECT_NEAR(occluded.overdrawMetrics().pixelsDrawnOpaque(), 0, 1);
894     EXPECT_NEAR(occluded.overdrawMetrics().pixelsDrawnTranslucent(), 210000 + 180000, 1);
895     EXPECT_NEAR(occluded.overdrawMetrics().pixelsCulled(), 30000 + 60000, 1);
896
897     layer->fakeLayerTextureUpdater()->clearPrepareRectCount();
898
899     // Now the visible region is even smaller than the occlusion, it should have the same result.
900     occluded.setOcclusion(IntRect(200, 200, 300, 150));
901     layer->setVisibleLayerRect(IntRect(0, 0, 600, 340));
902     layer->invalidateRect(IntRect(0, 0, 600, 600));
903     layer->prepareToUpdate(IntRect(0, 0, 600, 600), &occluded);
904     EXPECT_EQ(24-6, layer->fakeLayerTextureUpdater()->prepareRectCount());
905
906     EXPECT_NEAR(occluded.overdrawMetrics().pixelsDrawnOpaque(), 0, 1);
907     EXPECT_NEAR(occluded.overdrawMetrics().pixelsDrawnTranslucent(), 210000 + 180000 + 180000, 1);
908     EXPECT_NEAR(occluded.overdrawMetrics().pixelsCulled(), 30000 + 60000 + 60000, 1);
909
910 }
911
912 TEST(TiledLayerChromiumTest, tilesNotPaintedWithoutInvalidation)
913 {
914     OwnPtr<TextureManager> textureManager = TextureManager::create(4*1024*1024, 2*1024*1024, 1024);
915     RefPtr<FakeTiledLayerChromium> layer = adoptRef(new FakeTiledLayerChromium(textureManager.get()));
916     TestCCOcclusionTracker occluded;
917
918     // The tile size is 100x100.
919
920     layer->setBounds(IntSize(600, 600));
921     layer->setDrawTransform(TransformationMatrix(1, 0, 0, 1, layer->bounds().width() / 2.0, layer->bounds().height() / 2.0));
922
923     occluded.setOcclusion(IntRect(200, 200, 300, 100));
924     layer->setVisibleLayerRect(IntRect(0, 0, 600, 600));
925     layer->invalidateRect(IntRect(0, 0, 600, 600));
926     layer->prepareToUpdate(IntRect(0, 0, 600, 600), &occluded);
927     EXPECT_EQ(36-3, layer->fakeLayerTextureUpdater()->prepareRectCount());
928
929     EXPECT_NEAR(occluded.overdrawMetrics().pixelsDrawnOpaque(), 0, 1);
930     EXPECT_NEAR(occluded.overdrawMetrics().pixelsDrawnTranslucent(), 330000, 1);
931     EXPECT_NEAR(occluded.overdrawMetrics().pixelsCulled(), 30000, 1);
932
933     layer->fakeLayerTextureUpdater()->clearPrepareRectCount();
934
935     // Repaint without marking it dirty.
936     layer->prepareToUpdate(IntRect(0, 0, 600, 600), &occluded);
937     EXPECT_EQ(0, layer->fakeLayerTextureUpdater()->prepareRectCount());
938
939     EXPECT_NEAR(occluded.overdrawMetrics().pixelsDrawnOpaque(), 0, 1);
940     EXPECT_NEAR(occluded.overdrawMetrics().pixelsDrawnTranslucent(), 330000, 1);
941     EXPECT_NEAR(occluded.overdrawMetrics().pixelsCulled(), 30000, 1);
942 }
943
944 TEST(TiledLayerChromiumTest, tilesPaintedWithOcclusionAndTransforms)
945 {
946     OwnPtr<TextureManager> textureManager = TextureManager::create(4*1024*1024, 2*1024*1024, 1024);
947     RefPtr<FakeTiledLayerChromium> layer = adoptRef(new FakeTiledLayerChromium(textureManager.get()));
948     TestCCOcclusionTracker occluded;
949
950     // The tile size is 100x100.
951
952     // This makes sure the painting works when the occluded region (in screen space)
953     // is transformed differently than the layer.
954     layer->setBounds(IntSize(600, 600));
955     TransformationMatrix screenTransform;
956     screenTransform.scale(0.5);
957     layer->setScreenSpaceTransform(screenTransform);
958     layer->setDrawTransform(screenTransform * TransformationMatrix(1, 0, 0, 1, layer->bounds().width() / 2.0, layer->bounds().height() / 2.0));
959
960     occluded.setOcclusion(IntRect(100, 100, 150, 50));
961     layer->setVisibleLayerRect(IntRect(IntPoint(), layer->bounds()));
962     layer->invalidateRect(IntRect(0, 0, 600, 600));
963     layer->prepareToUpdate(IntRect(0, 0, 600, 600), &occluded);
964     EXPECT_EQ(36-3, layer->fakeLayerTextureUpdater()->prepareRectCount());
965
966     EXPECT_NEAR(occluded.overdrawMetrics().pixelsDrawnOpaque(), 0, 1);
967     EXPECT_NEAR(occluded.overdrawMetrics().pixelsDrawnTranslucent(), 330000, 1);
968     EXPECT_NEAR(occluded.overdrawMetrics().pixelsCulled(), 30000, 1);
969 }
970
971 TEST(TiledLayerChromiumTest, tilesPaintedWithOcclusionAndScaling)
972 {
973     OwnPtr<TextureManager> textureManager = TextureManager::create(4*1024*1024, 2*1024*1024, 1024);
974     RefPtr<FakeTiledLayerChromium> layer = adoptRef(new FakeTiledLayerChromium(textureManager.get()));
975     TestCCOcclusionTracker occluded;
976
977     // The tile size is 100x100.
978
979     // This makes sure the painting works when the content space is scaled to
980     // a different layer space. In this case tiles are scaled to be 200x200
981     // pixels, which means none should be occluded.
982     layer->setContentsScale(0.5);
983     layer->setBounds(IntSize(600, 600));
984     layer->setDrawTransform(TransformationMatrix(1, 0, 0, 1, layer->bounds().width() / 2.0, layer->bounds().height() / 2.0));
985
986     occluded.setOcclusion(IntRect(200, 200, 300, 100));
987     layer->setVisibleLayerRect(IntRect(IntPoint(), layer->bounds()));
988     layer->invalidateRect(IntRect(0, 0, 600, 600));
989     layer->prepareToUpdate(IntRect(0, 0, 600, 600), &occluded);
990     // The content is half the size of the layer (so the number of tiles is fewer).
991     // In this case, the content is 300x300, and since the tile size is 100, the
992     // number of tiles 3x3.
993     EXPECT_EQ(9, layer->fakeLayerTextureUpdater()->prepareRectCount());
994
995     EXPECT_NEAR(occluded.overdrawMetrics().pixelsDrawnOpaque(), 0, 1);
996     EXPECT_NEAR(occluded.overdrawMetrics().pixelsDrawnTranslucent(), 90000, 1);
997     EXPECT_NEAR(occluded.overdrawMetrics().pixelsCulled(), 0, 1);
998
999     layer->fakeLayerTextureUpdater()->clearPrepareRectCount();
1000
1001     // This makes sure the painting works when the content space is scaled to
1002     // a different layer space. In this case the occluded region catches the
1003     // blown up tiles.
1004     occluded.setOcclusion(IntRect(200, 200, 300, 200));
1005     layer->setVisibleLayerRect(IntRect(IntPoint(), layer->bounds()));
1006     layer->invalidateRect(IntRect(0, 0, 600, 600));
1007     layer->prepareToUpdate(IntRect(0, 0, 600, 600), &occluded);
1008     EXPECT_EQ(9-1, layer->fakeLayerTextureUpdater()->prepareRectCount());
1009
1010     EXPECT_NEAR(occluded.overdrawMetrics().pixelsDrawnOpaque(), 0, 1);
1011     EXPECT_NEAR(occluded.overdrawMetrics().pixelsDrawnTranslucent(), 90000 + 80000, 1);
1012     EXPECT_NEAR(occluded.overdrawMetrics().pixelsCulled(), 10000, 1);
1013
1014     layer->fakeLayerTextureUpdater()->clearPrepareRectCount();
1015
1016     // This makes sure content scaling and transforms work together.
1017     TransformationMatrix screenTransform;
1018     screenTransform.scale(0.5);
1019     layer->setScreenSpaceTransform(screenTransform);
1020     layer->setDrawTransform(screenTransform * TransformationMatrix(1, 0, 0, 1, layer->bounds().width() / 2.0, layer->bounds().height() / 2.0));
1021
1022     occluded.setOcclusion(IntRect(100, 100, 150, 100));
1023     layer->setVisibleLayerRect(IntRect(IntPoint(), layer->bounds()));
1024     layer->invalidateRect(IntRect(0, 0, 600, 600));
1025     layer->prepareToUpdate(IntRect(0, 0, 600, 600), &occluded);
1026     EXPECT_EQ(9-1, layer->fakeLayerTextureUpdater()->prepareRectCount());
1027
1028     EXPECT_NEAR(occluded.overdrawMetrics().pixelsDrawnOpaque(), 0, 1);
1029     EXPECT_NEAR(occluded.overdrawMetrics().pixelsDrawnTranslucent(), 90000 + 80000 + 80000, 1);
1030     EXPECT_NEAR(occluded.overdrawMetrics().pixelsCulled(), 10000 + 10000, 1);
1031 }
1032
1033 TEST(TiledLayerChromiumTest, opaqueContentsRegion)
1034 {
1035     OwnPtr<TextureManager> textureManager = TextureManager::create(4*1024*1024, 2*1024*1024, 1024);
1036     RefPtr<FakeTiledLayerChromium> layer = adoptRef(new FakeTiledLayerChromium(textureManager.get()));
1037     TestCCOcclusionTracker occluded;
1038
1039     // The tile size is 100x100, so this invalidates and then paints two tiles in various ways.
1040
1041     IntRect opaquePaintRect;
1042     Region opaqueContents;
1043
1044     IntRect contentBounds = IntRect(0, 0, 100, 200);
1045     IntRect visibleBounds = IntRect(0, 0, 100, 150);
1046
1047     layer->setBounds(contentBounds.size());
1048     layer->setDrawTransform(TransformationMatrix(1, 0, 0, 1, layer->bounds().width() / 2.0, layer->bounds().height() / 2.0));
1049     layer->setVisibleLayerRect(visibleBounds);
1050     layer->setDrawOpacity(1);
1051
1052     // If the layer doesn't paint opaque content, then the opaqueContentsRegion should be empty.
1053     layer->fakeLayerTextureUpdater()->setOpaquePaintRect(IntRect());
1054     layer->invalidateRect(contentBounds);
1055     layer->prepareToUpdate(contentBounds, &occluded);
1056     opaqueContents = layer->opaqueContentsRegion();
1057     EXPECT_TRUE(opaqueContents.isEmpty());
1058
1059     EXPECT_NEAR(occluded.overdrawMetrics().pixelsDrawnOpaque(), 0, 1);
1060     EXPECT_NEAR(occluded.overdrawMetrics().pixelsDrawnTranslucent(), 20000, 1);
1061     EXPECT_NEAR(occluded.overdrawMetrics().pixelsCulled(), 0, 1);
1062
1063     // opaqueContentsRegion should match the visible part of what is painted opaque.
1064     opaquePaintRect = IntRect(10, 10, 90, 190);
1065     layer->fakeLayerTextureUpdater()->setOpaquePaintRect(opaquePaintRect);
1066     layer->invalidateRect(contentBounds);
1067     layer->prepareToUpdate(contentBounds, &occluded);
1068     opaqueContents = layer->opaqueContentsRegion();
1069     EXPECT_EQ_RECT(intersection(opaquePaintRect, visibleBounds), opaqueContents.bounds());
1070     EXPECT_EQ(1u, opaqueContents.rects().size());
1071
1072     EXPECT_NEAR(occluded.overdrawMetrics().pixelsDrawnOpaque(), 17100, 1);
1073     EXPECT_NEAR(occluded.overdrawMetrics().pixelsDrawnTranslucent(), 20000 + 20000 - 17100, 1);
1074     EXPECT_NEAR(occluded.overdrawMetrics().pixelsCulled(), 0, 1);
1075
1076     // If we paint again without invalidating, the same stuff should be opaque.
1077     layer->fakeLayerTextureUpdater()->setOpaquePaintRect(IntRect());
1078     layer->prepareToUpdate(contentBounds, &occluded);
1079     opaqueContents = layer->opaqueContentsRegion();
1080     EXPECT_EQ_RECT(intersection(opaquePaintRect, visibleBounds), opaqueContents.bounds());
1081     EXPECT_EQ(1u, opaqueContents.rects().size());
1082
1083     EXPECT_NEAR(occluded.overdrawMetrics().pixelsDrawnOpaque(), 17100, 1);
1084     EXPECT_NEAR(occluded.overdrawMetrics().pixelsDrawnTranslucent(), 20000 + 20000 - 17100, 1);
1085     EXPECT_NEAR(occluded.overdrawMetrics().pixelsCulled(), 0, 1);
1086
1087     // If we repaint a non-opaque part of the tile, then it shouldn't lose its opaque-ness. And other tiles should
1088     // not be affected.
1089     layer->fakeLayerTextureUpdater()->setOpaquePaintRect(IntRect());
1090     layer->invalidateRect(IntRect(0, 0, 1, 1));
1091     layer->prepareToUpdate(contentBounds, &occluded);
1092     opaqueContents = layer->opaqueContentsRegion();
1093     EXPECT_EQ_RECT(intersection(opaquePaintRect, visibleBounds), opaqueContents.bounds());
1094     EXPECT_EQ(1u, opaqueContents.rects().size());
1095
1096     EXPECT_NEAR(occluded.overdrawMetrics().pixelsDrawnOpaque(), 17100, 1);
1097     EXPECT_NEAR(occluded.overdrawMetrics().pixelsDrawnTranslucent(), 20000 + 20000 - 17100 + 1, 1);
1098     EXPECT_NEAR(occluded.overdrawMetrics().pixelsCulled(), 0, 1);
1099
1100     // If we repaint an opaque part of the tile, then it should lose its opaque-ness. But other tiles should still
1101     // not be affected.
1102     layer->fakeLayerTextureUpdater()->setOpaquePaintRect(IntRect());
1103     layer->invalidateRect(IntRect(10, 10, 1, 1));
1104     layer->prepareToUpdate(contentBounds, &occluded);
1105     opaqueContents = layer->opaqueContentsRegion();
1106     EXPECT_EQ_RECT(intersection(IntRect(10, 100, 90, 100), visibleBounds), opaqueContents.bounds());
1107     EXPECT_EQ(1u, opaqueContents.rects().size());
1108
1109     EXPECT_NEAR(occluded.overdrawMetrics().pixelsDrawnOpaque(), 17100, 1);
1110     EXPECT_NEAR(occluded.overdrawMetrics().pixelsDrawnTranslucent(), 20000 + 20000 - 17100 + 1 + 1, 1);
1111     EXPECT_NEAR(occluded.overdrawMetrics().pixelsCulled(), 0, 1);
1112 }
1113
1114 } // namespace