Delete unused downloads page asset.
[chromium-blink-merge.git] / cc / layers / picture_layer_impl_unittest.cc
blobdaa80e9b015c5b01290f6db5ff3500608d4d4839
1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "cc/layers/picture_layer_impl.h"
7 #include <algorithm>
8 #include <limits>
9 #include <set>
10 #include <utility>
12 #include "base/location.h"
13 #include "base/thread_task_runner_handle.h"
14 #include "cc/base/math_util.h"
15 #include "cc/layers/append_quads_data.h"
16 #include "cc/layers/picture_layer.h"
17 #include "cc/quads/draw_quad.h"
18 #include "cc/quads/tile_draw_quad.h"
19 #include "cc/test/begin_frame_args_test.h"
20 #include "cc/test/fake_content_layer_client.h"
21 #include "cc/test/fake_impl_proxy.h"
22 #include "cc/test/fake_layer_tree_host_impl.h"
23 #include "cc/test/fake_output_surface.h"
24 #include "cc/test/fake_picture_layer_impl.h"
25 #include "cc/test/fake_picture_pile_impl.h"
26 #include "cc/test/geometry_test_utils.h"
27 #include "cc/test/gpu_rasterization_enabled_settings.h"
28 #include "cc/test/layer_test_common.h"
29 #include "cc/test/test_shared_bitmap_manager.h"
30 #include "cc/test/test_task_graph_runner.h"
31 #include "cc/test/test_web_graphics_context_3d.h"
32 #include "cc/tiles/tiling_set_raster_queue_all.h"
33 #include "cc/tiles/tiling_set_raster_queue_required.h"
34 #include "cc/trees/layer_tree_impl.h"
35 #include "testing/gtest/include/gtest/gtest.h"
36 #include "ui/gfx/geometry/rect_conversions.h"
37 #include "ui/gfx/geometry/size_conversions.h"
39 namespace cc {
40 namespace {
42 #define EXPECT_BOTH_EQ(expression, x) \
43 do { \
44 EXPECT_EQ(x, pending_layer_->expression); \
45 EXPECT_EQ(x, active_layer_->expression); \
46 } while (false)
48 #define EXPECT_BOTH_NE(expression, x) \
49 do { \
50 EXPECT_NE(x, pending_layer_->expression); \
51 EXPECT_NE(x, active_layer_->expression); \
52 } while (false)
54 #define EXPECT_BOTH_TRUE(expression) \
55 do { \
56 EXPECT_TRUE(pending_layer_->expression); \
57 EXPECT_TRUE(active_layer_->expression); \
58 } while (false)
60 #define EXPECT_BOTH_FALSE(expression) \
61 do { \
62 EXPECT_FALSE(pending_layer_->expression); \
63 EXPECT_FALSE(active_layer_->expression); \
64 } while (false)
66 class MockCanvas : public SkCanvas {
67 public:
68 explicit MockCanvas(int w, int h) : SkCanvas(w, h) {}
70 void onDrawRect(const SkRect& rect, const SkPaint& paint) override {
71 // Capture calls before SkCanvas quickReject() kicks in.
72 rects_.push_back(rect);
75 std::vector<SkRect> rects_;
78 class PictureLayerImplTestSettings : public GpuRasterizationEnabledSettings {
79 public:
80 PictureLayerImplTestSettings() {
81 layer_transforms_should_scale_layer_contents = true;
85 class NoLowResTilingsSettings : public PictureLayerImplTestSettings {};
87 class LowResTilingsSettings : public PictureLayerImplTestSettings {
88 public:
89 LowResTilingsSettings() { create_low_res_tiling = true; }
92 class PictureLayerImplTest : public testing::Test {
93 public:
94 PictureLayerImplTest()
95 : proxy_(base::ThreadTaskRunnerHandle::Get()),
96 host_impl_(LowResTilingsSettings(),
97 &proxy_,
98 &shared_bitmap_manager_,
99 &task_graph_runner_),
100 root_id_(6),
101 id_(7),
102 pending_layer_(nullptr),
103 old_pending_layer_(nullptr),
104 active_layer_(nullptr) {
105 host_impl_.SetViewportSize(gfx::Size(10000, 10000));
108 explicit PictureLayerImplTest(const LayerTreeSettings& settings)
109 : proxy_(base::ThreadTaskRunnerHandle::Get()),
110 host_impl_(settings,
111 &proxy_,
112 &shared_bitmap_manager_,
113 &task_graph_runner_),
114 root_id_(6),
115 id_(7) {
116 host_impl_.SetViewportSize(gfx::Size(10000, 10000));
119 ~PictureLayerImplTest() override {}
121 void SetUp() override { InitializeRenderer(); }
123 virtual void InitializeRenderer() {
124 host_impl_.InitializeRenderer(FakeOutputSurface::Create3d());
127 void SetupDefaultTrees(const gfx::Size& layer_bounds) {
128 gfx::Size tile_size(100, 100);
130 scoped_refptr<FakePicturePileImpl> pending_pile =
131 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
132 scoped_refptr<FakePicturePileImpl> active_pile =
133 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
135 SetupTrees(pending_pile, active_pile);
138 void SetupDefaultTreesWithInvalidation(const gfx::Size& layer_bounds,
139 const Region& invalidation) {
140 gfx::Size tile_size(100, 100);
142 scoped_refptr<FakePicturePileImpl> pending_pile =
143 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
144 scoped_refptr<FakePicturePileImpl> active_pile =
145 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
147 SetupTreesWithInvalidation(pending_pile, active_pile, invalidation);
150 void ActivateTree() {
151 host_impl_.ActivateSyncTree();
152 CHECK(!host_impl_.pending_tree());
153 CHECK(host_impl_.recycle_tree());
154 old_pending_layer_ = pending_layer_;
155 pending_layer_ = nullptr;
156 active_layer_ = static_cast<FakePictureLayerImpl*>(
157 host_impl_.active_tree()->LayerById(id_));
159 bool update_lcd_text = false;
160 host_impl_.active_tree()->UpdateDrawProperties(update_lcd_text);
163 void SetupDefaultTreesWithFixedTileSize(const gfx::Size& layer_bounds,
164 const gfx::Size& tile_size,
165 const Region& invalidation) {
166 gfx::Size pile_tile_size(100, 100);
168 scoped_refptr<FakePicturePileImpl> pending_pile =
169 FakePicturePileImpl::CreateFilledPile(pile_tile_size, layer_bounds);
170 scoped_refptr<FakePicturePileImpl> active_pile =
171 FakePicturePileImpl::CreateFilledPile(pile_tile_size, layer_bounds);
173 SetupTreesWithFixedTileSize(pending_pile, active_pile, tile_size,
174 invalidation);
177 void SetupTrees(
178 scoped_refptr<PicturePileImpl> pending_pile,
179 scoped_refptr<PicturePileImpl> active_pile) {
180 SetupPendingTree(active_pile);
181 ActivateTree();
182 SetupPendingTreeInternal(pending_pile, gfx::Size(), Region());
185 void SetupTreesWithInvalidation(scoped_refptr<PicturePileImpl> pending_pile,
186 scoped_refptr<PicturePileImpl> active_pile,
187 const Region& pending_invalidation) {
188 SetupPendingTreeInternal(active_pile, gfx::Size(), Region());
189 ActivateTree();
190 SetupPendingTreeInternal(pending_pile, gfx::Size(), pending_invalidation);
193 void SetupTreesWithFixedTileSize(scoped_refptr<PicturePileImpl> pending_pile,
194 scoped_refptr<PicturePileImpl> active_pile,
195 const gfx::Size& tile_size,
196 const Region& pending_invalidation) {
197 SetupPendingTreeInternal(active_pile, tile_size, Region());
198 ActivateTree();
199 SetupPendingTreeInternal(pending_pile, tile_size, pending_invalidation);
202 void SetupPendingTree(scoped_refptr<RasterSource> raster_source) {
203 SetupPendingTreeInternal(raster_source, gfx::Size(), Region());
206 void SetupPendingTreeWithInvalidation(
207 scoped_refptr<RasterSource> raster_source,
208 const Region& invalidation) {
209 SetupPendingTreeInternal(raster_source, gfx::Size(), invalidation);
212 void SetupPendingTreeWithFixedTileSize(
213 scoped_refptr<RasterSource> raster_source,
214 const gfx::Size& tile_size,
215 const Region& invalidation) {
216 SetupPendingTreeInternal(raster_source, tile_size, invalidation);
219 void SetupPendingTreeInternal(scoped_refptr<RasterSource> raster_source,
220 const gfx::Size& tile_size,
221 const Region& invalidation) {
222 host_impl_.CreatePendingTree();
223 host_impl_.pending_tree()->PushPageScaleFromMainThread(1.f, 0.00001f,
224 100000.f);
225 LayerTreeImpl* pending_tree = host_impl_.pending_tree();
227 // Steal from the recycled tree if possible.
228 scoped_ptr<LayerImpl> pending_root = pending_tree->DetachLayerTree();
229 scoped_ptr<FakePictureLayerImpl> pending_layer;
230 DCHECK_IMPLIES(pending_root, pending_root->id() == root_id_);
231 if (!pending_root) {
232 pending_root = LayerImpl::Create(pending_tree, root_id_);
233 pending_layer = FakePictureLayerImpl::Create(pending_tree, id_);
234 if (!tile_size.IsEmpty())
235 pending_layer->set_fixed_tile_size(tile_size);
236 pending_layer->SetDrawsContent(true);
237 } else {
238 pending_layer.reset(static_cast<FakePictureLayerImpl*>(
239 pending_root->RemoveChild(pending_root->children()[0]).release()));
240 if (!tile_size.IsEmpty())
241 pending_layer->set_fixed_tile_size(tile_size);
243 pending_root->SetHasRenderSurface(true);
244 // The bounds() just mirror the pile size.
245 pending_layer->SetBounds(raster_source->GetSize());
246 pending_layer->SetRasterSourceOnPending(raster_source, invalidation);
248 pending_root->AddChild(pending_layer.Pass());
249 pending_tree->SetRootLayer(pending_root.Pass());
251 pending_layer_ = static_cast<FakePictureLayerImpl*>(
252 host_impl_.pending_tree()->LayerById(id_));
254 // Add tilings/tiles for the layer.
255 bool update_lcd_text = false;
256 host_impl_.pending_tree()->UpdateDrawProperties(update_lcd_text);
259 void SetupDrawPropertiesAndUpdateTiles(
260 FakePictureLayerImpl* layer,
261 float ideal_contents_scale,
262 float device_scale_factor,
263 float page_scale_factor,
264 float maximum_animation_contents_scale,
265 float starting_animation_contents_scale,
266 bool animating_transform_to_screen) {
267 host_impl_.SetDeviceScaleFactor(device_scale_factor);
268 host_impl_.SetPageScaleOnActiveTree(page_scale_factor);
270 gfx::Transform scale_transform;
271 scale_transform.Scale(ideal_contents_scale, ideal_contents_scale);
272 layer->draw_properties().target_space_transform = scale_transform;
273 DCHECK_EQ(layer->GetIdealContentsScale(), ideal_contents_scale);
274 layer->draw_properties().maximum_animation_contents_scale =
275 maximum_animation_contents_scale;
276 layer->draw_properties().starting_animation_contents_scale =
277 starting_animation_contents_scale;
278 layer->draw_properties().screen_space_transform_is_animating =
279 animating_transform_to_screen;
280 bool resourceless_software_draw = false;
281 layer->UpdateTiles(resourceless_software_draw);
283 static void VerifyAllPrioritizedTilesExistAndHavePile(
284 const PictureLayerTiling* tiling,
285 PicturePileImpl* pile) {
286 auto prioritized_tiles =
287 tiling->UpdateAndGetAllPrioritizedTilesForTesting();
288 for (PictureLayerTiling::CoverageIterator iter(
289 tiling,
290 tiling->contents_scale(),
291 gfx::Rect(tiling->tiling_size()));
292 iter;
293 ++iter) {
294 EXPECT_TRUE(*iter);
295 EXPECT_EQ(pile, prioritized_tiles[*iter].raster_source());
299 void SetContentsScaleOnBothLayers(float contents_scale,
300 float device_scale_factor,
301 float page_scale_factor,
302 float maximum_animation_contents_scale,
303 float starting_animation_contents_scale,
304 bool animating_transform) {
305 SetupDrawPropertiesAndUpdateTiles(
306 pending_layer_, contents_scale, device_scale_factor, page_scale_factor,
307 maximum_animation_contents_scale, starting_animation_contents_scale,
308 animating_transform);
310 SetupDrawPropertiesAndUpdateTiles(
311 active_layer_, contents_scale, device_scale_factor, page_scale_factor,
312 maximum_animation_contents_scale, starting_animation_contents_scale,
313 animating_transform);
316 void ResetTilingsAndRasterScales() {
317 if (pending_layer_) {
318 pending_layer_->ReleaseResources();
319 EXPECT_FALSE(pending_layer_->tilings());
320 pending_layer_->RecreateResources();
321 EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings());
324 if (active_layer_) {
325 active_layer_->ReleaseResources();
326 EXPECT_FALSE(active_layer_->tilings());
327 active_layer_->RecreateResources();
328 EXPECT_EQ(0u, active_layer_->tilings()->num_tilings());
332 size_t NumberOfTilesRequired(PictureLayerTiling* tiling) {
333 size_t num_required = 0;
334 std::vector<Tile*> tiles = tiling->AllTilesForTesting();
335 for (size_t i = 0; i < tiles.size(); ++i) {
336 if (tiles[i]->required_for_activation())
337 num_required++;
339 return num_required;
342 void AssertAllTilesRequired(PictureLayerTiling* tiling) {
343 std::vector<Tile*> tiles = tiling->AllTilesForTesting();
344 for (size_t i = 0; i < tiles.size(); ++i)
345 EXPECT_TRUE(tiles[i]->required_for_activation()) << "i: " << i;
346 EXPECT_GT(tiles.size(), 0u);
349 void AssertNoTilesRequired(PictureLayerTiling* tiling) {
350 std::vector<Tile*> tiles = tiling->AllTilesForTesting();
351 for (size_t i = 0; i < tiles.size(); ++i)
352 EXPECT_FALSE(tiles[i]->required_for_activation()) << "i: " << i;
353 EXPECT_GT(tiles.size(), 0u);
356 protected:
357 void TestQuadsForSolidColor(bool test_for_solid);
359 FakeImplProxy proxy_;
360 TestSharedBitmapManager shared_bitmap_manager_;
361 TestTaskGraphRunner task_graph_runner_;
362 FakeLayerTreeHostImpl host_impl_;
363 int root_id_;
364 int id_;
365 FakePictureLayerImpl* pending_layer_;
366 FakePictureLayerImpl* old_pending_layer_;
367 FakePictureLayerImpl* active_layer_;
368 LayerSettings layer_settings_;
370 private:
371 DISALLOW_COPY_AND_ASSIGN(PictureLayerImplTest);
374 class NoLowResPictureLayerImplTest : public PictureLayerImplTest {
375 public:
376 NoLowResPictureLayerImplTest()
377 : PictureLayerImplTest(NoLowResTilingsSettings()) {}
380 TEST_F(PictureLayerImplTest, TileGridAlignment) {
381 // Layer to span 4 raster tiles in x and in y
382 LayerTreeSettings settings;
383 gfx::Size layer_size(settings.default_tile_size.width() * 7 / 2,
384 settings.default_tile_size.height() * 7 / 2);
386 scoped_refptr<FakePicturePileImpl> pending_pile =
387 FakePicturePileImpl::CreateFilledPile(layer_size, layer_size);
389 scoped_ptr<FakePicturePile> active_recording =
390 FakePicturePile::CreateFilledPile(layer_size, layer_size);
391 scoped_refptr<FakePicturePileImpl> active_pile =
392 FakePicturePileImpl::CreateFromPile(active_recording.get(), nullptr);
394 SetupTrees(pending_pile, active_pile);
396 // Add 1x1 rects at the centers of each tile, then re-record pile contents
397 active_layer_->tilings()->tiling_at(0)->CreateAllTilesForTesting();
398 std::vector<Tile*> tiles =
399 active_layer_->tilings()->tiling_at(0)->AllTilesForTesting();
400 EXPECT_EQ(16u, tiles.size());
401 std::vector<SkRect> rects;
402 std::vector<Tile*>::const_iterator tile_iter;
403 for (tile_iter = tiles.begin(); tile_iter < tiles.end(); tile_iter++) {
404 gfx::Point tile_center = (*tile_iter)->content_rect().CenterPoint();
405 gfx::Rect rect(tile_center.x(), tile_center.y(), 1, 1);
406 active_recording->add_draw_rect(rect);
407 rects.push_back(SkRect::MakeXYWH(rect.x(), rect.y(), 1, 1));
410 // Force re-raster with newly injected content
411 active_recording->RemoveRecordingAt(0, 0);
412 active_recording->AddRecordingAt(0, 0);
414 scoped_refptr<FakePicturePileImpl> updated_active_pile =
415 FakePicturePileImpl::CreateFromPile(active_recording.get(), nullptr);
417 std::vector<SkRect>::const_iterator rect_iter = rects.begin();
418 for (tile_iter = tiles.begin(); tile_iter < tiles.end(); tile_iter++) {
419 MockCanvas mock_canvas(1000, 1000);
420 updated_active_pile->PlaybackToSharedCanvas(
421 &mock_canvas, (*tile_iter)->content_rect(), 1.0f);
423 // This test verifies that when drawing the contents of a specific tile
424 // at content scale 1.0, the playback canvas never receives content from
425 // neighboring tiles which indicates that the tile grid embedded in
426 // SkPicture is perfectly aligned with the compositor's tiles.
427 EXPECT_EQ(1u, mock_canvas.rects_.size());
428 EXPECT_EQ(*rect_iter, mock_canvas.rects_[0]);
429 rect_iter++;
433 TEST_F(PictureLayerImplTest, CloneNoInvalidation) {
434 gfx::Size tile_size(100, 100);
435 gfx::Size layer_bounds(400, 400);
437 scoped_refptr<FakePicturePileImpl> pending_pile =
438 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
439 scoped_refptr<FakePicturePileImpl> active_pile =
440 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
442 SetupTreesWithInvalidation(pending_pile, active_pile, Region());
444 EXPECT_EQ(pending_layer_->tilings()->num_tilings(),
445 active_layer_->tilings()->num_tilings());
447 const PictureLayerTilingSet* tilings = pending_layer_->tilings();
448 EXPECT_GT(tilings->num_tilings(), 0u);
449 for (size_t i = 0; i < tilings->num_tilings(); ++i)
450 EXPECT_TRUE(tilings->tiling_at(i)->AllTilesForTesting().empty());
453 TEST_F(PictureLayerImplTest, ExternalViewportRectForPrioritizingTiles) {
454 host_impl_.AdvanceToNextFrame(base::TimeDelta::FromMilliseconds(1));
455 gfx::Size tile_size(100, 100);
456 gfx::Size layer_bounds(400, 400);
458 scoped_refptr<FakePicturePileImpl> pending_pile =
459 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
460 scoped_refptr<FakePicturePileImpl> active_pile =
461 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
463 SetupTreesWithInvalidation(pending_pile, active_pile, Region());
465 SetupDrawPropertiesAndUpdateTiles(active_layer_, 1.f, 1.f, 1.f, 1.f, 0.f,
466 false);
468 host_impl_.AdvanceToNextFrame(base::TimeDelta::FromMilliseconds(200));
470 // Update tiles with viewport for tile priority as (0, 0, 100, 100) and the
471 // identify transform for tile priority.
472 bool resourceless_software_draw = false;
473 gfx::Rect viewport = gfx::Rect(layer_bounds),
474 viewport_rect_for_tile_priority = gfx::Rect(0, 0, 100, 100);
475 gfx::Transform transform, transform_for_tile_priority;
477 host_impl_.SetExternalDrawConstraints(transform,
478 viewport,
479 viewport,
480 viewport_rect_for_tile_priority,
481 transform_for_tile_priority,
482 resourceless_software_draw);
483 bool update_lcd_text = false;
484 host_impl_.active_tree()->UpdateDrawProperties(update_lcd_text);
486 gfx::Rect viewport_rect_for_tile_priority_in_view_space =
487 viewport_rect_for_tile_priority;
489 // Verify the viewport rect for tile priority is used in picture layer tiling.
490 EXPECT_EQ(viewport_rect_for_tile_priority_in_view_space,
491 active_layer_->viewport_rect_for_tile_priority_in_content_space());
492 PictureLayerTilingSet* tilings = active_layer_->tilings();
493 for (size_t i = 0; i < tilings->num_tilings(); i++) {
494 PictureLayerTiling* tiling = tilings->tiling_at(i);
495 EXPECT_EQ(
496 tiling->GetCurrentVisibleRectForTesting(),
497 gfx::ScaleToEnclosingRect(viewport_rect_for_tile_priority_in_view_space,
498 tiling->contents_scale()));
501 // Update tiles with viewport for tile priority as (200, 200, 100, 100) in
502 // screen space and the transform for tile priority is translated and
503 // rotated. The actual viewport for tile priority used by PictureLayerImpl
504 // should be (200, 200, 100, 100) applied with the said transform.
505 host_impl_.AdvanceToNextFrame(base::TimeDelta::FromMilliseconds(200));
507 viewport_rect_for_tile_priority = gfx::Rect(200, 200, 100, 100);
508 transform_for_tile_priority.Translate(100, 100);
509 transform_for_tile_priority.Rotate(45);
510 host_impl_.SetExternalDrawConstraints(transform,
511 viewport,
512 viewport,
513 viewport_rect_for_tile_priority,
514 transform_for_tile_priority,
515 resourceless_software_draw);
516 host_impl_.active_tree()->UpdateDrawProperties(update_lcd_text);
518 gfx::Transform screen_to_view(gfx::Transform::kSkipInitialization);
519 bool success = transform_for_tile_priority.GetInverse(&screen_to_view);
520 EXPECT_TRUE(success);
522 // Note that we don't clip this to the layer bounds, since it is expected that
523 // the rect will sometimes be outside of the layer bounds. If we clip to
524 // bounds, then tile priorities will end up being incorrect in cases of fully
525 // offscreen layer.
526 viewport_rect_for_tile_priority_in_view_space =
527 gfx::ToEnclosingRect(MathUtil::ProjectClippedRect(
528 screen_to_view, viewport_rect_for_tile_priority));
530 EXPECT_EQ(viewport_rect_for_tile_priority_in_view_space,
531 active_layer_->viewport_rect_for_tile_priority_in_content_space());
532 tilings = active_layer_->tilings();
533 for (size_t i = 0; i < tilings->num_tilings(); i++) {
534 PictureLayerTiling* tiling = tilings->tiling_at(i);
535 EXPECT_EQ(
536 tiling->GetCurrentVisibleRectForTesting(),
537 gfx::ScaleToEnclosingRect(viewport_rect_for_tile_priority_in_view_space,
538 tiling->contents_scale()));
542 TEST_F(PictureLayerImplTest, InvalidViewportForPrioritizingTiles) {
543 host_impl_.AdvanceToNextFrame(base::TimeDelta::FromMilliseconds(1));
545 gfx::Size tile_size(100, 100);
546 gfx::Size layer_bounds(400, 400);
548 scoped_refptr<FakePicturePileImpl> pending_pile =
549 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
550 scoped_refptr<FakePicturePileImpl> active_pile =
551 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
553 SetupTreesWithInvalidation(pending_pile, active_pile, Region());
555 SetupDrawPropertiesAndUpdateTiles(active_layer_, 1.f, 1.f, 1.f, 1.f, 0.f,
556 false);
558 // UpdateTiles with valid viewport. Should update tile viewport.
559 // Note viewport is considered invalid if and only if in resourceless
560 // software draw.
561 bool resourceless_software_draw = false;
562 gfx::Rect viewport = gfx::Rect(layer_bounds);
563 gfx::Transform transform;
564 host_impl_.SetExternalDrawConstraints(transform,
565 viewport,
566 viewport,
567 viewport,
568 transform,
569 resourceless_software_draw);
570 active_layer_->draw_properties().visible_layer_rect = viewport;
571 active_layer_->draw_properties().screen_space_transform = transform;
572 active_layer_->UpdateTiles(resourceless_software_draw);
574 gfx::Rect visible_rect_for_tile_priority =
575 active_layer_->visible_rect_for_tile_priority();
576 EXPECT_FALSE(visible_rect_for_tile_priority.IsEmpty());
577 gfx::Transform screen_space_transform_for_tile_priority =
578 active_layer_->screen_space_transform();
580 // Expand viewport and set it as invalid for prioritizing tiles.
581 // Should update viewport and transform, but not update visible rect.
582 host_impl_.AdvanceToNextFrame(base::TimeDelta::FromMilliseconds(200));
583 resourceless_software_draw = true;
584 viewport = gfx::ScaleToEnclosingRect(viewport, 2);
585 transform.Translate(1.f, 1.f);
586 active_layer_->draw_properties().visible_layer_rect = viewport;
587 active_layer_->draw_properties().screen_space_transform = transform;
588 host_impl_.SetExternalDrawConstraints(transform,
589 viewport,
590 viewport,
591 viewport,
592 transform,
593 resourceless_software_draw);
594 active_layer_->UpdateTiles(resourceless_software_draw);
596 // Transform for tile priority is updated.
597 EXPECT_TRANSFORMATION_MATRIX_EQ(transform,
598 active_layer_->screen_space_transform());
599 // Visible rect for tile priority retains old value.
600 EXPECT_EQ(visible_rect_for_tile_priority,
601 active_layer_->visible_rect_for_tile_priority());
603 // Keep expanded viewport but mark it valid. Should update tile viewport.
604 host_impl_.AdvanceToNextFrame(base::TimeDelta::FromMilliseconds(200));
605 resourceless_software_draw = false;
606 host_impl_.SetExternalDrawConstraints(transform,
607 viewport,
608 viewport,
609 viewport,
610 transform,
611 resourceless_software_draw);
612 active_layer_->UpdateTiles(resourceless_software_draw);
614 EXPECT_TRANSFORMATION_MATRIX_EQ(transform,
615 active_layer_->screen_space_transform());
616 EXPECT_EQ(viewport, active_layer_->visible_rect_for_tile_priority());
619 TEST_F(PictureLayerImplTest, ViewportRectForTilePriorityIsCached) {
620 host_impl_.AdvanceToNextFrame(base::TimeDelta::FromMilliseconds(1));
621 gfx::Size tile_size(100, 100);
622 gfx::Size layer_bounds(400, 400);
624 scoped_refptr<FakePicturePileImpl> pending_pile =
625 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
626 scoped_refptr<FakePicturePileImpl> active_pile =
627 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
629 SetupTreesWithInvalidation(pending_pile, active_pile, Region());
631 SetupDrawPropertiesAndUpdateTiles(active_layer_, 1.f, 1.f, 1.f, 1.f, 0.f,
632 false);
634 host_impl_.AdvanceToNextFrame(base::TimeDelta::FromMilliseconds(200));
636 bool resourceless_software_draw = false;
637 gfx::Rect viewport = gfx::Rect(layer_bounds);
638 gfx::Rect viewport_rect_for_tile_priority(0, 0, 100, 100);
639 gfx::Transform transform, transform_for_tile_priority;
641 host_impl_.SetExternalDrawConstraints(
642 transform, viewport, viewport, viewport_rect_for_tile_priority,
643 transform_for_tile_priority, resourceless_software_draw);
644 bool update_lcd_text = false;
645 host_impl_.active_tree()->UpdateDrawProperties(update_lcd_text);
647 EXPECT_EQ(viewport_rect_for_tile_priority,
648 active_layer_->viewport_rect_for_tile_priority_in_content_space());
650 host_impl_.AdvanceToNextFrame(base::TimeDelta::FromMilliseconds(200));
652 gfx::Rect another_viewport_rect_for_tile_priority(11, 11, 50, 50);
653 host_impl_.SetExternalDrawConstraints(
654 transform, viewport, viewport, another_viewport_rect_for_tile_priority,
655 transform_for_tile_priority, resourceless_software_draw);
657 // Didn't call UpdateDrawProperties yet. The viewport rect for tile priority
658 // should remain to be the previously cached value.
659 EXPECT_EQ(viewport_rect_for_tile_priority,
660 active_layer_->viewport_rect_for_tile_priority_in_content_space());
661 host_impl_.active_tree()->UpdateDrawProperties(update_lcd_text);
663 // Now the UpdateDrawProperties is called. The viewport rect for tile
664 // priority should be the latest value.
665 EXPECT_EQ(another_viewport_rect_for_tile_priority,
666 active_layer_->viewport_rect_for_tile_priority_in_content_space());
669 TEST_F(PictureLayerImplTest, ClonePartialInvalidation) {
670 gfx::Size tile_size(100, 100);
671 gfx::Size layer_bounds(400, 400);
672 gfx::Rect layer_invalidation(150, 200, 30, 180);
674 scoped_refptr<FakePicturePileImpl> pending_pile =
675 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
676 scoped_refptr<FakePicturePileImpl> active_pile =
677 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
678 scoped_refptr<FakePicturePileImpl> lost_pile =
679 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
681 SetupPendingTreeWithFixedTileSize(lost_pile, gfx::Size(50, 50), Region());
682 ActivateTree();
683 // Add a unique tiling on the active tree.
684 PictureLayerTiling* tiling = active_layer_->AddTiling(3.f);
685 tiling->set_resolution(HIGH_RESOLUTION);
686 tiling->CreateAllTilesForTesting();
688 // Ensure UpdateTiles won't remove any tilings.
689 active_layer_->MarkAllTilingsUsed();
691 // Then setup a new pending tree and activate it.
692 SetupTreesWithFixedTileSize(pending_pile, active_pile, gfx::Size(50, 50),
693 layer_invalidation);
695 EXPECT_EQ(1u, pending_layer_->num_tilings());
696 EXPECT_EQ(3u, active_layer_->num_tilings());
698 const PictureLayerTilingSet* tilings = pending_layer_->tilings();
699 EXPECT_GT(tilings->num_tilings(), 0u);
700 for (size_t i = 0; i < tilings->num_tilings(); ++i) {
701 const PictureLayerTiling* tiling = tilings->tiling_at(i);
702 gfx::Rect content_invalidation = gfx::ScaleToEnclosingRect(
703 layer_invalidation,
704 tiling->contents_scale());
705 auto prioritized_tiles =
706 tiling->UpdateAndGetAllPrioritizedTilesForTesting();
707 for (PictureLayerTiling::CoverageIterator iter(
708 tiling,
709 tiling->contents_scale(),
710 gfx::Rect(tiling->tiling_size()));
711 iter;
712 ++iter) {
713 // We don't always have a tile, but when we do it's because it was
714 // invalidated and it has the latest raster source.
715 if (*iter) {
716 EXPECT_FALSE(iter.geometry_rect().IsEmpty());
717 EXPECT_EQ(pending_pile.get(), prioritized_tiles[*iter].raster_source());
718 EXPECT_TRUE(iter.geometry_rect().Intersects(content_invalidation));
719 } else {
720 // We don't create tiles in non-invalidated regions.
721 EXPECT_FALSE(iter.geometry_rect().Intersects(content_invalidation));
726 tilings = active_layer_->tilings();
727 EXPECT_GT(tilings->num_tilings(), 0u);
728 for (size_t i = 0; i < tilings->num_tilings(); ++i) {
729 const PictureLayerTiling* tiling = tilings->tiling_at(i);
730 gfx::Rect content_invalidation =
731 gfx::ScaleToEnclosingRect(layer_invalidation, tiling->contents_scale());
732 auto prioritized_tiles =
733 tiling->UpdateAndGetAllPrioritizedTilesForTesting();
734 for (PictureLayerTiling::CoverageIterator iter(
735 tiling,
736 tiling->contents_scale(),
737 gfx::Rect(tiling->tiling_size()));
738 iter;
739 ++iter) {
740 EXPECT_TRUE(*iter);
741 EXPECT_FALSE(iter.geometry_rect().IsEmpty());
742 // Pile will be updated upon activation.
743 EXPECT_EQ(active_pile.get(), prioritized_tiles[*iter].raster_source());
748 TEST_F(PictureLayerImplTest, CloneFullInvalidation) {
749 gfx::Size tile_size(90, 80);
750 gfx::Size layer_bounds(300, 500);
752 scoped_refptr<FakePicturePileImpl> pending_pile =
753 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
754 scoped_refptr<FakePicturePileImpl> active_pile =
755 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
757 SetupTreesWithInvalidation(pending_pile, active_pile,
758 gfx::Rect(layer_bounds));
760 EXPECT_EQ(pending_layer_->tilings()->num_tilings(),
761 active_layer_->tilings()->num_tilings());
763 const PictureLayerTilingSet* tilings = pending_layer_->tilings();
764 EXPECT_GT(tilings->num_tilings(), 0u);
765 for (size_t i = 0; i < tilings->num_tilings(); ++i)
766 VerifyAllPrioritizedTilesExistAndHavePile(tilings->tiling_at(i),
767 pending_pile.get());
770 TEST_F(PictureLayerImplTest, UpdateTilesCreatesTilings) {
771 gfx::Size tile_size(400, 400);
772 gfx::Size layer_bounds(1300, 1900);
774 scoped_refptr<FakePicturePileImpl> pending_pile =
775 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
776 scoped_refptr<FakePicturePileImpl> active_pile =
777 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
779 SetupTrees(pending_pile, active_pile);
781 float low_res_factor = host_impl_.settings().low_res_contents_scale_factor;
782 EXPECT_LT(low_res_factor, 1.f);
784 active_layer_->ReleaseResources();
785 EXPECT_FALSE(active_layer_->tilings());
786 active_layer_->RecreateResources();
787 EXPECT_EQ(0u, active_layer_->tilings()->num_tilings());
789 SetupDrawPropertiesAndUpdateTiles(active_layer_,
790 6.f, // ideal contents scale
791 3.f, // device scale
792 2.f, // page scale
793 1.f, // maximum animation scale
794 0.f, // starting animation scale
795 false);
796 ASSERT_EQ(2u, active_layer_->tilings()->num_tilings());
797 EXPECT_FLOAT_EQ(6.f,
798 active_layer_->tilings()->tiling_at(0)->contents_scale());
799 EXPECT_FLOAT_EQ(6.f * low_res_factor,
800 active_layer_->tilings()->tiling_at(1)->contents_scale());
802 // If we change the page scale factor, then we should get new tilings.
803 SetupDrawPropertiesAndUpdateTiles(active_layer_,
804 6.6f, // ideal contents scale
805 3.f, // device scale
806 2.2f, // page scale
807 1.f, // maximum animation scale
808 0.f, // starting animation scale
809 false);
810 ASSERT_EQ(4u, active_layer_->tilings()->num_tilings());
811 EXPECT_FLOAT_EQ(6.6f,
812 active_layer_->tilings()->tiling_at(0)->contents_scale());
813 EXPECT_FLOAT_EQ(6.6f * low_res_factor,
814 active_layer_->tilings()->tiling_at(2)->contents_scale());
816 // If we change the device scale factor, then we should get new tilings.
817 SetupDrawPropertiesAndUpdateTiles(active_layer_,
818 7.26f, // ideal contents scale
819 3.3f, // device scale
820 2.2f, // page scale
821 1.f, // maximum animation scale
822 0.f, // starting animation scale
823 false);
824 ASSERT_EQ(6u, active_layer_->tilings()->num_tilings());
825 EXPECT_FLOAT_EQ(7.26f,
826 active_layer_->tilings()->tiling_at(0)->contents_scale());
827 EXPECT_FLOAT_EQ(7.26f * low_res_factor,
828 active_layer_->tilings()->tiling_at(3)->contents_scale());
830 // If we change the device scale factor, but end up at the same total scale
831 // factor somehow, then we don't get new tilings.
832 SetupDrawPropertiesAndUpdateTiles(active_layer_,
833 7.26f, // ideal contents scale
834 2.2f, // device scale
835 3.3f, // page scale
836 1.f, // maximum animation scale
837 0.f, // starting animation scale
838 false);
839 ASSERT_EQ(6u, active_layer_->tilings()->num_tilings());
840 EXPECT_FLOAT_EQ(7.26f,
841 active_layer_->tilings()->tiling_at(0)->contents_scale());
842 EXPECT_FLOAT_EQ(7.26f * low_res_factor,
843 active_layer_->tilings()->tiling_at(3)->contents_scale());
846 TEST_F(PictureLayerImplTest, PendingLayerOnlyHasHighResTiling) {
847 gfx::Size tile_size(400, 400);
848 gfx::Size layer_bounds(1300, 1900);
850 scoped_refptr<FakePicturePileImpl> pending_pile =
851 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
852 scoped_refptr<FakePicturePileImpl> active_pile =
853 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
855 SetupTrees(pending_pile, active_pile);
857 float low_res_factor = host_impl_.settings().low_res_contents_scale_factor;
858 EXPECT_LT(low_res_factor, 1.f);
860 pending_layer_->ReleaseResources();
861 EXPECT_FALSE(pending_layer_->tilings());
862 pending_layer_->RecreateResources();
863 EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings());
865 SetupDrawPropertiesAndUpdateTiles(pending_layer_,
866 6.f, // ideal contents scale
867 3.f, // device scale
868 2.f, // page scale
869 1.f, // maximum animation scale
870 0.f, // starting animation scale
871 false);
872 ASSERT_EQ(1u, pending_layer_->tilings()->num_tilings());
873 EXPECT_FLOAT_EQ(6.f,
874 pending_layer_->tilings()->tiling_at(0)->contents_scale());
876 // If we change the page scale factor, then we should get new tilings.
877 SetupDrawPropertiesAndUpdateTiles(pending_layer_,
878 6.6f, // ideal contents scale
879 3.f, // device scale
880 2.2f, // page scale
881 1.f, // maximum animation scale
882 0.f, // starting animation scale
883 false);
884 ASSERT_EQ(1u, pending_layer_->tilings()->num_tilings());
885 EXPECT_FLOAT_EQ(6.6f,
886 pending_layer_->tilings()->tiling_at(0)->contents_scale());
888 // If we change the device scale factor, then we should get new tilings.
889 SetupDrawPropertiesAndUpdateTiles(pending_layer_,
890 7.26f, // ideal contents scale
891 3.3f, // device scale
892 2.2f, // page scale
893 1.f, // maximum animation scale
894 0.f, // starting animation scale
895 false);
896 ASSERT_EQ(1u, pending_layer_->tilings()->num_tilings());
897 EXPECT_FLOAT_EQ(7.26f,
898 pending_layer_->tilings()->tiling_at(0)->contents_scale());
900 // If we change the device scale factor, but end up at the same total scale
901 // factor somehow, then we don't get new tilings.
902 SetupDrawPropertiesAndUpdateTiles(pending_layer_,
903 7.26f, // ideal contents scale
904 2.2f, // device scale
905 3.3f, // page scale
906 1.f, // maximum animation scale
907 0.f, // starting animation scale
908 false);
909 ASSERT_EQ(1u, pending_layer_->tilings()->num_tilings());
910 EXPECT_FLOAT_EQ(7.26f,
911 pending_layer_->tilings()->tiling_at(0)->contents_scale());
914 TEST_F(PictureLayerImplTest, CreateTilingsEvenIfTwinHasNone) {
915 // This test makes sure that if a layer can have tilings, then a commit makes
916 // it not able to have tilings (empty size), and then a future commit that
917 // makes it valid again should be able to create tilings.
918 gfx::Size tile_size(400, 400);
919 gfx::Size layer_bounds(1300, 1900);
921 scoped_refptr<FakePicturePileImpl> empty_pile =
922 FakePicturePileImpl::CreateEmptyPile(tile_size, layer_bounds);
923 scoped_refptr<FakePicturePileImpl> valid_pile =
924 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
926 SetupPendingTree(valid_pile);
927 ASSERT_EQ(1u, pending_layer_->tilings()->num_tilings());
929 ActivateTree();
930 SetupPendingTree(empty_pile);
931 EXPECT_FALSE(pending_layer_->CanHaveTilings());
932 ASSERT_EQ(2u, active_layer_->tilings()->num_tilings());
933 ASSERT_EQ(0u, pending_layer_->tilings()->num_tilings());
935 ActivateTree();
936 EXPECT_FALSE(active_layer_->CanHaveTilings());
937 ASSERT_EQ(0u, active_layer_->tilings()->num_tilings());
939 SetupPendingTree(valid_pile);
940 ASSERT_EQ(1u, pending_layer_->tilings()->num_tilings());
941 ASSERT_EQ(0u, active_layer_->tilings()->num_tilings());
944 TEST_F(PictureLayerImplTest, LowResTilingStaysOnActiveTree) {
945 gfx::Size tile_size(400, 400);
946 gfx::Size layer_bounds(1300, 1900);
948 scoped_refptr<FakePicturePileImpl> valid_pile =
949 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
950 scoped_refptr<FakePicturePileImpl> other_valid_pile =
951 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
953 SetupPendingTree(valid_pile);
954 ASSERT_EQ(1u, pending_layer_->tilings()->num_tilings());
956 ActivateTree();
957 SetupPendingTree(other_valid_pile);
958 ASSERT_EQ(2u, active_layer_->tilings()->num_tilings());
959 ASSERT_EQ(1u, pending_layer_->tilings()->num_tilings());
960 auto* low_res_tiling =
961 active_layer_->tilings()->FindTilingWithResolution(LOW_RESOLUTION);
962 EXPECT_TRUE(low_res_tiling);
964 ActivateTree();
965 ASSERT_EQ(2u, active_layer_->tilings()->num_tilings());
966 auto* other_low_res_tiling =
967 active_layer_->tilings()->FindTilingWithResolution(LOW_RESOLUTION);
968 EXPECT_TRUE(other_low_res_tiling);
969 EXPECT_EQ(low_res_tiling, other_low_res_tiling);
972 TEST_F(PictureLayerImplTest, ZoomOutCrash) {
973 gfx::Size tile_size(400, 400);
974 gfx::Size layer_bounds(1300, 1900);
976 // Set up the high and low res tilings before pinch zoom.
977 scoped_refptr<FakePicturePileImpl> pending_pile =
978 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
979 scoped_refptr<FakePicturePileImpl> active_pile =
980 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
982 SetupTrees(pending_pile, active_pile);
983 ResetTilingsAndRasterScales();
984 EXPECT_EQ(0u, active_layer_->tilings()->num_tilings());
985 SetContentsScaleOnBothLayers(32.0f, 1.0f, 32.0f, 1.0f, 0.f, false);
986 EXPECT_EQ(32.f, active_layer_->HighResTiling()->contents_scale());
987 host_impl_.PinchGestureBegin();
988 SetContentsScaleOnBothLayers(1.0f, 1.0f, 1.0f, 1.0f, 0.f, false);
989 SetContentsScaleOnBothLayers(1.0f, 1.0f, 1.0f, 1.0f, 0.f, false);
990 EXPECT_EQ(active_layer_->tilings()->NumHighResTilings(), 1);
993 TEST_F(PictureLayerImplTest, PinchGestureTilings) {
994 gfx::Size tile_size(400, 400);
995 gfx::Size layer_bounds(1300, 1900);
997 float low_res_factor = host_impl_.settings().low_res_contents_scale_factor;
999 scoped_refptr<FakePicturePileImpl> pending_pile =
1000 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1001 scoped_refptr<FakePicturePileImpl> active_pile =
1002 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1004 // Set up the high and low res tilings before pinch zoom.
1005 SetupTrees(pending_pile, active_pile);
1006 ResetTilingsAndRasterScales();
1008 SetContentsScaleOnBothLayers(2.f, 1.0f, 2.f, 1.0f, 0.f, false);
1009 EXPECT_EQ(active_layer_->num_tilings(), 2u);
1010 EXPECT_EQ(pending_layer_->num_tilings(), 1u);
1011 EXPECT_EQ(active_layer_->tilings()->tiling_at(0)->contents_scale(), 2.f);
1012 EXPECT_EQ(active_layer_->tilings()->tiling_at(1)->contents_scale(),
1013 2.f * low_res_factor);
1014 // One of the tilings has to be a low resolution one.
1015 EXPECT_EQ(LOW_RESOLUTION,
1016 active_layer_->tilings()->tiling_at(1)->resolution());
1018 // Ensure UpdateTiles won't remove any tilings.
1019 active_layer_->MarkAllTilingsUsed();
1021 // Start a pinch gesture.
1022 host_impl_.PinchGestureBegin();
1024 // Zoom out by a small amount. We should create a tiling at half
1025 // the scale (2/kMaxScaleRatioDuringPinch).
1026 SetContentsScaleOnBothLayers(1.8f, 1.0f, 1.8f, 1.0f, 0.f, false);
1027 EXPECT_EQ(3u, active_layer_->tilings()->num_tilings());
1028 EXPECT_FLOAT_EQ(2.0f,
1029 active_layer_->tilings()->tiling_at(0)->contents_scale());
1030 EXPECT_FLOAT_EQ(1.0f,
1031 active_layer_->tilings()->tiling_at(1)->contents_scale());
1032 EXPECT_FLOAT_EQ(2.0f * low_res_factor,
1033 active_layer_->tilings()->tiling_at(2)->contents_scale());
1034 // Since we're pinching, we shouldn't create a low resolution tiling.
1035 EXPECT_FALSE(
1036 active_layer_->tilings()->FindTilingWithResolution(LOW_RESOLUTION));
1038 // Ensure UpdateTiles won't remove any tilings.
1039 active_layer_->MarkAllTilingsUsed();
1041 // Zoom out further, close to our low-res scale factor. We should
1042 // use that tiling as high-res, and not create a new tiling.
1043 SetContentsScaleOnBothLayers(low_res_factor * 2.1f, 1.0f,
1044 low_res_factor * 2.1f, 1.0f, 0.f, false);
1045 EXPECT_EQ(3u, active_layer_->tilings()->num_tilings());
1046 EXPECT_FALSE(
1047 active_layer_->tilings()->FindTilingWithResolution(LOW_RESOLUTION));
1049 // Zoom in a lot now. Since we increase by increments of
1050 // kMaxScaleRatioDuringPinch, this will create a new tiling at 4.0.
1051 SetContentsScaleOnBothLayers(3.8f, 1.0f, 3.8f, 1.f, 0.f, false);
1052 EXPECT_EQ(4u, active_layer_->tilings()->num_tilings());
1053 EXPECT_FLOAT_EQ(4.0f,
1054 active_layer_->tilings()->tiling_at(0)->contents_scale());
1055 // Although one of the tilings matches the low resolution scale, it still
1056 // shouldn't be marked as low resolution since we're pinching.
1057 auto* low_res_tiling =
1058 active_layer_->tilings()->FindTilingWithScale(4.f * low_res_factor);
1059 EXPECT_TRUE(low_res_tiling);
1060 EXPECT_NE(LOW_RESOLUTION, low_res_tiling->resolution());
1062 // Stop a pinch gesture.
1063 host_impl_.PinchGestureEnd();
1065 // Ensure UpdateTiles won't remove any tilings.
1066 active_layer_->MarkAllTilingsUsed();
1068 // After pinch ends, set the scale to what the raster scale was updated to
1069 // (checked above).
1070 SetContentsScaleOnBothLayers(4.0f, 1.0f, 4.0f, 1.f, 0.f, false);
1071 EXPECT_EQ(4u, active_layer_->tilings()->num_tilings());
1072 EXPECT_FLOAT_EQ(4.0f,
1073 active_layer_->tilings()->tiling_at(0)->contents_scale());
1074 // Now that we stopped pinching, the low resolution tiling that existed should
1075 // now be marked as low resolution.
1076 low_res_tiling =
1077 active_layer_->tilings()->FindTilingWithScale(4.f * low_res_factor);
1078 EXPECT_TRUE(low_res_tiling);
1079 EXPECT_EQ(LOW_RESOLUTION, low_res_tiling->resolution());
1082 TEST_F(PictureLayerImplTest, SnappedTilingDuringZoom) {
1083 gfx::Size tile_size(300, 300);
1084 gfx::Size layer_bounds(2600, 3800);
1086 scoped_refptr<FakePicturePileImpl> pending_pile =
1087 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1088 scoped_refptr<FakePicturePileImpl> active_pile =
1089 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1091 SetupTrees(pending_pile, active_pile);
1093 ResetTilingsAndRasterScales();
1094 EXPECT_EQ(0u, active_layer_->tilings()->num_tilings());
1096 // Set up the high and low res tilings before pinch zoom.
1097 SetContentsScaleOnBothLayers(0.24f, 1.0f, 0.24f, 1.0f, 0.f, false);
1098 EXPECT_EQ(2u, active_layer_->tilings()->num_tilings());
1099 EXPECT_FLOAT_EQ(0.24f,
1100 active_layer_->tilings()->tiling_at(0)->contents_scale());
1101 EXPECT_FLOAT_EQ(0.0625f,
1102 active_layer_->tilings()->tiling_at(1)->contents_scale());
1104 // Ensure UpdateTiles won't remove any tilings.
1105 active_layer_->MarkAllTilingsUsed();
1107 // Start a pinch gesture.
1108 host_impl_.PinchGestureBegin();
1110 // Zoom out by a small amount. We should create a tiling at half
1111 // the scale (1/kMaxScaleRatioDuringPinch).
1112 SetContentsScaleOnBothLayers(0.2f, 1.0f, 0.2f, 1.0f, 0.f, false);
1113 EXPECT_EQ(3u, active_layer_->tilings()->num_tilings());
1114 EXPECT_FLOAT_EQ(0.24f,
1115 active_layer_->tilings()->tiling_at(0)->contents_scale());
1116 EXPECT_FLOAT_EQ(0.12f,
1117 active_layer_->tilings()->tiling_at(1)->contents_scale());
1118 EXPECT_FLOAT_EQ(0.0625,
1119 active_layer_->tilings()->tiling_at(2)->contents_scale());
1121 // Ensure UpdateTiles won't remove any tilings.
1122 active_layer_->MarkAllTilingsUsed();
1124 // Zoom out further, close to our low-res scale factor. We should
1125 // use that tiling as high-res, and not create a new tiling.
1126 SetContentsScaleOnBothLayers(0.1f, 1.0f, 0.1f, 1.0f, 0.f, false);
1127 EXPECT_EQ(3u, active_layer_->tilings()->num_tilings());
1129 // Zoom in. 0.25(desired_scale) should be snapped to 0.24 during zoom-in
1130 // because 0.25(desired_scale) is within the ratio(1.2).
1131 SetContentsScaleOnBothLayers(0.25f, 1.0f, 0.25f, 1.0f, 0.f, false);
1132 EXPECT_EQ(3u, active_layer_->tilings()->num_tilings());
1134 // Zoom in a lot. Since we move in factors of two, we should get a scale that
1135 // is a power of 2 times 0.24.
1136 SetContentsScaleOnBothLayers(1.f, 1.0f, 1.f, 1.0f, 0.f, false);
1137 EXPECT_EQ(4u, active_layer_->tilings()->num_tilings());
1138 EXPECT_FLOAT_EQ(1.92f,
1139 active_layer_->tilings()->tiling_at(0)->contents_scale());
1142 TEST_F(PictureLayerImplTest, CleanUpTilings) {
1143 gfx::Size tile_size(400, 400);
1144 gfx::Size layer_bounds(1300, 1900);
1146 scoped_refptr<FakePicturePileImpl> pending_pile =
1147 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1148 scoped_refptr<FakePicturePileImpl> active_pile =
1149 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1151 std::vector<PictureLayerTiling*> used_tilings;
1153 float low_res_factor = host_impl_.settings().low_res_contents_scale_factor;
1154 EXPECT_LT(low_res_factor, 1.f);
1156 float scale = 1.f;
1157 float page_scale = 1.f;
1159 SetupTrees(pending_pile, active_pile);
1160 EXPECT_EQ(2u, active_layer_->tilings()->num_tilings());
1161 EXPECT_EQ(1.f, active_layer_->HighResTiling()->contents_scale());
1163 // Ensure UpdateTiles won't remove any tilings. Note this is unrelated to
1164 // |used_tilings| variable, and it's here only to ensure that active_layer_
1165 // won't remove tilings before the test has a chance to verify behavior.
1166 active_layer_->MarkAllTilingsUsed();
1168 // We only have ideal tilings, so they aren't removed.
1169 used_tilings.clear();
1170 active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
1171 EXPECT_EQ(2u, active_layer_->tilings()->num_tilings());
1173 host_impl_.PinchGestureBegin();
1175 // Changing the ideal but not creating new tilings.
1176 scale = 1.5f;
1177 page_scale = 1.5f;
1178 SetContentsScaleOnBothLayers(scale, 1.f, page_scale, 1.f, 0.f, false);
1179 EXPECT_EQ(2u, active_layer_->tilings()->num_tilings());
1181 // The tilings are still our target scale, so they aren't removed.
1182 used_tilings.clear();
1183 active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
1184 ASSERT_EQ(2u, active_layer_->tilings()->num_tilings());
1186 host_impl_.PinchGestureEnd();
1188 // Create a 1.2 scale tiling. Now we have 1.0 and 1.2 tilings. Ideal = 1.2.
1189 scale = 1.2f;
1190 page_scale = 1.2f;
1191 SetContentsScaleOnBothLayers(1.2f, 1.f, page_scale, 1.f, 0.f, false);
1192 ASSERT_EQ(4u, active_layer_->tilings()->num_tilings());
1193 EXPECT_FLOAT_EQ(
1194 1.f,
1195 active_layer_->tilings()->tiling_at(1)->contents_scale());
1196 EXPECT_FLOAT_EQ(
1197 1.f * low_res_factor,
1198 active_layer_->tilings()->tiling_at(3)->contents_scale());
1200 // Ensure UpdateTiles won't remove any tilings.
1201 active_layer_->MarkAllTilingsUsed();
1203 // Mark the non-ideal tilings as used. They won't be removed.
1204 used_tilings.clear();
1205 used_tilings.push_back(active_layer_->tilings()->tiling_at(1));
1206 used_tilings.push_back(active_layer_->tilings()->tiling_at(3));
1207 active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
1208 ASSERT_EQ(4u, active_layer_->tilings()->num_tilings());
1210 // Now move the ideal scale to 0.5. Our target stays 1.2.
1211 SetContentsScaleOnBothLayers(0.5f, 1.f, page_scale, 1.f, 0.f, false);
1213 // The high resolution tiling is between target and ideal, so is not
1214 // removed. The low res tiling for the old ideal=1.0 scale is removed.
1215 used_tilings.clear();
1216 active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
1217 ASSERT_EQ(3u, active_layer_->tilings()->num_tilings());
1219 // Now move the ideal scale to 1.0. Our target stays 1.2.
1220 SetContentsScaleOnBothLayers(1.f, 1.f, page_scale, 1.f, 0.f, false);
1222 // All the tilings are between are target and the ideal, so they are not
1223 // removed.
1224 used_tilings.clear();
1225 active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
1226 ASSERT_EQ(3u, active_layer_->tilings()->num_tilings());
1228 // Now move the ideal scale to 1.1 on the active layer. Our target stays 1.2.
1229 SetupDrawPropertiesAndUpdateTiles(active_layer_, 1.1f, 1.f, page_scale, 1.f,
1230 0.f, false);
1232 // Because the pending layer's ideal scale is still 1.0, our tilings fall
1233 // in the range [1.0,1.2] and are kept.
1234 used_tilings.clear();
1235 active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
1236 ASSERT_EQ(3u, active_layer_->tilings()->num_tilings());
1238 // Move the ideal scale on the pending layer to 1.1 as well. Our target stays
1239 // 1.2 still.
1240 SetupDrawPropertiesAndUpdateTiles(pending_layer_, 1.1f, 1.f, page_scale, 1.f,
1241 0.f, false);
1243 // Our 1.0 tiling now falls outside the range between our ideal scale and our
1244 // target raster scale. But it is in our used tilings set, so nothing is
1245 // deleted.
1246 used_tilings.clear();
1247 used_tilings.push_back(active_layer_->tilings()->tiling_at(1));
1248 active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
1249 ASSERT_EQ(3u, active_layer_->tilings()->num_tilings());
1251 // If we remove it from our used tilings set, it is outside the range to keep
1252 // so it is deleted.
1253 used_tilings.clear();
1254 active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
1255 ASSERT_EQ(2u, active_layer_->tilings()->num_tilings());
1258 TEST_F(PictureLayerImplTest, DontAddLowResDuringAnimation) {
1259 // Make sure this layer covers multiple tiles, since otherwise low
1260 // res won't get created because it is too small.
1261 gfx::Size tile_size(host_impl_.settings().default_tile_size);
1262 // Avoid max untiled layer size heuristics via fixed tile size.
1263 gfx::Size layer_bounds(tile_size.width() + 1, tile_size.height() + 1);
1264 SetupDefaultTreesWithFixedTileSize(layer_bounds, tile_size, Region());
1266 float low_res_factor = host_impl_.settings().low_res_contents_scale_factor;
1267 float contents_scale = 1.f;
1268 float device_scale = 1.f;
1269 float page_scale = 1.f;
1270 float maximum_animation_scale = 1.f;
1271 float starting_animation_scale = 0.f;
1272 bool animating_transform = true;
1274 ResetTilingsAndRasterScales();
1276 // Animating, so don't create low res even if there isn't one already.
1277 SetContentsScaleOnBothLayers(contents_scale, device_scale, page_scale,
1278 maximum_animation_scale,
1279 starting_animation_scale, animating_transform);
1280 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 1.f);
1281 EXPECT_BOTH_EQ(num_tilings(), 1u);
1283 // Stop animating, low res gets created.
1284 animating_transform = false;
1285 SetContentsScaleOnBothLayers(contents_scale, device_scale, page_scale,
1286 maximum_animation_scale,
1287 starting_animation_scale, animating_transform);
1288 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 1.f);
1289 EXPECT_EQ(active_layer_->LowResTiling()->contents_scale(), low_res_factor);
1290 EXPECT_EQ(active_layer_->num_tilings(), 2u);
1291 EXPECT_EQ(pending_layer_->num_tilings(), 1u);
1293 // Ensure UpdateTiles won't remove any tilings.
1294 active_layer_->MarkAllTilingsUsed();
1296 // Page scale animation, new high res, but no low res. We still have
1297 // a tiling at the previous scale, it's just not marked as low res on the
1298 // active layer. The pending layer drops non-ideal tilings.
1299 contents_scale = 2.f;
1300 page_scale = 2.f;
1301 maximum_animation_scale = 2.f;
1302 animating_transform = true;
1303 SetContentsScaleOnBothLayers(contents_scale, device_scale, page_scale,
1304 maximum_animation_scale,
1305 starting_animation_scale, animating_transform);
1306 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 2.f);
1307 EXPECT_FALSE(active_layer_->LowResTiling());
1308 EXPECT_FALSE(pending_layer_->LowResTiling());
1309 EXPECT_EQ(3u, active_layer_->num_tilings());
1310 EXPECT_EQ(1u, pending_layer_->num_tilings());
1312 // Stop animating, new low res gets created for final page scale.
1313 animating_transform = false;
1314 SetContentsScaleOnBothLayers(contents_scale, device_scale, page_scale,
1315 maximum_animation_scale,
1316 starting_animation_scale, animating_transform);
1317 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 2.f);
1318 EXPECT_EQ(active_layer_->LowResTiling()->contents_scale(),
1319 2.f * low_res_factor);
1320 EXPECT_EQ(4u, active_layer_->num_tilings());
1321 EXPECT_EQ(1u, pending_layer_->num_tilings());
1324 TEST_F(PictureLayerImplTest, DontAddLowResForSmallLayers) {
1325 gfx::Size layer_bounds(host_impl_.settings().default_tile_size);
1326 gfx::Size tile_size(100, 100);
1328 scoped_refptr<FakePicturePileImpl> pending_pile =
1329 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1330 scoped_refptr<FakePicturePileImpl> active_pile =
1331 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1333 SetupTrees(pending_pile, active_pile);
1335 float low_res_factor = host_impl_.settings().low_res_contents_scale_factor;
1336 float device_scale = 1.f;
1337 float page_scale = 1.f;
1338 float maximum_animation_scale = 1.f;
1339 float starting_animation_scale = 0.f;
1340 bool animating_transform = false;
1342 // Contents exactly fit on one tile at scale 1, no low res.
1343 float contents_scale = 1.f;
1344 SetContentsScaleOnBothLayers(contents_scale, device_scale, page_scale,
1345 maximum_animation_scale,
1346 starting_animation_scale, animating_transform);
1347 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), contents_scale);
1348 EXPECT_BOTH_EQ(num_tilings(), 1u);
1350 ResetTilingsAndRasterScales();
1352 // Contents that are smaller than one tile, no low res.
1353 contents_scale = 0.123f;
1354 SetContentsScaleOnBothLayers(contents_scale, device_scale, page_scale,
1355 maximum_animation_scale,
1356 starting_animation_scale, animating_transform);
1357 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), contents_scale);
1358 EXPECT_BOTH_EQ(num_tilings(), 1u);
1360 ResetTilingsAndRasterScales();
1362 // Any content bounds that would create more than one tile will
1363 // generate a low res tiling.
1364 contents_scale = 2.5f;
1365 SetContentsScaleOnBothLayers(contents_scale, device_scale, page_scale,
1366 maximum_animation_scale,
1367 starting_animation_scale, animating_transform);
1368 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), contents_scale);
1369 EXPECT_EQ(active_layer_->LowResTiling()->contents_scale(),
1370 contents_scale * low_res_factor);
1371 EXPECT_FALSE(pending_layer_->LowResTiling());
1372 EXPECT_EQ(active_layer_->num_tilings(), 2u);
1373 EXPECT_EQ(pending_layer_->num_tilings(), 1u);
1375 // Mask layers dont create low res since they always fit on one tile.
1376 scoped_ptr<FakePictureLayerImpl> mask =
1377 FakePictureLayerImpl::CreateMaskWithRasterSource(
1378 host_impl_.pending_tree(), 3, pending_pile);
1379 mask->SetBounds(layer_bounds);
1380 mask->SetDrawsContent(true);
1382 SetupDrawPropertiesAndUpdateTiles(
1383 mask.get(), contents_scale, device_scale, page_scale,
1384 maximum_animation_scale, starting_animation_scale, animating_transform);
1385 EXPECT_EQ(mask->HighResTiling()->contents_scale(), contents_scale);
1386 EXPECT_EQ(mask->num_tilings(), 1u);
1389 TEST_F(PictureLayerImplTest, HugeMasksGetScaledDown) {
1390 host_impl_.AdvanceToNextFrame(base::TimeDelta::FromMilliseconds(1));
1392 gfx::Size tile_size(100, 100);
1393 gfx::Size layer_bounds(1000, 1000);
1395 scoped_refptr<FakePicturePileImpl> valid_pile =
1396 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1397 SetupPendingTree(valid_pile);
1399 scoped_ptr<FakePictureLayerImpl> mask_ptr =
1400 FakePictureLayerImpl::CreateMaskWithRasterSource(
1401 host_impl_.pending_tree(), 3, valid_pile);
1402 mask_ptr->SetBounds(layer_bounds);
1403 mask_ptr->SetDrawsContent(true);
1404 pending_layer_->SetMaskLayer(mask_ptr.Pass());
1405 pending_layer_->SetHasRenderSurface(true);
1407 host_impl_.pending_tree()->BuildPropertyTreesForTesting();
1408 host_impl_.AdvanceToNextFrame(base::TimeDelta::FromMilliseconds(1));
1409 bool update_lcd_text = false;
1410 host_impl_.pending_tree()->UpdateDrawProperties(update_lcd_text);
1412 FakePictureLayerImpl* pending_mask =
1413 static_cast<FakePictureLayerImpl*>(pending_layer_->mask_layer());
1415 EXPECT_EQ(1.f, pending_mask->HighResTiling()->contents_scale());
1416 EXPECT_EQ(1u, pending_mask->num_tilings());
1418 host_impl_.tile_manager()->InitializeTilesWithResourcesForTesting(
1419 pending_mask->HighResTiling()->AllTilesForTesting());
1421 ActivateTree();
1423 FakePictureLayerImpl* active_mask =
1424 static_cast<FakePictureLayerImpl*>(active_layer_->mask_layer());
1426 // Mask layers have a tiling with a single tile in it.
1427 EXPECT_EQ(1u, active_mask->HighResTiling()->AllTilesForTesting().size());
1428 // The mask resource exists.
1429 ResourceId mask_resource_id;
1430 gfx::Size mask_texture_size;
1431 active_mask->GetContentsResourceId(&mask_resource_id, &mask_texture_size);
1432 EXPECT_NE(0u, mask_resource_id);
1433 EXPECT_EQ(active_mask->bounds(), mask_texture_size);
1435 // Drop resources and recreate them, still the same.
1436 pending_mask->ReleaseResources();
1437 active_mask->ReleaseResources();
1438 pending_mask->RecreateResources();
1439 active_mask->RecreateResources();
1440 SetupDrawPropertiesAndUpdateTiles(active_mask, 1.f, 1.f, 1.f, 1.f, 0.f,
1441 false);
1442 active_mask->HighResTiling()->CreateAllTilesForTesting();
1443 EXPECT_EQ(1u, active_mask->HighResTiling()->AllTilesForTesting().size());
1444 EXPECT_NE(0u, mask_resource_id);
1445 EXPECT_EQ(active_mask->bounds(), mask_texture_size);
1447 // Resize larger than the max texture size.
1448 int max_texture_size = host_impl_.GetRendererCapabilities().max_texture_size;
1449 gfx::Size huge_bounds(max_texture_size + 1, 10);
1450 scoped_refptr<FakePicturePileImpl> huge_pile =
1451 FakePicturePileImpl::CreateFilledPile(tile_size, huge_bounds);
1453 SetupPendingTree(huge_pile);
1454 pending_mask->SetBounds(huge_bounds);
1455 pending_mask->SetRasterSourceOnPending(huge_pile, Region());
1457 host_impl_.AdvanceToNextFrame(base::TimeDelta::FromMilliseconds(1));
1458 host_impl_.pending_tree()->UpdateDrawProperties(update_lcd_text);
1460 // The mask tiling gets scaled down.
1461 EXPECT_LT(pending_mask->HighResTiling()->contents_scale(), 1.f);
1462 EXPECT_EQ(1u, pending_mask->num_tilings());
1464 host_impl_.tile_manager()->InitializeTilesWithResourcesForTesting(
1465 pending_mask->HighResTiling()->AllTilesForTesting());
1467 ActivateTree();
1469 // Mask layers have a tiling with a single tile in it.
1470 EXPECT_EQ(1u, active_mask->HighResTiling()->AllTilesForTesting().size());
1471 // The mask resource exists.
1472 active_mask->GetContentsResourceId(&mask_resource_id, &mask_texture_size);
1473 EXPECT_NE(0u, mask_resource_id);
1474 gfx::Size expected_size = active_mask->bounds();
1475 expected_size.SetToMin(gfx::Size(max_texture_size, max_texture_size));
1476 EXPECT_EQ(expected_size, mask_texture_size);
1478 // Drop resources and recreate them, still the same.
1479 pending_mask->ReleaseResources();
1480 active_mask->ReleaseResources();
1481 pending_mask->RecreateResources();
1482 active_mask->RecreateResources();
1483 SetupDrawPropertiesAndUpdateTiles(active_mask, 1.f, 1.f, 1.f, 1.f, 0.f,
1484 false);
1485 active_mask->HighResTiling()->CreateAllTilesForTesting();
1486 EXPECT_EQ(1u, active_mask->HighResTiling()->AllTilesForTesting().size());
1487 EXPECT_NE(0u, mask_resource_id);
1488 EXPECT_EQ(expected_size, mask_texture_size);
1490 // Do another activate, the same holds.
1491 SetupPendingTree(huge_pile);
1492 ActivateTree();
1493 EXPECT_EQ(1u, active_mask->HighResTiling()->AllTilesForTesting().size());
1494 active_layer_->GetContentsResourceId(&mask_resource_id, &mask_texture_size);
1495 EXPECT_EQ(expected_size, mask_texture_size);
1496 EXPECT_EQ(0u, mask_resource_id);
1498 // Resize even larger, so that the scale would be smaller than the minimum
1499 // contents scale. Then the layer should no longer have any tiling.
1500 float min_contents_scale = host_impl_.settings().minimum_contents_scale;
1501 gfx::Size extra_huge_bounds(max_texture_size / min_contents_scale + 1, 10);
1502 scoped_refptr<FakePicturePileImpl> extra_huge_pile =
1503 FakePicturePileImpl::CreateFilledPile(tile_size, extra_huge_bounds);
1505 SetupPendingTree(extra_huge_pile);
1506 pending_mask->SetBounds(extra_huge_bounds);
1507 pending_mask->SetRasterSourceOnPending(extra_huge_pile, Region());
1509 EXPECT_FALSE(pending_mask->CanHaveTilings());
1511 host_impl_.AdvanceToNextFrame(base::TimeDelta::FromMilliseconds(1));
1512 host_impl_.pending_tree()->UpdateDrawProperties(update_lcd_text);
1514 EXPECT_EQ(0u, pending_mask->num_tilings());
1517 TEST_F(PictureLayerImplTest, ScaledMaskLayer) {
1518 host_impl_.AdvanceToNextFrame(base::TimeDelta::FromMilliseconds(1));
1520 gfx::Size tile_size(100, 100);
1521 gfx::Size layer_bounds(1000, 1000);
1523 host_impl_.SetDeviceScaleFactor(1.3f);
1525 scoped_refptr<FakePicturePileImpl> valid_pile =
1526 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1527 SetupPendingTree(valid_pile);
1529 scoped_ptr<FakePictureLayerImpl> mask_ptr =
1530 FakePictureLayerImpl::CreateMaskWithRasterSource(
1531 host_impl_.pending_tree(), 3, valid_pile);
1532 mask_ptr->SetBounds(layer_bounds);
1533 mask_ptr->SetDrawsContent(true);
1534 pending_layer_->SetMaskLayer(mask_ptr.Pass());
1535 pending_layer_->SetHasRenderSurface(true);
1537 host_impl_.pending_tree()->BuildPropertyTreesForTesting();
1538 host_impl_.AdvanceToNextFrame(base::TimeDelta::FromMilliseconds(1));
1539 bool update_lcd_text = false;
1540 host_impl_.pending_tree()->UpdateDrawProperties(update_lcd_text);
1542 FakePictureLayerImpl* pending_mask =
1543 static_cast<FakePictureLayerImpl*>(pending_layer_->mask_layer());
1545 // Masks are scaled, and do not have a low res tiling.
1546 EXPECT_EQ(1.3f, pending_mask->HighResTiling()->contents_scale());
1547 EXPECT_EQ(1u, pending_mask->num_tilings());
1549 host_impl_.tile_manager()->InitializeTilesWithResourcesForTesting(
1550 pending_mask->HighResTiling()->AllTilesForTesting());
1552 ActivateTree();
1554 FakePictureLayerImpl* active_mask =
1555 static_cast<FakePictureLayerImpl*>(active_layer_->mask_layer());
1557 // Mask layers have a tiling with a single tile in it.
1558 EXPECT_EQ(1u, active_mask->HighResTiling()->AllTilesForTesting().size());
1559 // The mask resource exists.
1560 ResourceId mask_resource_id;
1561 gfx::Size mask_texture_size;
1562 active_mask->GetContentsResourceId(&mask_resource_id, &mask_texture_size);
1563 EXPECT_NE(0u, mask_resource_id);
1564 gfx::Size expected_mask_texture_size =
1565 gfx::ToCeiledSize(gfx::ScaleSize(active_mask->bounds(), 1.3f));
1566 EXPECT_EQ(mask_texture_size, expected_mask_texture_size);
1569 TEST_F(PictureLayerImplTest, ReleaseResources) {
1570 gfx::Size tile_size(400, 400);
1571 gfx::Size layer_bounds(1300, 1900);
1573 scoped_refptr<FakePicturePileImpl> pending_pile =
1574 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1575 scoped_refptr<FakePicturePileImpl> active_pile =
1576 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1578 SetupTrees(pending_pile, active_pile);
1579 EXPECT_EQ(1u, pending_layer_->tilings()->num_tilings());
1581 // All tilings should be removed when losing output surface.
1582 active_layer_->ReleaseResources();
1583 EXPECT_FALSE(active_layer_->tilings());
1584 active_layer_->RecreateResources();
1585 EXPECT_EQ(0u, active_layer_->tilings()->num_tilings());
1586 pending_layer_->ReleaseResources();
1587 EXPECT_FALSE(pending_layer_->tilings());
1588 pending_layer_->RecreateResources();
1589 EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings());
1591 // This should create new tilings.
1592 SetupDrawPropertiesAndUpdateTiles(pending_layer_,
1593 1.f, // ideal contents scale
1594 1.f, // device scale
1595 1.f, // page scale
1596 1.f, // maximum animation scale
1597 0.f, // starting animation_scale
1598 false);
1599 EXPECT_EQ(1u, pending_layer_->tilings()->num_tilings());
1602 TEST_F(PictureLayerImplTest, ClampTilesToMaxTileSize) {
1603 // The default max tile size is larger than 400x400.
1604 gfx::Size tile_size(400, 400);
1605 gfx::Size layer_bounds(5000, 5000);
1607 scoped_refptr<FakePicturePileImpl> pending_pile =
1608 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1610 SetupPendingTree(pending_pile);
1611 EXPECT_GE(pending_layer_->tilings()->num_tilings(), 1u);
1613 pending_layer_->tilings()->tiling_at(0)->CreateAllTilesForTesting();
1615 // The default value.
1616 EXPECT_EQ(gfx::Size(256, 256).ToString(),
1617 host_impl_.settings().default_tile_size.ToString());
1619 Tile* tile = pending_layer_->tilings()->tiling_at(0)->AllTilesForTesting()[0];
1620 EXPECT_EQ(gfx::Size(256, 256).ToString(),
1621 tile->content_rect().size().ToString());
1623 ResetTilingsAndRasterScales();
1625 // Change the max texture size on the output surface context.
1626 scoped_ptr<TestWebGraphicsContext3D> context =
1627 TestWebGraphicsContext3D::Create();
1628 context->set_max_texture_size(140);
1629 host_impl_.DidLoseOutputSurface();
1630 host_impl_.InitializeRenderer(
1631 FakeOutputSurface::Create3d(context.Pass()).Pass());
1633 SetupDrawPropertiesAndUpdateTiles(pending_layer_, 1.f, 1.f, 1.f, 1.f, 0.f,
1634 false);
1635 ASSERT_EQ(1u, pending_layer_->tilings()->num_tilings());
1637 pending_layer_->tilings()->tiling_at(0)->CreateAllTilesForTesting();
1639 // Verify the tiles are not larger than the context's max texture size.
1640 tile = pending_layer_->tilings()->tiling_at(0)->AllTilesForTesting()[0];
1641 EXPECT_GE(140, tile->content_rect().width());
1642 EXPECT_GE(140, tile->content_rect().height());
1645 TEST_F(PictureLayerImplTest, ClampSingleTileToToMaxTileSize) {
1646 // The default max tile size is larger than 400x400.
1647 gfx::Size tile_size(400, 400);
1648 gfx::Size layer_bounds(500, 500);
1650 scoped_refptr<FakePicturePileImpl> pending_pile =
1651 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1652 scoped_refptr<FakePicturePileImpl> active_pile =
1653 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1655 SetupTrees(pending_pile, active_pile);
1656 EXPECT_GE(active_layer_->tilings()->num_tilings(), 1u);
1658 active_layer_->tilings()->tiling_at(0)->CreateAllTilesForTesting();
1660 // The default value. The layer is smaller than this.
1661 EXPECT_EQ(gfx::Size(512, 512).ToString(),
1662 host_impl_.settings().max_untiled_layer_size.ToString());
1664 // There should be a single tile since the layer is small.
1665 PictureLayerTiling* high_res_tiling = active_layer_->tilings()->tiling_at(0);
1666 EXPECT_EQ(1u, high_res_tiling->AllTilesForTesting().size());
1668 ResetTilingsAndRasterScales();
1670 // Change the max texture size on the output surface context.
1671 scoped_ptr<TestWebGraphicsContext3D> context =
1672 TestWebGraphicsContext3D::Create();
1673 context->set_max_texture_size(140);
1674 host_impl_.DidLoseOutputSurface();
1675 host_impl_.InitializeRenderer(
1676 FakeOutputSurface::Create3d(context.Pass()).Pass());
1678 SetupDrawPropertiesAndUpdateTiles(active_layer_, 1.f, 1.f, 1.f, 1.f, 0.f,
1679 false);
1680 ASSERT_LE(1u, active_layer_->tilings()->num_tilings());
1682 active_layer_->tilings()->tiling_at(0)->CreateAllTilesForTesting();
1684 // There should be more than one tile since the max texture size won't cover
1685 // the layer.
1686 high_res_tiling = active_layer_->tilings()->tiling_at(0);
1687 EXPECT_LT(1u, high_res_tiling->AllTilesForTesting().size());
1689 // Verify the tiles are not larger than the context's max texture size.
1690 Tile* tile = active_layer_->tilings()->tiling_at(0)->AllTilesForTesting()[0];
1691 EXPECT_GE(140, tile->content_rect().width());
1692 EXPECT_GE(140, tile->content_rect().height());
1695 TEST_F(PictureLayerImplTest, DisallowTileDrawQuads) {
1696 scoped_ptr<RenderPass> render_pass = RenderPass::Create();
1698 gfx::Size tile_size(400, 400);
1699 gfx::Size layer_bounds(1300, 1900);
1701 scoped_refptr<FakePicturePileImpl> pending_pile =
1702 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1703 scoped_refptr<FakePicturePileImpl> active_pile =
1704 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1706 gfx::Rect layer_invalidation(150, 200, 30, 180);
1707 SetupTreesWithInvalidation(pending_pile, active_pile, layer_invalidation);
1709 active_layer_->draw_properties().visible_layer_rect = gfx::Rect(layer_bounds);
1711 AppendQuadsData data;
1712 active_layer_->WillDraw(DRAW_MODE_RESOURCELESS_SOFTWARE, nullptr);
1713 active_layer_->AppendQuads(render_pass.get(), &data);
1714 active_layer_->DidDraw(nullptr);
1716 ASSERT_EQ(1U, render_pass->quad_list.size());
1717 EXPECT_EQ(DrawQuad::PICTURE_CONTENT,
1718 render_pass->quad_list.front()->material);
1721 TEST_F(PictureLayerImplTest, SolidColorLayerHasVisibleFullCoverage) {
1722 scoped_ptr<RenderPass> render_pass = RenderPass::Create();
1724 gfx::Size tile_size(1000, 1000);
1725 gfx::Size layer_bounds(1500, 1500);
1726 gfx::Rect visible_rect(250, 250, 1000, 1000);
1728 scoped_ptr<FakePicturePile> empty_recording =
1729 FakePicturePile::CreateEmptyPile(tile_size, layer_bounds);
1730 empty_recording->SetIsSolidColor(true);
1732 scoped_refptr<FakePicturePileImpl> pending_pile =
1733 FakePicturePileImpl::CreateFromPile(empty_recording.get(), nullptr);
1734 scoped_refptr<FakePicturePileImpl> active_pile =
1735 FakePicturePileImpl::CreateFromPile(empty_recording.get(), nullptr);
1737 SetupTrees(pending_pile, active_pile);
1739 active_layer_->draw_properties().visible_layer_rect = visible_rect;
1741 AppendQuadsData data;
1742 active_layer_->WillDraw(DRAW_MODE_SOFTWARE, nullptr);
1743 active_layer_->AppendQuads(render_pass.get(), &data);
1744 active_layer_->DidDraw(nullptr);
1746 Region remaining = visible_rect;
1747 for (const auto& quad : render_pass->quad_list) {
1748 EXPECT_TRUE(visible_rect.Contains(quad->rect));
1749 EXPECT_TRUE(remaining.Contains(quad->rect));
1750 remaining.Subtract(quad->rect);
1753 EXPECT_TRUE(remaining.IsEmpty());
1756 TEST_F(PictureLayerImplTest, TileScalesWithSolidColorPile) {
1757 gfx::Size layer_bounds(200, 200);
1758 gfx::Size tile_size(host_impl_.settings().default_tile_size);
1759 scoped_refptr<FakePicturePileImpl> pending_pile =
1760 FakePicturePileImpl::CreateEmptyPileThatThinksItHasRecordings(
1761 tile_size, layer_bounds, false);
1762 scoped_refptr<FakePicturePileImpl> active_pile =
1763 FakePicturePileImpl::CreateEmptyPileThatThinksItHasRecordings(
1764 tile_size, layer_bounds, true);
1766 SetupTrees(pending_pile, active_pile);
1767 // Solid color pile should not allow tilings at any scale.
1768 EXPECT_FALSE(active_layer_->CanHaveTilings());
1769 EXPECT_EQ(0.f, active_layer_->ideal_contents_scale());
1771 // Activate non-solid-color pending pile makes active layer can have tilings.
1772 ActivateTree();
1773 EXPECT_TRUE(active_layer_->CanHaveTilings());
1774 EXPECT_GT(active_layer_->ideal_contents_scale(), 0.f);
1777 TEST_F(NoLowResPictureLayerImplTest, MarkRequiredOffscreenTiles) {
1778 gfx::Size tile_size(100, 100);
1779 gfx::Size layer_bounds(200, 200);
1781 gfx::Transform transform;
1782 gfx::Transform transform_for_tile_priority;
1783 bool resourceless_software_draw = false;
1784 gfx::Rect viewport(0, 0, 100, 200);
1785 host_impl_.SetExternalDrawConstraints(transform,
1786 viewport,
1787 viewport,
1788 viewport,
1789 transform,
1790 resourceless_software_draw);
1792 scoped_refptr<FakePicturePileImpl> pending_pile =
1793 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1794 SetupPendingTreeWithFixedTileSize(pending_pile, tile_size, Region());
1796 EXPECT_EQ(1u, pending_layer_->num_tilings());
1797 EXPECT_EQ(viewport, pending_layer_->visible_rect_for_tile_priority());
1799 base::TimeTicks time_ticks;
1800 host_impl_.AdvanceToNextFrame(base::TimeDelta::FromMilliseconds(1));
1801 pending_layer_->UpdateTiles(resourceless_software_draw);
1803 int num_visible = 0;
1804 int num_offscreen = 0;
1806 scoped_ptr<TilingSetRasterQueueAll> queue(new TilingSetRasterQueueAll(
1807 pending_layer_->picture_layer_tiling_set(), false));
1808 for (; !queue->IsEmpty(); queue->Pop()) {
1809 const PrioritizedTile& prioritized_tile = queue->Top();
1810 DCHECK(prioritized_tile.tile());
1811 if (prioritized_tile.priority().distance_to_visible == 0.f) {
1812 EXPECT_TRUE(prioritized_tile.tile()->required_for_activation());
1813 num_visible++;
1814 } else {
1815 EXPECT_FALSE(prioritized_tile.tile()->required_for_activation());
1816 num_offscreen++;
1820 EXPECT_GT(num_visible, 0);
1821 EXPECT_GT(num_offscreen, 0);
1824 TEST_F(NoLowResPictureLayerImplTest,
1825 TileOutsideOfViewportForTilePriorityNotRequired) {
1826 host_impl_.AdvanceToNextFrame(base::TimeDelta::FromMilliseconds(1));
1828 gfx::Size tile_size(100, 100);
1829 gfx::Size layer_bounds(400, 400);
1830 gfx::Rect external_viewport_for_tile_priority(400, 200);
1831 gfx::Rect visible_layer_rect(200, 400);
1833 scoped_refptr<FakePicturePileImpl> active_pile =
1834 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1835 scoped_refptr<FakePicturePileImpl> pending_pile =
1836 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1837 SetupTreesWithFixedTileSize(pending_pile, active_pile, tile_size, Region());
1839 ASSERT_EQ(1u, pending_layer_->num_tilings());
1840 ASSERT_EQ(1.f, pending_layer_->HighResTiling()->contents_scale());
1842 // Set external viewport for tile priority.
1843 gfx::Rect viewport = gfx::Rect(layer_bounds);
1844 gfx::Transform transform;
1845 gfx::Transform transform_for_tile_priority;
1846 bool resourceless_software_draw = false;
1847 host_impl_.SetExternalDrawConstraints(transform,
1848 viewport,
1849 viewport,
1850 external_viewport_for_tile_priority,
1851 transform_for_tile_priority,
1852 resourceless_software_draw);
1853 host_impl_.AdvanceToNextFrame(base::TimeDelta::FromMilliseconds(1));
1854 bool update_lcd_text = false;
1855 host_impl_.pending_tree()->UpdateDrawProperties(update_lcd_text);
1857 // Set visible content rect that is different from
1858 // external_viewport_for_tile_priority.
1859 pending_layer_->draw_properties().visible_layer_rect = visible_layer_rect;
1860 host_impl_.AdvanceToNextFrame(base::TimeDelta::FromMilliseconds(200));
1861 pending_layer_->UpdateTiles(resourceless_software_draw);
1863 // Intersect the two rects. Any tile outside should not be required for
1864 // activation.
1865 gfx::Rect viewport_for_tile_priority =
1866 pending_layer_->viewport_rect_for_tile_priority_in_content_space();
1867 viewport_for_tile_priority.Intersect(pending_layer_->visible_layer_rect());
1869 EXPECT_TRUE(pending_layer_->HighResTiling()->AllTilesForTesting().empty());
1871 int num_inside = 0;
1872 int num_outside = 0;
1873 for (PictureLayerTiling::CoverageIterator iter(active_layer_->HighResTiling(),
1874 1.f, gfx::Rect(layer_bounds));
1875 iter; ++iter) {
1876 if (!*iter)
1877 continue;
1878 Tile* tile = *iter;
1879 if (viewport_for_tile_priority.Intersects(iter.geometry_rect())) {
1880 num_inside++;
1881 // Mark everything in viewport for tile priority as ready to draw.
1882 TileDrawInfo& draw_info = tile->draw_info();
1883 draw_info.SetSolidColorForTesting(SK_ColorRED);
1884 } else {
1885 num_outside++;
1886 EXPECT_FALSE(tile->required_for_activation());
1890 EXPECT_GT(num_inside, 0);
1891 EXPECT_GT(num_outside, 0);
1893 // Activate and draw active layer.
1894 host_impl_.ActivateSyncTree();
1895 host_impl_.active_tree()->UpdateDrawProperties(update_lcd_text);
1896 active_layer_->draw_properties().visible_layer_rect = visible_layer_rect;
1898 scoped_ptr<RenderPass> render_pass = RenderPass::Create();
1899 AppendQuadsData data;
1900 active_layer_->WillDraw(DRAW_MODE_SOFTWARE, nullptr);
1901 active_layer_->AppendQuads(render_pass.get(), &data);
1902 active_layer_->DidDraw(nullptr);
1904 // All tiles in activation rect is ready to draw.
1905 EXPECT_EQ(0u, data.num_missing_tiles);
1906 EXPECT_EQ(0u, data.num_incomplete_tiles);
1907 EXPECT_FALSE(active_layer_->only_used_low_res_last_append_quads());
1910 TEST_F(PictureLayerImplTest, HighResTileIsComplete) {
1911 host_impl_.AdvanceToNextFrame(base::TimeDelta::FromMilliseconds(1));
1913 gfx::Size tile_size(100, 100);
1914 gfx::Size layer_bounds(200, 200);
1916 scoped_refptr<FakePicturePileImpl> pending_pile =
1917 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1919 SetupPendingTreeWithFixedTileSize(pending_pile, tile_size, Region());
1920 ActivateTree();
1922 // All high res tiles have resources.
1923 std::vector<Tile*> tiles =
1924 active_layer_->tilings()->tiling_at(0)->AllTilesForTesting();
1925 host_impl_.tile_manager()->InitializeTilesWithResourcesForTesting(tiles);
1927 scoped_ptr<RenderPass> render_pass = RenderPass::Create();
1928 AppendQuadsData data;
1929 active_layer_->WillDraw(DRAW_MODE_SOFTWARE, nullptr);
1930 active_layer_->AppendQuads(render_pass.get(), &data);
1931 active_layer_->DidDraw(nullptr);
1933 // All high res tiles drew, nothing was incomplete.
1934 EXPECT_EQ(9u, render_pass->quad_list.size());
1935 EXPECT_EQ(0u, data.num_missing_tiles);
1936 EXPECT_EQ(0u, data.num_incomplete_tiles);
1937 EXPECT_FALSE(active_layer_->only_used_low_res_last_append_quads());
1940 TEST_F(PictureLayerImplTest, HighResTileIsIncomplete) {
1941 host_impl_.AdvanceToNextFrame(base::TimeDelta::FromMilliseconds(1));
1943 gfx::Size tile_size(100, 100);
1944 gfx::Size layer_bounds(200, 200);
1946 scoped_refptr<FakePicturePileImpl> pending_pile =
1947 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1948 SetupPendingTreeWithFixedTileSize(pending_pile, tile_size, Region());
1949 ActivateTree();
1951 scoped_ptr<RenderPass> render_pass = RenderPass::Create();
1952 AppendQuadsData data;
1953 active_layer_->WillDraw(DRAW_MODE_SOFTWARE, nullptr);
1954 active_layer_->AppendQuads(render_pass.get(), &data);
1955 active_layer_->DidDraw(nullptr);
1957 EXPECT_EQ(1u, render_pass->quad_list.size());
1958 EXPECT_EQ(1u, data.num_missing_tiles);
1959 EXPECT_EQ(0u, data.num_incomplete_tiles);
1960 EXPECT_TRUE(active_layer_->only_used_low_res_last_append_quads());
1963 TEST_F(PictureLayerImplTest, HighResTileIsIncompleteLowResComplete) {
1964 host_impl_.AdvanceToNextFrame(base::TimeDelta::FromMilliseconds(1));
1966 gfx::Size tile_size(100, 100);
1967 gfx::Size layer_bounds(200, 200);
1969 scoped_refptr<FakePicturePileImpl> pending_pile =
1970 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1971 SetupPendingTreeWithFixedTileSize(pending_pile, tile_size, Region());
1972 ActivateTree();
1974 std::vector<Tile*> low_tiles =
1975 active_layer_->tilings()->tiling_at(1)->AllTilesForTesting();
1976 host_impl_.tile_manager()->InitializeTilesWithResourcesForTesting(low_tiles);
1978 scoped_ptr<RenderPass> render_pass = RenderPass::Create();
1979 AppendQuadsData data;
1980 active_layer_->WillDraw(DRAW_MODE_SOFTWARE, nullptr);
1981 active_layer_->AppendQuads(render_pass.get(), &data);
1982 active_layer_->DidDraw(nullptr);
1984 EXPECT_EQ(1u, render_pass->quad_list.size());
1985 EXPECT_EQ(0u, data.num_missing_tiles);
1986 EXPECT_EQ(1u, data.num_incomplete_tiles);
1987 EXPECT_TRUE(active_layer_->only_used_low_res_last_append_quads());
1990 TEST_F(PictureLayerImplTest, LowResTileIsIncomplete) {
1991 host_impl_.AdvanceToNextFrame(base::TimeDelta::FromMilliseconds(1));
1993 gfx::Size tile_size(100, 100);
1994 gfx::Size layer_bounds(200, 200);
1996 scoped_refptr<FakePicturePileImpl> pending_pile =
1997 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1998 SetupPendingTreeWithFixedTileSize(pending_pile, tile_size, Region());
1999 ActivateTree();
2001 // All high res tiles have resources except one.
2002 std::vector<Tile*> high_tiles =
2003 active_layer_->tilings()->tiling_at(0)->AllTilesForTesting();
2004 high_tiles.erase(high_tiles.begin());
2005 host_impl_.tile_manager()->InitializeTilesWithResourcesForTesting(high_tiles);
2007 // All low res tiles have resources.
2008 std::vector<Tile*> low_tiles =
2009 active_layer_->tilings()->tiling_at(1)->AllTilesForTesting();
2010 host_impl_.tile_manager()->InitializeTilesWithResourcesForTesting(low_tiles);
2012 scoped_ptr<RenderPass> render_pass = RenderPass::Create();
2013 AppendQuadsData data;
2014 active_layer_->WillDraw(DRAW_MODE_SOFTWARE, nullptr);
2015 active_layer_->AppendQuads(render_pass.get(), &data);
2016 active_layer_->DidDraw(nullptr);
2018 // The missing high res tile was replaced by a low res tile.
2019 EXPECT_EQ(9u, render_pass->quad_list.size());
2020 EXPECT_EQ(0u, data.num_missing_tiles);
2021 EXPECT_EQ(1u, data.num_incomplete_tiles);
2022 EXPECT_FALSE(active_layer_->only_used_low_res_last_append_quads());
2025 TEST_F(PictureLayerImplTest,
2026 HighResAndIdealResTileIsCompleteWhenRasterScaleIsNotIdeal) {
2027 host_impl_.AdvanceToNextFrame(base::TimeDelta::FromMilliseconds(1));
2029 gfx::Size tile_size(100, 100);
2030 gfx::Size layer_bounds(200, 200);
2031 gfx::Size viewport_size(400, 400);
2033 host_impl_.SetViewportSize(viewport_size);
2034 host_impl_.SetDeviceScaleFactor(2.f);
2036 scoped_refptr<FakePicturePileImpl> pending_pile =
2037 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
2038 scoped_refptr<FakePicturePileImpl> active_pile =
2039 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
2040 SetupTreesWithFixedTileSize(pending_pile, active_pile, tile_size, Region());
2042 // One ideal tile exists, this will get used when drawing.
2043 std::vector<Tile*> ideal_tiles;
2044 EXPECT_EQ(2.f, active_layer_->HighResTiling()->contents_scale());
2045 ideal_tiles.push_back(active_layer_->HighResTiling()->TileAt(0, 0));
2046 host_impl_.tile_manager()->InitializeTilesWithResourcesForTesting(
2047 ideal_tiles);
2049 // Due to layer scale throttling, the raster contents scale is changed to 1,
2050 // while the ideal is still 2.
2051 SetupDrawPropertiesAndUpdateTiles(active_layer_, 1.f, 1.f, 1.f, 1.f, 0.f,
2052 false);
2053 SetupDrawPropertiesAndUpdateTiles(active_layer_, 2.f, 1.f, 1.f, 1.f, 0.f,
2054 false);
2056 EXPECT_EQ(1.f, active_layer_->HighResTiling()->contents_scale());
2057 EXPECT_EQ(1.f, active_layer_->raster_contents_scale());
2058 EXPECT_EQ(2.f, active_layer_->ideal_contents_scale());
2060 // Both tilings still exist.
2061 EXPECT_EQ(2.f, active_layer_->tilings()->tiling_at(0)->contents_scale());
2062 EXPECT_EQ(1.f, active_layer_->tilings()->tiling_at(1)->contents_scale());
2064 // All high res tiles have resources.
2065 std::vector<Tile*> high_tiles =
2066 active_layer_->HighResTiling()->AllTilesForTesting();
2067 host_impl_.tile_manager()->InitializeTilesWithResourcesForTesting(high_tiles);
2069 scoped_ptr<RenderPass> render_pass = RenderPass::Create();
2070 AppendQuadsData data;
2071 active_layer_->WillDraw(DRAW_MODE_SOFTWARE, nullptr);
2072 active_layer_->AppendQuads(render_pass.get(), &data);
2073 active_layer_->DidDraw(nullptr);
2075 // All high res tiles drew, and the one ideal res tile drew.
2076 ASSERT_GT(render_pass->quad_list.size(), 9u);
2077 EXPECT_EQ(gfx::SizeF(99.f, 99.f),
2078 TileDrawQuad::MaterialCast(render_pass->quad_list.front())
2079 ->tex_coord_rect.size());
2080 EXPECT_EQ(gfx::SizeF(49.5f, 49.5f),
2081 TileDrawQuad::MaterialCast(render_pass->quad_list.ElementAt(1))
2082 ->tex_coord_rect.size());
2084 // Neither the high res nor the ideal tiles were considered as incomplete.
2085 EXPECT_EQ(0u, data.num_missing_tiles);
2086 EXPECT_EQ(0u, data.num_incomplete_tiles);
2087 EXPECT_FALSE(active_layer_->only_used_low_res_last_append_quads());
2090 TEST_F(PictureLayerImplTest, HighResRequiredWhenActiveAllReady) {
2091 gfx::Size layer_bounds(400, 400);
2092 gfx::Size tile_size(100, 100);
2094 SetupDefaultTreesWithFixedTileSize(layer_bounds, tile_size,
2095 gfx::Rect(layer_bounds));
2097 active_layer_->SetAllTilesReady();
2099 // All active tiles ready, so pending can only activate with all high res
2100 // tiles.
2101 pending_layer_->HighResTiling()->UpdateAllRequiredStateForTesting();
2102 EXPECT_FALSE(pending_layer_->LowResTiling());
2104 AssertAllTilesRequired(pending_layer_->HighResTiling());
2107 TEST_F(PictureLayerImplTest, HighResRequiredWhenMissingHighResFlagOn) {
2108 gfx::Size layer_bounds(400, 400);
2109 gfx::Size tile_size(100, 100);
2111 // No invalidation.
2112 SetupDefaultTreesWithFixedTileSize(layer_bounds, tile_size, Region());
2114 // Verify active tree not ready.
2115 Tile* some_active_tile =
2116 active_layer_->HighResTiling()->AllTilesForTesting()[0];
2117 EXPECT_FALSE(some_active_tile->draw_info().IsReadyToDraw());
2119 // When high res are required, all tiles in active high res tiling should be
2120 // required for activation.
2121 host_impl_.SetRequiresHighResToDraw();
2123 pending_layer_->HighResTiling()->UpdateAllRequiredStateForTesting();
2124 EXPECT_FALSE(pending_layer_->LowResTiling());
2125 active_layer_->HighResTiling()->UpdateAllRequiredStateForTesting();
2126 active_layer_->LowResTiling()->UpdateAllRequiredStateForTesting();
2128 EXPECT_TRUE(pending_layer_->HighResTiling()->AllTilesForTesting().empty());
2129 AssertAllTilesRequired(active_layer_->HighResTiling());
2130 AssertNoTilesRequired(active_layer_->LowResTiling());
2133 TEST_F(PictureLayerImplTest, AllHighResRequiredEvenIfNotChanged) {
2134 gfx::Size layer_bounds(400, 400);
2135 gfx::Size tile_size(100, 100);
2137 SetupDefaultTreesWithFixedTileSize(layer_bounds, tile_size, Region());
2139 Tile* some_active_tile =
2140 active_layer_->HighResTiling()->AllTilesForTesting()[0];
2141 EXPECT_FALSE(some_active_tile->draw_info().IsReadyToDraw());
2143 // Since there are no invalidations, pending tree should have no tiles.
2144 EXPECT_TRUE(pending_layer_->HighResTiling()->AllTilesForTesting().empty());
2145 EXPECT_FALSE(pending_layer_->LowResTiling());
2147 active_layer_->HighResTiling()->UpdateAllRequiredStateForTesting();
2148 active_layer_->LowResTiling()->UpdateAllRequiredStateForTesting();
2150 AssertAllTilesRequired(active_layer_->HighResTiling());
2151 AssertNoTilesRequired(active_layer_->LowResTiling());
2154 TEST_F(PictureLayerImplTest, DisallowRequiredForActivation) {
2155 gfx::Size layer_bounds(400, 400);
2156 gfx::Size tile_size(100, 100);
2158 SetupDefaultTreesWithFixedTileSize(layer_bounds, tile_size, Region());
2160 Tile* some_active_tile =
2161 active_layer_->HighResTiling()->AllTilesForTesting()[0];
2162 EXPECT_FALSE(some_active_tile->draw_info().IsReadyToDraw());
2164 EXPECT_TRUE(pending_layer_->HighResTiling()->AllTilesForTesting().empty());
2165 EXPECT_FALSE(pending_layer_->LowResTiling());
2166 active_layer_->HighResTiling()->set_can_require_tiles_for_activation(false);
2167 active_layer_->LowResTiling()->set_can_require_tiles_for_activation(false);
2168 pending_layer_->HighResTiling()->set_can_require_tiles_for_activation(false);
2170 // If we disallow required for activation, no tiles can be required.
2171 active_layer_->HighResTiling()->UpdateAllRequiredStateForTesting();
2172 active_layer_->LowResTiling()->UpdateAllRequiredStateForTesting();
2174 AssertNoTilesRequired(active_layer_->HighResTiling());
2175 AssertNoTilesRequired(active_layer_->LowResTiling());
2178 TEST_F(PictureLayerImplTest, NothingRequiredIfActiveMissingTiles) {
2179 gfx::Size layer_bounds(400, 400);
2180 gfx::Size tile_size(100, 100);
2182 scoped_refptr<FakePicturePileImpl> pending_pile =
2183 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
2184 // This pile will create tilings, but has no recordings so will not create any
2185 // tiles. This is attempting to simulate scrolling past the end of recorded
2186 // content on the active layer, where the recordings are so far away that
2187 // no tiles are created.
2188 bool is_solid_color = false;
2189 scoped_refptr<FakePicturePileImpl> active_pile =
2190 FakePicturePileImpl::CreateEmptyPileThatThinksItHasRecordings(
2191 tile_size, layer_bounds, is_solid_color);
2193 SetupTreesWithFixedTileSize(pending_pile, active_pile, tile_size, Region());
2195 // Active layer has tilings, but no tiles due to missing recordings.
2196 EXPECT_TRUE(active_layer_->CanHaveTilings());
2197 EXPECT_EQ(active_layer_->tilings()->num_tilings(), 2u);
2198 EXPECT_EQ(active_layer_->HighResTiling()->AllTilesForTesting().size(), 0u);
2200 // Since the active layer has no tiles at all, the pending layer doesn't
2201 // need content in order to activate.
2202 pending_layer_->HighResTiling()->UpdateAllRequiredStateForTesting();
2203 EXPECT_FALSE(pending_layer_->LowResTiling());
2205 AssertNoTilesRequired(pending_layer_->HighResTiling());
2208 TEST_F(PictureLayerImplTest, HighResRequiredIfActiveCantHaveTiles) {
2209 gfx::Size layer_bounds(400, 400);
2210 gfx::Size tile_size(100, 100);
2212 scoped_refptr<FakePicturePileImpl> pending_pile =
2213 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
2214 scoped_refptr<FakePicturePileImpl> active_pile =
2215 FakePicturePileImpl::CreateEmptyPile(tile_size, layer_bounds);
2216 SetupTreesWithFixedTileSize(pending_pile, active_pile, tile_size, Region());
2218 // Active layer can't have tiles.
2219 EXPECT_FALSE(active_layer_->CanHaveTilings());
2221 // All high res tiles required. This should be considered identical
2222 // to the case where there is no active layer, to avoid flashing content.
2223 // This can happen if a layer exists for a while and switches from
2224 // not being able to have content to having content.
2225 pending_layer_->HighResTiling()->UpdateAllRequiredStateForTesting();
2226 EXPECT_FALSE(pending_layer_->LowResTiling());
2228 AssertAllTilesRequired(pending_layer_->HighResTiling());
2231 TEST_F(PictureLayerImplTest, HighResRequiredWhenActiveHasDifferentBounds) {
2232 gfx::Size pending_layer_bounds(400, 400);
2233 gfx::Size active_layer_bounds(200, 200);
2234 gfx::Size tile_size(100, 100);
2236 scoped_refptr<FakePicturePileImpl> pending_pile =
2237 FakePicturePileImpl::CreateFilledPile(tile_size, pending_layer_bounds);
2238 scoped_refptr<FakePicturePileImpl> active_pile =
2239 FakePicturePileImpl::CreateFilledPile(tile_size, active_layer_bounds);
2241 SetupTreesWithFixedTileSize(pending_pile, active_pile, tile_size, Region());
2243 // Since the active layer has different bounds, the pending layer needs all
2244 // high res tiles in order to activate.
2245 pending_layer_->HighResTiling()->UpdateAllRequiredStateForTesting();
2246 EXPECT_FALSE(pending_layer_->LowResTiling());
2247 active_layer_->HighResTiling()->UpdateAllRequiredStateForTesting();
2248 active_layer_->LowResTiling()->UpdateAllRequiredStateForTesting();
2250 AssertAllTilesRequired(pending_layer_->HighResTiling());
2251 AssertAllTilesRequired(active_layer_->HighResTiling());
2252 AssertNoTilesRequired(active_layer_->LowResTiling());
2255 TEST_F(PictureLayerImplTest, ActivateUninitializedLayer) {
2256 gfx::Size tile_size(100, 100);
2257 gfx::Size layer_bounds(400, 400);
2258 scoped_refptr<FakePicturePileImpl> pending_pile =
2259 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
2261 host_impl_.CreatePendingTree();
2262 LayerTreeImpl* pending_tree = host_impl_.pending_tree();
2264 scoped_ptr<FakePictureLayerImpl> pending_layer =
2265 FakePictureLayerImpl::CreateWithRasterSource(pending_tree, id_,
2266 pending_pile);
2267 pending_layer->SetDrawsContent(true);
2268 pending_tree->SetRootLayer(pending_layer.Pass());
2270 pending_layer_ = static_cast<FakePictureLayerImpl*>(
2271 host_impl_.pending_tree()->LayerById(id_));
2273 // Set some state on the pending layer, make sure it is not clobbered
2274 // by a sync from the active layer. This could happen because if the
2275 // pending layer has not been post-commit initialized it will attempt
2276 // to sync from the active layer.
2277 float raster_page_scale = 10.f * pending_layer_->raster_page_scale();
2278 pending_layer_->set_raster_page_scale(raster_page_scale);
2280 host_impl_.ActivateSyncTree();
2282 active_layer_ = static_cast<FakePictureLayerImpl*>(
2283 host_impl_.active_tree()->LayerById(id_));
2285 EXPECT_EQ(0u, active_layer_->num_tilings());
2286 EXPECT_EQ(raster_page_scale, active_layer_->raster_page_scale());
2289 TEST_F(PictureLayerImplTest, ShareTilesOnNextFrame) {
2290 gfx::Size layer_bounds(1500, 1500);
2291 gfx::Size tile_size(100, 100);
2293 scoped_refptr<FakePicturePileImpl> pending_pile =
2294 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
2296 SetupPendingTree(pending_pile);
2298 PictureLayerTiling* tiling = pending_layer_->HighResTiling();
2299 gfx::Rect first_invalidate = tiling->TilingDataForTesting().TileBounds(0, 0);
2300 first_invalidate.Inset(tiling->TilingDataForTesting().border_texels(),
2301 tiling->TilingDataForTesting().border_texels());
2302 gfx::Rect second_invalidate = tiling->TilingDataForTesting().TileBounds(1, 1);
2303 second_invalidate.Inset(tiling->TilingDataForTesting().border_texels(),
2304 tiling->TilingDataForTesting().border_texels());
2306 ActivateTree();
2308 // Make a pending tree with an invalidated raster tile 0,0.
2309 SetupPendingTreeWithInvalidation(pending_pile, first_invalidate);
2311 // Activate and make a pending tree with an invalidated raster tile 1,1.
2312 ActivateTree();
2314 SetupPendingTreeWithInvalidation(pending_pile, second_invalidate);
2316 PictureLayerTiling* pending_tiling = pending_layer_->tilings()->tiling_at(0);
2317 PictureLayerTiling* active_tiling = active_layer_->tilings()->tiling_at(0);
2319 // pending_tiling->CreateAllTilesForTesting();
2321 // Tile 0,0 not exist on pending, but tile 1,1 should.
2322 EXPECT_TRUE(active_tiling->TileAt(0, 0));
2323 EXPECT_TRUE(active_tiling->TileAt(1, 0));
2324 EXPECT_TRUE(active_tiling->TileAt(0, 1));
2325 EXPECT_FALSE(pending_tiling->TileAt(0, 0));
2326 EXPECT_FALSE(pending_tiling->TileAt(1, 0));
2327 EXPECT_FALSE(pending_tiling->TileAt(0, 1));
2328 EXPECT_NE(active_tiling->TileAt(1, 1), pending_tiling->TileAt(1, 1));
2329 EXPECT_TRUE(active_tiling->TileAt(1, 1));
2330 EXPECT_TRUE(pending_tiling->TileAt(1, 1));
2332 // Drop the tiles on the active tree and recreate them.
2333 active_tiling->ComputeTilePriorityRects(gfx::Rect(), 1.f, 1.0, Occlusion());
2334 EXPECT_TRUE(active_tiling->AllTilesForTesting().empty());
2335 active_tiling->CreateAllTilesForTesting();
2337 // Tile 0,0 not exist on pending, but tile 1,1 should.
2338 EXPECT_TRUE(active_tiling->TileAt(0, 0));
2339 EXPECT_TRUE(active_tiling->TileAt(1, 0));
2340 EXPECT_TRUE(active_tiling->TileAt(0, 1));
2341 EXPECT_FALSE(pending_tiling->TileAt(0, 0));
2342 EXPECT_FALSE(pending_tiling->TileAt(1, 0));
2343 EXPECT_FALSE(pending_tiling->TileAt(0, 1));
2344 EXPECT_NE(active_tiling->TileAt(1, 1), pending_tiling->TileAt(1, 1));
2345 EXPECT_TRUE(active_tiling->TileAt(1, 1));
2346 EXPECT_TRUE(pending_tiling->TileAt(1, 1));
2349 TEST_F(PictureLayerImplTest, PendingHasNoTilesWithNoInvalidation) {
2350 SetupDefaultTrees(gfx::Size(1500, 1500));
2352 EXPECT_GE(active_layer_->num_tilings(), 1u);
2353 EXPECT_GE(pending_layer_->num_tilings(), 1u);
2355 // No invalidation.
2356 PictureLayerTiling* active_tiling = active_layer_->tilings()->tiling_at(0);
2357 PictureLayerTiling* pending_tiling = pending_layer_->tilings()->tiling_at(0);
2358 ASSERT_TRUE(active_tiling);
2359 ASSERT_TRUE(pending_tiling);
2361 EXPECT_TRUE(active_tiling->TileAt(0, 0));
2362 EXPECT_TRUE(active_tiling->TileAt(1, 0));
2363 EXPECT_TRUE(active_tiling->TileAt(0, 1));
2364 EXPECT_TRUE(active_tiling->TileAt(1, 1));
2366 EXPECT_FALSE(pending_tiling->TileAt(0, 0));
2367 EXPECT_FALSE(pending_tiling->TileAt(1, 0));
2368 EXPECT_FALSE(pending_tiling->TileAt(0, 1));
2369 EXPECT_FALSE(pending_tiling->TileAt(1, 1));
2372 TEST_F(PictureLayerImplTest, ShareInvalidActiveTreeTiles) {
2373 gfx::Size tile_size(100, 100);
2374 gfx::Size layer_bounds(1500, 1500);
2376 scoped_refptr<FakePicturePileImpl> pending_pile =
2377 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
2378 scoped_refptr<FakePicturePileImpl> active_pile =
2379 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
2380 SetupTreesWithInvalidation(pending_pile, active_pile, gfx::Rect(1, 1));
2381 // Activate the invalidation.
2382 ActivateTree();
2383 // Make another pending tree without any invalidation in it.
2384 scoped_refptr<FakePicturePileImpl> pending_pile2 =
2385 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
2386 SetupPendingTree(pending_pile2);
2388 EXPECT_GE(active_layer_->num_tilings(), 1u);
2389 EXPECT_GE(pending_layer_->num_tilings(), 1u);
2391 // The active tree invalidation was handled by the active tiles.
2392 PictureLayerTiling* active_tiling = active_layer_->tilings()->tiling_at(0);
2393 PictureLayerTiling* pending_tiling = pending_layer_->tilings()->tiling_at(0);
2394 ASSERT_TRUE(active_tiling);
2395 ASSERT_TRUE(pending_tiling);
2397 EXPECT_TRUE(active_tiling->TileAt(0, 0));
2398 EXPECT_TRUE(active_tiling->TileAt(1, 0));
2399 EXPECT_TRUE(active_tiling->TileAt(0, 1));
2400 EXPECT_TRUE(active_tiling->TileAt(1, 1));
2402 EXPECT_FALSE(pending_tiling->TileAt(0, 0));
2403 EXPECT_FALSE(pending_tiling->TileAt(1, 0));
2404 EXPECT_FALSE(pending_tiling->TileAt(0, 1));
2405 EXPECT_FALSE(pending_tiling->TileAt(1, 1));
2408 TEST_F(PictureLayerImplTest, RecreateInvalidPendingTreeTiles) {
2409 // Set some invalidation on the pending tree. We should replace raster tiles
2410 // that touch this.
2411 SetupDefaultTreesWithInvalidation(gfx::Size(1500, 1500), gfx::Rect(1, 1));
2413 EXPECT_GE(active_layer_->num_tilings(), 1u);
2414 EXPECT_GE(pending_layer_->num_tilings(), 1u);
2416 // The pending tree invalidation creates tiles on the pending tree.
2417 PictureLayerTiling* active_tiling = active_layer_->tilings()->tiling_at(0);
2418 PictureLayerTiling* pending_tiling = pending_layer_->tilings()->tiling_at(0);
2419 ASSERT_TRUE(active_tiling);
2420 ASSERT_TRUE(pending_tiling);
2422 EXPECT_TRUE(active_tiling->TileAt(0, 0));
2423 EXPECT_TRUE(active_tiling->TileAt(1, 0));
2424 EXPECT_TRUE(active_tiling->TileAt(0, 1));
2425 EXPECT_TRUE(active_tiling->TileAt(1, 1));
2427 EXPECT_TRUE(pending_tiling->TileAt(0, 0));
2428 EXPECT_FALSE(pending_tiling->TileAt(1, 0));
2429 EXPECT_FALSE(pending_tiling->TileAt(0, 1));
2430 EXPECT_FALSE(pending_tiling->TileAt(1, 1));
2432 EXPECT_NE(active_tiling->TileAt(0, 0), pending_tiling->TileAt(0, 0));
2435 TEST_F(PictureLayerImplTest, SyncTilingAfterGpuRasterizationToggles) {
2436 host_impl_.AdvanceToNextFrame(base::TimeDelta::FromMilliseconds(1));
2438 gfx::Size tile_size(100, 100);
2439 gfx::Size layer_bounds(10, 10);
2441 scoped_refptr<FakePicturePileImpl> pending_pile =
2442 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
2443 scoped_refptr<FakePicturePileImpl> active_pile =
2444 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
2446 SetupTrees(pending_pile, active_pile);
2448 EXPECT_TRUE(pending_layer_->tilings()->FindTilingWithScale(1.f));
2449 EXPECT_TRUE(active_layer_->tilings()->FindTilingWithScale(1.f));
2451 // Gpu rasterization is disabled by default.
2452 EXPECT_FALSE(host_impl_.use_gpu_rasterization());
2453 // Toggling the gpu rasterization clears all tilings on both trees.
2454 host_impl_.SetHasGpuRasterizationTrigger(true);
2455 host_impl_.SetContentIsSuitableForGpuRasterization(true);
2456 host_impl_.UpdateTreeResourcesForGpuRasterizationIfNeeded();
2457 EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings());
2458 EXPECT_EQ(0u, active_layer_->tilings()->num_tilings());
2460 // Make sure that we can still add tiling to the pending layer,
2461 // that gets synced to the active layer.
2462 host_impl_.AdvanceToNextFrame(base::TimeDelta::FromMilliseconds(1));
2463 bool update_lcd_text = false;
2464 host_impl_.pending_tree()->UpdateDrawProperties(update_lcd_text);
2465 EXPECT_TRUE(pending_layer_->tilings()->FindTilingWithScale(1.f));
2467 ActivateTree();
2468 EXPECT_TRUE(active_layer_->tilings()->FindTilingWithScale(1.f));
2470 SetupPendingTree(pending_pile);
2471 EXPECT_TRUE(pending_layer_->tilings()->FindTilingWithScale(1.f));
2473 // Toggling the gpu rasterization clears all tilings on both trees.
2474 EXPECT_TRUE(host_impl_.use_gpu_rasterization());
2475 host_impl_.SetHasGpuRasterizationTrigger(false);
2476 host_impl_.UpdateTreeResourcesForGpuRasterizationIfNeeded();
2477 EXPECT_EQ(GpuRasterizationStatus::OFF_VIEWPORT,
2478 host_impl_.gpu_rasterization_status());
2479 EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings());
2480 EXPECT_EQ(0u, active_layer_->tilings()->num_tilings());
2482 host_impl_.SetHasGpuRasterizationTrigger(true);
2483 host_impl_.SetContentIsSuitableForGpuRasterization(false);
2484 EXPECT_EQ(GpuRasterizationStatus::OFF_CONTENT,
2485 host_impl_.gpu_rasterization_status());
2488 TEST_F(PictureLayerImplTest, HighResCreatedWhenBoundsShrink) {
2489 gfx::Size tile_size(100, 100);
2491 // Put 0.5 as high res.
2492 host_impl_.SetDeviceScaleFactor(0.5f);
2494 scoped_refptr<FakePicturePileImpl> pending_pile =
2495 FakePicturePileImpl::CreateFilledPile(tile_size, gfx::Size(10, 10));
2496 SetupPendingTree(pending_pile);
2498 // Sanity checks.
2499 EXPECT_EQ(1u, pending_layer_->tilings()->num_tilings());
2500 EXPECT_TRUE(pending_layer_->tilings()->FindTilingWithScale(0.5f));
2502 ActivateTree();
2504 // Now, set the bounds to be 1x1, so that minimum contents scale becomes 1.
2505 pending_pile =
2506 FakePicturePileImpl::CreateFilledPile(tile_size, gfx::Size(1, 1));
2507 SetupPendingTree(pending_pile);
2509 // Another sanity check.
2510 EXPECT_EQ(1.f, pending_layer_->MinimumContentsScale());
2512 // Since the MinContentsScale is 1, the 0.5 tiling should have been replaced
2513 // by a 1.0 tiling during the UDP in SetupPendingTree.
2514 EXPECT_EQ(1u, pending_layer_->tilings()->num_tilings());
2515 PictureLayerTiling* tiling =
2516 pending_layer_->tilings()->FindTilingWithScale(1.0f);
2517 ASSERT_TRUE(tiling);
2518 EXPECT_EQ(HIGH_RESOLUTION, tiling->resolution());
2521 TEST_F(PictureLayerImplTest, LowResTilingWithoutGpuRasterization) {
2522 gfx::Size default_tile_size(host_impl_.settings().default_tile_size);
2523 gfx::Size layer_bounds(default_tile_size.width() * 4,
2524 default_tile_size.height() * 4);
2526 host_impl_.SetHasGpuRasterizationTrigger(false);
2528 SetupDefaultTrees(layer_bounds);
2529 EXPECT_FALSE(host_impl_.use_gpu_rasterization());
2530 // Should have only a high-res tiling.
2531 EXPECT_EQ(1u, pending_layer_->tilings()->num_tilings());
2532 ActivateTree();
2533 // Should add a high and a low res for active tree.
2534 EXPECT_EQ(2u, active_layer_->tilings()->num_tilings());
2537 TEST_F(PictureLayerImplTest, NoLowResTilingWithGpuRasterization) {
2538 gfx::Size default_tile_size(host_impl_.settings().default_tile_size);
2539 gfx::Size layer_bounds(default_tile_size.width() * 4,
2540 default_tile_size.height() * 4);
2542 host_impl_.SetHasGpuRasterizationTrigger(true);
2543 host_impl_.SetContentIsSuitableForGpuRasterization(true);
2545 SetupDefaultTrees(layer_bounds);
2546 EXPECT_TRUE(host_impl_.use_gpu_rasterization());
2547 // Should only have the high-res tiling.
2548 EXPECT_EQ(1u, pending_layer_->tilings()->num_tilings());
2549 ActivateTree();
2550 // Should only have the high-res tiling.
2551 EXPECT_EQ(1u, active_layer_->tilings()->num_tilings());
2554 TEST_F(PictureLayerImplTest, RequiredTilesWithGpuRasterization) {
2555 host_impl_.SetHasGpuRasterizationTrigger(true);
2556 host_impl_.SetContentIsSuitableForGpuRasterization(true);
2558 gfx::Size viewport_size(1000, 1000);
2559 host_impl_.SetViewportSize(viewport_size);
2561 gfx::Size layer_bounds(4000, 4000);
2562 SetupDefaultTrees(layer_bounds);
2563 EXPECT_TRUE(host_impl_.use_gpu_rasterization());
2565 // Should only have the high-res tiling.
2566 EXPECT_EQ(1u, active_layer_->tilings()->num_tilings());
2568 active_layer_->HighResTiling()->UpdateAllRequiredStateForTesting();
2570 // High res tiling should have 64 tiles (4x16 tile grid).
2571 EXPECT_EQ(64u, active_layer_->HighResTiling()->AllTilesForTesting().size());
2573 // Visible viewport should be covered by 4 tiles. No other
2574 // tiles should be required for activation.
2575 EXPECT_EQ(4u, NumberOfTilesRequired(active_layer_->HighResTiling()));
2578 TEST_F(PictureLayerImplTest, NoTilingIfDoesNotDrawContent) {
2579 // Set up layers with tilings.
2580 SetupDefaultTrees(gfx::Size(10, 10));
2581 SetContentsScaleOnBothLayers(1.f, 1.f, 1.f, 1.f, 0.f, false);
2582 pending_layer_->PushPropertiesTo(active_layer_);
2583 EXPECT_TRUE(pending_layer_->DrawsContent());
2584 EXPECT_TRUE(pending_layer_->CanHaveTilings());
2585 EXPECT_GE(pending_layer_->num_tilings(), 0u);
2586 EXPECT_GE(active_layer_->num_tilings(), 0u);
2588 // Set content to false, which should make CanHaveTilings return false.
2589 pending_layer_->SetDrawsContent(false);
2590 EXPECT_FALSE(pending_layer_->DrawsContent());
2591 EXPECT_FALSE(pending_layer_->CanHaveTilings());
2593 // No tilings should be pushed to active layer.
2594 pending_layer_->PushPropertiesTo(active_layer_);
2595 EXPECT_EQ(0u, active_layer_->num_tilings());
2598 TEST_F(PictureLayerImplTest, FirstTilingDuringPinch) {
2599 SetupDefaultTrees(gfx::Size(10, 10));
2601 // We start with a tiling at scale 1.
2602 EXPECT_EQ(1.f, pending_layer_->HighResTiling()->contents_scale());
2604 // When we page scale up by 2.3, we get a new tiling that is a power of 2, in
2605 // this case 4.
2606 host_impl_.PinchGestureBegin();
2607 float high_res_scale = 2.3f;
2608 SetContentsScaleOnBothLayers(high_res_scale, 1.f, high_res_scale, 1.f, 0.f,
2609 false);
2610 EXPECT_EQ(4.f, pending_layer_->HighResTiling()->contents_scale());
2613 TEST_F(PictureLayerImplTest, PinchingTooSmall) {
2614 SetupDefaultTrees(gfx::Size(10, 10));
2616 // We start with a tiling at scale 1.
2617 EXPECT_EQ(1.f, pending_layer_->HighResTiling()->contents_scale());
2619 host_impl_.PinchGestureBegin();
2620 float high_res_scale = 0.0001f;
2621 EXPECT_LT(high_res_scale, pending_layer_->MinimumContentsScale());
2623 SetContentsScaleOnBothLayers(high_res_scale, 1.f, high_res_scale, 1.f, 0.f,
2624 false);
2625 EXPECT_FLOAT_EQ(pending_layer_->MinimumContentsScale(),
2626 pending_layer_->HighResTiling()->contents_scale());
2629 TEST_F(PictureLayerImplTest, PinchingTooSmallWithContentsScale) {
2630 SetupDefaultTrees(gfx::Size(10, 10));
2632 ResetTilingsAndRasterScales();
2634 float contents_scale = 0.15f;
2635 SetContentsScaleOnBothLayers(contents_scale, 1.f, 1.f, 1.f, 0.f, false);
2637 ASSERT_GE(pending_layer_->num_tilings(), 0u);
2638 EXPECT_FLOAT_EQ(contents_scale,
2639 pending_layer_->HighResTiling()->contents_scale());
2641 host_impl_.PinchGestureBegin();
2643 float page_scale = 0.0001f;
2644 EXPECT_LT(page_scale * contents_scale,
2645 pending_layer_->MinimumContentsScale());
2647 SetContentsScaleOnBothLayers(contents_scale * page_scale, 1.f, page_scale,
2648 1.f, 0.f, false);
2649 ASSERT_GE(pending_layer_->num_tilings(), 0u);
2650 EXPECT_FLOAT_EQ(pending_layer_->MinimumContentsScale(),
2651 pending_layer_->HighResTiling()->contents_scale());
2654 TEST_F(PictureLayerImplTest, ConsiderAnimationStartScaleForRasterScale) {
2655 gfx::Size viewport_size(1000, 1000);
2656 host_impl_.SetViewportSize(viewport_size);
2658 gfx::Size layer_bounds(100, 100);
2659 SetupDefaultTrees(layer_bounds);
2661 float contents_scale = 2.f;
2662 float device_scale = 1.f;
2663 float page_scale = 1.f;
2664 float maximum_animation_scale = 3.f;
2665 float starting_animation_scale = 1.f;
2666 bool animating_transform = true;
2668 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 1.f);
2670 // Maximum animation scale is greater than starting animation scale
2671 SetContentsScaleOnBothLayers(contents_scale, device_scale, page_scale,
2672 maximum_animation_scale,
2673 starting_animation_scale, animating_transform);
2674 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 3.f);
2676 animating_transform = false;
2678 // Once we stop animating, a new high-res tiling should be created.
2679 SetContentsScaleOnBothLayers(contents_scale, device_scale, page_scale,
2680 maximum_animation_scale,
2681 starting_animation_scale, animating_transform);
2682 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 2.f);
2684 // Starting animation scale greater than maximum animation scale
2685 // Bounds at starting scale within the viewport
2686 animating_transform = true;
2687 starting_animation_scale = 5.f;
2689 SetContentsScaleOnBothLayers(contents_scale, device_scale, page_scale,
2690 maximum_animation_scale,
2691 starting_animation_scale, animating_transform);
2692 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 5.f);
2694 // Once we stop animating, a new high-res tiling should be created.
2695 animating_transform = false;
2696 SetContentsScaleOnBothLayers(contents_scale, device_scale, page_scale,
2697 maximum_animation_scale,
2698 starting_animation_scale, animating_transform);
2699 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 2.f);
2701 // Starting Animation scale greater than maximum animation scale
2702 // Bounds at starting scale outisde the viewport
2703 animating_transform = true;
2704 starting_animation_scale = 11.f;
2706 SetContentsScaleOnBothLayers(contents_scale, device_scale, page_scale,
2707 maximum_animation_scale,
2708 starting_animation_scale, animating_transform);
2709 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 3.f);
2712 TEST_F(PictureLayerImplTest, HighResTilingDuringAnimationForCpuRasterization) {
2713 gfx::Size viewport_size(1000, 1000);
2714 host_impl_.SetViewportSize(viewport_size);
2716 gfx::Size layer_bounds(100, 100);
2717 SetupDefaultTrees(layer_bounds);
2719 float contents_scale = 1.f;
2720 float device_scale = 1.f;
2721 float page_scale = 1.f;
2722 float maximum_animation_scale = 1.f;
2723 float starting_animation_scale = 0.f;
2724 bool animating_transform = false;
2726 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 1.f);
2728 // Since we're CPU-rasterizing, starting an animation should cause tiling
2729 // resolution to get set to the maximum animation scale factor.
2730 animating_transform = true;
2731 maximum_animation_scale = 3.f;
2732 contents_scale = 2.f;
2734 SetContentsScaleOnBothLayers(contents_scale, device_scale, page_scale,
2735 maximum_animation_scale,
2736 starting_animation_scale, animating_transform);
2737 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 3.f);
2738 EXPECT_BOTH_TRUE(GetRasterSource()->ShouldAttemptToUseDistanceFieldText());
2740 // Further changes to scale during the animation should not cause a new
2741 // high-res tiling to get created.
2742 contents_scale = 4.f;
2743 maximum_animation_scale = 5.f;
2745 SetContentsScaleOnBothLayers(contents_scale, device_scale, page_scale,
2746 maximum_animation_scale,
2747 starting_animation_scale, animating_transform);
2748 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 3.f);
2750 // Once we stop animating, a new high-res tiling should be created.
2751 animating_transform = false;
2753 SetContentsScaleOnBothLayers(contents_scale, device_scale, page_scale,
2754 maximum_animation_scale,
2755 starting_animation_scale, animating_transform);
2756 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 4.f);
2758 // When animating with an unknown maximum animation scale factor, a new
2759 // high-res tiling should be created at a source scale of 1.
2760 animating_transform = true;
2761 contents_scale = 2.f;
2762 maximum_animation_scale = 0.f;
2764 SetContentsScaleOnBothLayers(contents_scale, device_scale, page_scale,
2765 maximum_animation_scale,
2766 starting_animation_scale, animating_transform);
2767 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), page_scale * device_scale);
2769 // Further changes to scale during the animation should not cause a new
2770 // high-res tiling to get created.
2771 contents_scale = 3.f;
2773 SetContentsScaleOnBothLayers(contents_scale, device_scale, page_scale,
2774 maximum_animation_scale,
2775 starting_animation_scale, animating_transform);
2776 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), page_scale * device_scale);
2778 // Once we stop animating, a new high-res tiling should be created.
2779 animating_transform = false;
2780 contents_scale = 4.f;
2782 SetContentsScaleOnBothLayers(contents_scale, device_scale, page_scale,
2783 maximum_animation_scale,
2784 starting_animation_scale, animating_transform);
2785 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 4.f);
2787 // When animating with a maxmium animation scale factor that is so large
2788 // that the layer grows larger than the viewport at this scale, a new
2789 // high-res tiling should get created at a source scale of 1, not at its
2790 // maximum scale.
2791 animating_transform = true;
2792 contents_scale = 2.f;
2793 maximum_animation_scale = 11.f;
2795 SetContentsScaleOnBothLayers(contents_scale, device_scale, page_scale,
2796 maximum_animation_scale,
2797 starting_animation_scale, animating_transform);
2798 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), page_scale * device_scale);
2800 // Once we stop animating, a new high-res tiling should be created.
2801 animating_transform = false;
2802 contents_scale = 11.f;
2804 SetContentsScaleOnBothLayers(contents_scale, device_scale, page_scale,
2805 maximum_animation_scale,
2806 starting_animation_scale, animating_transform);
2807 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 11.f);
2809 // When animating with a maxmium animation scale factor that is so large
2810 // that the layer grows larger than the viewport at this scale, and where
2811 // the intial source scale is < 1, a new high-res tiling should get created
2812 // at source scale 1.
2813 animating_transform = true;
2814 contents_scale = 0.1f;
2815 maximum_animation_scale = 11.f;
2817 SetContentsScaleOnBothLayers(contents_scale, device_scale, page_scale,
2818 maximum_animation_scale,
2819 starting_animation_scale, animating_transform);
2820 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), device_scale * page_scale);
2822 // Once we stop animating, a new high-res tiling should be created.
2823 animating_transform = false;
2824 contents_scale = 12.f;
2826 SetContentsScaleOnBothLayers(contents_scale, device_scale, page_scale,
2827 maximum_animation_scale,
2828 starting_animation_scale, animating_transform);
2829 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 12.f);
2831 // When animating toward a smaller scale, but that is still so large that the
2832 // layer grows larger than the viewport at this scale, a new high-res tiling
2833 // should get created at source scale 1.
2834 animating_transform = true;
2835 contents_scale = 11.f;
2836 maximum_animation_scale = 11.f;
2838 SetContentsScaleOnBothLayers(contents_scale, device_scale, page_scale,
2839 maximum_animation_scale,
2840 starting_animation_scale, animating_transform);
2841 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), device_scale * page_scale);
2843 // Once we stop animating, a new high-res tiling should be created.
2844 animating_transform = false;
2845 contents_scale = 11.f;
2847 SetContentsScaleOnBothLayers(contents_scale, device_scale, page_scale,
2848 maximum_animation_scale,
2849 starting_animation_scale, animating_transform);
2850 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 11.f);
2853 TEST_F(PictureLayerImplTest, HighResTilingDuringAnimationForGpuRasterization) {
2854 gfx::Size layer_bounds(100, 100);
2855 gfx::Size viewport_size(1000, 1000);
2856 SetupDefaultTrees(layer_bounds);
2857 host_impl_.SetViewportSize(viewport_size);
2858 host_impl_.SetHasGpuRasterizationTrigger(true);
2859 host_impl_.SetContentIsSuitableForGpuRasterization(true);
2860 host_impl_.UpdateTreeResourcesForGpuRasterizationIfNeeded();
2862 float contents_scale = 1.f;
2863 float device_scale = 1.3f;
2864 float page_scale = 1.4f;
2865 float maximum_animation_scale = 1.f;
2866 float starting_animation_scale = 0.f;
2867 bool animating_transform = false;
2869 SetContentsScaleOnBothLayers(contents_scale, device_scale, page_scale,
2870 maximum_animation_scale,
2871 starting_animation_scale, animating_transform);
2872 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 1.f);
2873 EXPECT_BOTH_FALSE(GetRasterSource()->ShouldAttemptToUseDistanceFieldText());
2875 // Since we're GPU-rasterizing, starting an animation should cause tiling
2876 // resolution to get set to the current contents scale.
2877 animating_transform = true;
2878 contents_scale = 2.f;
2879 maximum_animation_scale = 4.f;
2881 SetContentsScaleOnBothLayers(contents_scale, device_scale, page_scale,
2882 maximum_animation_scale,
2883 starting_animation_scale, animating_transform);
2884 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 2.f);
2885 EXPECT_BOTH_TRUE(GetRasterSource()->ShouldAttemptToUseDistanceFieldText());
2887 // Further changes to scale during the animation should cause a new high-res
2888 // tiling to get created.
2889 contents_scale = 3.f;
2891 SetContentsScaleOnBothLayers(contents_scale, device_scale, page_scale,
2892 maximum_animation_scale,
2893 starting_animation_scale, animating_transform);
2894 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 3.f);
2896 // Since we're re-rasterizing during the animation, scales smaller than 1
2897 // should be respected.
2898 contents_scale = 0.25f;
2900 SetContentsScaleOnBothLayers(contents_scale, device_scale, page_scale,
2901 maximum_animation_scale,
2902 starting_animation_scale, animating_transform);
2903 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 0.25f);
2905 // Once we stop animating, a new high-res tiling should be created.
2906 contents_scale = 4.f;
2907 animating_transform = false;
2909 SetContentsScaleOnBothLayers(contents_scale, device_scale, page_scale,
2910 maximum_animation_scale,
2911 starting_animation_scale, animating_transform);
2912 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 4.f);
2915 TEST_F(PictureLayerImplTest, TilingSetRasterQueue) {
2916 host_impl_.AdvanceToNextFrame(base::TimeDelta::FromMilliseconds(1));
2918 host_impl_.SetViewportSize(gfx::Size(500, 500));
2920 gfx::Size recording_tile_size(100, 100);
2921 gfx::Size layer_bounds(1000, 1000);
2923 scoped_refptr<FakePicturePileImpl> pending_pile =
2924 FakePicturePileImpl::CreateFilledPile(recording_tile_size, layer_bounds);
2926 SetupPendingTree(pending_pile);
2927 EXPECT_EQ(1u, pending_layer_->num_tilings());
2929 std::set<Tile*> unique_tiles;
2930 bool reached_prepaint = false;
2931 int non_ideal_tile_count = 0u;
2932 int low_res_tile_count = 0u;
2933 int high_res_tile_count = 0u;
2934 int high_res_now_tiles = 0u;
2935 scoped_ptr<TilingSetRasterQueueAll> queue(new TilingSetRasterQueueAll(
2936 pending_layer_->picture_layer_tiling_set(), false));
2937 while (!queue->IsEmpty()) {
2938 PrioritizedTile prioritized_tile = queue->Top();
2939 TilePriority priority = prioritized_tile.priority();
2941 EXPECT_TRUE(prioritized_tile.tile());
2943 // Non-high res tiles only get visible tiles. Also, prepaint should only
2944 // come at the end of the iteration.
2945 if (priority.resolution != HIGH_RESOLUTION) {
2946 EXPECT_EQ(TilePriority::NOW, priority.priority_bin);
2947 } else if (reached_prepaint) {
2948 EXPECT_NE(TilePriority::NOW, priority.priority_bin);
2949 } else {
2950 reached_prepaint = priority.priority_bin != TilePriority::NOW;
2951 if (!reached_prepaint)
2952 ++high_res_now_tiles;
2955 non_ideal_tile_count += priority.resolution == NON_IDEAL_RESOLUTION;
2956 low_res_tile_count += priority.resolution == LOW_RESOLUTION;
2957 high_res_tile_count += priority.resolution == HIGH_RESOLUTION;
2959 unique_tiles.insert(prioritized_tile.tile());
2960 queue->Pop();
2963 EXPECT_TRUE(reached_prepaint);
2964 EXPECT_EQ(0, non_ideal_tile_count);
2965 EXPECT_EQ(0, low_res_tile_count);
2967 // With layer size being 1000x1000 and default tile size 256x256, we expect to
2968 // see 4 now tiles out of 16 total high res tiles.
2969 EXPECT_EQ(16, high_res_tile_count);
2970 EXPECT_EQ(4, high_res_now_tiles);
2971 EXPECT_EQ(low_res_tile_count + high_res_tile_count + non_ideal_tile_count,
2972 static_cast<int>(unique_tiles.size()));
2974 scoped_ptr<TilingSetRasterQueueRequired> required_queue(
2975 new TilingSetRasterQueueRequired(
2976 pending_layer_->picture_layer_tiling_set(),
2977 RasterTilePriorityQueue::Type::REQUIRED_FOR_DRAW));
2978 EXPECT_TRUE(required_queue->IsEmpty());
2980 required_queue.reset(new TilingSetRasterQueueRequired(
2981 pending_layer_->picture_layer_tiling_set(),
2982 RasterTilePriorityQueue::Type::REQUIRED_FOR_ACTIVATION));
2983 EXPECT_FALSE(required_queue->IsEmpty());
2984 int required_for_activation_count = 0;
2985 while (!required_queue->IsEmpty()) {
2986 PrioritizedTile prioritized_tile = required_queue->Top();
2987 EXPECT_TRUE(prioritized_tile.tile()->required_for_activation());
2988 EXPECT_FALSE(prioritized_tile.tile()->draw_info().IsReadyToDraw());
2989 ++required_for_activation_count;
2990 required_queue->Pop();
2993 // All of the high res tiles should be required for activation, since there is
2994 // no active twin.
2995 EXPECT_EQ(high_res_now_tiles, required_for_activation_count);
2997 // No NOW tiles.
2998 host_impl_.AdvanceToNextFrame(base::TimeDelta::FromMilliseconds(200));
3000 pending_layer_->draw_properties().visible_layer_rect =
3001 gfx::Rect(1100, 1100, 500, 500);
3002 bool resourceless_software_draw = false;
3003 pending_layer_->UpdateTiles(resourceless_software_draw);
3005 unique_tiles.clear();
3006 high_res_tile_count = 0u;
3007 queue.reset(new TilingSetRasterQueueAll(
3008 pending_layer_->picture_layer_tiling_set(), false));
3009 while (!queue->IsEmpty()) {
3010 PrioritizedTile prioritized_tile = queue->Top();
3011 TilePriority priority = prioritized_tile.priority();
3013 EXPECT_TRUE(prioritized_tile.tile());
3015 // Non-high res tiles only get visible tiles.
3016 EXPECT_EQ(HIGH_RESOLUTION, priority.resolution);
3017 EXPECT_NE(TilePriority::NOW, priority.priority_bin);
3019 high_res_tile_count += priority.resolution == HIGH_RESOLUTION;
3021 unique_tiles.insert(prioritized_tile.tile());
3022 queue->Pop();
3025 EXPECT_EQ(16, high_res_tile_count);
3026 EXPECT_EQ(high_res_tile_count, static_cast<int>(unique_tiles.size()));
3028 host_impl_.AdvanceToNextFrame(base::TimeDelta::FromMilliseconds(200));
3030 pending_layer_->draw_properties().visible_layer_rect =
3031 gfx::Rect(0, 0, 500, 500);
3032 pending_layer_->UpdateTiles(resourceless_software_draw);
3034 std::vector<Tile*> high_res_tiles =
3035 pending_layer_->HighResTiling()->AllTilesForTesting();
3036 for (std::vector<Tile*>::iterator tile_it = high_res_tiles.begin();
3037 tile_it != high_res_tiles.end();
3038 ++tile_it) {
3039 Tile* tile = *tile_it;
3040 TileDrawInfo& draw_info = tile->draw_info();
3041 draw_info.SetSolidColorForTesting(SK_ColorRED);
3044 queue.reset(new TilingSetRasterQueueAll(
3045 pending_layer_->picture_layer_tiling_set(), true));
3046 EXPECT_TRUE(queue->IsEmpty());
3049 TEST_F(PictureLayerImplTest, TilingSetRasterQueueActiveTree) {
3050 host_impl_.AdvanceToNextFrame(base::TimeDelta::FromMilliseconds(1));
3052 host_impl_.SetViewportSize(gfx::Size(500, 500));
3054 gfx::Size tile_size(100, 100);
3055 gfx::Size layer_bounds(1000, 1000);
3057 scoped_refptr<FakePicturePileImpl> pending_pile =
3058 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
3060 SetupPendingTree(pending_pile);
3061 ActivateTree();
3062 EXPECT_EQ(2u, active_layer_->num_tilings());
3064 scoped_ptr<TilingSetRasterQueueRequired> queue(
3065 new TilingSetRasterQueueRequired(
3066 active_layer_->picture_layer_tiling_set(),
3067 RasterTilePriorityQueue::Type::REQUIRED_FOR_DRAW));
3068 EXPECT_FALSE(queue->IsEmpty());
3069 while (!queue->IsEmpty()) {
3070 PrioritizedTile prioritized_tile = queue->Top();
3071 EXPECT_TRUE(prioritized_tile.tile()->required_for_draw());
3072 EXPECT_FALSE(prioritized_tile.tile()->draw_info().IsReadyToDraw());
3073 queue->Pop();
3076 queue.reset(new TilingSetRasterQueueRequired(
3077 active_layer_->picture_layer_tiling_set(),
3078 RasterTilePriorityQueue::Type::REQUIRED_FOR_ACTIVATION));
3079 EXPECT_TRUE(queue->IsEmpty());
3082 TEST_F(PictureLayerImplTest, TilingSetRasterQueueRequiredNoHighRes) {
3083 scoped_ptr<FakePicturePile> empty_recording =
3084 FakePicturePile::CreateEmptyPile(gfx::Size(256, 256),
3085 gfx::Size(1024, 1024));
3086 empty_recording->SetIsSolidColor(true);
3088 scoped_refptr<FakePicturePileImpl> pending_pile =
3089 FakePicturePileImpl::CreateFromPile(empty_recording.get(), nullptr);
3091 SetupPendingTree(pending_pile);
3092 EXPECT_FALSE(
3093 pending_layer_->picture_layer_tiling_set()->FindTilingWithResolution(
3094 HIGH_RESOLUTION));
3096 scoped_ptr<TilingSetRasterQueueRequired> queue(
3097 new TilingSetRasterQueueRequired(
3098 pending_layer_->picture_layer_tiling_set(),
3099 RasterTilePriorityQueue::Type::REQUIRED_FOR_ACTIVATION));
3100 EXPECT_TRUE(queue->IsEmpty());
3103 TEST_F(PictureLayerImplTest, TilingSetEvictionQueue) {
3104 gfx::Size tile_size(100, 100);
3105 gfx::Size layer_bounds(1000, 1000);
3106 float low_res_factor = host_impl_.settings().low_res_contents_scale_factor;
3108 host_impl_.SetViewportSize(gfx::Size(500, 500));
3110 scoped_refptr<FakePicturePileImpl> pending_pile =
3111 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
3113 // TODO(vmpstr): Add a test with tilings other than high res on the active
3114 // tree (crbug.com/519607).
3115 SetupPendingTree(pending_pile);
3116 EXPECT_EQ(1u, pending_layer_->num_tilings());
3118 std::vector<Tile*> all_tiles;
3119 for (size_t i = 0; i < pending_layer_->num_tilings(); ++i) {
3120 PictureLayerTiling* tiling = pending_layer_->tilings()->tiling_at(i);
3121 std::vector<Tile*> tiles = tiling->AllTilesForTesting();
3122 all_tiles.insert(all_tiles.end(), tiles.begin(), tiles.end());
3125 std::set<Tile*> all_tiles_set(all_tiles.begin(), all_tiles.end());
3127 bool mark_required = false;
3128 size_t number_of_marked_tiles = 0u;
3129 size_t number_of_unmarked_tiles = 0u;
3130 for (size_t i = 0; i < pending_layer_->num_tilings(); ++i) {
3131 PictureLayerTiling* tiling = pending_layer_->tilings()->tiling_at(i);
3132 for (PictureLayerTiling::CoverageIterator iter(
3133 tiling, 1.f, pending_layer_->visible_layer_rect());
3134 iter; ++iter) {
3135 if (mark_required) {
3136 number_of_marked_tiles++;
3137 iter->set_required_for_activation(true);
3138 } else {
3139 number_of_unmarked_tiles++;
3141 mark_required = !mark_required;
3145 // Sanity checks.
3146 EXPECT_EQ(16u, all_tiles.size());
3147 EXPECT_EQ(16u, all_tiles_set.size());
3148 EXPECT_GT(number_of_marked_tiles, 1u);
3149 EXPECT_GT(number_of_unmarked_tiles, 1u);
3151 // Tiles don't have resources yet.
3152 scoped_ptr<TilingSetEvictionQueue> queue(
3153 new TilingSetEvictionQueue(pending_layer_->picture_layer_tiling_set()));
3154 EXPECT_TRUE(queue->IsEmpty());
3156 host_impl_.tile_manager()->InitializeTilesWithResourcesForTesting(all_tiles);
3158 std::set<Tile*> unique_tiles;
3159 float expected_scales[] = {low_res_factor, 1.f};
3160 size_t scale_index = 0;
3161 bool reached_visible = false;
3162 PrioritizedTile last_tile;
3163 size_t distance_decreasing = 0;
3164 size_t distance_increasing = 0;
3165 queue.reset(
3166 new TilingSetEvictionQueue(pending_layer_->picture_layer_tiling_set()));
3167 while (!queue->IsEmpty()) {
3168 PrioritizedTile prioritized_tile = queue->Top();
3169 Tile* tile = prioritized_tile.tile();
3170 if (!last_tile.tile())
3171 last_tile = prioritized_tile;
3173 EXPECT_TRUE(tile);
3175 TilePriority priority = prioritized_tile.priority();
3177 if (priority.priority_bin == TilePriority::NOW) {
3178 reached_visible = true;
3179 last_tile = prioritized_tile;
3180 break;
3183 EXPECT_FALSE(tile->required_for_activation());
3185 while (std::abs(tile->contents_scale() - expected_scales[scale_index]) >
3186 std::numeric_limits<float>::epsilon()) {
3187 ++scale_index;
3188 ASSERT_LT(scale_index, arraysize(expected_scales));
3191 EXPECT_FLOAT_EQ(tile->contents_scale(), expected_scales[scale_index]);
3192 unique_tiles.insert(tile);
3194 if (tile->required_for_activation() ==
3195 last_tile.tile()->required_for_activation() &&
3196 std::abs(tile->contents_scale() - last_tile.tile()->contents_scale()) <
3197 std::numeric_limits<float>::epsilon()) {
3198 if (priority.distance_to_visible <=
3199 last_tile.priority().distance_to_visible)
3200 ++distance_decreasing;
3201 else
3202 ++distance_increasing;
3205 last_tile = prioritized_tile;
3206 queue->Pop();
3209 // 4 high res tiles are inside the viewport, the rest are evicted.
3210 EXPECT_TRUE(reached_visible);
3211 EXPECT_EQ(12u, unique_tiles.size());
3212 EXPECT_EQ(1u, distance_increasing);
3213 EXPECT_EQ(11u, distance_decreasing);
3215 scale_index = 0;
3216 bool reached_required = false;
3217 while (!queue->IsEmpty()) {
3218 PrioritizedTile prioritized_tile = queue->Top();
3219 Tile* tile = prioritized_tile.tile();
3220 EXPECT_TRUE(tile);
3222 TilePriority priority = prioritized_tile.priority();
3223 EXPECT_EQ(TilePriority::NOW, priority.priority_bin);
3225 if (reached_required) {
3226 EXPECT_TRUE(tile->required_for_activation());
3227 } else if (tile->required_for_activation()) {
3228 reached_required = true;
3229 scale_index = 0;
3232 while (std::abs(tile->contents_scale() - expected_scales[scale_index]) >
3233 std::numeric_limits<float>::epsilon()) {
3234 ++scale_index;
3235 ASSERT_LT(scale_index, arraysize(expected_scales));
3238 EXPECT_FLOAT_EQ(tile->contents_scale(), expected_scales[scale_index]);
3239 unique_tiles.insert(tile);
3240 queue->Pop();
3243 EXPECT_TRUE(reached_required);
3244 EXPECT_EQ(all_tiles_set.size(), unique_tiles.size());
3247 TEST_F(PictureLayerImplTest, Occlusion) {
3248 gfx::Size tile_size(102, 102);
3249 gfx::Size layer_bounds(1000, 1000);
3250 gfx::Size viewport_size(1000, 1000);
3252 LayerTestCommon::LayerImplTest impl;
3253 host_impl_.SetViewportSize(viewport_size);
3255 scoped_refptr<FakePicturePileImpl> pending_pile =
3256 FakePicturePileImpl::CreateFilledPile(layer_bounds, layer_bounds);
3257 SetupPendingTreeWithFixedTileSize(pending_pile, tile_size, Region());
3258 ActivateTree();
3260 std::vector<Tile*> tiles =
3261 active_layer_->HighResTiling()->AllTilesForTesting();
3262 host_impl_.tile_manager()->InitializeTilesWithResourcesForTesting(tiles);
3265 SCOPED_TRACE("No occlusion");
3266 gfx::Rect occluded;
3267 impl.AppendQuadsWithOcclusion(active_layer_, occluded);
3269 LayerTestCommon::VerifyQuadsExactlyCoverRect(impl.quad_list(),
3270 gfx::Rect(layer_bounds));
3271 EXPECT_EQ(100u, impl.quad_list().size());
3275 SCOPED_TRACE("Full occlusion");
3276 gfx::Rect occluded(active_layer_->visible_layer_rect());
3277 impl.AppendQuadsWithOcclusion(active_layer_, occluded);
3279 LayerTestCommon::VerifyQuadsExactlyCoverRect(impl.quad_list(), gfx::Rect());
3280 EXPECT_EQ(impl.quad_list().size(), 0u);
3284 SCOPED_TRACE("Partial occlusion");
3285 gfx::Rect occluded(150, 0, 200, 1000);
3286 impl.AppendQuadsWithOcclusion(active_layer_, occluded);
3288 size_t partially_occluded_count = 0;
3289 LayerTestCommon::VerifyQuadsAreOccluded(
3290 impl.quad_list(), occluded, &partially_occluded_count);
3291 // The layer outputs one quad, which is partially occluded.
3292 EXPECT_EQ(100u - 10u, impl.quad_list().size());
3293 EXPECT_EQ(10u + 10u, partially_occluded_count);
3297 TEST_F(PictureLayerImplTest, RasterScaleChangeWithoutAnimation) {
3298 gfx::Size tile_size(host_impl_.settings().default_tile_size);
3299 SetupDefaultTrees(tile_size);
3301 ResetTilingsAndRasterScales();
3303 float contents_scale = 2.f;
3304 float device_scale = 1.f;
3305 float page_scale = 1.f;
3306 float maximum_animation_scale = 1.f;
3307 float starting_animation_scale = 0.f;
3308 bool animating_transform = false;
3310 SetContentsScaleOnBothLayers(contents_scale, device_scale, page_scale,
3311 maximum_animation_scale,
3312 starting_animation_scale, animating_transform);
3313 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 2.f);
3315 // Changing the source scale without being in an animation will cause
3316 // the layer to reset its source scale to 1.f.
3317 contents_scale = 3.f;
3319 SetContentsScaleOnBothLayers(contents_scale, device_scale, page_scale,
3320 maximum_animation_scale,
3321 starting_animation_scale, animating_transform);
3322 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 1.f);
3324 // Further changes to the source scale will no longer be reflected in the
3325 // contents scale.
3326 contents_scale = 0.5f;
3328 SetContentsScaleOnBothLayers(contents_scale, device_scale, page_scale,
3329 maximum_animation_scale,
3330 starting_animation_scale, animating_transform);
3331 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 1.f);
3334 TEST_F(PictureLayerImplTest, LowResReadyToDrawNotEnoughToActivate) {
3335 gfx::Size tile_size(100, 100);
3336 gfx::Size layer_bounds(1000, 1000);
3338 // Make sure pending tree has tiles.
3339 gfx::Rect invalidation(gfx::Point(50, 50), tile_size);
3340 SetupDefaultTreesWithFixedTileSize(layer_bounds, tile_size, invalidation);
3342 // All pending layer tiles required are not ready.
3343 EXPECT_FALSE(host_impl_.tile_manager()->IsReadyToActivate());
3345 // Initialize all low-res tiles.
3346 EXPECT_FALSE(pending_layer_->LowResTiling());
3347 pending_layer_->SetAllTilesReadyInTiling(active_layer_->LowResTiling());
3349 // Low-res tiles should not be enough.
3350 EXPECT_FALSE(host_impl_.tile_manager()->IsReadyToActivate());
3352 // Initialize remaining tiles.
3353 pending_layer_->SetAllTilesReady();
3354 active_layer_->SetAllTilesReady();
3356 EXPECT_TRUE(host_impl_.tile_manager()->IsReadyToActivate());
3359 TEST_F(PictureLayerImplTest, HighResReadyToDrawEnoughToActivate) {
3360 gfx::Size tile_size(100, 100);
3361 gfx::Size layer_bounds(1000, 1000);
3363 // Make sure pending tree has tiles.
3364 gfx::Rect invalidation(gfx::Point(50, 50), tile_size);
3365 SetupDefaultTreesWithFixedTileSize(layer_bounds, tile_size, invalidation);
3367 // All pending layer tiles required are not ready.
3368 EXPECT_FALSE(host_impl_.tile_manager()->IsReadyToActivate());
3370 // Initialize all high-res tiles.
3371 pending_layer_->SetAllTilesReadyInTiling(pending_layer_->HighResTiling());
3372 active_layer_->SetAllTilesReadyInTiling(active_layer_->HighResTiling());
3374 // High-res tiles should be enough, since they cover everything visible.
3375 EXPECT_TRUE(host_impl_.tile_manager()->IsReadyToActivate());
3378 TEST_F(PictureLayerImplTest, ActiveHighResReadyNotEnoughToActivate) {
3379 gfx::Size tile_size(100, 100);
3380 gfx::Size layer_bounds(1000, 1000);
3382 // Make sure pending tree has tiles.
3383 gfx::Rect invalidation(gfx::Point(50, 50), tile_size);
3384 SetupDefaultTreesWithFixedTileSize(layer_bounds, tile_size, invalidation);
3386 // Initialize all high-res tiles in the active layer.
3387 active_layer_->SetAllTilesReadyInTiling(active_layer_->HighResTiling());
3389 // The pending high-res tiles are not ready, so we cannot activate.
3390 EXPECT_FALSE(host_impl_.tile_manager()->IsReadyToActivate());
3392 // When the pending high-res tiles are ready, we can activate.
3393 pending_layer_->SetAllTilesReadyInTiling(pending_layer_->HighResTiling());
3394 EXPECT_TRUE(host_impl_.tile_manager()->IsReadyToActivate());
3397 TEST_F(NoLowResPictureLayerImplTest, ManageTilingsCreatesTilings) {
3398 gfx::Size tile_size(400, 400);
3399 gfx::Size layer_bounds(1300, 1900);
3401 scoped_refptr<FakePicturePileImpl> pending_pile =
3402 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
3403 scoped_refptr<FakePicturePileImpl> active_pile =
3404 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
3406 SetupTrees(pending_pile, active_pile);
3408 float low_res_factor = host_impl_.settings().low_res_contents_scale_factor;
3409 EXPECT_LT(low_res_factor, 1.f);
3411 ResetTilingsAndRasterScales();
3413 SetupDrawPropertiesAndUpdateTiles(active_layer_,
3414 6.f, // ideal contents scale
3415 3.f, // device scale
3416 2.f, // page scale
3417 1.f, // maximum animation scale
3418 0.f, // starting animation scale
3419 false);
3420 ASSERT_EQ(1u, active_layer_->tilings()->num_tilings());
3421 EXPECT_FLOAT_EQ(6.f,
3422 active_layer_->tilings()->tiling_at(0)->contents_scale());
3424 // If we change the page scale factor, then we should get new tilings.
3425 SetupDrawPropertiesAndUpdateTiles(active_layer_,
3426 6.6f, // ideal contents scale
3427 3.f, // device scale
3428 2.2f, // page scale
3429 1.f, // maximum animation scale
3430 0.f, // starting animation scale
3431 false);
3432 ASSERT_EQ(2u, active_layer_->tilings()->num_tilings());
3433 EXPECT_FLOAT_EQ(6.6f,
3434 active_layer_->tilings()->tiling_at(0)->contents_scale());
3436 // If we change the device scale factor, then we should get new tilings.
3437 SetupDrawPropertiesAndUpdateTiles(active_layer_,
3438 7.26f, // ideal contents scale
3439 3.3f, // device scale
3440 2.2f, // page scale
3441 1.f, // maximum animation scale
3442 0.f, // starting animation scale
3443 false);
3444 ASSERT_EQ(3u, active_layer_->tilings()->num_tilings());
3445 EXPECT_FLOAT_EQ(7.26f,
3446 active_layer_->tilings()->tiling_at(0)->contents_scale());
3448 // If we change the device scale factor, but end up at the same total scale
3449 // factor somehow, then we don't get new tilings.
3450 SetupDrawPropertiesAndUpdateTiles(active_layer_,
3451 7.26f, // ideal contents scale
3452 2.2f, // device scale
3453 3.3f, // page scale
3454 1.f, // maximum animation scale
3455 0.f, // starting animation scale
3456 false);
3457 ASSERT_EQ(3u, active_layer_->tilings()->num_tilings());
3458 EXPECT_FLOAT_EQ(7.26f,
3459 active_layer_->tilings()->tiling_at(0)->contents_scale());
3462 TEST_F(NoLowResPictureLayerImplTest, PendingLayerOnlyHasHighResTiling) {
3463 gfx::Size tile_size(400, 400);
3464 gfx::Size layer_bounds(1300, 1900);
3466 scoped_refptr<FakePicturePileImpl> pending_pile =
3467 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
3468 scoped_refptr<FakePicturePileImpl> active_pile =
3469 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
3471 SetupTrees(pending_pile, active_pile);
3473 float low_res_factor = host_impl_.settings().low_res_contents_scale_factor;
3474 EXPECT_LT(low_res_factor, 1.f);
3476 ResetTilingsAndRasterScales();
3478 SetupDrawPropertiesAndUpdateTiles(pending_layer_,
3479 6.f, // ideal contents scale
3480 3.f, // device scale
3481 2.f, // page scale
3482 1.f, // maximum animation scale
3483 0.f, // starting animation scale
3484 false);
3485 ASSERT_EQ(1u, pending_layer_->tilings()->num_tilings());
3486 EXPECT_FLOAT_EQ(6.f,
3487 pending_layer_->tilings()->tiling_at(0)->contents_scale());
3489 // If we change the page scale factor, then we should get new tilings.
3490 SetupDrawPropertiesAndUpdateTiles(pending_layer_,
3491 6.6f, // ideal contents scale
3492 3.f, // device scale
3493 2.2f, // page scale
3494 1.f, // maximum animation scale
3495 0.f, // starting animation scale
3496 false);
3497 ASSERT_EQ(1u, pending_layer_->tilings()->num_tilings());
3498 EXPECT_FLOAT_EQ(6.6f,
3499 pending_layer_->tilings()->tiling_at(0)->contents_scale());
3501 // If we change the device scale factor, then we should get new tilings.
3502 SetupDrawPropertiesAndUpdateTiles(pending_layer_,
3503 7.26f, // ideal contents scale
3504 3.3f, // device scale
3505 2.2f, // page scale
3506 1.f, // maximum animation scale
3507 0.f, // starting animation scale
3508 false);
3509 ASSERT_EQ(1u, pending_layer_->tilings()->num_tilings());
3510 EXPECT_FLOAT_EQ(7.26f,
3511 pending_layer_->tilings()->tiling_at(0)->contents_scale());
3513 // If we change the device scale factor, but end up at the same total scale
3514 // factor somehow, then we don't get new tilings.
3515 SetupDrawPropertiesAndUpdateTiles(pending_layer_,
3516 7.26f, // ideal contents scale
3517 2.2f, // device scale
3518 3.3f, // page scale
3519 1.f, // maximum animation scale
3520 0.f, // starting animation scale
3521 false);
3522 ASSERT_EQ(1u, pending_layer_->tilings()->num_tilings());
3523 EXPECT_FLOAT_EQ(7.26f,
3524 pending_layer_->tilings()->tiling_at(0)->contents_scale());
3527 TEST_F(NoLowResPictureLayerImplTest, AllHighResRequiredEvenIfNotChanged) {
3528 gfx::Size layer_bounds(400, 400);
3529 gfx::Size tile_size(100, 100);
3531 SetupDefaultTreesWithFixedTileSize(layer_bounds, tile_size, Region());
3533 Tile* some_active_tile =
3534 active_layer_->HighResTiling()->AllTilesForTesting()[0];
3535 EXPECT_FALSE(some_active_tile->draw_info().IsReadyToDraw());
3537 // Since there is no invalidation, pending tree should have no tiles.
3538 EXPECT_TRUE(pending_layer_->HighResTiling()->AllTilesForTesting().empty());
3539 if (host_impl_.settings().create_low_res_tiling)
3540 EXPECT_TRUE(pending_layer_->LowResTiling()->AllTilesForTesting().empty());
3542 active_layer_->HighResTiling()->UpdateAllRequiredStateForTesting();
3543 if (host_impl_.settings().create_low_res_tiling)
3544 active_layer_->LowResTiling()->UpdateAllRequiredStateForTesting();
3546 AssertAllTilesRequired(active_layer_->HighResTiling());
3547 if (host_impl_.settings().create_low_res_tiling)
3548 AssertNoTilesRequired(active_layer_->LowResTiling());
3551 TEST_F(NoLowResPictureLayerImplTest, NothingRequiredIfActiveMissingTiles) {
3552 gfx::Size layer_bounds(400, 400);
3553 gfx::Size tile_size(100, 100);
3555 scoped_refptr<FakePicturePileImpl> pending_pile =
3556 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
3557 // This pile will create tilings, but has no recordings so will not create any
3558 // tiles. This is attempting to simulate scrolling past the end of recorded
3559 // content on the active layer, where the recordings are so far away that
3560 // no tiles are created.
3561 bool is_solid_color = false;
3562 scoped_refptr<FakePicturePileImpl> active_pile =
3563 FakePicturePileImpl::CreateEmptyPileThatThinksItHasRecordings(
3564 tile_size, layer_bounds, is_solid_color);
3566 SetupTreesWithFixedTileSize(pending_pile, active_pile, tile_size, Region());
3568 // Active layer has tilings, but no tiles due to missing recordings.
3569 EXPECT_TRUE(active_layer_->CanHaveTilings());
3570 EXPECT_EQ(active_layer_->tilings()->num_tilings(),
3571 host_impl_.settings().create_low_res_tiling ? 2u : 1u);
3572 EXPECT_EQ(active_layer_->HighResTiling()->AllTilesForTesting().size(), 0u);
3574 // Since the active layer has no tiles at all, the pending layer doesn't
3575 // need content in order to activate.
3576 pending_layer_->HighResTiling()->UpdateAllRequiredStateForTesting();
3577 if (host_impl_.settings().create_low_res_tiling)
3578 pending_layer_->LowResTiling()->UpdateAllRequiredStateForTesting();
3580 AssertNoTilesRequired(pending_layer_->HighResTiling());
3581 if (host_impl_.settings().create_low_res_tiling)
3582 AssertNoTilesRequired(pending_layer_->LowResTiling());
3585 TEST_F(NoLowResPictureLayerImplTest, InvalidViewportForPrioritizingTiles) {
3586 host_impl_.AdvanceToNextFrame(base::TimeDelta::FromMilliseconds(1));
3588 gfx::Size tile_size(100, 100);
3589 gfx::Size layer_bounds(400, 400);
3591 scoped_refptr<FakePicturePileImpl> pending_pile =
3592 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
3593 scoped_refptr<FakePicturePileImpl> active_pile =
3594 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
3596 SetupTreesWithInvalidation(pending_pile, active_pile, Region());
3598 SetupDrawPropertiesAndUpdateTiles(active_layer_, 1.f, 1.f, 1.f, 1.f, 0.f,
3599 false);
3601 // UpdateTiles with valid viewport. Should update tile viewport.
3602 // Note viewport is considered invalid if and only if in resourceless
3603 // software draw.
3604 bool resourceless_software_draw = false;
3605 gfx::Rect viewport = gfx::Rect(layer_bounds);
3606 gfx::Transform transform;
3607 host_impl_.SetExternalDrawConstraints(transform,
3608 viewport,
3609 viewport,
3610 viewport,
3611 transform,
3612 resourceless_software_draw);
3613 active_layer_->draw_properties().visible_layer_rect = viewport;
3614 active_layer_->draw_properties().screen_space_transform = transform;
3615 active_layer_->UpdateTiles(resourceless_software_draw);
3617 gfx::Rect visible_rect_for_tile_priority =
3618 active_layer_->visible_rect_for_tile_priority();
3619 EXPECT_FALSE(visible_rect_for_tile_priority.IsEmpty());
3620 gfx::Transform screen_space_transform_for_tile_priority =
3621 active_layer_->screen_space_transform();
3623 // Expand viewport and set it as invalid for prioritizing tiles.
3624 // Should update viewport and transform, but not update visible rect.
3625 host_impl_.AdvanceToNextFrame(base::TimeDelta::FromMilliseconds(200));
3626 resourceless_software_draw = true;
3627 viewport = gfx::ScaleToEnclosingRect(viewport, 2);
3628 transform.Translate(1.f, 1.f);
3629 active_layer_->draw_properties().visible_layer_rect = viewport;
3630 active_layer_->draw_properties().screen_space_transform = transform;
3631 host_impl_.SetExternalDrawConstraints(transform,
3632 viewport,
3633 viewport,
3634 viewport,
3635 transform,
3636 resourceless_software_draw);
3637 active_layer_->UpdateTiles(resourceless_software_draw);
3639 // Transform for tile priority is updated.
3640 EXPECT_TRANSFORMATION_MATRIX_EQ(transform,
3641 active_layer_->screen_space_transform());
3642 // Visible rect for tile priority retains old value.
3643 EXPECT_EQ(visible_rect_for_tile_priority,
3644 active_layer_->visible_rect_for_tile_priority());
3646 // Keep expanded viewport but mark it valid. Should update tile viewport.
3647 host_impl_.AdvanceToNextFrame(base::TimeDelta::FromMilliseconds(200));
3648 resourceless_software_draw = false;
3649 host_impl_.SetExternalDrawConstraints(transform,
3650 viewport,
3651 viewport,
3652 viewport,
3653 transform,
3654 resourceless_software_draw);
3655 active_layer_->UpdateTiles(resourceless_software_draw);
3657 EXPECT_TRANSFORMATION_MATRIX_EQ(transform,
3658 active_layer_->screen_space_transform());
3659 EXPECT_EQ(viewport, active_layer_->visible_rect_for_tile_priority());
3662 TEST_F(NoLowResPictureLayerImplTest, CleanUpTilings) {
3663 gfx::Size tile_size(400, 400);
3664 gfx::Size layer_bounds(1300, 1900);
3666 scoped_refptr<FakePicturePileImpl> pending_pile =
3667 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
3668 scoped_refptr<FakePicturePileImpl> active_pile =
3669 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
3671 std::vector<PictureLayerTiling*> used_tilings;
3673 SetupTrees(pending_pile, active_pile);
3675 float low_res_factor = host_impl_.settings().low_res_contents_scale_factor;
3676 EXPECT_LT(low_res_factor, 1.f);
3678 float device_scale = 1.7f;
3679 float page_scale = 3.2f;
3680 float scale = 1.f;
3682 ResetTilingsAndRasterScales();
3684 SetContentsScaleOnBothLayers(scale, device_scale, page_scale, 1.f, 0.f,
3685 false);
3686 ASSERT_EQ(1u, active_layer_->tilings()->num_tilings());
3688 // Ensure UpdateTiles won't remove any tilings. Note this is unrelated to
3689 // |used_tilings| variable, and it's here only to ensure that active_layer_
3690 // won't remove tilings before the test has a chance to verify behavior.
3691 active_layer_->MarkAllTilingsUsed();
3693 // We only have ideal tilings, so they aren't removed.
3694 used_tilings.clear();
3695 active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
3696 ASSERT_EQ(1u, active_layer_->tilings()->num_tilings());
3698 host_impl_.PinchGestureBegin();
3700 // Changing the ideal but not creating new tilings.
3701 scale *= 1.5f;
3702 page_scale *= 1.5f;
3703 SetContentsScaleOnBothLayers(scale, device_scale, page_scale, 1.f, 0.f,
3704 false);
3705 ASSERT_EQ(1u, active_layer_->tilings()->num_tilings());
3707 // The tilings are still our target scale, so they aren't removed.
3708 used_tilings.clear();
3709 active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
3710 ASSERT_EQ(1u, active_layer_->tilings()->num_tilings());
3712 host_impl_.PinchGestureEnd();
3714 // Create a 1.2 scale tiling. Now we have 1.0 and 1.2 tilings. Ideal = 1.2.
3715 scale /= 4.f;
3716 page_scale /= 4.f;
3717 SetContentsScaleOnBothLayers(1.2f, device_scale, page_scale, 1.f, 0.f, false);
3718 ASSERT_EQ(2u, active_layer_->tilings()->num_tilings());
3719 EXPECT_FLOAT_EQ(1.f,
3720 active_layer_->tilings()->tiling_at(1)->contents_scale());
3722 // Ensure UpdateTiles won't remove any tilings.
3723 active_layer_->MarkAllTilingsUsed();
3725 // Mark the non-ideal tilings as used. They won't be removed.
3726 used_tilings.clear();
3727 used_tilings.push_back(active_layer_->tilings()->tiling_at(1));
3728 active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
3729 ASSERT_EQ(2u, active_layer_->tilings()->num_tilings());
3731 // Now move the ideal scale to 0.5. Our target stays 1.2.
3732 SetContentsScaleOnBothLayers(0.5f, device_scale, page_scale, 1.f, 0.f, false);
3734 // The high resolution tiling is between target and ideal, so is not
3735 // removed. The low res tiling for the old ideal=1.0 scale is removed.
3736 used_tilings.clear();
3737 active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
3738 ASSERT_EQ(2u, active_layer_->tilings()->num_tilings());
3740 // Now move the ideal scale to 1.0. Our target stays 1.2.
3741 SetContentsScaleOnBothLayers(1.f, device_scale, page_scale, 1.f, 0.f, false);
3743 // All the tilings are between are target and the ideal, so they are not
3744 // removed.
3745 used_tilings.clear();
3746 active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
3747 ASSERT_EQ(2u, active_layer_->tilings()->num_tilings());
3749 // Now move the ideal scale to 1.1 on the active layer. Our target stays 1.2.
3750 SetupDrawPropertiesAndUpdateTiles(active_layer_, 1.1f, device_scale,
3751 page_scale, 1.f, 0.f, false);
3753 // Because the pending layer's ideal scale is still 1.0, our tilings fall
3754 // in the range [1.0,1.2] and are kept.
3755 used_tilings.clear();
3756 active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
3757 ASSERT_EQ(2u, active_layer_->tilings()->num_tilings());
3759 // Move the ideal scale on the pending layer to 1.1 as well. Our target stays
3760 // 1.2 still.
3761 SetupDrawPropertiesAndUpdateTiles(pending_layer_, 1.1f, device_scale,
3762 page_scale, 1.f, 0.f, false);
3764 // Our 1.0 tiling now falls outside the range between our ideal scale and our
3765 // target raster scale. But it is in our used tilings set, so nothing is
3766 // deleted.
3767 used_tilings.clear();
3768 used_tilings.push_back(active_layer_->tilings()->tiling_at(1));
3769 active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
3770 ASSERT_EQ(2u, active_layer_->tilings()->num_tilings());
3772 // If we remove it from our used tilings set, it is outside the range to keep
3773 // so it is deleted.
3774 used_tilings.clear();
3775 active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
3776 ASSERT_EQ(1u, active_layer_->tilings()->num_tilings());
3779 TEST_F(NoLowResPictureLayerImplTest, ReleaseResources) {
3780 gfx::Size tile_size(400, 400);
3781 gfx::Size layer_bounds(1300, 1900);
3783 scoped_refptr<FakePicturePileImpl> pending_pile =
3784 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
3785 scoped_refptr<FakePicturePileImpl> active_pile =
3786 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
3788 SetupTrees(pending_pile, active_pile);
3789 EXPECT_EQ(1u, pending_layer_->tilings()->num_tilings());
3790 EXPECT_EQ(1u, active_layer_->tilings()->num_tilings());
3792 // All tilings should be removed when losing output surface.
3793 active_layer_->ReleaseResources();
3794 EXPECT_FALSE(active_layer_->tilings());
3795 active_layer_->RecreateResources();
3796 EXPECT_EQ(0u, active_layer_->tilings()->num_tilings());
3797 pending_layer_->ReleaseResources();
3798 EXPECT_FALSE(pending_layer_->tilings());
3799 pending_layer_->RecreateResources();
3800 EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings());
3802 // This should create new tilings.
3803 SetupDrawPropertiesAndUpdateTiles(pending_layer_,
3804 1.3f, // ideal contents scale
3805 2.7f, // device scale
3806 3.2f, // page scale
3807 1.f, // maximum animation scale
3808 0.f, // starting animation scale
3809 false);
3810 EXPECT_EQ(1u, pending_layer_->tilings()->num_tilings());
3813 TEST_F(PictureLayerImplTest, SharedQuadStateContainsMaxTilingScale) {
3814 scoped_ptr<RenderPass> render_pass = RenderPass::Create();
3816 gfx::Size tile_size(400, 400);
3817 gfx::Size layer_bounds(1000, 2000);
3819 host_impl_.SetViewportSize(gfx::Size(10000, 20000));
3821 scoped_refptr<FakePicturePileImpl> pending_pile =
3822 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
3823 scoped_refptr<FakePicturePileImpl> active_pile =
3824 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
3826 SetupTrees(pending_pile, active_pile);
3828 ResetTilingsAndRasterScales();
3829 SetupDrawPropertiesAndUpdateTiles(active_layer_, 2.5f, 1.f, 1.f, 1.f, 0.f,
3830 false);
3832 float max_contents_scale = active_layer_->MaximumTilingContentsScale();
3833 EXPECT_EQ(2.5f, max_contents_scale);
3835 gfx::Transform scaled_draw_transform = active_layer_->draw_transform();
3836 scaled_draw_transform.Scale(SK_MScalar1 / max_contents_scale,
3837 SK_MScalar1 / max_contents_scale);
3839 AppendQuadsData data;
3840 active_layer_->AppendQuads(render_pass.get(), &data);
3842 // SharedQuadState should have be of size 1, as we are doing AppenQuad once.
3843 EXPECT_EQ(1u, render_pass->shared_quad_state_list.size());
3844 // The quad_to_target_transform should be scaled by the
3845 // MaximumTilingContentsScale on the layer.
3846 EXPECT_EQ(scaled_draw_transform.ToString(),
3847 render_pass->shared_quad_state_list.front()
3848 ->quad_to_target_transform.ToString());
3849 // The content_bounds should be scaled by the
3850 // MaximumTilingContentsScale on the layer.
3851 EXPECT_EQ(gfx::Size(2500u, 5000u).ToString(),
3852 render_pass->shared_quad_state_list.front()
3853 ->quad_layer_bounds.ToString());
3854 // The visible_layer_rect should be scaled by the
3855 // MaximumTilingContentsScale on the layer.
3856 EXPECT_EQ(gfx::Rect(0u, 0u, 2500u, 5000u).ToString(),
3857 render_pass->shared_quad_state_list.front()
3858 ->visible_quad_layer_rect.ToString());
3861 class PictureLayerImplTestWithDelegatingRenderer : public PictureLayerImplTest {
3862 public:
3863 PictureLayerImplTestWithDelegatingRenderer() : PictureLayerImplTest() {}
3865 void InitializeRenderer() override {
3866 host_impl_.InitializeRenderer(FakeOutputSurface::CreateDelegating3d());
3870 TEST_F(PictureLayerImplTestWithDelegatingRenderer,
3871 DelegatingRendererWithTileOOM) {
3872 // This test is added for crbug.com/402321, where quad should be produced when
3873 // raster on demand is not allowed and tile is OOM.
3874 gfx::Size tile_size = host_impl_.settings().default_tile_size;
3875 gfx::Size layer_bounds(1000, 1000);
3877 // Create tiles.
3878 scoped_refptr<FakePicturePileImpl> pending_pile =
3879 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
3880 SetupPendingTree(pending_pile);
3881 pending_layer_->SetBounds(layer_bounds);
3882 ActivateTree();
3883 bool update_lcd_text = false;
3884 host_impl_.active_tree()->UpdateDrawProperties(update_lcd_text);
3885 std::vector<Tile*> tiles =
3886 active_layer_->HighResTiling()->AllTilesForTesting();
3887 host_impl_.tile_manager()->InitializeTilesWithResourcesForTesting(tiles);
3889 // Force tiles after max_tiles to be OOM. TileManager uses
3890 // GlobalStateThatImpactsTilesPriority from LayerTreeHostImpl, and we cannot
3891 // directly set state to host_impl_, so we set policy that would change the
3892 // state. We also need to update tree priority separately.
3893 GlobalStateThatImpactsTilePriority state;
3894 size_t max_tiles = 1;
3895 size_t memory_limit = max_tiles * 4 * tile_size.width() * tile_size.height();
3896 size_t resource_limit = max_tiles;
3897 ManagedMemoryPolicy policy(memory_limit,
3898 gpu::MemoryAllocation::CUTOFF_ALLOW_EVERYTHING,
3899 resource_limit);
3900 host_impl_.SetMemoryPolicy(policy);
3901 host_impl_.SetTreePriority(SAME_PRIORITY_FOR_BOTH_TREES);
3902 host_impl_.PrepareTiles();
3904 scoped_ptr<RenderPass> render_pass = RenderPass::Create();
3905 AppendQuadsData data;
3906 active_layer_->WillDraw(DRAW_MODE_HARDWARE, nullptr);
3907 active_layer_->AppendQuads(render_pass.get(), &data);
3908 active_layer_->DidDraw(nullptr);
3910 // Even when OOM, quads should be produced, and should be different material
3911 // from quads with resource.
3912 EXPECT_LT(max_tiles, render_pass->quad_list.size());
3913 EXPECT_EQ(DrawQuad::Material::TILED_CONTENT,
3914 render_pass->quad_list.front()->material);
3915 EXPECT_EQ(DrawQuad::Material::SOLID_COLOR,
3916 render_pass->quad_list.back()->material);
3919 class OcclusionTrackingSettings : public LowResTilingsSettings {
3920 public:
3921 OcclusionTrackingSettings() { use_occlusion_for_tile_prioritization = true; }
3924 class OcclusionTrackingPictureLayerImplTest : public PictureLayerImplTest {
3925 public:
3926 OcclusionTrackingPictureLayerImplTest()
3927 : PictureLayerImplTest(OcclusionTrackingSettings()) {}
3929 void VerifyEvictionConsidersOcclusion(FakePictureLayerImpl* layer,
3930 WhichTree tree,
3931 size_t expected_occluded_tile_count,
3932 int source_line) {
3933 size_t occluded_tile_count = 0u;
3934 PrioritizedTile last_tile;
3936 scoped_ptr<TilingSetEvictionQueue> queue(
3937 new TilingSetEvictionQueue(layer->picture_layer_tiling_set()));
3938 while (!queue->IsEmpty()) {
3939 PrioritizedTile prioritized_tile = queue->Top();
3940 Tile* tile = prioritized_tile.tile();
3941 if (!last_tile.tile())
3942 last_tile = prioritized_tile;
3944 // The only way we will encounter an occluded tile after an unoccluded
3945 // tile is if the priorty bin decreased, the tile is required for
3946 // activation, or the scale changed.
3947 bool tile_is_occluded = prioritized_tile.is_occluded();
3948 if (tile_is_occluded) {
3949 occluded_tile_count++;
3951 bool last_tile_is_occluded = last_tile.is_occluded();
3952 if (!last_tile_is_occluded) {
3953 TilePriority::PriorityBin tile_priority_bin =
3954 prioritized_tile.priority().priority_bin;
3955 TilePriority::PriorityBin last_tile_priority_bin =
3956 last_tile.priority().priority_bin;
3958 EXPECT_TRUE(tile_priority_bin < last_tile_priority_bin ||
3959 tile->required_for_activation() ||
3960 tile->contents_scale() !=
3961 last_tile.tile()->contents_scale())
3962 << "line: " << source_line;
3965 last_tile = prioritized_tile;
3966 queue->Pop();
3968 EXPECT_EQ(expected_occluded_tile_count, occluded_tile_count)
3969 << "line: " << source_line;
3973 TEST_F(OcclusionTrackingPictureLayerImplTest,
3974 OccludedTilesSkippedDuringRasterization) {
3975 host_impl_.AdvanceToNextFrame(base::TimeDelta::FromMilliseconds(1));
3977 gfx::Size tile_size(102, 102);
3978 gfx::Size layer_bounds(1000, 1000);
3979 gfx::Size viewport_size(500, 500);
3980 gfx::Point occluding_layer_position(310, 0);
3982 host_impl_.SetViewportSize(viewport_size);
3984 scoped_refptr<FakePicturePileImpl> pending_pile =
3985 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
3986 SetupPendingTreeWithFixedTileSize(pending_pile, tile_size, Region());
3988 // No occlusion.
3989 int unoccluded_tile_count = 0;
3990 scoped_ptr<TilingSetRasterQueueAll> queue(new TilingSetRasterQueueAll(
3991 pending_layer_->picture_layer_tiling_set(), false));
3992 while (!queue->IsEmpty()) {
3993 PrioritizedTile prioritized_tile = queue->Top();
3994 Tile* tile = prioritized_tile.tile();
3996 // Occluded tiles should not be iterated over.
3997 EXPECT_FALSE(prioritized_tile.is_occluded());
3999 // Some tiles may not be visible (i.e. outside the viewport). The rest are
4000 // visible and at least partially unoccluded, verified by the above expect.
4001 bool tile_is_visible =
4002 tile->content_rect().Intersects(pending_layer_->visible_layer_rect());
4003 if (tile_is_visible)
4004 unoccluded_tile_count++;
4005 queue->Pop();
4007 EXPECT_EQ(unoccluded_tile_count, 25);
4009 // Partial occlusion.
4010 pending_layer_->AddChild(LayerImpl::Create(host_impl_.pending_tree(), 1));
4011 LayerImpl* layer1 = pending_layer_->children()[0];
4012 layer1->SetBounds(layer_bounds);
4013 layer1->SetDrawsContent(true);
4014 layer1->SetContentsOpaque(true);
4015 layer1->SetPosition(occluding_layer_position);
4017 host_impl_.AdvanceToNextFrame(base::TimeDelta::FromMilliseconds(200));
4018 bool update_lcd_text = false;
4019 host_impl_.pending_tree()->UpdateDrawProperties(update_lcd_text);
4021 unoccluded_tile_count = 0;
4022 queue.reset(new TilingSetRasterQueueAll(
4023 pending_layer_->picture_layer_tiling_set(), false));
4024 while (!queue->IsEmpty()) {
4025 PrioritizedTile prioritized_tile = queue->Top();
4026 Tile* tile = prioritized_tile.tile();
4028 EXPECT_FALSE(prioritized_tile.is_occluded());
4030 bool tile_is_visible =
4031 tile->content_rect().Intersects(pending_layer_->visible_layer_rect());
4032 if (tile_is_visible)
4033 unoccluded_tile_count++;
4034 queue->Pop();
4036 EXPECT_EQ(20, unoccluded_tile_count);
4038 // Full occlusion.
4039 layer1->SetPosition(gfx::Point(0, 0));
4041 host_impl_.AdvanceToNextFrame(base::TimeDelta::FromMilliseconds(200));
4042 host_impl_.pending_tree()->UpdateDrawProperties(update_lcd_text);
4044 unoccluded_tile_count = 0;
4045 queue.reset(new TilingSetRasterQueueAll(
4046 pending_layer_->picture_layer_tiling_set(), false));
4047 while (!queue->IsEmpty()) {
4048 PrioritizedTile prioritized_tile = queue->Top();
4049 Tile* tile = prioritized_tile.tile();
4051 EXPECT_FALSE(prioritized_tile.is_occluded());
4053 bool tile_is_visible =
4054 tile->content_rect().Intersects(pending_layer_->visible_layer_rect());
4055 if (tile_is_visible)
4056 unoccluded_tile_count++;
4057 queue->Pop();
4059 EXPECT_EQ(unoccluded_tile_count, 0);
4062 TEST_F(OcclusionTrackingPictureLayerImplTest,
4063 OccludedTilesNotMarkedAsRequired) {
4064 host_impl_.AdvanceToNextFrame(base::TimeDelta::FromMilliseconds(1));
4066 gfx::Size tile_size(102, 102);
4067 gfx::Size layer_bounds(1000, 1000);
4068 gfx::Size viewport_size(500, 500);
4069 gfx::Point occluding_layer_position(310, 0);
4071 host_impl_.SetViewportSize(viewport_size);
4073 scoped_refptr<FakePicturePileImpl> pending_pile =
4074 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
4075 SetupPendingTreeWithFixedTileSize(pending_pile, tile_size, Region());
4077 // No occlusion.
4078 int occluded_tile_count = 0;
4079 for (size_t i = 0; i < pending_layer_->num_tilings(); ++i) {
4080 PictureLayerTiling* tiling = pending_layer_->tilings()->tiling_at(i);
4081 auto prioritized_tiles =
4082 tiling->UpdateAndGetAllPrioritizedTilesForTesting();
4084 occluded_tile_count = 0;
4085 for (PictureLayerTiling::CoverageIterator iter(tiling, 1.f,
4086 gfx::Rect(layer_bounds));
4087 iter; ++iter) {
4088 if (!*iter)
4089 continue;
4090 const Tile* tile = *iter;
4092 // Fully occluded tiles are not required for activation.
4093 if (prioritized_tiles[tile].is_occluded()) {
4094 EXPECT_FALSE(tile->required_for_activation());
4095 occluded_tile_count++;
4098 EXPECT_EQ(occluded_tile_count, 0);
4101 // Partial occlusion.
4102 pending_layer_->AddChild(LayerImpl::Create(host_impl_.pending_tree(), 1));
4103 LayerImpl* layer1 = pending_layer_->children()[0];
4104 layer1->SetBounds(layer_bounds);
4105 layer1->SetDrawsContent(true);
4106 layer1->SetContentsOpaque(true);
4107 layer1->SetPosition(occluding_layer_position);
4109 host_impl_.AdvanceToNextFrame(base::TimeDelta::FromMilliseconds(200));
4110 bool update_lcd_text = false;
4111 host_impl_.pending_tree()->UpdateDrawProperties(update_lcd_text);
4113 for (size_t i = 0; i < pending_layer_->num_tilings(); ++i) {
4114 PictureLayerTiling* tiling = pending_layer_->tilings()->tiling_at(i);
4115 auto prioritized_tiles =
4116 tiling->UpdateAndGetAllPrioritizedTilesForTesting();
4118 occluded_tile_count = 0;
4119 for (PictureLayerTiling::CoverageIterator iter(tiling, 1.f,
4120 gfx::Rect(layer_bounds));
4121 iter; ++iter) {
4122 if (!*iter)
4123 continue;
4124 const Tile* tile = *iter;
4126 if (prioritized_tiles[tile].is_occluded()) {
4127 EXPECT_FALSE(tile->required_for_activation());
4128 occluded_tile_count++;
4131 switch (i) {
4132 case 0:
4133 EXPECT_EQ(occluded_tile_count, 5);
4134 break;
4135 case 1:
4136 EXPECT_EQ(occluded_tile_count, 2);
4137 break;
4138 default:
4139 NOTREACHED();
4143 // Full occlusion.
4144 layer1->SetPosition(gfx::PointF(0, 0));
4146 host_impl_.AdvanceToNextFrame(base::TimeDelta::FromMilliseconds(200));
4147 host_impl_.pending_tree()->UpdateDrawProperties(update_lcd_text);
4149 for (size_t i = 0; i < pending_layer_->num_tilings(); ++i) {
4150 PictureLayerTiling* tiling = pending_layer_->tilings()->tiling_at(i);
4151 auto prioritized_tiles =
4152 tiling->UpdateAndGetAllPrioritizedTilesForTesting();
4154 occluded_tile_count = 0;
4155 for (PictureLayerTiling::CoverageIterator iter(tiling, 1.f,
4156 gfx::Rect(layer_bounds));
4157 iter; ++iter) {
4158 if (!*iter)
4159 continue;
4160 const Tile* tile = *iter;
4162 if (prioritized_tiles[tile].is_occluded()) {
4163 EXPECT_FALSE(tile->required_for_activation());
4164 occluded_tile_count++;
4167 switch (i) {
4168 case 0:
4169 EXPECT_EQ(25, occluded_tile_count);
4170 break;
4171 case 1:
4172 EXPECT_EQ(4, occluded_tile_count);
4173 break;
4174 default:
4175 NOTREACHED();
4180 TEST_F(OcclusionTrackingPictureLayerImplTest, OcclusionForDifferentScales) {
4181 host_impl_.AdvanceToNextFrame(base::TimeDelta::FromMilliseconds(1));
4183 gfx::Size tile_size(102, 102);
4184 gfx::Size layer_bounds(1000, 1000);
4185 gfx::Size viewport_size(500, 500);
4186 gfx::Point occluding_layer_position(310, 0);
4188 scoped_refptr<FakePicturePileImpl> pending_pile =
4189 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
4191 host_impl_.SetViewportSize(viewport_size);
4193 SetupPendingTreeWithFixedTileSize(pending_pile, tile_size, Region());
4194 ASSERT_TRUE(pending_layer_->CanHaveTilings());
4196 pending_layer_->AddChild(LayerImpl::Create(host_impl_.pending_tree(), 1));
4197 LayerImpl* layer1 = pending_layer_->children()[0];
4198 layer1->SetBounds(layer_bounds);
4199 layer1->SetDrawsContent(true);
4200 layer1->SetContentsOpaque(true);
4201 layer1->SetPosition(occluding_layer_position);
4203 pending_layer_->tilings()->RemoveAllTilings();
4204 float low_res_factor = host_impl_.settings().low_res_contents_scale_factor;
4205 pending_layer_->AddTiling(low_res_factor)->set_resolution(LOW_RESOLUTION);
4206 pending_layer_->AddTiling(0.3f)->set_resolution(HIGH_RESOLUTION);
4207 pending_layer_->AddTiling(0.7f)->set_resolution(HIGH_RESOLUTION);
4208 pending_layer_->AddTiling(1.0f)->set_resolution(HIGH_RESOLUTION);
4209 pending_layer_->AddTiling(2.0f)->set_resolution(HIGH_RESOLUTION);
4211 host_impl_.AdvanceToNextFrame(base::TimeDelta::FromMilliseconds(1));
4212 // UpdateDrawProperties with the occluding layer.
4213 bool update_lcd_text = false;
4214 host_impl_.pending_tree()->UpdateDrawProperties(update_lcd_text);
4216 EXPECT_EQ(5u, pending_layer_->num_tilings());
4218 int occluded_tile_count = 0;
4219 for (size_t i = 0; i < pending_layer_->num_tilings(); ++i) {
4220 PictureLayerTiling* tiling = pending_layer_->tilings()->tiling_at(i);
4221 auto prioritized_tiles =
4222 tiling->UpdateAndGetAllPrioritizedTilesForTesting();
4223 std::vector<Tile*> tiles = tiling->AllTilesForTesting();
4225 occluded_tile_count = 0;
4226 for (size_t j = 0; j < tiles.size(); ++j) {
4227 if (prioritized_tiles[tiles[j]].is_occluded()) {
4228 gfx::Rect scaled_content_rect = ScaleToEnclosingRect(
4229 tiles[j]->content_rect(), 1.0f / tiles[j]->contents_scale());
4230 EXPECT_GE(scaled_content_rect.x(), occluding_layer_position.x());
4231 occluded_tile_count++;
4235 switch (i) {
4236 case 0:
4237 EXPECT_EQ(occluded_tile_count, 30);
4238 break;
4239 case 1:
4240 EXPECT_EQ(occluded_tile_count, 5);
4241 break;
4242 case 2:
4243 EXPECT_EQ(occluded_tile_count, 4);
4244 break;
4245 case 4:
4246 case 3:
4247 EXPECT_EQ(occluded_tile_count, 2);
4248 break;
4249 default:
4250 NOTREACHED();
4255 TEST_F(OcclusionTrackingPictureLayerImplTest, DifferentOcclusionOnTrees) {
4256 gfx::Size tile_size(102, 102);
4257 gfx::Size layer_bounds(1000, 1000);
4258 gfx::Size viewport_size(1000, 1000);
4259 gfx::Point occluding_layer_position(310, 0);
4260 gfx::Rect invalidation_rect(230, 230, 102, 102);
4262 scoped_refptr<FakePicturePileImpl> pending_pile =
4263 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
4264 scoped_refptr<FakePicturePileImpl> active_pile =
4265 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
4267 host_impl_.SetViewportSize(viewport_size);
4268 SetupPendingTree(active_pile);
4270 // Partially occlude the active layer.
4271 pending_layer_->AddChild(LayerImpl::Create(host_impl_.pending_tree(), 2));
4272 LayerImpl* layer1 = pending_layer_->children()[0];
4273 layer1->SetBounds(layer_bounds);
4274 layer1->SetDrawsContent(true);
4275 layer1->SetContentsOpaque(true);
4276 layer1->SetPosition(occluding_layer_position);
4278 ActivateTree();
4280 for (size_t i = 0; i < active_layer_->num_tilings(); ++i) {
4281 PictureLayerTiling* tiling = active_layer_->tilings()->tiling_at(i);
4282 auto prioritized_tiles =
4283 tiling->UpdateAndGetAllPrioritizedTilesForTesting();
4285 for (PictureLayerTiling::CoverageIterator iter(tiling, 1.f,
4286 gfx::Rect(layer_bounds));
4287 iter; ++iter) {
4288 if (!*iter)
4289 continue;
4290 const Tile* tile = *iter;
4292 gfx::Rect scaled_content_rect = ScaleToEnclosingRect(
4293 tile->content_rect(), 1.0f / tile->contents_scale());
4294 // Tiles are occluded on the active tree iff they lie beneath the
4295 // occluding layer.
4296 EXPECT_EQ(prioritized_tiles[tile].is_occluded(),
4297 scaled_content_rect.x() >= occluding_layer_position.x());
4301 // Partially invalidate the pending layer.
4302 SetupPendingTreeWithInvalidation(pending_pile, invalidation_rect);
4304 for (size_t i = 0; i < active_layer_->num_tilings(); ++i) {
4305 PictureLayerTiling* tiling = active_layer_->tilings()->tiling_at(i);
4306 auto prioritized_tiles =
4307 tiling->UpdateAndGetAllPrioritizedTilesForTesting();
4309 for (PictureLayerTiling::CoverageIterator iter(tiling, 1.f,
4310 gfx::Rect(layer_bounds));
4311 iter; ++iter) {
4312 if (!*iter)
4313 continue;
4314 const Tile* tile = *iter;
4315 EXPECT_TRUE(tile);
4317 // All tiles are unoccluded, because the pending tree has no occlusion.
4318 EXPECT_FALSE(prioritized_tiles[tile].is_occluded());
4320 if (tiling->resolution() == LOW_RESOLUTION) {
4321 EXPECT_FALSE(active_layer_->GetPendingOrActiveTwinTiling(tiling));
4322 continue;
4325 Tile* twin_tile = active_layer_->GetPendingOrActiveTwinTiling(tiling)
4326 ->TileAt(iter.i(), iter.j());
4327 gfx::Rect scaled_content_rect = ScaleToEnclosingRect(
4328 tile->content_rect(), 1.0f / tile->contents_scale());
4330 if (scaled_content_rect.Intersects(invalidation_rect)) {
4331 // Tiles inside the invalidation rect exist on both trees.
4332 EXPECT_TRUE(twin_tile);
4333 EXPECT_NE(tile, twin_tile);
4334 } else {
4335 // Tiles outside the invalidation rect only exist on the active tree.
4336 EXPECT_FALSE(twin_tile);
4342 TEST_F(OcclusionTrackingPictureLayerImplTest,
4343 OccludedTilesConsideredDuringEviction) {
4344 host_impl_.AdvanceToNextFrame(base::TimeDelta::FromMilliseconds(1));
4346 gfx::Size tile_size(102, 102);
4347 gfx::Size layer_bounds(1000, 1000);
4348 gfx::Size viewport_size(1000, 1000);
4349 gfx::Point pending_occluding_layer_position(310, 0);
4350 gfx::Point active_occluding_layer_position(0, 310);
4351 gfx::Rect invalidation_rect(230, 230, 152, 152);
4353 host_impl_.SetViewportSize(viewport_size);
4354 host_impl_.SetDeviceScaleFactor(2.f);
4356 scoped_refptr<FakePicturePileImpl> pending_pile =
4357 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
4358 scoped_refptr<FakePicturePileImpl> active_pile =
4359 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
4361 SetupPendingTreeWithFixedTileSize(active_pile, tile_size, Region());
4363 // Partially occlude the active layer.
4364 pending_layer_->AddChild(LayerImpl::Create(host_impl_.pending_tree(), 2));
4365 LayerImpl* active_occluding_layer = pending_layer_->children()[0];
4366 active_occluding_layer->SetBounds(layer_bounds);
4367 active_occluding_layer->SetDrawsContent(true);
4368 active_occluding_layer->SetContentsOpaque(true);
4369 active_occluding_layer->SetPosition(active_occluding_layer_position);
4371 ActivateTree();
4373 // Partially invalidate the pending layer. Tiles inside the invalidation rect
4374 // are created.
4375 SetupPendingTreeWithFixedTileSize(pending_pile, tile_size, invalidation_rect);
4377 // Partially occlude the pending layer in a different way.
4378 pending_layer_->AddChild(LayerImpl::Create(host_impl_.pending_tree(), 3));
4379 LayerImpl* pending_occluding_layer = pending_layer_->children()[0];
4380 pending_occluding_layer->SetBounds(layer_bounds);
4381 pending_occluding_layer->SetDrawsContent(true);
4382 pending_occluding_layer->SetContentsOpaque(true);
4383 pending_occluding_layer->SetPosition(pending_occluding_layer_position);
4385 EXPECT_EQ(1u, pending_layer_->num_tilings());
4386 EXPECT_EQ(2u, active_layer_->num_tilings());
4388 host_impl_.AdvanceToNextFrame(base::TimeDelta::FromMilliseconds(1));
4389 // UpdateDrawProperties with the occluding layer.
4390 bool update_lcd_text = false;
4391 host_impl_.pending_tree()->UpdateDrawProperties(update_lcd_text);
4393 // The expected number of occluded tiles on each of the 2 tilings for each of
4394 // the 3 tree priorities.
4395 size_t expected_occluded_tile_count_on_pending[] = {4u, 0u};
4396 size_t expected_occluded_tile_count_on_active[] = {12u, 3u};
4397 size_t total_expected_occluded_tile_count_on_trees[] = {15u, 4u};
4399 // Verify number of occluded tiles on the pending layer for each tiling.
4400 for (size_t i = 0; i < pending_layer_->num_tilings(); ++i) {
4401 PictureLayerTiling* tiling = pending_layer_->tilings()->tiling_at(i);
4402 auto prioritized_tiles =
4403 tiling->UpdateAndGetAllPrioritizedTilesForTesting();
4405 size_t occluded_tile_count_on_pending = 0u;
4406 for (PictureLayerTiling::CoverageIterator iter(tiling, 1.f,
4407 gfx::Rect(layer_bounds));
4408 iter; ++iter) {
4409 Tile* tile = *iter;
4411 if (invalidation_rect.Intersects(iter.geometry_rect()))
4412 EXPECT_TRUE(tile);
4413 else
4414 EXPECT_FALSE(tile);
4416 if (!tile)
4417 continue;
4418 if (prioritized_tiles[tile].is_occluded())
4419 occluded_tile_count_on_pending++;
4421 EXPECT_EQ(expected_occluded_tile_count_on_pending[i],
4422 occluded_tile_count_on_pending)
4423 << tiling->contents_scale();
4426 // Verify number of occluded tiles on the active layer for each tiling.
4427 for (size_t i = 0; i < active_layer_->num_tilings(); ++i) {
4428 PictureLayerTiling* tiling = active_layer_->tilings()->tiling_at(i);
4429 auto prioritized_tiles =
4430 tiling->UpdateAndGetAllPrioritizedTilesForTesting();
4432 size_t occluded_tile_count_on_active = 0u;
4433 for (PictureLayerTiling::CoverageIterator iter(tiling, 1.f,
4434 gfx::Rect(layer_bounds));
4435 iter; ++iter) {
4436 Tile* tile = *iter;
4438 if (!tile)
4439 continue;
4440 if (prioritized_tiles[tile].is_occluded())
4441 occluded_tile_count_on_active++;
4443 EXPECT_EQ(expected_occluded_tile_count_on_active[i],
4444 occluded_tile_count_on_active)
4445 << i;
4448 std::vector<Tile*> all_tiles;
4449 for (size_t i = 0; i < pending_layer_->num_tilings(); ++i) {
4450 PictureLayerTiling* tiling = pending_layer_->tilings()->tiling_at(i);
4451 std::vector<Tile*> tiles = tiling->AllTilesForTesting();
4452 all_tiles.insert(all_tiles.end(), tiles.begin(), tiles.end());
4454 for (size_t i = 0; i < active_layer_->num_tilings(); ++i) {
4455 PictureLayerTiling* tiling = active_layer_->tilings()->tiling_at(i);
4456 std::vector<Tile*> tiles = tiling->AllTilesForTesting();
4457 all_tiles.insert(all_tiles.end(), tiles.begin(), tiles.end());
4460 host_impl_.tile_manager()->InitializeTilesWithResourcesForTesting(all_tiles);
4462 VerifyEvictionConsidersOcclusion(
4463 pending_layer_, PENDING_TREE,
4464 total_expected_occluded_tile_count_on_trees[PENDING_TREE], __LINE__);
4465 VerifyEvictionConsidersOcclusion(
4466 active_layer_, ACTIVE_TREE,
4467 total_expected_occluded_tile_count_on_trees[ACTIVE_TREE], __LINE__);
4469 // Repeat the tests without valid active tree priorities.
4470 active_layer_->set_has_valid_tile_priorities(false);
4471 VerifyEvictionConsidersOcclusion(
4472 pending_layer_, PENDING_TREE,
4473 total_expected_occluded_tile_count_on_trees[PENDING_TREE], __LINE__);
4474 VerifyEvictionConsidersOcclusion(
4475 active_layer_, ACTIVE_TREE,
4476 total_expected_occluded_tile_count_on_trees[ACTIVE_TREE], __LINE__);
4477 active_layer_->set_has_valid_tile_priorities(true);
4479 // Repeat the tests without valid pending tree priorities.
4480 pending_layer_->set_has_valid_tile_priorities(false);
4481 VerifyEvictionConsidersOcclusion(
4482 active_layer_, ACTIVE_TREE,
4483 total_expected_occluded_tile_count_on_trees[ACTIVE_TREE], __LINE__);
4484 VerifyEvictionConsidersOcclusion(
4485 pending_layer_, PENDING_TREE,
4486 total_expected_occluded_tile_count_on_trees[PENDING_TREE], __LINE__);
4487 pending_layer_->set_has_valid_tile_priorities(true);
4490 TEST_F(PictureLayerImplTest, PendingOrActiveTwinLayer) {
4491 gfx::Size tile_size(102, 102);
4492 gfx::Size layer_bounds(1000, 1000);
4494 scoped_refptr<FakePicturePileImpl> pile =
4495 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
4496 SetupPendingTree(pile);
4497 EXPECT_FALSE(pending_layer_->GetPendingOrActiveTwinLayer());
4499 ActivateTree();
4500 EXPECT_FALSE(active_layer_->GetPendingOrActiveTwinLayer());
4502 SetupPendingTree(pile);
4503 EXPECT_TRUE(pending_layer_->GetPendingOrActiveTwinLayer());
4504 EXPECT_TRUE(active_layer_->GetPendingOrActiveTwinLayer());
4505 EXPECT_EQ(pending_layer_, active_layer_->GetPendingOrActiveTwinLayer());
4506 EXPECT_EQ(active_layer_, pending_layer_->GetPendingOrActiveTwinLayer());
4508 ActivateTree();
4509 EXPECT_FALSE(active_layer_->GetPendingOrActiveTwinLayer());
4511 // Make an empty pending tree.
4512 host_impl_.CreatePendingTree();
4513 host_impl_.pending_tree()->DetachLayerTree();
4514 EXPECT_FALSE(active_layer_->GetPendingOrActiveTwinLayer());
4517 void PictureLayerImplTest::TestQuadsForSolidColor(bool test_for_solid) {
4518 host_impl_.AdvanceToNextFrame(base::TimeDelta::FromMilliseconds(1));
4520 gfx::Size tile_size(100, 100);
4521 gfx::Size layer_bounds(200, 200);
4522 gfx::Rect layer_rect(layer_bounds);
4524 FakeContentLayerClient client;
4525 scoped_refptr<PictureLayer> layer =
4526 PictureLayer::Create(layer_settings_, &client);
4527 FakeLayerTreeHostClient host_client(FakeLayerTreeHostClient::DIRECT_3D);
4528 TestTaskGraphRunner task_graph_runner;
4529 scoped_ptr<FakeLayerTreeHost> host =
4530 FakeLayerTreeHost::Create(&host_client, &task_graph_runner);
4531 host->SetRootLayer(layer);
4532 RecordingSource* recording_source = layer->GetRecordingSourceForTesting();
4534 int frame_number = 0;
4536 client.set_fill_with_nonsolid_color(!test_for_solid);
4538 Region invalidation(layer_rect);
4539 recording_source->UpdateAndExpandInvalidation(
4540 &client, &invalidation, layer_bounds, layer_rect, frame_number++,
4541 RecordingSource::RECORD_NORMALLY);
4543 scoped_refptr<RasterSource> pending_raster_source =
4544 recording_source->CreateRasterSource(true);
4546 SetupPendingTreeWithFixedTileSize(pending_raster_source, tile_size, Region());
4547 ActivateTree();
4549 if (test_for_solid) {
4550 EXPECT_EQ(0u, active_layer_->tilings()->num_tilings());
4551 } else {
4552 ASSERT_TRUE(active_layer_->tilings());
4553 ASSERT_GT(active_layer_->tilings()->num_tilings(), 0u);
4554 std::vector<Tile*> tiles =
4555 active_layer_->tilings()->tiling_at(0)->AllTilesForTesting();
4556 EXPECT_FALSE(tiles.empty());
4557 host_impl_.tile_manager()->InitializeTilesWithResourcesForTesting(tiles);
4560 scoped_ptr<RenderPass> render_pass = RenderPass::Create();
4561 AppendQuadsData data;
4562 active_layer_->WillDraw(DRAW_MODE_SOFTWARE, nullptr);
4563 active_layer_->AppendQuads(render_pass.get(), &data);
4564 active_layer_->DidDraw(nullptr);
4566 DrawQuad::Material expected = test_for_solid
4567 ? DrawQuad::Material::SOLID_COLOR
4568 : DrawQuad::Material::TILED_CONTENT;
4569 EXPECT_EQ(expected, render_pass->quad_list.front()->material);
4572 TEST_F(PictureLayerImplTest, DrawSolidQuads) {
4573 TestQuadsForSolidColor(true);
4576 TEST_F(PictureLayerImplTest, DrawNonSolidQuads) {
4577 TestQuadsForSolidColor(false);
4580 TEST_F(PictureLayerImplTest, NonSolidToSolidNoTilings) {
4581 host_impl_.AdvanceToNextFrame(base::TimeDelta::FromMilliseconds(1));
4583 gfx::Size tile_size(100, 100);
4584 gfx::Size layer_bounds(200, 200);
4585 gfx::Rect layer_rect(layer_bounds);
4587 FakeContentLayerClient client;
4588 scoped_refptr<PictureLayer> layer =
4589 PictureLayer::Create(layer_settings_, &client);
4590 FakeLayerTreeHostClient host_client(FakeLayerTreeHostClient::DIRECT_3D);
4591 TestTaskGraphRunner task_graph_runner;
4592 scoped_ptr<FakeLayerTreeHost> host =
4593 FakeLayerTreeHost::Create(&host_client, &task_graph_runner);
4594 host->SetRootLayer(layer);
4595 RecordingSource* recording_source = layer->GetRecordingSourceForTesting();
4597 int frame_number = 0;
4599 client.set_fill_with_nonsolid_color(true);
4601 Region invalidation1(layer_rect);
4602 recording_source->UpdateAndExpandInvalidation(
4603 &client, &invalidation1, layer_bounds, layer_rect, frame_number++,
4604 RecordingSource::RECORD_NORMALLY);
4606 scoped_refptr<RasterSource> raster_source1 =
4607 recording_source->CreateRasterSource(true);
4609 SetupPendingTree(raster_source1);
4610 ActivateTree();
4611 bool update_lcd_text = false;
4612 host_impl_.active_tree()->UpdateDrawProperties(update_lcd_text);
4614 // We've started with a solid layer that contains some tilings.
4615 ASSERT_TRUE(active_layer_->tilings());
4616 EXPECT_NE(0u, active_layer_->tilings()->num_tilings());
4618 client.set_fill_with_nonsolid_color(false);
4620 Region invalidation2(layer_rect);
4621 recording_source->UpdateAndExpandInvalidation(
4622 &client, &invalidation2, layer_bounds, layer_rect, frame_number++,
4623 RecordingSource::RECORD_NORMALLY);
4625 scoped_refptr<RasterSource> raster_source2 =
4626 recording_source->CreateRasterSource(true);
4628 SetupPendingTree(raster_source2);
4629 ActivateTree();
4631 // We've switched to a solid color, so we should end up with no tilings.
4632 ASSERT_TRUE(active_layer_->tilings());
4633 EXPECT_EQ(0u, active_layer_->tilings()->num_tilings());
4636 TEST_F(PictureLayerImplTest, ChangeInViewportAllowsTilingUpdates) {
4637 host_impl_.AdvanceToNextFrame(base::TimeDelta::FromMilliseconds(1));
4639 gfx::Size tile_size(100, 100);
4640 gfx::Size layer_bounds(400, 4000);
4642 scoped_refptr<FakePicturePileImpl> pending_pile =
4643 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
4644 scoped_refptr<FakePicturePileImpl> active_pile =
4645 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
4647 SetupTrees(pending_pile, active_pile);
4649 Region invalidation;
4650 gfx::Rect viewport = gfx::Rect(0, 0, 100, 100);
4651 gfx::Transform transform;
4653 host_impl_.SetRequiresHighResToDraw();
4655 // Update tiles.
4656 pending_layer_->draw_properties().visible_layer_rect = viewport;
4657 pending_layer_->draw_properties().screen_space_transform = transform;
4658 SetupDrawPropertiesAndUpdateTiles(pending_layer_, 1.f, 1.f, 1.f, 1.f, 0.f,
4659 false);
4660 pending_layer_->HighResTiling()->UpdateAllRequiredStateForTesting();
4662 // Ensure we can't activate.
4663 EXPECT_FALSE(host_impl_.tile_manager()->IsReadyToActivate());
4665 // Now in the same frame, move the viewport (this can happen during
4666 // animation).
4667 viewport = gfx::Rect(0, 2000, 100, 100);
4669 // Update tiles.
4670 pending_layer_->draw_properties().visible_layer_rect = viewport;
4671 pending_layer_->draw_properties().screen_space_transform = transform;
4672 SetupDrawPropertiesAndUpdateTiles(pending_layer_, 1.f, 1.f, 1.f, 1.f, 0.f,
4673 false);
4674 pending_layer_->HighResTiling()->UpdateAllRequiredStateForTesting();
4676 // Make sure all viewport tiles (viewport from the tiling) are ready to draw.
4677 std::vector<Tile*> tiles;
4678 for (PictureLayerTiling::CoverageIterator iter(
4679 pending_layer_->HighResTiling(),
4680 1.f,
4681 pending_layer_->HighResTiling()->GetCurrentVisibleRectForTesting());
4682 iter;
4683 ++iter) {
4684 if (*iter)
4685 tiles.push_back(*iter);
4687 for (PictureLayerTiling::CoverageIterator iter(
4688 active_layer_->HighResTiling(), 1.f,
4689 active_layer_->HighResTiling()->GetCurrentVisibleRectForTesting());
4690 iter; ++iter) {
4691 if (*iter)
4692 tiles.push_back(*iter);
4695 host_impl_.tile_manager()->InitializeTilesWithResourcesForTesting(tiles);
4697 // Ensure we can activate.
4698 EXPECT_TRUE(host_impl_.tile_manager()->IsReadyToActivate());
4701 TEST_F(PictureLayerImplTest, CloneMissingRecordings) {
4702 gfx::Size tile_size(100, 100);
4703 gfx::Size layer_bounds(400, 400);
4705 scoped_refptr<FakePicturePileImpl> filled_pile =
4706 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
4708 scoped_ptr<FakePicturePile> partial_recording =
4709 FakePicturePile::CreateEmptyPile(tile_size, layer_bounds);
4710 for (int i = 1; i < partial_recording->tiling().num_tiles_x(); ++i) {
4711 for (int j = 1; j < partial_recording->tiling().num_tiles_y(); ++j)
4712 partial_recording->AddRecordingAt(i, j);
4714 scoped_refptr<FakePicturePileImpl> partial_pile =
4715 FakePicturePileImpl::CreateFromPile(partial_recording.get(), nullptr);
4717 SetupPendingTreeWithFixedTileSize(filled_pile, tile_size, Region());
4718 ActivateTree();
4720 PictureLayerTiling* pending_tiling = old_pending_layer_->HighResTiling();
4721 PictureLayerTiling* active_tiling = active_layer_->HighResTiling();
4723 // We should have all tiles on active, and none on pending.
4724 EXPECT_EQ(0u, pending_tiling->AllTilesForTesting().size());
4725 EXPECT_EQ(5u * 5u, active_tiling->AllTilesForTesting().size());
4727 // Now put a partially-recorded pile on the pending tree (and invalidate
4728 // everything, since the main thread PicturePile will invalidate dropped
4729 // recordings). This will cause us to be missing some tiles.
4730 SetupPendingTreeWithFixedTileSize(partial_pile, tile_size,
4731 Region(gfx::Rect(layer_bounds)));
4732 EXPECT_EQ(3u * 3u, pending_tiling->AllTilesForTesting().size());
4733 EXPECT_FALSE(pending_tiling->TileAt(0, 0));
4734 EXPECT_FALSE(pending_tiling->TileAt(1, 1));
4735 EXPECT_TRUE(pending_tiling->TileAt(2, 2));
4737 // Active is not affected yet.
4738 EXPECT_EQ(5u * 5u, active_tiling->AllTilesForTesting().size());
4740 // Activate the tree. The same tiles go missing on the active tree.
4741 ActivateTree();
4742 EXPECT_EQ(3u * 3u, active_tiling->AllTilesForTesting().size());
4743 EXPECT_FALSE(active_tiling->TileAt(0, 0));
4744 EXPECT_FALSE(active_tiling->TileAt(1, 1));
4745 EXPECT_TRUE(active_tiling->TileAt(2, 2));
4747 // Now put a full recording on the pending tree again. We'll get all our tiles
4748 // back.
4749 SetupPendingTreeWithFixedTileSize(filled_pile, tile_size,
4750 Region(gfx::Rect(layer_bounds)));
4751 EXPECT_EQ(5u * 5u, pending_tiling->AllTilesForTesting().size());
4752 Tile* tile00 = pending_tiling->TileAt(0, 0);
4753 Tile* tile11 = pending_tiling->TileAt(1, 1);
4754 Tile* tile22 = pending_tiling->TileAt(2, 2);
4756 // Active is not affected yet.
4757 EXPECT_EQ(3u * 3u, active_tiling->AllTilesForTesting().size());
4759 // Activate the tree. The tiles are moved to the active tree.
4760 ActivateTree();
4761 EXPECT_EQ(5u * 5u, active_tiling->AllTilesForTesting().size());
4762 EXPECT_EQ(tile00, active_tiling->TileAt(0, 0));
4763 EXPECT_EQ(tile11, active_tiling->TileAt(1, 1));
4764 EXPECT_EQ(tile22, active_tiling->TileAt(2, 2));
4767 TEST_F(PictureLayerImplTest, ScrollPastLiveTilesRectAndBack) {
4768 host_impl_.AdvanceToNextFrame(base::TimeDelta::FromMilliseconds(1));
4770 gfx::Size tile_size(102, 102);
4771 gfx::Size layer_bounds(100, 100);
4772 gfx::Size viewport_size(100, 100);
4774 host_impl_.SetViewportSize(viewport_size);
4775 host_impl_.SetDeviceScaleFactor(1.f);
4777 scoped_refptr<FakePicturePileImpl> pending_pile =
4778 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
4779 scoped_refptr<FakePicturePileImpl> active_pile =
4780 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
4782 SetupPendingTreeWithFixedTileSize(active_pile, tile_size, Region());
4784 ActivateTree();
4785 EXPECT_TRUE(active_layer_->HighResTiling()->has_tiles());
4787 host_impl_.SetExternalDrawConstraints(
4788 gfx::Transform(), // transform
4789 gfx::Rect(), // clip
4790 gfx::Rect(), // viewport
4791 gfx::Rect(0, 5000, 100, 100), // viewport_rect_for_tile_priority
4792 gfx::Transform(), // transform_for_tile_priority
4793 false);
4795 SetupPendingTreeWithFixedTileSize(pending_pile, tile_size, gfx::Rect());
4797 EXPECT_FALSE(pending_layer_->HighResTiling()->has_tiles());
4798 EXPECT_TRUE(pending_layer_->HighResTiling()->live_tiles_rect().IsEmpty());
4799 ActivateTree();
4800 EXPECT_FALSE(active_layer_->HighResTiling()->has_tiles());
4801 EXPECT_TRUE(active_layer_->HighResTiling()->live_tiles_rect().IsEmpty());
4803 host_impl_.SetExternalDrawConstraints(
4804 gfx::Transform(), // transform
4805 gfx::Rect(), // clip
4806 gfx::Rect(), // viewport
4807 gfx::Rect(0, 110, 100, 100), // viewport_rect_for_tile_priority
4808 gfx::Transform(), // transform_for_tile_priority
4809 false);
4811 SetupPendingTreeWithFixedTileSize(pending_pile, tile_size, gfx::Rect());
4813 EXPECT_FALSE(pending_layer_->HighResTiling()->has_tiles());
4814 EXPECT_FALSE(pending_layer_->HighResTiling()->live_tiles_rect().IsEmpty());
4815 ActivateTree();
4816 EXPECT_TRUE(active_layer_->HighResTiling()->has_tiles());
4817 EXPECT_FALSE(active_layer_->HighResTiling()->live_tiles_rect().IsEmpty());
4820 TEST_F(PictureLayerImplTest, ScrollPropagatesToPending) {
4821 host_impl_.AdvanceToNextFrame(base::TimeDelta::FromMilliseconds(1));
4823 gfx::Size tile_size(102, 102);
4824 gfx::Size layer_bounds(1000, 1000);
4825 gfx::Size viewport_size(100, 100);
4827 host_impl_.SetViewportSize(viewport_size);
4828 host_impl_.SetDeviceScaleFactor(1.f);
4830 scoped_refptr<FakePicturePileImpl> pending_pile =
4831 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
4832 scoped_refptr<FakePicturePileImpl> active_pile =
4833 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
4835 SetupTrees(pending_pile, active_pile);
4837 active_layer_->SetCurrentScrollOffset(gfx::ScrollOffset(0.0, 50.0));
4838 host_impl_.active_tree()->UpdateDrawProperties(false);
4839 EXPECT_EQ("0,50 100x100", active_layer_->HighResTiling()
4840 ->GetCurrentVisibleRectForTesting()
4841 .ToString());
4843 EXPECT_EQ("0,0 100x100", pending_layer_->HighResTiling()
4844 ->GetCurrentVisibleRectForTesting()
4845 .ToString());
4846 host_impl_.pending_tree()->UpdateDrawProperties(false);
4847 EXPECT_EQ("0,50 100x100", pending_layer_->HighResTiling()
4848 ->GetCurrentVisibleRectForTesting()
4849 .ToString());
4852 TEST_F(PictureLayerImplTest, UpdateLCDInvalidatesPendingTree) {
4853 host_impl_.AdvanceToNextFrame(base::TimeDelta::FromMilliseconds(1));
4855 gfx::Size tile_size(102, 102);
4856 gfx::Size layer_bounds(100, 100);
4857 gfx::Size viewport_size(100, 100);
4859 host_impl_.SetViewportSize(viewport_size);
4860 host_impl_.SetDeviceScaleFactor(1.f);
4862 scoped_refptr<FakePicturePileImpl> pending_pile =
4863 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
4864 SetupPendingTreeWithFixedTileSize(pending_pile, tile_size, Region());
4866 EXPECT_TRUE(pending_layer_->RasterSourceUsesLCDText());
4867 EXPECT_TRUE(pending_layer_->HighResTiling()->has_tiles());
4868 std::vector<Tile*> tiles =
4869 pending_layer_->HighResTiling()->AllTilesForTesting();
4870 auto prioritized_tiles = pending_layer_->HighResTiling()
4871 ->UpdateAndGetAllPrioritizedTilesForTesting();
4873 for (Tile* tile : tiles)
4874 EXPECT_EQ(pending_layer_->raster_source(),
4875 prioritized_tiles[tile].raster_source());
4877 pending_layer_->draw_properties().can_use_lcd_text = false;
4878 pending_layer_->UpdateCanUseLCDTextAfterCommit();
4880 EXPECT_FALSE(pending_layer_->RasterSourceUsesLCDText());
4881 EXPECT_NE(pending_pile.get(), pending_layer_->raster_source());
4882 EXPECT_TRUE(pending_layer_->HighResTiling()->has_tiles());
4883 tiles = pending_layer_->HighResTiling()->AllTilesForTesting();
4884 prioritized_tiles = pending_layer_->HighResTiling()
4885 ->UpdateAndGetAllPrioritizedTilesForTesting();
4886 for (Tile* tile : tiles)
4887 EXPECT_EQ(pending_layer_->raster_source(),
4888 prioritized_tiles[tile].raster_source());
4891 TEST_F(PictureLayerImplTest, TilingAllTilesDone) {
4892 gfx::Size tile_size = host_impl_.settings().default_tile_size;
4893 size_t tile_mem = 4 * tile_size.width() * tile_size.height();
4894 gfx::Size layer_bounds(1000, 1000);
4896 // Create tiles.
4897 scoped_refptr<FakePicturePileImpl> pending_pile =
4898 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
4899 SetupPendingTree(pending_pile);
4900 pending_layer_->SetBounds(layer_bounds);
4901 ActivateTree();
4902 host_impl_.tile_manager()->InitializeTilesWithResourcesForTesting(
4903 active_layer_->HighResTiling()->AllTilesForTesting());
4904 host_impl_.SetTreePriority(SAME_PRIORITY_FOR_BOTH_TREES);
4906 EXPECT_FALSE(active_layer_->HighResTiling()->all_tiles_done());
4909 // Set a memory policy that will fit all tiles.
4910 size_t max_tiles = 16;
4911 size_t memory_limit = max_tiles * tile_mem;
4912 ManagedMemoryPolicy policy(memory_limit,
4913 gpu::MemoryAllocation::CUTOFF_ALLOW_EVERYTHING,
4914 max_tiles);
4915 host_impl_.SetMemoryPolicy(policy);
4916 host_impl_.PrepareTiles();
4918 EXPECT_TRUE(active_layer_->HighResTiling()->all_tiles_done());
4922 // Set a memory policy that will cause tile eviction.
4923 size_t max_tiles = 1;
4924 size_t memory_limit = max_tiles * tile_mem;
4925 ManagedMemoryPolicy policy(memory_limit,
4926 gpu::MemoryAllocation::CUTOFF_ALLOW_EVERYTHING,
4927 max_tiles);
4928 host_impl_.SetMemoryPolicy(policy);
4929 host_impl_.PrepareTiles();
4931 EXPECT_FALSE(active_layer_->HighResTiling()->all_tiles_done());
4935 class TileSizeSettings : public PictureLayerImplTestSettings {
4936 public:
4937 TileSizeSettings() {
4938 default_tile_size = gfx::Size(100, 100);
4939 max_untiled_layer_size = gfx::Size(200, 200);
4943 class TileSizeTest : public PictureLayerImplTest {
4944 public:
4945 TileSizeTest() : PictureLayerImplTest(TileSizeSettings()) {}
4948 TEST_F(TileSizeTest, TileSizes) {
4949 host_impl_.CreatePendingTree();
4951 LayerTreeImpl* pending_tree = host_impl_.pending_tree();
4952 scoped_ptr<FakePictureLayerImpl> layer =
4953 FakePictureLayerImpl::Create(pending_tree, id_);
4955 host_impl_.SetViewportSize(gfx::Size(1000, 1000));
4956 gfx::Size result;
4958 host_impl_.SetContentIsSuitableForGpuRasterization(true);
4959 host_impl_.SetHasGpuRasterizationTrigger(false);
4960 EXPECT_EQ(host_impl_.gpu_rasterization_status(),
4961 GpuRasterizationStatus::OFF_VIEWPORT);
4963 // Default tile-size for large layers.
4964 result = layer->CalculateTileSize(gfx::Size(10000, 10000));
4965 EXPECT_EQ(result.width(), 100);
4966 EXPECT_EQ(result.height(), 100);
4967 // Don't tile and round-up, when under max_untiled_layer_size.
4968 result = layer->CalculateTileSize(gfx::Size(42, 42));
4969 EXPECT_EQ(result.width(), 64);
4970 EXPECT_EQ(result.height(), 64);
4971 result = layer->CalculateTileSize(gfx::Size(191, 191));
4972 EXPECT_EQ(result.width(), 192);
4973 EXPECT_EQ(result.height(), 192);
4974 result = layer->CalculateTileSize(gfx::Size(199, 199));
4975 EXPECT_EQ(result.width(), 200);
4976 EXPECT_EQ(result.height(), 200);
4978 // Gpu-rasterization uses 25% viewport-height tiles.
4979 // The +2's below are for border texels.
4980 host_impl_.SetHasGpuRasterizationTrigger(true);
4981 EXPECT_EQ(host_impl_.gpu_rasterization_status(), GpuRasterizationStatus::ON);
4982 host_impl_.SetViewportSize(gfx::Size(2000, 2000));
4984 layer->set_gpu_raster_max_texture_size(host_impl_.device_viewport_size());
4985 result = layer->CalculateTileSize(gfx::Size(10000, 10000));
4986 EXPECT_EQ(result.width(), 2000 + 2 * PictureLayerTiling::kBorderTexels);
4987 EXPECT_EQ(result.height(), 500 + 2);
4989 // Clamp and round-up, when smaller than viewport.
4990 // Tile-height doubles to 50% when width shrinks to <= 50%.
4991 host_impl_.SetViewportSize(gfx::Size(1000, 1000));
4992 layer->set_gpu_raster_max_texture_size(host_impl_.device_viewport_size());
4993 result = layer->CalculateTileSize(gfx::Size(447, 10000));
4994 EXPECT_EQ(result.width(), 448);
4995 EXPECT_EQ(result.height(), 500 + 2);
4997 // Largest layer is 50% of viewport width (rounded up), and
4998 // 50% of viewport in height.
4999 result = layer->CalculateTileSize(gfx::Size(447, 400));
5000 EXPECT_EQ(result.width(), 448);
5001 EXPECT_EQ(result.height(), 448);
5002 result = layer->CalculateTileSize(gfx::Size(500, 499));
5003 EXPECT_EQ(result.width(), 512);
5004 EXPECT_EQ(result.height(), 500 + 2);
5007 TEST_F(NoLowResPictureLayerImplTest, LowResWasHighResCollision) {
5008 gfx::Size tile_size(400, 400);
5009 gfx::Size layer_bounds(1300, 1900);
5011 float low_res_factor = host_impl_.settings().low_res_contents_scale_factor;
5013 scoped_refptr<FakePicturePileImpl> pending_pile =
5014 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
5015 scoped_refptr<FakePicturePileImpl> active_pile =
5016 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
5018 // Set up the high and low res tilings before pinch zoom.
5019 SetupTrees(pending_pile, active_pile);
5020 ResetTilingsAndRasterScales();
5022 float page_scale = 2.f;
5023 SetContentsScaleOnBothLayers(page_scale, 1.0f, page_scale, 1.0f, 0.f, false);
5024 EXPECT_BOTH_EQ(num_tilings(), 1u);
5025 EXPECT_BOTH_EQ(tilings()->tiling_at(0)->contents_scale(), page_scale);
5027 host_impl_.PinchGestureBegin();
5029 // Zoom out to exactly the low res factor so that the previous high res
5030 // would be equal to the current low res (if it were possible to have one).
5031 float zoomed = page_scale / low_res_factor;
5032 SetContentsScaleOnBothLayers(zoomed, 1.0f, zoomed, 1.0f, 0.f, false);
5033 EXPECT_EQ(1u, pending_layer_->num_tilings());
5034 EXPECT_EQ(zoomed, pending_layer_->tilings()->tiling_at(0)->contents_scale());
5037 TEST_F(PictureLayerImplTest, HighResWasLowResCollision) {
5038 gfx::Size tile_size(400, 400);
5039 gfx::Size layer_bounds(1300, 1900);
5041 float low_res_factor = host_impl_.settings().low_res_contents_scale_factor;
5043 scoped_refptr<FakePicturePileImpl> pending_pile =
5044 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
5045 scoped_refptr<FakePicturePileImpl> active_pile =
5046 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
5048 // Set up the high and low res tilings before pinch zoom.
5049 SetupTrees(pending_pile, active_pile);
5050 ResetTilingsAndRasterScales();
5052 float page_scale = 4.f;
5053 float low_res = page_scale * low_res_factor;
5054 float extra_low_res = low_res * low_res_factor;
5055 SetupDrawPropertiesAndUpdateTiles(active_layer_, page_scale, 1.0f, page_scale,
5056 1.0f, 0.f, false);
5057 EXPECT_EQ(2u, active_layer_->tilings()->num_tilings());
5058 EXPECT_EQ(page_scale,
5059 active_layer_->tilings()->tiling_at(0)->contents_scale());
5060 EXPECT_EQ(low_res, active_layer_->tilings()->tiling_at(1)->contents_scale());
5062 // Grab a current low res tile.
5063 PictureLayerTiling* old_low_res_tiling =
5064 active_layer_->tilings()->tiling_at(1);
5065 Tile* old_low_res_tile = active_layer_->tilings()->tiling_at(1)->TileAt(0, 0);
5067 // The tiling knows it has low res content.
5068 EXPECT_TRUE(active_layer_->tilings()
5069 ->tiling_at(1)
5070 ->may_contain_low_resolution_tiles());
5072 host_impl_.AdvanceToNextFrame(base::TimeDelta::FromMilliseconds(1));
5074 // Zoom in to exactly the low res factor so that the previous low res
5075 // would be equal to the current high res.
5076 SetupDrawPropertiesAndUpdateTiles(active_layer_, low_res, 1.0f, low_res, 1.0f,
5077 0.f, false);
5078 // 3 tilings. The old high res, the new high res (old low res) and the new low
5079 // res.
5080 EXPECT_EQ(3u, active_layer_->num_tilings());
5082 PictureLayerTilingSet* tilings = active_layer_->tilings();
5083 EXPECT_EQ(page_scale, tilings->tiling_at(0)->contents_scale());
5084 EXPECT_EQ(low_res, tilings->tiling_at(1)->contents_scale());
5085 EXPECT_EQ(extra_low_res, tilings->tiling_at(2)->contents_scale());
5087 EXPECT_EQ(NON_IDEAL_RESOLUTION, tilings->tiling_at(0)->resolution());
5088 EXPECT_EQ(HIGH_RESOLUTION, tilings->tiling_at(1)->resolution());
5089 EXPECT_EQ(LOW_RESOLUTION, tilings->tiling_at(2)->resolution());
5091 // The old low res tile was destroyed and replaced.
5092 EXPECT_EQ(old_low_res_tiling, tilings->tiling_at(1));
5093 EXPECT_NE(old_low_res_tile, tilings->tiling_at(1)->TileAt(0, 0));
5094 EXPECT_TRUE(tilings->tiling_at(1)->TileAt(0, 0));
5096 // New high res tiling.
5097 EXPECT_FALSE(tilings->tiling_at(0)->may_contain_low_resolution_tiles());
5098 // New low res tiling.
5099 EXPECT_TRUE(tilings->tiling_at(2)->may_contain_low_resolution_tiles());
5101 // This tiling will be high res now, it won't contain low res content since it
5102 // was all destroyed.
5103 EXPECT_FALSE(tilings->tiling_at(1)->may_contain_low_resolution_tiles());
5106 } // namespace
5107 } // namespace cc