Use multiline attribute to check for IA2_STATE_MULTILINE.
[chromium-blink-merge.git] / cc / layers / picture_layer_impl_unittest.cc
blobc56d0aecee55e902821427bfc307d227450f2964
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 "cc/base/math_util.h"
13 #include "cc/layers/append_quads_data.h"
14 #include "cc/layers/picture_layer.h"
15 #include "cc/quads/draw_quad.h"
16 #include "cc/quads/tile_draw_quad.h"
17 #include "cc/resources/tiling_set_raster_queue_all.h"
18 #include "cc/resources/tiling_set_raster_queue_required.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/impl_side_painting_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/trees/layer_tree_impl.h"
33 #include "testing/gtest/include/gtest/gtest.h"
34 #include "ui/gfx/geometry/rect_conversions.h"
35 #include "ui/gfx/geometry/size_conversions.h"
37 namespace cc {
38 namespace {
40 #define EXPECT_BOTH_EQ(expression, x) \
41 do { \
42 EXPECT_EQ(x, pending_layer_->expression); \
43 EXPECT_EQ(x, active_layer_->expression); \
44 } while (false)
46 #define EXPECT_BOTH_NE(expression, x) \
47 do { \
48 EXPECT_NE(x, pending_layer_->expression); \
49 EXPECT_NE(x, active_layer_->expression); \
50 } while (false)
52 class MockCanvas : public SkCanvas {
53 public:
54 explicit MockCanvas(int w, int h) : SkCanvas(w, h) {}
56 void onDrawRect(const SkRect& rect, const SkPaint& paint) override {
57 // Capture calls before SkCanvas quickReject() kicks in.
58 rects_.push_back(rect);
61 std::vector<SkRect> rects_;
64 class NoLowResTilingsSettings : public ImplSidePaintingSettings {};
66 class LowResTilingsSettings : public ImplSidePaintingSettings {
67 public:
68 LowResTilingsSettings() { create_low_res_tiling = true; }
71 class PictureLayerImplTest : public testing::Test {
72 public:
73 PictureLayerImplTest()
74 : proxy_(base::MessageLoopProxy::current()),
75 host_impl_(LowResTilingsSettings(),
76 &proxy_,
77 &shared_bitmap_manager_,
78 &task_graph_runner_),
79 root_id_(6),
80 id_(7),
81 pending_layer_(nullptr),
82 old_pending_layer_(nullptr),
83 active_layer_(nullptr) {
84 host_impl_.SetViewportSize(gfx::Size(10000, 10000));
87 explicit PictureLayerImplTest(const LayerTreeSettings& settings)
88 : proxy_(base::MessageLoopProxy::current()),
89 host_impl_(settings,
90 &proxy_,
91 &shared_bitmap_manager_,
92 &task_graph_runner_),
93 root_id_(6),
94 id_(7) {
95 host_impl_.SetViewportSize(gfx::Size(10000, 10000));
98 ~PictureLayerImplTest() override {}
100 void SetUp() override { InitializeRenderer(); }
102 virtual void InitializeRenderer() {
103 host_impl_.InitializeRenderer(FakeOutputSurface::Create3d());
106 void SetupDefaultTrees(const gfx::Size& layer_bounds) {
107 gfx::Size tile_size(100, 100);
109 scoped_refptr<FakePicturePileImpl> pending_pile =
110 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
111 scoped_refptr<FakePicturePileImpl> active_pile =
112 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
114 SetupTrees(pending_pile, active_pile);
117 void SetupDefaultTreesWithInvalidation(const gfx::Size& layer_bounds,
118 const Region& invalidation) {
119 gfx::Size tile_size(100, 100);
121 scoped_refptr<FakePicturePileImpl> pending_pile =
122 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
123 scoped_refptr<FakePicturePileImpl> active_pile =
124 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
126 SetupTreesWithInvalidation(pending_pile, active_pile, invalidation);
129 void ActivateTree() {
130 host_impl_.ActivateSyncTree();
131 CHECK(!host_impl_.pending_tree());
132 CHECK(host_impl_.recycle_tree());
133 old_pending_layer_ = pending_layer_;
134 pending_layer_ = nullptr;
135 active_layer_ = static_cast<FakePictureLayerImpl*>(
136 host_impl_.active_tree()->LayerById(id_));
138 bool update_lcd_text = false;
139 host_impl_.active_tree()->UpdateDrawProperties(update_lcd_text);
142 void SetupDefaultTreesWithFixedTileSize(const gfx::Size& layer_bounds,
143 const gfx::Size& tile_size,
144 const Region& invalidation) {
145 gfx::Size pile_tile_size(100, 100);
147 scoped_refptr<FakePicturePileImpl> pending_pile =
148 FakePicturePileImpl::CreateFilledPile(pile_tile_size, layer_bounds);
149 scoped_refptr<FakePicturePileImpl> active_pile =
150 FakePicturePileImpl::CreateFilledPile(pile_tile_size, layer_bounds);
152 SetupTreesWithFixedTileSize(pending_pile, active_pile, tile_size,
153 invalidation);
156 void SetupTrees(
157 scoped_refptr<PicturePileImpl> pending_pile,
158 scoped_refptr<PicturePileImpl> active_pile) {
159 SetupPendingTree(active_pile);
160 ActivateTree();
161 SetupPendingTreeInternal(pending_pile, gfx::Size(), Region());
164 void SetupTreesWithInvalidation(scoped_refptr<PicturePileImpl> pending_pile,
165 scoped_refptr<PicturePileImpl> active_pile,
166 const Region& pending_invalidation) {
167 SetupPendingTreeInternal(active_pile, gfx::Size(), Region());
168 ActivateTree();
169 SetupPendingTreeInternal(pending_pile, gfx::Size(), pending_invalidation);
172 void SetupTreesWithFixedTileSize(scoped_refptr<PicturePileImpl> pending_pile,
173 scoped_refptr<PicturePileImpl> active_pile,
174 const gfx::Size& tile_size,
175 const Region& pending_invalidation) {
176 SetupPendingTreeInternal(active_pile, tile_size, Region());
177 ActivateTree();
178 SetupPendingTreeInternal(pending_pile, tile_size, pending_invalidation);
181 void SetupPendingTree(scoped_refptr<RasterSource> raster_source) {
182 SetupPendingTreeInternal(raster_source, gfx::Size(), Region());
185 void SetupPendingTreeWithInvalidation(
186 scoped_refptr<RasterSource> raster_source,
187 const Region& invalidation) {
188 SetupPendingTreeInternal(raster_source, gfx::Size(), invalidation);
191 void SetupPendingTreeWithFixedTileSize(
192 scoped_refptr<RasterSource> raster_source,
193 const gfx::Size& tile_size,
194 const Region& invalidation) {
195 SetupPendingTreeInternal(raster_source, tile_size, invalidation);
198 void SetupPendingTreeInternal(scoped_refptr<RasterSource> raster_source,
199 const gfx::Size& tile_size,
200 const Region& invalidation) {
201 host_impl_.CreatePendingTree();
202 host_impl_.pending_tree()->PushPageScaleFromMainThread(1.f, 0.25f, 100.f);
203 LayerTreeImpl* pending_tree = host_impl_.pending_tree();
205 // Steal from the recycled tree if possible.
206 scoped_ptr<LayerImpl> pending_root = pending_tree->DetachLayerTree();
207 scoped_ptr<FakePictureLayerImpl> pending_layer;
208 DCHECK_IMPLIES(pending_root, pending_root->id() == root_id_);
209 if (!pending_root) {
210 pending_root = LayerImpl::Create(pending_tree, root_id_);
211 pending_layer = FakePictureLayerImpl::Create(pending_tree, id_);
212 if (!tile_size.IsEmpty())
213 pending_layer->set_fixed_tile_size(tile_size);
214 pending_layer->SetDrawsContent(true);
215 } else {
216 pending_layer.reset(static_cast<FakePictureLayerImpl*>(
217 pending_root->RemoveChild(pending_root->children()[0]).release()));
218 if (!tile_size.IsEmpty())
219 pending_layer->set_fixed_tile_size(tile_size);
221 pending_root->SetHasRenderSurface(true);
222 // The bounds() just mirror the pile size.
223 pending_layer->SetBounds(raster_source->GetSize());
224 pending_layer->SetContentBounds(raster_source->GetSize());
225 pending_layer->SetRasterSourceOnPending(raster_source, invalidation);
227 pending_root->AddChild(pending_layer.Pass());
228 pending_tree->SetRootLayer(pending_root.Pass());
230 pending_layer_ = static_cast<FakePictureLayerImpl*>(
231 host_impl_.pending_tree()->LayerById(id_));
233 // Add tilings/tiles for the layer.
234 bool update_lcd_text = false;
235 host_impl_.pending_tree()->UpdateDrawProperties(update_lcd_text);
238 void SetupDrawPropertiesAndUpdateTiles(FakePictureLayerImpl* layer,
239 float ideal_contents_scale,
240 float device_scale_factor,
241 float page_scale_factor,
242 float maximum_animation_contents_scale,
243 bool animating_transform_to_screen) {
244 layer->draw_properties().ideal_contents_scale = ideal_contents_scale;
245 layer->draw_properties().device_scale_factor = device_scale_factor;
246 layer->draw_properties().page_scale_factor = page_scale_factor;
247 layer->draw_properties().maximum_animation_contents_scale =
248 maximum_animation_contents_scale;
249 layer->draw_properties().screen_space_transform_is_animating =
250 animating_transform_to_screen;
251 bool resourceless_software_draw = false;
252 layer->UpdateTiles(resourceless_software_draw);
254 static void VerifyAllTilesExistAndHavePile(
255 const PictureLayerTiling* tiling,
256 PicturePileImpl* pile) {
257 for (PictureLayerTiling::CoverageIterator iter(
258 tiling,
259 tiling->contents_scale(),
260 gfx::Rect(tiling->tiling_size()));
261 iter;
262 ++iter) {
263 EXPECT_TRUE(*iter);
264 EXPECT_EQ(pile, iter->raster_source());
268 void SetContentsScaleOnBothLayers(float contents_scale,
269 float device_scale_factor,
270 float page_scale_factor,
271 float maximum_animation_contents_scale,
272 bool animating_transform) {
273 SetupDrawPropertiesAndUpdateTiles(pending_layer_,
274 contents_scale,
275 device_scale_factor,
276 page_scale_factor,
277 maximum_animation_contents_scale,
278 animating_transform);
280 SetupDrawPropertiesAndUpdateTiles(active_layer_,
281 contents_scale,
282 device_scale_factor,
283 page_scale_factor,
284 maximum_animation_contents_scale,
285 animating_transform);
288 void ResetTilingsAndRasterScales() {
289 pending_layer_->ReleaseResources();
290 EXPECT_FALSE(pending_layer_->tilings());
291 pending_layer_->RecreateResources();
292 EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings());
294 active_layer_->ReleaseResources();
295 EXPECT_FALSE(active_layer_->tilings());
296 active_layer_->RecreateResources();
297 EXPECT_EQ(0u, active_layer_->tilings()->num_tilings());
300 void AssertAllTilesRequired(PictureLayerTiling* tiling) {
301 std::vector<Tile*> tiles = tiling->AllTilesForTesting();
302 for (size_t i = 0; i < tiles.size(); ++i)
303 EXPECT_TRUE(tiles[i]->required_for_activation()) << "i: " << i;
304 EXPECT_GT(tiles.size(), 0u);
307 void AssertNoTilesRequired(PictureLayerTiling* tiling) {
308 std::vector<Tile*> tiles = tiling->AllTilesForTesting();
309 for (size_t i = 0; i < tiles.size(); ++i)
310 EXPECT_FALSE(tiles[i]->required_for_activation()) << "i: " << i;
311 EXPECT_GT(tiles.size(), 0u);
314 protected:
315 void TestQuadsForSolidColor(bool test_for_solid);
317 FakeImplProxy proxy_;
318 TestSharedBitmapManager shared_bitmap_manager_;
319 TestTaskGraphRunner task_graph_runner_;
320 FakeLayerTreeHostImpl host_impl_;
321 int root_id_;
322 int id_;
323 FakePictureLayerImpl* pending_layer_;
324 FakePictureLayerImpl* old_pending_layer_;
325 FakePictureLayerImpl* active_layer_;
327 private:
328 DISALLOW_COPY_AND_ASSIGN(PictureLayerImplTest);
331 class NoLowResPictureLayerImplTest : public PictureLayerImplTest {
332 public:
333 NoLowResPictureLayerImplTest()
334 : PictureLayerImplTest(NoLowResTilingsSettings()) {}
337 TEST_F(PictureLayerImplTest, TileGridAlignment) {
338 // Layer to span 4 raster tiles in x and in y
339 ImplSidePaintingSettings settings;
340 gfx::Size layer_size(settings.default_tile_size.width() * 7 / 2,
341 settings.default_tile_size.height() * 7 / 2);
343 scoped_refptr<FakePicturePileImpl> pending_pile =
344 FakePicturePileImpl::CreateFilledPile(layer_size, layer_size);
346 scoped_ptr<FakePicturePile> active_recording =
347 FakePicturePile::CreateFilledPile(layer_size, layer_size);
348 scoped_refptr<FakePicturePileImpl> active_pile =
349 FakePicturePileImpl::CreateFromPile(active_recording.get(), nullptr);
351 SetupTrees(pending_pile, active_pile);
353 // Add 1x1 rects at the centers of each tile, then re-record pile contents
354 active_layer_->tilings()->tiling_at(0)->CreateAllTilesForTesting();
355 std::vector<Tile*> tiles =
356 active_layer_->tilings()->tiling_at(0)->AllTilesForTesting();
357 EXPECT_EQ(16u, tiles.size());
358 std::vector<SkRect> rects;
359 std::vector<Tile*>::const_iterator tile_iter;
360 for (tile_iter = tiles.begin(); tile_iter < tiles.end(); tile_iter++) {
361 gfx::Point tile_center = (*tile_iter)->content_rect().CenterPoint();
362 gfx::Rect rect(tile_center.x(), tile_center.y(), 1, 1);
363 active_recording->add_draw_rect(rect);
364 rects.push_back(SkRect::MakeXYWH(rect.x(), rect.y(), 1, 1));
367 // Force re-raster with newly injected content
368 active_recording->RemoveRecordingAt(0, 0);
369 active_recording->AddRecordingAt(0, 0);
371 scoped_refptr<FakePicturePileImpl> updated_active_pile =
372 FakePicturePileImpl::CreateFromPile(active_recording.get(), nullptr);
374 std::vector<SkRect>::const_iterator rect_iter = rects.begin();
375 for (tile_iter = tiles.begin(); tile_iter < tiles.end(); tile_iter++) {
376 MockCanvas mock_canvas(1000, 1000);
377 updated_active_pile->PlaybackToSharedCanvas(
378 &mock_canvas, (*tile_iter)->content_rect(), 1.0f);
380 // This test verifies that when drawing the contents of a specific tile
381 // at content scale 1.0, the playback canvas never receives content from
382 // neighboring tiles which indicates that the tile grid embedded in
383 // SkPicture is perfectly aligned with the compositor's tiles.
384 EXPECT_EQ(1u, mock_canvas.rects_.size());
385 EXPECT_EQ(*rect_iter, mock_canvas.rects_[0]);
386 rect_iter++;
390 TEST_F(PictureLayerImplTest, CloneNoInvalidation) {
391 gfx::Size tile_size(100, 100);
392 gfx::Size layer_bounds(400, 400);
394 scoped_refptr<FakePicturePileImpl> pending_pile =
395 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
396 scoped_refptr<FakePicturePileImpl> active_pile =
397 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
399 SetupTreesWithInvalidation(pending_pile, active_pile, Region());
401 EXPECT_EQ(pending_layer_->tilings()->num_tilings(),
402 active_layer_->tilings()->num_tilings());
404 const PictureLayerTilingSet* tilings = pending_layer_->tilings();
405 EXPECT_GT(tilings->num_tilings(), 0u);
406 for (size_t i = 0; i < tilings->num_tilings(); ++i)
407 VerifyAllTilesExistAndHavePile(tilings->tiling_at(i), pending_pile.get());
410 TEST_F(PictureLayerImplTest, ExternalViewportRectForPrioritizingTiles) {
411 base::TimeTicks time_ticks;
412 time_ticks += base::TimeDelta::FromMilliseconds(1);
413 host_impl_.SetCurrentBeginFrameArgs(
414 CreateBeginFrameArgsForTesting(BEGINFRAME_FROM_HERE, time_ticks));
415 gfx::Size tile_size(100, 100);
416 gfx::Size layer_bounds(400, 400);
418 scoped_refptr<FakePicturePileImpl> pending_pile =
419 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
420 scoped_refptr<FakePicturePileImpl> active_pile =
421 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
423 SetupTreesWithInvalidation(pending_pile, active_pile, Region());
425 SetupDrawPropertiesAndUpdateTiles(active_layer_, 1.f, 1.f, 1.f, 1.f, false);
427 time_ticks += base::TimeDelta::FromMilliseconds(200);
428 host_impl_.SetCurrentBeginFrameArgs(
429 CreateBeginFrameArgsForTesting(BEGINFRAME_FROM_HERE, time_ticks));
431 // Update tiles with viewport for tile priority as (0, 0, 100, 100) and the
432 // identify transform for tile priority.
433 bool resourceless_software_draw = false;
434 gfx::Rect viewport = gfx::Rect(layer_bounds),
435 viewport_rect_for_tile_priority = gfx::Rect(0, 0, 100, 100);
436 gfx::Transform transform, transform_for_tile_priority;
438 host_impl_.SetExternalDrawConstraints(transform,
439 viewport,
440 viewport,
441 viewport_rect_for_tile_priority,
442 transform_for_tile_priority,
443 resourceless_software_draw);
444 bool update_lcd_text = false;
445 host_impl_.active_tree()->UpdateDrawProperties(update_lcd_text);
447 gfx::Rect viewport_rect_for_tile_priority_in_view_space =
448 viewport_rect_for_tile_priority;
450 // Verify the viewport rect for tile priority is used in picture layer tiling.
451 EXPECT_EQ(viewport_rect_for_tile_priority_in_view_space,
452 active_layer_->viewport_rect_for_tile_priority_in_content_space());
453 PictureLayerTilingSet* tilings = active_layer_->tilings();
454 for (size_t i = 0; i < tilings->num_tilings(); i++) {
455 PictureLayerTiling* tiling = tilings->tiling_at(i);
456 EXPECT_EQ(
457 tiling->GetCurrentVisibleRectForTesting(),
458 gfx::ScaleToEnclosingRect(viewport_rect_for_tile_priority_in_view_space,
459 tiling->contents_scale()));
462 // Update tiles with viewport for tile priority as (200, 200, 100, 100) in
463 // screen space and the transform for tile priority is translated and
464 // rotated. The actual viewport for tile priority used by PictureLayerImpl
465 // should be (200, 200, 100, 100) applied with the said transform.
466 time_ticks += base::TimeDelta::FromMilliseconds(200);
467 host_impl_.SetCurrentBeginFrameArgs(
468 CreateBeginFrameArgsForTesting(BEGINFRAME_FROM_HERE, time_ticks));
470 viewport_rect_for_tile_priority = gfx::Rect(200, 200, 100, 100);
471 transform_for_tile_priority.Translate(100, 100);
472 transform_for_tile_priority.Rotate(45);
473 host_impl_.SetExternalDrawConstraints(transform,
474 viewport,
475 viewport,
476 viewport_rect_for_tile_priority,
477 transform_for_tile_priority,
478 resourceless_software_draw);
479 host_impl_.active_tree()->UpdateDrawProperties(update_lcd_text);
481 gfx::Transform screen_to_view(gfx::Transform::kSkipInitialization);
482 bool success = transform_for_tile_priority.GetInverse(&screen_to_view);
483 EXPECT_TRUE(success);
485 // Note that we don't clip this to the layer bounds, since it is expected that
486 // the rect will sometimes be outside of the layer bounds. If we clip to
487 // bounds, then tile priorities will end up being incorrect in cases of fully
488 // offscreen layer.
489 viewport_rect_for_tile_priority_in_view_space =
490 gfx::ToEnclosingRect(MathUtil::ProjectClippedRect(
491 screen_to_view, viewport_rect_for_tile_priority));
493 EXPECT_EQ(viewport_rect_for_tile_priority_in_view_space,
494 active_layer_->viewport_rect_for_tile_priority_in_content_space());
495 tilings = active_layer_->tilings();
496 for (size_t i = 0; i < tilings->num_tilings(); i++) {
497 PictureLayerTiling* tiling = tilings->tiling_at(i);
498 EXPECT_EQ(
499 tiling->GetCurrentVisibleRectForTesting(),
500 gfx::ScaleToEnclosingRect(viewport_rect_for_tile_priority_in_view_space,
501 tiling->contents_scale()));
505 TEST_F(PictureLayerImplTest, InvalidViewportForPrioritizingTiles) {
506 base::TimeTicks time_ticks;
507 time_ticks += base::TimeDelta::FromMilliseconds(1);
508 host_impl_.SetCurrentBeginFrameArgs(
509 CreateBeginFrameArgsForTesting(BEGINFRAME_FROM_HERE, time_ticks));
511 gfx::Size tile_size(100, 100);
512 gfx::Size layer_bounds(400, 400);
514 scoped_refptr<FakePicturePileImpl> pending_pile =
515 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
516 scoped_refptr<FakePicturePileImpl> active_pile =
517 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
519 SetupTreesWithInvalidation(pending_pile, active_pile, Region());
521 SetupDrawPropertiesAndUpdateTiles(active_layer_, 1.f, 1.f, 1.f, 1.f, false);
523 // UpdateTiles with valid viewport. Should update tile viewport.
524 // Note viewport is considered invalid if and only if in resourceless
525 // software draw.
526 bool resourceless_software_draw = false;
527 gfx::Rect viewport = gfx::Rect(layer_bounds);
528 gfx::Transform transform;
529 host_impl_.SetExternalDrawConstraints(transform,
530 viewport,
531 viewport,
532 viewport,
533 transform,
534 resourceless_software_draw);
535 active_layer_->draw_properties().visible_content_rect = viewport;
536 active_layer_->draw_properties().screen_space_transform = transform;
537 active_layer_->UpdateTiles(resourceless_software_draw);
539 gfx::Rect visible_rect_for_tile_priority =
540 active_layer_->visible_rect_for_tile_priority();
541 EXPECT_FALSE(visible_rect_for_tile_priority.IsEmpty());
542 gfx::Transform screen_space_transform_for_tile_priority =
543 active_layer_->screen_space_transform();
545 // Expand viewport and set it as invalid for prioritizing tiles.
546 // Should update viewport and transform, but not update visible rect.
547 time_ticks += base::TimeDelta::FromMilliseconds(200);
548 host_impl_.SetCurrentBeginFrameArgs(
549 CreateBeginFrameArgsForTesting(BEGINFRAME_FROM_HERE, time_ticks));
550 resourceless_software_draw = true;
551 viewport = gfx::ScaleToEnclosingRect(viewport, 2);
552 transform.Translate(1.f, 1.f);
553 active_layer_->draw_properties().visible_content_rect = viewport;
554 active_layer_->draw_properties().screen_space_transform = transform;
555 host_impl_.SetExternalDrawConstraints(transform,
556 viewport,
557 viewport,
558 viewport,
559 transform,
560 resourceless_software_draw);
561 active_layer_->UpdateTiles(resourceless_software_draw);
563 // Transform for tile priority is updated.
564 EXPECT_TRANSFORMATION_MATRIX_EQ(transform,
565 active_layer_->screen_space_transform());
566 // Visible rect for tile priority retains old value.
567 EXPECT_EQ(visible_rect_for_tile_priority,
568 active_layer_->visible_rect_for_tile_priority());
570 // Keep expanded viewport but mark it valid. Should update tile viewport.
571 time_ticks += base::TimeDelta::FromMilliseconds(200);
572 host_impl_.SetCurrentBeginFrameArgs(
573 CreateBeginFrameArgsForTesting(BEGINFRAME_FROM_HERE, time_ticks));
574 resourceless_software_draw = false;
575 host_impl_.SetExternalDrawConstraints(transform,
576 viewport,
577 viewport,
578 viewport,
579 transform,
580 resourceless_software_draw);
581 active_layer_->UpdateTiles(resourceless_software_draw);
583 EXPECT_TRANSFORMATION_MATRIX_EQ(transform,
584 active_layer_->screen_space_transform());
585 EXPECT_EQ(viewport, active_layer_->visible_rect_for_tile_priority());
588 TEST_F(PictureLayerImplTest, ViewportRectForTilePriorityIsCached) {
589 base::TimeTicks time_ticks;
590 time_ticks += base::TimeDelta::FromMilliseconds(1);
591 host_impl_.SetCurrentBeginFrameArgs(
592 CreateBeginFrameArgsForTesting(BEGINFRAME_FROM_HERE, time_ticks));
593 gfx::Size tile_size(100, 100);
594 gfx::Size layer_bounds(400, 400);
596 scoped_refptr<FakePicturePileImpl> pending_pile =
597 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
598 scoped_refptr<FakePicturePileImpl> active_pile =
599 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
601 SetupTreesWithInvalidation(pending_pile, active_pile, Region());
603 SetupDrawPropertiesAndUpdateTiles(active_layer_, 1.f, 1.f, 1.f, 1.f, false);
605 time_ticks += base::TimeDelta::FromMilliseconds(200);
606 host_impl_.SetCurrentBeginFrameArgs(
607 CreateBeginFrameArgsForTesting(BEGINFRAME_FROM_HERE, time_ticks));
609 bool resourceless_software_draw = false;
610 gfx::Rect viewport = gfx::Rect(layer_bounds);
611 gfx::Rect viewport_rect_for_tile_priority(0, 0, 100, 100);
612 gfx::Transform transform, transform_for_tile_priority;
614 host_impl_.SetExternalDrawConstraints(
615 transform, viewport, viewport, viewport_rect_for_tile_priority,
616 transform_for_tile_priority, resourceless_software_draw);
617 bool update_lcd_text = false;
618 host_impl_.active_tree()->UpdateDrawProperties(update_lcd_text);
620 EXPECT_EQ(viewport_rect_for_tile_priority,
621 active_layer_->viewport_rect_for_tile_priority_in_content_space());
623 time_ticks += base::TimeDelta::FromMilliseconds(200);
624 host_impl_.SetCurrentBeginFrameArgs(
625 CreateBeginFrameArgsForTesting(BEGINFRAME_FROM_HERE, time_ticks));
627 gfx::Rect another_viewport_rect_for_tile_priority(11, 11, 50, 50);
628 host_impl_.SetExternalDrawConstraints(
629 transform, viewport, viewport, another_viewport_rect_for_tile_priority,
630 transform_for_tile_priority, resourceless_software_draw);
632 // Didn't call UpdateDrawProperties yet. The viewport rect for tile priority
633 // should remain to be the previously cached value.
634 EXPECT_EQ(viewport_rect_for_tile_priority,
635 active_layer_->viewport_rect_for_tile_priority_in_content_space());
636 host_impl_.active_tree()->UpdateDrawProperties(update_lcd_text);
638 // Now the UpdateDrawProperties is called. The viewport rect for tile
639 // priority should be the latest value.
640 EXPECT_EQ(another_viewport_rect_for_tile_priority,
641 active_layer_->viewport_rect_for_tile_priority_in_content_space());
644 TEST_F(PictureLayerImplTest, ClonePartialInvalidation) {
645 gfx::Size tile_size(100, 100);
646 gfx::Size layer_bounds(400, 400);
647 gfx::Rect layer_invalidation(150, 200, 30, 180);
649 scoped_refptr<FakePicturePileImpl> pending_pile =
650 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
651 scoped_refptr<FakePicturePileImpl> active_pile =
652 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
653 scoped_refptr<FakePicturePileImpl> lost_pile =
654 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
656 SetupPendingTreeWithFixedTileSize(lost_pile, gfx::Size(50, 50), Region());
657 ActivateTree();
658 // Add a non-shared tiling on the active tree.
659 PictureLayerTiling* tiling = active_layer_->AddTiling(3.f);
660 tiling->CreateAllTilesForTesting();
662 // Ensure UpdateTiles won't remove any tilings.
663 active_layer_->MarkAllTilingsUsed();
665 // Then setup a new pending tree and activate it.
666 SetupTreesWithFixedTileSize(pending_pile, active_pile, gfx::Size(50, 50),
667 layer_invalidation);
669 EXPECT_EQ(2u, pending_layer_->num_tilings());
670 EXPECT_EQ(3u, active_layer_->num_tilings());
672 const PictureLayerTilingSet* tilings = pending_layer_->tilings();
673 EXPECT_GT(tilings->num_tilings(), 0u);
674 for (size_t i = 0; i < tilings->num_tilings(); ++i) {
675 const PictureLayerTiling* tiling = tilings->tiling_at(i);
676 gfx::Rect content_invalidation = gfx::ScaleToEnclosingRect(
677 layer_invalidation,
678 tiling->contents_scale());
679 for (PictureLayerTiling::CoverageIterator iter(
680 tiling,
681 tiling->contents_scale(),
682 gfx::Rect(tiling->tiling_size()));
683 iter;
684 ++iter) {
685 EXPECT_TRUE(*iter);
686 EXPECT_FALSE(iter.geometry_rect().IsEmpty());
687 EXPECT_EQ(pending_pile.get(), iter->raster_source());
691 tilings = active_layer_->tilings();
692 EXPECT_GT(tilings->num_tilings(), 0u);
693 for (size_t i = 0; i < tilings->num_tilings(); ++i) {
694 const PictureLayerTiling* tiling = tilings->tiling_at(i);
695 gfx::Rect content_invalidation =
696 gfx::ScaleToEnclosingRect(layer_invalidation, tiling->contents_scale());
697 for (PictureLayerTiling::CoverageIterator iter(
698 tiling,
699 tiling->contents_scale(),
700 gfx::Rect(tiling->tiling_size()));
701 iter;
702 ++iter) {
703 EXPECT_TRUE(*iter);
704 EXPECT_FALSE(iter.geometry_rect().IsEmpty());
705 if (iter.geometry_rect().Intersects(content_invalidation))
706 EXPECT_EQ(active_pile.get(), iter->raster_source());
707 else if (!active_layer_->GetPendingOrActiveTwinTiling(tiling))
708 EXPECT_EQ(active_pile.get(), iter->raster_source());
709 else
710 EXPECT_EQ(pending_pile.get(), iter->raster_source());
715 TEST_F(PictureLayerImplTest, CloneFullInvalidation) {
716 gfx::Size tile_size(90, 80);
717 gfx::Size layer_bounds(300, 500);
719 scoped_refptr<FakePicturePileImpl> pending_pile =
720 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
721 scoped_refptr<FakePicturePileImpl> active_pile =
722 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
724 SetupTreesWithInvalidation(pending_pile, active_pile,
725 gfx::Rect(layer_bounds));
727 EXPECT_EQ(pending_layer_->tilings()->num_tilings(),
728 active_layer_->tilings()->num_tilings());
730 const PictureLayerTilingSet* tilings = pending_layer_->tilings();
731 EXPECT_GT(tilings->num_tilings(), 0u);
732 for (size_t i = 0; i < tilings->num_tilings(); ++i)
733 VerifyAllTilesExistAndHavePile(tilings->tiling_at(i), pending_pile.get());
736 TEST_F(PictureLayerImplTest, UpdateTilesCreatesTilings) {
737 gfx::Size tile_size(400, 400);
738 gfx::Size layer_bounds(1300, 1900);
740 scoped_refptr<FakePicturePileImpl> pending_pile =
741 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
742 scoped_refptr<FakePicturePileImpl> active_pile =
743 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
745 SetupTrees(pending_pile, active_pile);
747 float low_res_factor = host_impl_.settings().low_res_contents_scale_factor;
748 EXPECT_LT(low_res_factor, 1.f);
750 active_layer_->ReleaseResources();
751 EXPECT_FALSE(active_layer_->tilings());
752 active_layer_->RecreateResources();
753 EXPECT_EQ(0u, active_layer_->tilings()->num_tilings());
755 SetupDrawPropertiesAndUpdateTiles(active_layer_,
756 6.f, // ideal contents scale
757 3.f, // device scale
758 2.f, // page scale
759 1.f, // maximum animation scale
760 false);
761 ASSERT_EQ(2u, active_layer_->tilings()->num_tilings());
762 EXPECT_FLOAT_EQ(6.f,
763 active_layer_->tilings()->tiling_at(0)->contents_scale());
764 EXPECT_FLOAT_EQ(6.f * low_res_factor,
765 active_layer_->tilings()->tiling_at(1)->contents_scale());
767 // If we change the page scale factor, then we should get new tilings.
768 SetupDrawPropertiesAndUpdateTiles(active_layer_,
769 6.6f, // ideal contents scale
770 3.f, // device scale
771 2.2f, // page scale
772 1.f, // maximum animation scale
773 false);
774 ASSERT_EQ(4u, active_layer_->tilings()->num_tilings());
775 EXPECT_FLOAT_EQ(6.6f,
776 active_layer_->tilings()->tiling_at(0)->contents_scale());
777 EXPECT_FLOAT_EQ(6.6f * low_res_factor,
778 active_layer_->tilings()->tiling_at(2)->contents_scale());
780 // If we change the device scale factor, then we should get new tilings.
781 SetupDrawPropertiesAndUpdateTiles(active_layer_,
782 7.26f, // ideal contents scale
783 3.3f, // device scale
784 2.2f, // page scale
785 1.f, // maximum animation scale
786 false);
787 ASSERT_EQ(6u, active_layer_->tilings()->num_tilings());
788 EXPECT_FLOAT_EQ(7.26f,
789 active_layer_->tilings()->tiling_at(0)->contents_scale());
790 EXPECT_FLOAT_EQ(7.26f * low_res_factor,
791 active_layer_->tilings()->tiling_at(3)->contents_scale());
793 // If we change the device scale factor, but end up at the same total scale
794 // factor somehow, then we don't get new tilings.
795 SetupDrawPropertiesAndUpdateTiles(active_layer_,
796 7.26f, // ideal contents scale
797 2.2f, // device scale
798 3.3f, // page scale
799 1.f, // maximum animation scale
800 false);
801 ASSERT_EQ(6u, active_layer_->tilings()->num_tilings());
802 EXPECT_FLOAT_EQ(7.26f,
803 active_layer_->tilings()->tiling_at(0)->contents_scale());
804 EXPECT_FLOAT_EQ(7.26f * low_res_factor,
805 active_layer_->tilings()->tiling_at(3)->contents_scale());
808 TEST_F(PictureLayerImplTest, PendingLayerOnlyHasHighAndLowResTiling) {
809 gfx::Size tile_size(400, 400);
810 gfx::Size layer_bounds(1300, 1900);
812 scoped_refptr<FakePicturePileImpl> pending_pile =
813 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
814 scoped_refptr<FakePicturePileImpl> active_pile =
815 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
817 SetupTrees(pending_pile, active_pile);
819 float low_res_factor = host_impl_.settings().low_res_contents_scale_factor;
820 EXPECT_LT(low_res_factor, 1.f);
822 pending_layer_->ReleaseResources();
823 EXPECT_FALSE(pending_layer_->tilings());
824 pending_layer_->RecreateResources();
825 EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings());
827 SetupDrawPropertiesAndUpdateTiles(pending_layer_,
828 6.f, // ideal contents scale
829 3.f, // device scale
830 2.f, // page scale
831 1.f, // maximum animation scale
832 false);
833 ASSERT_EQ(2u, pending_layer_->tilings()->num_tilings());
834 EXPECT_FLOAT_EQ(6.f,
835 pending_layer_->tilings()->tiling_at(0)->contents_scale());
836 EXPECT_FLOAT_EQ(6.f * low_res_factor,
837 pending_layer_->tilings()->tiling_at(1)->contents_scale());
839 // If we change the page scale factor, then we should get new tilings.
840 SetupDrawPropertiesAndUpdateTiles(pending_layer_,
841 6.6f, // ideal contents scale
842 3.f, // device scale
843 2.2f, // page scale
844 1.f, // maximum animation scale
845 false);
846 ASSERT_EQ(2u, pending_layer_->tilings()->num_tilings());
847 EXPECT_FLOAT_EQ(6.6f,
848 pending_layer_->tilings()->tiling_at(0)->contents_scale());
849 EXPECT_FLOAT_EQ(6.6f * low_res_factor,
850 pending_layer_->tilings()->tiling_at(1)->contents_scale());
852 // If we change the device scale factor, then we should get new tilings.
853 SetupDrawPropertiesAndUpdateTiles(pending_layer_,
854 7.26f, // ideal contents scale
855 3.3f, // device scale
856 2.2f, // page scale
857 1.f, // maximum animation scale
858 false);
859 ASSERT_EQ(2u, pending_layer_->tilings()->num_tilings());
860 EXPECT_FLOAT_EQ(7.26f,
861 pending_layer_->tilings()->tiling_at(0)->contents_scale());
862 EXPECT_FLOAT_EQ(7.26f * low_res_factor,
863 pending_layer_->tilings()->tiling_at(1)->contents_scale());
865 // If we change the device scale factor, but end up at the same total scale
866 // factor somehow, then we don't get new tilings.
867 SetupDrawPropertiesAndUpdateTiles(pending_layer_,
868 7.26f, // ideal contents scale
869 2.2f, // device scale
870 3.3f, // page scale
871 1.f, // maximum animation scale
872 false);
873 ASSERT_EQ(2u, pending_layer_->tilings()->num_tilings());
874 EXPECT_FLOAT_EQ(7.26f,
875 pending_layer_->tilings()->tiling_at(0)->contents_scale());
876 EXPECT_FLOAT_EQ(7.26f * low_res_factor,
877 pending_layer_->tilings()->tiling_at(1)->contents_scale());
880 TEST_F(PictureLayerImplTest, CreateTilingsEvenIfTwinHasNone) {
881 // This test makes sure that if a layer can have tilings, then a commit makes
882 // it not able to have tilings (empty size), and then a future commit that
883 // makes it valid again should be able to create tilings.
884 gfx::Size tile_size(400, 400);
885 gfx::Size layer_bounds(1300, 1900);
887 scoped_refptr<FakePicturePileImpl> empty_pile =
888 FakePicturePileImpl::CreateEmptyPile(tile_size, layer_bounds);
889 scoped_refptr<FakePicturePileImpl> valid_pile =
890 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
892 SetupPendingTree(valid_pile);
893 ASSERT_EQ(2u, pending_layer_->tilings()->num_tilings());
895 ActivateTree();
896 SetupPendingTree(empty_pile);
897 EXPECT_FALSE(pending_layer_->CanHaveTilings());
898 ASSERT_EQ(2u, active_layer_->tilings()->num_tilings());
899 ASSERT_EQ(0u, pending_layer_->tilings()->num_tilings());
901 ActivateTree();
902 EXPECT_FALSE(active_layer_->CanHaveTilings());
903 ASSERT_EQ(0u, active_layer_->tilings()->num_tilings());
905 SetupPendingTree(valid_pile);
906 ASSERT_EQ(2u, pending_layer_->tilings()->num_tilings());
907 ASSERT_EQ(0u, active_layer_->tilings()->num_tilings());
910 TEST_F(PictureLayerImplTest, ZoomOutCrash) {
911 gfx::Size tile_size(400, 400);
912 gfx::Size layer_bounds(1300, 1900);
914 // Set up the high and low res tilings before pinch zoom.
915 scoped_refptr<FakePicturePileImpl> pending_pile =
916 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
917 scoped_refptr<FakePicturePileImpl> active_pile =
918 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
920 SetupTrees(pending_pile, active_pile);
921 ResetTilingsAndRasterScales();
922 EXPECT_EQ(0u, active_layer_->tilings()->num_tilings());
923 SetContentsScaleOnBothLayers(32.0f, 1.0f, 32.0f, 1.0f, false);
924 EXPECT_EQ(32.f, active_layer_->HighResTiling()->contents_scale());
925 host_impl_.PinchGestureBegin();
926 SetContentsScaleOnBothLayers(1.0f, 1.0f, 1.0f, 1.0f, false);
927 SetContentsScaleOnBothLayers(1.0f, 1.0f, 1.0f, 1.0f, false);
928 EXPECT_EQ(active_layer_->tilings()->NumHighResTilings(), 1);
931 TEST_F(PictureLayerImplTest, PinchGestureTilings) {
932 gfx::Size tile_size(400, 400);
933 gfx::Size layer_bounds(1300, 1900);
935 float low_res_factor = host_impl_.settings().low_res_contents_scale_factor;
937 scoped_refptr<FakePicturePileImpl> pending_pile =
938 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
939 scoped_refptr<FakePicturePileImpl> active_pile =
940 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
942 // Set up the high and low res tilings before pinch zoom.
943 SetupTrees(pending_pile, active_pile);
944 ResetTilingsAndRasterScales();
946 SetContentsScaleOnBothLayers(2.f, 1.0f, 2.f, 1.0f, false);
947 EXPECT_BOTH_EQ(num_tilings(), 2u);
948 EXPECT_BOTH_EQ(tilings()->tiling_at(0)->contents_scale(), 2.f);
949 EXPECT_BOTH_EQ(tilings()->tiling_at(1)->contents_scale(),
950 2.f * low_res_factor);
952 // Ensure UpdateTiles won't remove any tilings.
953 active_layer_->MarkAllTilingsUsed();
955 // Start a pinch gesture.
956 host_impl_.PinchGestureBegin();
958 // Zoom out by a small amount. We should create a tiling at half
959 // the scale (2/kMaxScaleRatioDuringPinch).
960 SetContentsScaleOnBothLayers(1.8f, 1.0f, 1.8f, 1.0f, false);
961 EXPECT_EQ(3u, active_layer_->tilings()->num_tilings());
962 EXPECT_FLOAT_EQ(2.0f,
963 active_layer_->tilings()->tiling_at(0)->contents_scale());
964 EXPECT_FLOAT_EQ(1.0f,
965 active_layer_->tilings()->tiling_at(1)->contents_scale());
966 EXPECT_FLOAT_EQ(2.0f * low_res_factor,
967 active_layer_->tilings()->tiling_at(2)->contents_scale());
969 // Ensure UpdateTiles won't remove any tilings.
970 active_layer_->MarkAllTilingsUsed();
972 // Zoom out further, close to our low-res scale factor. We should
973 // use that tiling as high-res, and not create a new tiling.
974 SetContentsScaleOnBothLayers(low_res_factor * 2.1f, 1.0f,
975 low_res_factor * 2.1f, 1.0f, false);
976 EXPECT_EQ(3u, active_layer_->tilings()->num_tilings());
978 // Zoom in a lot now. Since we increase by increments of
979 // kMaxScaleRatioDuringPinch, this will create a new tiling at 4.0.
980 SetContentsScaleOnBothLayers(3.8f, 1.0f, 3.8f, 1.f, false);
981 EXPECT_EQ(4u, active_layer_->tilings()->num_tilings());
982 EXPECT_FLOAT_EQ(4.0f,
983 active_layer_->tilings()->tiling_at(0)->contents_scale());
986 TEST_F(PictureLayerImplTest, SnappedTilingDuringZoom) {
987 gfx::Size tile_size(300, 300);
988 gfx::Size layer_bounds(2600, 3800);
990 scoped_refptr<FakePicturePileImpl> pending_pile =
991 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
992 scoped_refptr<FakePicturePileImpl> active_pile =
993 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
995 SetupTrees(pending_pile, active_pile);
997 ResetTilingsAndRasterScales();
998 EXPECT_EQ(0u, active_layer_->tilings()->num_tilings());
1000 // Set up the high and low res tilings before pinch zoom.
1001 SetContentsScaleOnBothLayers(0.24f, 1.0f, 0.24f, 1.0f, false);
1002 EXPECT_EQ(2u, active_layer_->tilings()->num_tilings());
1003 EXPECT_FLOAT_EQ(0.24f,
1004 active_layer_->tilings()->tiling_at(0)->contents_scale());
1005 EXPECT_FLOAT_EQ(0.0625f,
1006 active_layer_->tilings()->tiling_at(1)->contents_scale());
1008 // Ensure UpdateTiles won't remove any tilings.
1009 active_layer_->MarkAllTilingsUsed();
1011 // Start a pinch gesture.
1012 host_impl_.PinchGestureBegin();
1014 // Zoom out by a small amount. We should create a tiling at half
1015 // the scale (1/kMaxScaleRatioDuringPinch).
1016 SetContentsScaleOnBothLayers(0.2f, 1.0f, 0.2f, 1.0f, false);
1017 EXPECT_EQ(3u, active_layer_->tilings()->num_tilings());
1018 EXPECT_FLOAT_EQ(0.24f,
1019 active_layer_->tilings()->tiling_at(0)->contents_scale());
1020 EXPECT_FLOAT_EQ(0.12f,
1021 active_layer_->tilings()->tiling_at(1)->contents_scale());
1022 EXPECT_FLOAT_EQ(0.0625,
1023 active_layer_->tilings()->tiling_at(2)->contents_scale());
1025 // Ensure UpdateTiles won't remove any tilings.
1026 active_layer_->MarkAllTilingsUsed();
1028 // Zoom out further, close to our low-res scale factor. We should
1029 // use that tiling as high-res, and not create a new tiling.
1030 SetContentsScaleOnBothLayers(0.1f, 1.0f, 0.1f, 1.0f, false);
1031 EXPECT_EQ(3u, active_layer_->tilings()->num_tilings());
1033 // Zoom in. 0.25(desired_scale) should be snapped to 0.24 during zoom-in
1034 // because 0.25(desired_scale) is within the ratio(1.2).
1035 SetContentsScaleOnBothLayers(0.25f, 1.0f, 0.25f, 1.0f, false);
1036 EXPECT_EQ(3u, active_layer_->tilings()->num_tilings());
1038 // Zoom in a lot. Since we move in factors of two, we should get a scale that
1039 // is a power of 2 times 0.24.
1040 SetContentsScaleOnBothLayers(1.f, 1.0f, 1.f, 1.0f, false);
1041 EXPECT_EQ(4u, active_layer_->tilings()->num_tilings());
1042 EXPECT_FLOAT_EQ(1.92f,
1043 active_layer_->tilings()->tiling_at(0)->contents_scale());
1046 TEST_F(PictureLayerImplTest, CleanUpTilings) {
1047 gfx::Size tile_size(400, 400);
1048 gfx::Size layer_bounds(1300, 1900);
1050 scoped_refptr<FakePicturePileImpl> pending_pile =
1051 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1052 scoped_refptr<FakePicturePileImpl> active_pile =
1053 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1055 std::vector<PictureLayerTiling*> used_tilings;
1057 float low_res_factor = host_impl_.settings().low_res_contents_scale_factor;
1058 EXPECT_LT(low_res_factor, 1.f);
1060 float scale = 1.f;
1061 float page_scale = 1.f;
1063 SetupTrees(pending_pile, active_pile);
1064 EXPECT_EQ(2u, active_layer_->tilings()->num_tilings());
1065 EXPECT_EQ(1.f, active_layer_->HighResTiling()->contents_scale());
1067 // Ensure UpdateTiles won't remove any tilings. Note this is unrelated to
1068 // |used_tilings| variable, and it's here only to ensure that active_layer_
1069 // won't remove tilings before the test has a chance to verify behavior.
1070 active_layer_->MarkAllTilingsUsed();
1072 // We only have ideal tilings, so they aren't removed.
1073 used_tilings.clear();
1074 active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
1075 EXPECT_EQ(2u, active_layer_->tilings()->num_tilings());
1077 host_impl_.PinchGestureBegin();
1079 // Changing the ideal but not creating new tilings.
1080 scale = 1.5f;
1081 page_scale = 1.5f;
1082 SetContentsScaleOnBothLayers(scale, 1.f, page_scale, 1.f, false);
1083 EXPECT_EQ(2u, active_layer_->tilings()->num_tilings());
1085 // The tilings are still our target scale, so they aren't removed.
1086 used_tilings.clear();
1087 active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
1088 ASSERT_EQ(2u, active_layer_->tilings()->num_tilings());
1090 host_impl_.PinchGestureEnd();
1092 // Create a 1.2 scale tiling. Now we have 1.0 and 1.2 tilings. Ideal = 1.2.
1093 scale = 1.2f;
1094 page_scale = 1.2f;
1095 SetContentsScaleOnBothLayers(1.2f, 1.f, page_scale, 1.f, false);
1096 ASSERT_EQ(4u, active_layer_->tilings()->num_tilings());
1097 EXPECT_FLOAT_EQ(
1098 1.f,
1099 active_layer_->tilings()->tiling_at(1)->contents_scale());
1100 EXPECT_FLOAT_EQ(
1101 1.f * low_res_factor,
1102 active_layer_->tilings()->tiling_at(3)->contents_scale());
1104 // Ensure UpdateTiles won't remove any tilings.
1105 active_layer_->MarkAllTilingsUsed();
1107 // Mark the non-ideal tilings as used. They won't be removed.
1108 used_tilings.clear();
1109 used_tilings.push_back(active_layer_->tilings()->tiling_at(1));
1110 used_tilings.push_back(active_layer_->tilings()->tiling_at(3));
1111 active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
1112 ASSERT_EQ(4u, active_layer_->tilings()->num_tilings());
1114 // Now move the ideal scale to 0.5. Our target stays 1.2.
1115 SetContentsScaleOnBothLayers(0.5f, 1.f, page_scale, 1.f, false);
1117 // The high resolution tiling is between target and ideal, so is not
1118 // removed. The low res tiling for the old ideal=1.0 scale is removed.
1119 used_tilings.clear();
1120 active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
1121 ASSERT_EQ(3u, active_layer_->tilings()->num_tilings());
1123 // Now move the ideal scale to 1.0. Our target stays 1.2.
1124 SetContentsScaleOnBothLayers(1.f, 1.f, page_scale, 1.f, false);
1126 // All the tilings are between are target and the ideal, so they are not
1127 // removed.
1128 used_tilings.clear();
1129 active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
1130 ASSERT_EQ(3u, active_layer_->tilings()->num_tilings());
1132 // Now move the ideal scale to 1.1 on the active layer. Our target stays 1.2.
1133 SetupDrawPropertiesAndUpdateTiles(active_layer_, 1.1f, 1.f, page_scale, 1.f,
1134 false);
1136 // Because the pending layer's ideal scale is still 1.0, our tilings fall
1137 // in the range [1.0,1.2] and are kept.
1138 used_tilings.clear();
1139 active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
1140 ASSERT_EQ(3u, active_layer_->tilings()->num_tilings());
1142 // Move the ideal scale on the pending layer to 1.1 as well. Our target stays
1143 // 1.2 still.
1144 SetupDrawPropertiesAndUpdateTiles(pending_layer_, 1.1f, 1.f, page_scale, 1.f,
1145 false);
1147 // Our 1.0 tiling now falls outside the range between our ideal scale and our
1148 // target raster scale. But it is in our used tilings set, so nothing is
1149 // deleted.
1150 used_tilings.clear();
1151 used_tilings.push_back(active_layer_->tilings()->tiling_at(1));
1152 active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
1153 ASSERT_EQ(3u, active_layer_->tilings()->num_tilings());
1155 // If we remove it from our used tilings set, it is outside the range to keep
1156 // so it is deleted.
1157 used_tilings.clear();
1158 active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
1159 ASSERT_EQ(2u, active_layer_->tilings()->num_tilings());
1162 TEST_F(PictureLayerImplTest, DontAddLowResDuringAnimation) {
1163 // Make sure this layer covers multiple tiles, since otherwise low
1164 // res won't get created because it is too small.
1165 gfx::Size tile_size(host_impl_.settings().default_tile_size);
1166 // Avoid max untiled layer size heuristics via fixed tile size.
1167 gfx::Size layer_bounds(tile_size.width() + 1, tile_size.height() + 1);
1168 SetupDefaultTreesWithFixedTileSize(layer_bounds, tile_size, Region());
1170 float low_res_factor = host_impl_.settings().low_res_contents_scale_factor;
1171 float contents_scale = 1.f;
1172 float device_scale = 1.f;
1173 float page_scale = 1.f;
1174 float maximum_animation_scale = 1.f;
1175 bool animating_transform = true;
1177 ResetTilingsAndRasterScales();
1179 // Animating, so don't create low res even if there isn't one already.
1180 SetContentsScaleOnBothLayers(contents_scale,
1181 device_scale,
1182 page_scale,
1183 maximum_animation_scale,
1184 animating_transform);
1185 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 1.f);
1186 EXPECT_BOTH_EQ(num_tilings(), 1u);
1188 // Stop animating, low res gets created.
1189 animating_transform = false;
1190 SetContentsScaleOnBothLayers(contents_scale,
1191 device_scale,
1192 page_scale,
1193 maximum_animation_scale,
1194 animating_transform);
1195 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 1.f);
1196 EXPECT_BOTH_EQ(LowResTiling()->contents_scale(), low_res_factor);
1197 EXPECT_BOTH_EQ(num_tilings(), 2u);
1199 // Ensure UpdateTiles won't remove any tilings.
1200 active_layer_->MarkAllTilingsUsed();
1202 // Page scale animation, new high res, but no low res. We still have
1203 // a tiling at the previous scale, it's just not marked as low res on the
1204 // active layer. The pending layer drops non-ideal tilings.
1205 contents_scale = 2.f;
1206 page_scale = 2.f;
1207 maximum_animation_scale = 2.f;
1208 animating_transform = true;
1209 SetContentsScaleOnBothLayers(contents_scale,
1210 device_scale,
1211 page_scale,
1212 maximum_animation_scale,
1213 animating_transform);
1214 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 2.f);
1215 EXPECT_FALSE(active_layer_->LowResTiling());
1216 EXPECT_FALSE(pending_layer_->LowResTiling());
1217 EXPECT_EQ(3u, active_layer_->num_tilings());
1218 EXPECT_EQ(1u, pending_layer_->num_tilings());
1220 // Stop animating, new low res gets created for final page scale.
1221 animating_transform = false;
1222 SetContentsScaleOnBothLayers(contents_scale,
1223 device_scale,
1224 page_scale,
1225 maximum_animation_scale,
1226 animating_transform);
1227 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 2.f);
1228 EXPECT_BOTH_EQ(LowResTiling()->contents_scale(), 2.f * low_res_factor);
1229 EXPECT_EQ(4u, active_layer_->num_tilings());
1230 EXPECT_EQ(2u, pending_layer_->num_tilings());
1233 TEST_F(PictureLayerImplTest, DontAddLowResForSmallLayers) {
1234 gfx::Size layer_bounds(host_impl_.settings().default_tile_size);
1235 gfx::Size tile_size(100, 100);
1237 scoped_refptr<FakePicturePileImpl> pending_pile =
1238 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1239 scoped_refptr<FakePicturePileImpl> active_pile =
1240 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1242 SetupTrees(pending_pile, active_pile);
1244 float low_res_factor = host_impl_.settings().low_res_contents_scale_factor;
1245 float device_scale = 1.f;
1246 float page_scale = 1.f;
1247 float maximum_animation_scale = 1.f;
1248 bool animating_transform = false;
1250 // Contents exactly fit on one tile at scale 1, no low res.
1251 float contents_scale = 1.f;
1252 SetContentsScaleOnBothLayers(contents_scale,
1253 device_scale,
1254 page_scale,
1255 maximum_animation_scale,
1256 animating_transform);
1257 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), contents_scale);
1258 EXPECT_BOTH_EQ(num_tilings(), 1u);
1260 ResetTilingsAndRasterScales();
1262 // Contents that are smaller than one tile, no low res.
1263 contents_scale = 0.123f;
1264 SetContentsScaleOnBothLayers(contents_scale,
1265 device_scale,
1266 page_scale,
1267 maximum_animation_scale,
1268 animating_transform);
1269 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), contents_scale);
1270 EXPECT_BOTH_EQ(num_tilings(), 1u);
1272 ResetTilingsAndRasterScales();
1274 // Any content bounds that would create more than one tile will
1275 // generate a low res tiling.
1276 contents_scale = 2.5f;
1277 SetContentsScaleOnBothLayers(contents_scale,
1278 device_scale,
1279 page_scale,
1280 maximum_animation_scale,
1281 animating_transform);
1282 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), contents_scale);
1283 EXPECT_BOTH_EQ(LowResTiling()->contents_scale(),
1284 contents_scale * low_res_factor);
1285 EXPECT_BOTH_EQ(num_tilings(), 2u);
1287 // Mask layers dont create low res since they always fit on one tile.
1288 scoped_ptr<FakePictureLayerImpl> mask =
1289 FakePictureLayerImpl::CreateMaskWithRasterSource(
1290 host_impl_.pending_tree(), 3, pending_pile);
1291 mask->SetBounds(layer_bounds);
1292 mask->SetContentBounds(layer_bounds);
1293 mask->SetDrawsContent(true);
1295 SetupDrawPropertiesAndUpdateTiles(mask.get(), contents_scale, device_scale,
1296 page_scale, maximum_animation_scale,
1297 animating_transform);
1298 EXPECT_EQ(mask->HighResTiling()->contents_scale(), contents_scale);
1299 EXPECT_EQ(mask->num_tilings(), 1u);
1302 TEST_F(PictureLayerImplTest, HugeMasksGetScaledDown) {
1303 base::TimeTicks time_ticks;
1304 time_ticks += base::TimeDelta::FromMilliseconds(1);
1305 host_impl_.SetCurrentBeginFrameArgs(
1306 CreateBeginFrameArgsForTesting(BEGINFRAME_FROM_HERE, time_ticks));
1308 gfx::Size tile_size(100, 100);
1309 gfx::Size layer_bounds(1000, 1000);
1311 scoped_refptr<FakePicturePileImpl> valid_pile =
1312 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1313 SetupPendingTree(valid_pile);
1315 scoped_ptr<FakePictureLayerImpl> mask_ptr =
1316 FakePictureLayerImpl::CreateMaskWithRasterSource(
1317 host_impl_.pending_tree(), 3, valid_pile);
1318 mask_ptr->SetBounds(layer_bounds);
1319 mask_ptr->SetContentBounds(layer_bounds);
1320 mask_ptr->SetDrawsContent(true);
1321 pending_layer_->SetMaskLayer(mask_ptr.Pass());
1322 pending_layer_->SetHasRenderSurface(true);
1324 time_ticks += base::TimeDelta::FromMilliseconds(1);
1325 host_impl_.SetCurrentBeginFrameArgs(
1326 CreateBeginFrameArgsForTesting(BEGINFRAME_FROM_HERE, time_ticks));
1327 bool update_lcd_text = false;
1328 host_impl_.pending_tree()->UpdateDrawProperties(update_lcd_text);
1330 FakePictureLayerImpl* pending_mask =
1331 static_cast<FakePictureLayerImpl*>(pending_layer_->mask_layer());
1333 EXPECT_EQ(1.f, pending_mask->HighResTiling()->contents_scale());
1334 EXPECT_EQ(1u, pending_mask->num_tilings());
1336 host_impl_.tile_manager()->InitializeTilesWithResourcesForTesting(
1337 pending_mask->HighResTiling()->AllTilesForTesting());
1339 ActivateTree();
1341 FakePictureLayerImpl* active_mask =
1342 static_cast<FakePictureLayerImpl*>(active_layer_->mask_layer());
1344 // Mask layers have a tiling with a single tile in it.
1345 EXPECT_EQ(1u, active_mask->HighResTiling()->AllTilesForTesting().size());
1346 // The mask resource exists.
1347 ResourceProvider::ResourceId mask_resource_id;
1348 gfx::Size mask_texture_size;
1349 active_mask->GetContentsResourceId(&mask_resource_id, &mask_texture_size);
1350 EXPECT_NE(0u, mask_resource_id);
1351 EXPECT_EQ(active_mask->bounds(), mask_texture_size);
1353 // Drop resources and recreate them, still the same.
1354 pending_mask->ReleaseResources();
1355 active_mask->ReleaseResources();
1356 pending_mask->RecreateResources();
1357 active_mask->RecreateResources();
1358 SetupDrawPropertiesAndUpdateTiles(active_mask, 1.f, 1.f, 1.f, 1.f, false);
1359 active_mask->HighResTiling()->CreateAllTilesForTesting();
1360 EXPECT_EQ(1u, active_mask->HighResTiling()->AllTilesForTesting().size());
1361 EXPECT_NE(0u, mask_resource_id);
1362 EXPECT_EQ(active_mask->bounds(), mask_texture_size);
1364 // Resize larger than the max texture size.
1365 int max_texture_size = host_impl_.GetRendererCapabilities().max_texture_size;
1366 gfx::Size huge_bounds(max_texture_size + 1, 10);
1367 scoped_refptr<FakePicturePileImpl> huge_pile =
1368 FakePicturePileImpl::CreateFilledPile(tile_size, huge_bounds);
1370 SetupPendingTree(huge_pile);
1371 pending_mask->SetBounds(huge_bounds);
1372 pending_mask->SetContentBounds(huge_bounds);
1373 pending_mask->SetRasterSourceOnPending(huge_pile, Region());
1375 time_ticks += base::TimeDelta::FromMilliseconds(1);
1376 host_impl_.SetCurrentBeginFrameArgs(
1377 CreateBeginFrameArgsForTesting(BEGINFRAME_FROM_HERE, time_ticks));
1378 host_impl_.pending_tree()->UpdateDrawProperties(update_lcd_text);
1380 // The mask tiling gets scaled down.
1381 EXPECT_LT(pending_mask->HighResTiling()->contents_scale(), 1.f);
1382 EXPECT_EQ(1u, pending_mask->num_tilings());
1384 host_impl_.tile_manager()->InitializeTilesWithResourcesForTesting(
1385 pending_mask->HighResTiling()->AllTilesForTesting());
1387 ActivateTree();
1389 // Mask layers have a tiling with a single tile in it.
1390 EXPECT_EQ(1u, active_mask->HighResTiling()->AllTilesForTesting().size());
1391 // The mask resource exists.
1392 active_mask->GetContentsResourceId(&mask_resource_id, &mask_texture_size);
1393 EXPECT_NE(0u, mask_resource_id);
1394 gfx::Size expected_size = active_mask->bounds();
1395 expected_size.SetToMin(gfx::Size(max_texture_size, max_texture_size));
1396 EXPECT_EQ(expected_size, mask_texture_size);
1398 // Drop resources and recreate them, still the same.
1399 pending_mask->ReleaseResources();
1400 active_mask->ReleaseResources();
1401 pending_mask->RecreateResources();
1402 active_mask->RecreateResources();
1403 SetupDrawPropertiesAndUpdateTiles(active_mask, 1.f, 1.f, 1.f, 1.f, false);
1404 active_mask->HighResTiling()->CreateAllTilesForTesting();
1405 EXPECT_EQ(1u, active_mask->HighResTiling()->AllTilesForTesting().size());
1406 EXPECT_NE(0u, mask_resource_id);
1407 EXPECT_EQ(expected_size, mask_texture_size);
1409 // Do another activate, the same holds.
1410 SetupPendingTree(huge_pile);
1411 ActivateTree();
1412 EXPECT_EQ(1u, active_mask->HighResTiling()->AllTilesForTesting().size());
1413 active_layer_->GetContentsResourceId(&mask_resource_id, &mask_texture_size);
1414 EXPECT_EQ(expected_size, mask_texture_size);
1415 EXPECT_EQ(0u, mask_resource_id);
1417 // Resize even larger, so that the scale would be smaller than the minimum
1418 // contents scale. Then the layer should no longer have any tiling.
1419 float min_contents_scale = host_impl_.settings().minimum_contents_scale;
1420 gfx::Size extra_huge_bounds(max_texture_size / min_contents_scale + 1, 10);
1421 scoped_refptr<FakePicturePileImpl> extra_huge_pile =
1422 FakePicturePileImpl::CreateFilledPile(tile_size, extra_huge_bounds);
1424 SetupPendingTree(extra_huge_pile);
1425 pending_mask->SetBounds(extra_huge_bounds);
1426 pending_mask->SetContentBounds(extra_huge_bounds);
1427 pending_mask->SetRasterSourceOnPending(extra_huge_pile, Region());
1429 EXPECT_FALSE(pending_mask->CanHaveTilings());
1431 time_ticks += base::TimeDelta::FromMilliseconds(1);
1432 host_impl_.SetCurrentBeginFrameArgs(
1433 CreateBeginFrameArgsForTesting(BEGINFRAME_FROM_HERE, time_ticks));
1434 host_impl_.pending_tree()->UpdateDrawProperties(update_lcd_text);
1436 EXPECT_EQ(0u, pending_mask->num_tilings());
1439 TEST_F(PictureLayerImplTest, ScaledMaskLayer) {
1440 base::TimeTicks time_ticks;
1441 time_ticks += base::TimeDelta::FromMilliseconds(1);
1442 host_impl_.SetCurrentBeginFrameArgs(
1443 CreateBeginFrameArgsForTesting(BEGINFRAME_FROM_HERE, time_ticks));
1445 gfx::Size tile_size(100, 100);
1446 gfx::Size layer_bounds(1000, 1000);
1448 host_impl_.SetDeviceScaleFactor(1.3f);
1450 scoped_refptr<FakePicturePileImpl> valid_pile =
1451 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1452 SetupPendingTree(valid_pile);
1454 scoped_ptr<FakePictureLayerImpl> mask_ptr =
1455 FakePictureLayerImpl::CreateMaskWithRasterSource(
1456 host_impl_.pending_tree(), 3, valid_pile);
1457 mask_ptr->SetBounds(layer_bounds);
1458 mask_ptr->SetContentBounds(layer_bounds);
1459 mask_ptr->SetDrawsContent(true);
1460 pending_layer_->SetMaskLayer(mask_ptr.Pass());
1461 pending_layer_->SetHasRenderSurface(true);
1463 time_ticks += base::TimeDelta::FromMilliseconds(1);
1464 host_impl_.SetCurrentBeginFrameArgs(
1465 CreateBeginFrameArgsForTesting(BEGINFRAME_FROM_HERE, time_ticks));
1466 bool update_lcd_text = false;
1467 host_impl_.pending_tree()->UpdateDrawProperties(update_lcd_text);
1469 FakePictureLayerImpl* pending_mask =
1470 static_cast<FakePictureLayerImpl*>(pending_layer_->mask_layer());
1472 // Masks are scaled, and do not have a low res tiling.
1473 EXPECT_EQ(1.3f, pending_mask->HighResTiling()->contents_scale());
1474 EXPECT_EQ(1u, pending_mask->num_tilings());
1476 host_impl_.tile_manager()->InitializeTilesWithResourcesForTesting(
1477 pending_mask->HighResTiling()->AllTilesForTesting());
1479 ActivateTree();
1481 FakePictureLayerImpl* active_mask =
1482 static_cast<FakePictureLayerImpl*>(active_layer_->mask_layer());
1484 // Mask layers have a tiling with a single tile in it.
1485 EXPECT_EQ(1u, active_mask->HighResTiling()->AllTilesForTesting().size());
1486 // The mask resource exists.
1487 ResourceProvider::ResourceId mask_resource_id;
1488 gfx::Size mask_texture_size;
1489 active_mask->GetContentsResourceId(&mask_resource_id, &mask_texture_size);
1490 EXPECT_NE(0u, mask_resource_id);
1491 gfx::Size expected_mask_texture_size =
1492 gfx::ToCeiledSize(gfx::ScaleSize(active_mask->bounds(), 1.3f));
1493 EXPECT_EQ(mask_texture_size, expected_mask_texture_size);
1496 TEST_F(PictureLayerImplTest, ReleaseResources) {
1497 gfx::Size tile_size(400, 400);
1498 gfx::Size layer_bounds(1300, 1900);
1500 scoped_refptr<FakePicturePileImpl> pending_pile =
1501 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1502 scoped_refptr<FakePicturePileImpl> active_pile =
1503 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1505 SetupTrees(pending_pile, active_pile);
1506 EXPECT_EQ(2u, pending_layer_->tilings()->num_tilings());
1508 // All tilings should be removed when losing output surface.
1509 active_layer_->ReleaseResources();
1510 EXPECT_FALSE(active_layer_->tilings());
1511 active_layer_->RecreateResources();
1512 EXPECT_EQ(0u, active_layer_->tilings()->num_tilings());
1513 pending_layer_->ReleaseResources();
1514 EXPECT_FALSE(pending_layer_->tilings());
1515 pending_layer_->RecreateResources();
1516 EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings());
1518 // This should create new tilings.
1519 SetupDrawPropertiesAndUpdateTiles(pending_layer_,
1520 1.f, // ideal contents scale
1521 1.f, // device scale
1522 1.f, // page scale
1523 1.f, // maximum animation scale
1524 false);
1525 EXPECT_EQ(2u, pending_layer_->tilings()->num_tilings());
1528 TEST_F(PictureLayerImplTest, ClampTilesToToMaxTileSize) {
1529 // The default max tile size is larger than 400x400.
1530 gfx::Size tile_size(400, 400);
1531 gfx::Size layer_bounds(5000, 5000);
1533 scoped_refptr<FakePicturePileImpl> pending_pile =
1534 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1535 scoped_refptr<FakePicturePileImpl> active_pile =
1536 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1538 SetupTrees(pending_pile, active_pile);
1539 EXPECT_GE(pending_layer_->tilings()->num_tilings(), 1u);
1541 pending_layer_->tilings()->tiling_at(0)->CreateAllTilesForTesting();
1543 // The default value.
1544 EXPECT_EQ(gfx::Size(256, 256).ToString(),
1545 host_impl_.settings().default_tile_size.ToString());
1547 Tile* tile = pending_layer_->tilings()->tiling_at(0)->AllTilesForTesting()[0];
1548 EXPECT_EQ(gfx::Size(256, 256).ToString(),
1549 tile->content_rect().size().ToString());
1551 ResetTilingsAndRasterScales();
1553 // Change the max texture size on the output surface context.
1554 scoped_ptr<TestWebGraphicsContext3D> context =
1555 TestWebGraphicsContext3D::Create();
1556 context->set_max_texture_size(140);
1557 host_impl_.DidLoseOutputSurface();
1558 host_impl_.InitializeRenderer(
1559 FakeOutputSurface::Create3d(context.Pass()).Pass());
1561 SetupDrawPropertiesAndUpdateTiles(pending_layer_, 1.f, 1.f, 1.f, 1.f, false);
1562 ASSERT_EQ(2u, pending_layer_->tilings()->num_tilings());
1564 pending_layer_->tilings()->tiling_at(0)->CreateAllTilesForTesting();
1566 // Verify the tiles are not larger than the context's max texture size.
1567 tile = pending_layer_->tilings()->tiling_at(0)->AllTilesForTesting()[0];
1568 EXPECT_GE(140, tile->content_rect().width());
1569 EXPECT_GE(140, tile->content_rect().height());
1572 TEST_F(PictureLayerImplTest, ClampSingleTileToToMaxTileSize) {
1573 // The default max tile size is larger than 400x400.
1574 gfx::Size tile_size(400, 400);
1575 gfx::Size layer_bounds(500, 500);
1577 scoped_refptr<FakePicturePileImpl> pending_pile =
1578 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1579 scoped_refptr<FakePicturePileImpl> active_pile =
1580 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1582 SetupTrees(pending_pile, active_pile);
1583 EXPECT_GE(pending_layer_->tilings()->num_tilings(), 1u);
1585 pending_layer_->tilings()->tiling_at(0)->CreateAllTilesForTesting();
1587 // The default value. The layer is smaller than this.
1588 EXPECT_EQ(gfx::Size(512, 512).ToString(),
1589 host_impl_.settings().max_untiled_layer_size.ToString());
1591 // There should be a single tile since the layer is small.
1592 PictureLayerTiling* high_res_tiling = pending_layer_->tilings()->tiling_at(0);
1593 EXPECT_EQ(1u, high_res_tiling->AllTilesForTesting().size());
1595 ResetTilingsAndRasterScales();
1597 // Change the max texture size on the output surface context.
1598 scoped_ptr<TestWebGraphicsContext3D> context =
1599 TestWebGraphicsContext3D::Create();
1600 context->set_max_texture_size(140);
1601 host_impl_.DidLoseOutputSurface();
1602 host_impl_.InitializeRenderer(
1603 FakeOutputSurface::Create3d(context.Pass()).Pass());
1605 SetupDrawPropertiesAndUpdateTiles(pending_layer_, 1.f, 1.f, 1.f, 1.f, false);
1606 ASSERT_LE(1u, pending_layer_->tilings()->num_tilings());
1608 pending_layer_->tilings()->tiling_at(0)->CreateAllTilesForTesting();
1610 // There should be more than one tile since the max texture size won't cover
1611 // the layer.
1612 high_res_tiling = pending_layer_->tilings()->tiling_at(0);
1613 EXPECT_LT(1u, high_res_tiling->AllTilesForTesting().size());
1615 // Verify the tiles are not larger than the context's max texture size.
1616 Tile* tile = pending_layer_->tilings()->tiling_at(0)->AllTilesForTesting()[0];
1617 EXPECT_GE(140, tile->content_rect().width());
1618 EXPECT_GE(140, tile->content_rect().height());
1621 TEST_F(PictureLayerImplTest, DisallowTileDrawQuads) {
1622 scoped_ptr<RenderPass> render_pass = RenderPass::Create();
1624 gfx::Size tile_size(400, 400);
1625 gfx::Size layer_bounds(1300, 1900);
1627 scoped_refptr<FakePicturePileImpl> pending_pile =
1628 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1629 scoped_refptr<FakePicturePileImpl> active_pile =
1630 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1632 gfx::Rect layer_invalidation(150, 200, 30, 180);
1633 SetupTreesWithInvalidation(pending_pile, active_pile, layer_invalidation);
1635 active_layer_->draw_properties().visible_content_rect =
1636 gfx::Rect(layer_bounds);
1638 AppendQuadsData data;
1639 active_layer_->WillDraw(DRAW_MODE_RESOURCELESS_SOFTWARE, nullptr);
1640 active_layer_->AppendQuads(render_pass.get(), &data);
1641 active_layer_->DidDraw(nullptr);
1643 ASSERT_EQ(1U, render_pass->quad_list.size());
1644 EXPECT_EQ(DrawQuad::PICTURE_CONTENT,
1645 render_pass->quad_list.front()->material);
1648 TEST_F(PictureLayerImplTest, SolidColorLayerHasVisibleFullCoverage) {
1649 scoped_ptr<RenderPass> render_pass = RenderPass::Create();
1651 gfx::Size tile_size(1000, 1000);
1652 gfx::Size layer_bounds(1500, 1500);
1653 gfx::Rect visible_rect(250, 250, 1000, 1000);
1655 scoped_ptr<FakePicturePile> empty_recording =
1656 FakePicturePile::CreateEmptyPile(tile_size, layer_bounds);
1657 empty_recording->SetIsSolidColor(true);
1659 scoped_refptr<FakePicturePileImpl> pending_pile =
1660 FakePicturePileImpl::CreateFromPile(empty_recording.get(), nullptr);
1661 scoped_refptr<FakePicturePileImpl> active_pile =
1662 FakePicturePileImpl::CreateFromPile(empty_recording.get(), nullptr);
1664 SetupTrees(pending_pile, active_pile);
1666 active_layer_->draw_properties().visible_content_rect = visible_rect;
1668 AppendQuadsData data;
1669 active_layer_->WillDraw(DRAW_MODE_SOFTWARE, nullptr);
1670 active_layer_->AppendQuads(render_pass.get(), &data);
1671 active_layer_->DidDraw(nullptr);
1673 Region remaining = visible_rect;
1674 for (const auto& quad : render_pass->quad_list) {
1675 EXPECT_TRUE(visible_rect.Contains(quad->rect));
1676 EXPECT_TRUE(remaining.Contains(quad->rect));
1677 remaining.Subtract(quad->rect);
1680 EXPECT_TRUE(remaining.IsEmpty());
1683 TEST_F(PictureLayerImplTest, TileScalesWithSolidColorPile) {
1684 gfx::Size layer_bounds(200, 200);
1685 gfx::Size tile_size(host_impl_.settings().default_tile_size);
1686 scoped_refptr<FakePicturePileImpl> pending_pile =
1687 FakePicturePileImpl::CreateEmptyPileThatThinksItHasRecordings(
1688 tile_size, layer_bounds, false);
1689 scoped_refptr<FakePicturePileImpl> active_pile =
1690 FakePicturePileImpl::CreateEmptyPileThatThinksItHasRecordings(
1691 tile_size, layer_bounds, true);
1693 SetupTrees(pending_pile, active_pile);
1694 // Solid color pile should not allow tilings at any scale.
1695 EXPECT_FALSE(active_layer_->CanHaveTilings());
1696 EXPECT_EQ(0.f, active_layer_->ideal_contents_scale());
1698 // Activate non-solid-color pending pile makes active layer can have tilings.
1699 ActivateTree();
1700 EXPECT_TRUE(active_layer_->CanHaveTilings());
1701 EXPECT_GT(active_layer_->ideal_contents_scale(), 0.f);
1704 TEST_F(NoLowResPictureLayerImplTest, MarkRequiredOffscreenTiles) {
1705 gfx::Size tile_size(100, 100);
1706 gfx::Size layer_bounds(200, 200);
1708 gfx::Transform transform;
1709 gfx::Transform transform_for_tile_priority;
1710 bool resourceless_software_draw = false;
1711 gfx::Rect viewport(0, 0, 100, 200);
1712 host_impl_.SetExternalDrawConstraints(transform,
1713 viewport,
1714 viewport,
1715 viewport,
1716 transform,
1717 resourceless_software_draw);
1719 scoped_refptr<FakePicturePileImpl> pending_pile =
1720 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1721 SetupPendingTreeWithFixedTileSize(pending_pile, tile_size, Region());
1723 EXPECT_EQ(1u, pending_layer_->num_tilings());
1724 EXPECT_EQ(viewport, pending_layer_->visible_rect_for_tile_priority());
1726 base::TimeTicks time_ticks;
1727 time_ticks += base::TimeDelta::FromMilliseconds(1);
1728 host_impl_.SetCurrentBeginFrameArgs(
1729 CreateBeginFrameArgsForTesting(BEGINFRAME_FROM_HERE, time_ticks));
1730 pending_layer_->UpdateTiles(resourceless_software_draw);
1732 int num_visible = 0;
1733 int num_offscreen = 0;
1735 scoped_ptr<TilingSetRasterQueueAll> queue(new TilingSetRasterQueueAll(
1736 pending_layer_->picture_layer_tiling_set(), false));
1737 for (; !queue->IsEmpty(); queue->Pop()) {
1738 const Tile* tile = queue->Top();
1739 DCHECK(tile);
1740 if (tile->priority(PENDING_TREE).distance_to_visible == 0.f) {
1741 EXPECT_TRUE(tile->required_for_activation());
1742 num_visible++;
1743 } else {
1744 EXPECT_FALSE(tile->required_for_activation());
1745 num_offscreen++;
1749 EXPECT_GT(num_visible, 0);
1750 EXPECT_GT(num_offscreen, 0);
1753 TEST_F(NoLowResPictureLayerImplTest,
1754 TileOutsideOfViewportForTilePriorityNotRequired) {
1755 base::TimeTicks time_ticks;
1756 time_ticks += base::TimeDelta::FromMilliseconds(1);
1757 host_impl_.SetCurrentBeginFrameArgs(
1758 CreateBeginFrameArgsForTesting(BEGINFRAME_FROM_HERE, time_ticks));
1760 gfx::Size tile_size(100, 100);
1761 gfx::Size layer_bounds(400, 400);
1762 gfx::Rect external_viewport_for_tile_priority(400, 200);
1763 gfx::Rect visible_content_rect(200, 400);
1765 scoped_refptr<FakePicturePileImpl> active_pile =
1766 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1767 scoped_refptr<FakePicturePileImpl> pending_pile =
1768 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1769 SetupTreesWithFixedTileSize(pending_pile, active_pile, tile_size, Region());
1771 ASSERT_EQ(1u, pending_layer_->num_tilings());
1772 ASSERT_EQ(1.f, pending_layer_->HighResTiling()->contents_scale());
1774 // Set external viewport for tile priority.
1775 gfx::Rect viewport = gfx::Rect(layer_bounds);
1776 gfx::Transform transform;
1777 gfx::Transform transform_for_tile_priority;
1778 bool resourceless_software_draw = false;
1779 host_impl_.SetExternalDrawConstraints(transform,
1780 viewport,
1781 viewport,
1782 external_viewport_for_tile_priority,
1783 transform_for_tile_priority,
1784 resourceless_software_draw);
1785 time_ticks += base::TimeDelta::FromMilliseconds(1);
1786 host_impl_.SetCurrentBeginFrameArgs(
1787 CreateBeginFrameArgsForTesting(BEGINFRAME_FROM_HERE, time_ticks));
1788 bool update_lcd_text = false;
1789 host_impl_.pending_tree()->UpdateDrawProperties(update_lcd_text);
1791 // Set visible content rect that is different from
1792 // external_viewport_for_tile_priority.
1793 pending_layer_->draw_properties().visible_content_rect = visible_content_rect;
1794 time_ticks += base::TimeDelta::FromMilliseconds(200);
1795 host_impl_.SetCurrentBeginFrameArgs(
1796 CreateBeginFrameArgsForTesting(BEGINFRAME_FROM_HERE, time_ticks));
1797 pending_layer_->UpdateTiles(resourceless_software_draw);
1799 // Intersect the two rects. Any tile outside should not be required for
1800 // activation.
1801 gfx::Rect viewport_for_tile_priority =
1802 pending_layer_->viewport_rect_for_tile_priority_in_content_space();
1803 viewport_for_tile_priority.Intersect(pending_layer_->visible_content_rect());
1805 int num_inside = 0;
1806 int num_outside = 0;
1807 for (PictureLayerTiling::CoverageIterator iter(
1808 pending_layer_->HighResTiling(), 1.f, gfx::Rect(layer_bounds));
1809 iter; ++iter) {
1810 if (!*iter)
1811 continue;
1812 Tile* tile = *iter;
1813 if (viewport_for_tile_priority.Intersects(iter.geometry_rect())) {
1814 num_inside++;
1815 // Mark everything in viewport for tile priority as ready to draw.
1816 TileDrawInfo& draw_info = tile->draw_info();
1817 draw_info.SetSolidColorForTesting(SK_ColorRED);
1818 } else {
1819 num_outside++;
1820 EXPECT_FALSE(tile->required_for_activation());
1824 EXPECT_GT(num_inside, 0);
1825 EXPECT_GT(num_outside, 0);
1827 // Activate and draw active layer.
1828 host_impl_.ActivateSyncTree();
1829 host_impl_.active_tree()->UpdateDrawProperties(update_lcd_text);
1830 active_layer_->draw_properties().visible_content_rect = visible_content_rect;
1832 scoped_ptr<RenderPass> render_pass = RenderPass::Create();
1833 AppendQuadsData data;
1834 active_layer_->WillDraw(DRAW_MODE_SOFTWARE, nullptr);
1835 active_layer_->AppendQuads(render_pass.get(), &data);
1836 active_layer_->DidDraw(nullptr);
1838 // All tiles in activation rect is ready to draw.
1839 EXPECT_EQ(0u, data.num_missing_tiles);
1840 EXPECT_EQ(0u, data.num_incomplete_tiles);
1841 EXPECT_FALSE(active_layer_->only_used_low_res_last_append_quads());
1844 TEST_F(PictureLayerImplTest, HighResTileIsComplete) {
1845 base::TimeTicks time_ticks;
1846 time_ticks += base::TimeDelta::FromMilliseconds(1);
1847 host_impl_.SetCurrentBeginFrameArgs(
1848 CreateBeginFrameArgsForTesting(BEGINFRAME_FROM_HERE, time_ticks));
1850 gfx::Size tile_size(100, 100);
1851 gfx::Size layer_bounds(200, 200);
1853 scoped_refptr<FakePicturePileImpl> pending_pile =
1854 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1856 SetupPendingTreeWithFixedTileSize(pending_pile, tile_size, Region());
1857 ActivateTree();
1859 // All high res tiles have resources.
1860 std::vector<Tile*> tiles =
1861 active_layer_->tilings()->tiling_at(0)->AllTilesForTesting();
1862 host_impl_.tile_manager()->InitializeTilesWithResourcesForTesting(tiles);
1864 scoped_ptr<RenderPass> render_pass = RenderPass::Create();
1865 AppendQuadsData data;
1866 active_layer_->WillDraw(DRAW_MODE_SOFTWARE, nullptr);
1867 active_layer_->AppendQuads(render_pass.get(), &data);
1868 active_layer_->DidDraw(nullptr);
1870 // All high res tiles drew, nothing was incomplete.
1871 EXPECT_EQ(9u, render_pass->quad_list.size());
1872 EXPECT_EQ(0u, data.num_missing_tiles);
1873 EXPECT_EQ(0u, data.num_incomplete_tiles);
1874 EXPECT_FALSE(active_layer_->only_used_low_res_last_append_quads());
1877 TEST_F(PictureLayerImplTest, HighResTileIsIncomplete) {
1878 base::TimeTicks time_ticks;
1879 time_ticks += base::TimeDelta::FromMilliseconds(1);
1880 host_impl_.SetCurrentBeginFrameArgs(
1881 CreateBeginFrameArgsForTesting(BEGINFRAME_FROM_HERE, time_ticks));
1883 gfx::Size tile_size(100, 100);
1884 gfx::Size layer_bounds(200, 200);
1886 scoped_refptr<FakePicturePileImpl> pending_pile =
1887 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1888 SetupPendingTreeWithFixedTileSize(pending_pile, tile_size, Region());
1889 ActivateTree();
1891 scoped_ptr<RenderPass> render_pass = RenderPass::Create();
1892 AppendQuadsData data;
1893 active_layer_->WillDraw(DRAW_MODE_SOFTWARE, nullptr);
1894 active_layer_->AppendQuads(render_pass.get(), &data);
1895 active_layer_->DidDraw(nullptr);
1897 EXPECT_EQ(1u, render_pass->quad_list.size());
1898 EXPECT_EQ(1u, data.num_missing_tiles);
1899 EXPECT_EQ(0u, data.num_incomplete_tiles);
1900 EXPECT_TRUE(active_layer_->only_used_low_res_last_append_quads());
1903 TEST_F(PictureLayerImplTest, HighResTileIsIncompleteLowResComplete) {
1904 base::TimeTicks time_ticks;
1905 time_ticks += base::TimeDelta::FromMilliseconds(1);
1906 host_impl_.SetCurrentBeginFrameArgs(
1907 CreateBeginFrameArgsForTesting(BEGINFRAME_FROM_HERE, time_ticks));
1909 gfx::Size tile_size(100, 100);
1910 gfx::Size layer_bounds(200, 200);
1912 scoped_refptr<FakePicturePileImpl> pending_pile =
1913 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1914 SetupPendingTreeWithFixedTileSize(pending_pile, tile_size, Region());
1915 ActivateTree();
1917 std::vector<Tile*> low_tiles =
1918 active_layer_->tilings()->tiling_at(1)->AllTilesForTesting();
1919 host_impl_.tile_manager()->InitializeTilesWithResourcesForTesting(low_tiles);
1921 scoped_ptr<RenderPass> render_pass = RenderPass::Create();
1922 AppendQuadsData data;
1923 active_layer_->WillDraw(DRAW_MODE_SOFTWARE, nullptr);
1924 active_layer_->AppendQuads(render_pass.get(), &data);
1925 active_layer_->DidDraw(nullptr);
1927 EXPECT_EQ(1u, render_pass->quad_list.size());
1928 EXPECT_EQ(0u, data.num_missing_tiles);
1929 EXPECT_EQ(1u, data.num_incomplete_tiles);
1930 EXPECT_TRUE(active_layer_->only_used_low_res_last_append_quads());
1933 TEST_F(PictureLayerImplTest, LowResTileIsIncomplete) {
1934 base::TimeTicks time_ticks;
1935 time_ticks += base::TimeDelta::FromMilliseconds(1);
1936 host_impl_.SetCurrentBeginFrameArgs(
1937 CreateBeginFrameArgsForTesting(BEGINFRAME_FROM_HERE, time_ticks));
1939 gfx::Size tile_size(100, 100);
1940 gfx::Size layer_bounds(200, 200);
1942 scoped_refptr<FakePicturePileImpl> pending_pile =
1943 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1944 SetupPendingTreeWithFixedTileSize(pending_pile, tile_size, Region());
1945 ActivateTree();
1947 // All high res tiles have resources except one.
1948 std::vector<Tile*> high_tiles =
1949 active_layer_->tilings()->tiling_at(0)->AllTilesForTesting();
1950 high_tiles.erase(high_tiles.begin());
1951 host_impl_.tile_manager()->InitializeTilesWithResourcesForTesting(high_tiles);
1953 // All low res tiles have resources.
1954 std::vector<Tile*> low_tiles =
1955 active_layer_->tilings()->tiling_at(1)->AllTilesForTesting();
1956 host_impl_.tile_manager()->InitializeTilesWithResourcesForTesting(low_tiles);
1958 scoped_ptr<RenderPass> render_pass = RenderPass::Create();
1959 AppendQuadsData data;
1960 active_layer_->WillDraw(DRAW_MODE_SOFTWARE, nullptr);
1961 active_layer_->AppendQuads(render_pass.get(), &data);
1962 active_layer_->DidDraw(nullptr);
1964 // The missing high res tile was replaced by a low res tile.
1965 EXPECT_EQ(9u, render_pass->quad_list.size());
1966 EXPECT_EQ(0u, data.num_missing_tiles);
1967 EXPECT_EQ(1u, data.num_incomplete_tiles);
1968 EXPECT_FALSE(active_layer_->only_used_low_res_last_append_quads());
1971 TEST_F(PictureLayerImplTest,
1972 HighResAndIdealResTileIsCompleteWhenRasterScaleIsNotIdeal) {
1973 base::TimeTicks time_ticks;
1974 time_ticks += base::TimeDelta::FromMilliseconds(1);
1975 host_impl_.SetCurrentBeginFrameArgs(
1976 CreateBeginFrameArgsForTesting(BEGINFRAME_FROM_HERE, time_ticks));
1978 gfx::Size tile_size(100, 100);
1979 gfx::Size layer_bounds(200, 200);
1980 gfx::Size viewport_size(400, 400);
1982 host_impl_.SetViewportSize(viewport_size);
1983 host_impl_.SetDeviceScaleFactor(2.f);
1985 scoped_refptr<FakePicturePileImpl> pending_pile =
1986 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1987 scoped_refptr<FakePicturePileImpl> active_pile =
1988 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1989 SetupTreesWithFixedTileSize(pending_pile, active_pile, tile_size, Region());
1991 // One ideal tile exists, this will get used when drawing.
1992 std::vector<Tile*> ideal_tiles;
1993 EXPECT_EQ(2.f, active_layer_->HighResTiling()->contents_scale());
1994 ideal_tiles.push_back(active_layer_->HighResTiling()->TileAt(0, 0));
1995 host_impl_.tile_manager()->InitializeTilesWithResourcesForTesting(
1996 ideal_tiles);
1998 // Due to layer scale throttling, the raster contents scale is changed to 1,
1999 // while the ideal is still 2.
2000 SetupDrawPropertiesAndUpdateTiles(active_layer_, 1.f, 1.f, 1.f, 1.f, false);
2001 SetupDrawPropertiesAndUpdateTiles(active_layer_, 2.f, 1.f, 1.f, 1.f, false);
2003 EXPECT_EQ(1.f, active_layer_->HighResTiling()->contents_scale());
2004 EXPECT_EQ(1.f, active_layer_->raster_contents_scale());
2005 EXPECT_EQ(2.f, active_layer_->ideal_contents_scale());
2007 // Both tilings still exist.
2008 EXPECT_EQ(2.f, active_layer_->tilings()->tiling_at(0)->contents_scale());
2009 EXPECT_EQ(1.f, active_layer_->tilings()->tiling_at(1)->contents_scale());
2011 // All high res tiles have resources.
2012 std::vector<Tile*> high_tiles =
2013 active_layer_->HighResTiling()->AllTilesForTesting();
2014 host_impl_.tile_manager()->InitializeTilesWithResourcesForTesting(high_tiles);
2016 scoped_ptr<RenderPass> render_pass = RenderPass::Create();
2017 AppendQuadsData data;
2018 active_layer_->WillDraw(DRAW_MODE_SOFTWARE, nullptr);
2019 active_layer_->AppendQuads(render_pass.get(), &data);
2020 active_layer_->DidDraw(nullptr);
2022 // All high res tiles drew, and the one ideal res tile drew.
2023 ASSERT_GT(render_pass->quad_list.size(), 9u);
2024 EXPECT_EQ(gfx::SizeF(99.f, 99.f),
2025 TileDrawQuad::MaterialCast(render_pass->quad_list.front())
2026 ->tex_coord_rect.size());
2027 EXPECT_EQ(gfx::SizeF(49.5f, 49.5f),
2028 TileDrawQuad::MaterialCast(render_pass->quad_list.ElementAt(1))
2029 ->tex_coord_rect.size());
2031 // Neither the high res nor the ideal tiles were considered as incomplete.
2032 EXPECT_EQ(0u, data.num_missing_tiles);
2033 EXPECT_EQ(0u, data.num_incomplete_tiles);
2034 EXPECT_FALSE(active_layer_->only_used_low_res_last_append_quads());
2037 TEST_F(PictureLayerImplTest, HighResRequiredWhenUnsharedActiveAllReady) {
2038 gfx::Size layer_bounds(400, 400);
2039 gfx::Size tile_size(100, 100);
2041 // No tiles shared.
2042 SetupDefaultTreesWithFixedTileSize(layer_bounds, tile_size,
2043 gfx::Rect(layer_bounds));
2045 active_layer_->SetAllTilesReady();
2047 // No shared tiles and all active tiles ready, so pending can only
2048 // activate with all high res tiles.
2049 pending_layer_->HighResTiling()->UpdateAllTilePrioritiesForTesting();
2050 pending_layer_->LowResTiling()->UpdateAllTilePrioritiesForTesting();
2052 AssertAllTilesRequired(pending_layer_->HighResTiling());
2053 AssertNoTilesRequired(pending_layer_->LowResTiling());
2056 TEST_F(PictureLayerImplTest, HighResRequiredWhenMissingHighResFlagOn) {
2057 gfx::Size layer_bounds(400, 400);
2058 gfx::Size tile_size(100, 100);
2060 // All tiles shared (no invalidation).
2061 SetupDefaultTreesWithFixedTileSize(layer_bounds, tile_size, Region());
2063 // Verify active tree not ready.
2064 Tile* some_active_tile =
2065 active_layer_->HighResTiling()->AllTilesForTesting()[0];
2066 EXPECT_FALSE(some_active_tile->IsReadyToDraw());
2068 // When high res are required, even if the active tree is not ready,
2069 // the high res tiles must be ready.
2070 host_impl_.SetRequiresHighResToDraw();
2072 pending_layer_->HighResTiling()->UpdateAllTilePrioritiesForTesting();
2073 pending_layer_->LowResTiling()->UpdateAllTilePrioritiesForTesting();
2075 AssertAllTilesRequired(pending_layer_->HighResTiling());
2076 AssertNoTilesRequired(pending_layer_->LowResTiling());
2079 TEST_F(PictureLayerImplTest, AllHighResRequiredEvenIfShared) {
2080 gfx::Size layer_bounds(400, 400);
2081 gfx::Size tile_size(100, 100);
2083 SetupDefaultTreesWithFixedTileSize(layer_bounds, tile_size, Region());
2085 Tile* some_active_tile =
2086 active_layer_->HighResTiling()->AllTilesForTesting()[0];
2087 EXPECT_FALSE(some_active_tile->IsReadyToDraw());
2089 // All tiles shared (no invalidation), so even though the active tree's
2090 // tiles aren't ready, the high res tiles are required for activation.
2091 pending_layer_->HighResTiling()->UpdateAllTilePrioritiesForTesting();
2092 pending_layer_->LowResTiling()->UpdateAllTilePrioritiesForTesting();
2094 AssertAllTilesRequired(pending_layer_->HighResTiling());
2095 AssertNoTilesRequired(pending_layer_->LowResTiling());
2098 TEST_F(PictureLayerImplTest, DisallowRequiredForActivation) {
2099 gfx::Size layer_bounds(400, 400);
2100 gfx::Size tile_size(100, 100);
2102 SetupDefaultTreesWithFixedTileSize(layer_bounds, tile_size, Region());
2104 Tile* some_active_tile =
2105 active_layer_->HighResTiling()->AllTilesForTesting()[0];
2106 EXPECT_FALSE(some_active_tile->IsReadyToDraw());
2108 pending_layer_->HighResTiling()->set_can_require_tiles_for_activation(false);
2109 pending_layer_->LowResTiling()->set_can_require_tiles_for_activation(false);
2111 // If we disallow required for activation, no tiles can be required.
2112 pending_layer_->HighResTiling()->UpdateAllTilePrioritiesForTesting();
2113 pending_layer_->LowResTiling()->UpdateAllTilePrioritiesForTesting();
2115 AssertNoTilesRequired(pending_layer_->HighResTiling());
2116 AssertNoTilesRequired(pending_layer_->LowResTiling());
2119 TEST_F(PictureLayerImplTest, NothingRequiredIfActiveMissingTiles) {
2120 gfx::Size layer_bounds(400, 400);
2121 gfx::Size tile_size(100, 100);
2123 scoped_refptr<FakePicturePileImpl> pending_pile =
2124 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
2125 // This pile will create tilings, but has no recordings so will not create any
2126 // tiles. This is attempting to simulate scrolling past the end of recorded
2127 // content on the active layer, where the recordings are so far away that
2128 // no tiles are created.
2129 bool is_solid_color = false;
2130 scoped_refptr<FakePicturePileImpl> active_pile =
2131 FakePicturePileImpl::CreateEmptyPileThatThinksItHasRecordings(
2132 tile_size, layer_bounds, is_solid_color);
2134 SetupTreesWithFixedTileSize(pending_pile, active_pile, tile_size, Region());
2136 // Active layer has tilings, but no tiles due to missing recordings.
2137 EXPECT_TRUE(active_layer_->CanHaveTilings());
2138 EXPECT_EQ(active_layer_->tilings()->num_tilings(), 2u);
2139 EXPECT_EQ(active_layer_->HighResTiling()->AllTilesForTesting().size(), 0u);
2141 // Since the active layer has no tiles at all, the pending layer doesn't
2142 // need content in order to activate.
2143 pending_layer_->HighResTiling()->UpdateAllTilePrioritiesForTesting();
2144 pending_layer_->LowResTiling()->UpdateAllTilePrioritiesForTesting();
2146 AssertNoTilesRequired(pending_layer_->HighResTiling());
2147 AssertNoTilesRequired(pending_layer_->LowResTiling());
2150 TEST_F(PictureLayerImplTest, HighResRequiredIfActiveCantHaveTiles) {
2151 gfx::Size layer_bounds(400, 400);
2152 gfx::Size tile_size(100, 100);
2154 scoped_refptr<FakePicturePileImpl> pending_pile =
2155 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
2156 scoped_refptr<FakePicturePileImpl> active_pile =
2157 FakePicturePileImpl::CreateEmptyPile(tile_size, layer_bounds);
2158 SetupTreesWithFixedTileSize(pending_pile, active_pile, tile_size, Region());
2160 // Active layer can't have tiles.
2161 EXPECT_FALSE(active_layer_->CanHaveTilings());
2163 // All high res tiles required. This should be considered identical
2164 // to the case where there is no active layer, to avoid flashing content.
2165 // This can happen if a layer exists for a while and switches from
2166 // not being able to have content to having content.
2167 pending_layer_->HighResTiling()->UpdateAllTilePrioritiesForTesting();
2168 pending_layer_->LowResTiling()->UpdateAllTilePrioritiesForTesting();
2170 AssertAllTilesRequired(pending_layer_->HighResTiling());
2171 AssertNoTilesRequired(pending_layer_->LowResTiling());
2174 TEST_F(PictureLayerImplTest, HighResRequiredWhenActiveHasDifferentBounds) {
2175 gfx::Size pending_layer_bounds(400, 400);
2176 gfx::Size active_layer_bounds(200, 200);
2177 gfx::Size tile_size(100, 100);
2179 scoped_refptr<FakePicturePileImpl> pending_pile =
2180 FakePicturePileImpl::CreateFilledPile(tile_size, pending_layer_bounds);
2181 scoped_refptr<FakePicturePileImpl> active_pile =
2182 FakePicturePileImpl::CreateFilledPile(tile_size, active_layer_bounds);
2184 SetupTreesWithFixedTileSize(pending_pile, active_pile, tile_size, Region());
2186 // Since the active layer has different bounds, the pending layer needs all
2187 // high res tiles in order to activate.
2188 pending_layer_->HighResTiling()->UpdateAllTilePrioritiesForTesting();
2189 pending_layer_->LowResTiling()->UpdateAllTilePrioritiesForTesting();
2191 AssertAllTilesRequired(pending_layer_->HighResTiling());
2192 AssertNoTilesRequired(pending_layer_->LowResTiling());
2195 TEST_F(PictureLayerImplTest, ActivateUninitializedLayer) {
2196 gfx::Size tile_size(100, 100);
2197 gfx::Size layer_bounds(400, 400);
2198 scoped_refptr<FakePicturePileImpl> pending_pile =
2199 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
2201 host_impl_.CreatePendingTree();
2202 LayerTreeImpl* pending_tree = host_impl_.pending_tree();
2204 scoped_ptr<FakePictureLayerImpl> pending_layer =
2205 FakePictureLayerImpl::CreateWithRasterSource(pending_tree, id_,
2206 pending_pile);
2207 pending_layer->SetDrawsContent(true);
2208 pending_tree->SetRootLayer(pending_layer.Pass());
2210 pending_layer_ = static_cast<FakePictureLayerImpl*>(
2211 host_impl_.pending_tree()->LayerById(id_));
2213 // Set some state on the pending layer, make sure it is not clobbered
2214 // by a sync from the active layer. This could happen because if the
2215 // pending layer has not been post-commit initialized it will attempt
2216 // to sync from the active layer.
2217 float raster_page_scale = 10.f * pending_layer_->raster_page_scale();
2218 pending_layer_->set_raster_page_scale(raster_page_scale);
2220 host_impl_.ActivateSyncTree();
2222 active_layer_ = static_cast<FakePictureLayerImpl*>(
2223 host_impl_.active_tree()->LayerById(id_));
2225 EXPECT_EQ(0u, active_layer_->num_tilings());
2226 EXPECT_EQ(raster_page_scale, active_layer_->raster_page_scale());
2229 TEST_F(PictureLayerImplTest, ShareTilesOnNextFrame) {
2230 gfx::Size layer_bounds(1500, 1500);
2231 gfx::Size tile_size(100, 100);
2233 scoped_refptr<FakePicturePileImpl> pending_pile =
2234 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
2236 SetupPendingTree(pending_pile);
2238 PictureLayerTiling* tiling = pending_layer_->HighResTiling();
2239 gfx::Rect first_invalidate = tiling->TilingDataForTesting().TileBounds(0, 0);
2240 first_invalidate.Inset(tiling->TilingDataForTesting().border_texels(),
2241 tiling->TilingDataForTesting().border_texels());
2242 gfx::Rect second_invalidate = tiling->TilingDataForTesting().TileBounds(1, 1);
2243 second_invalidate.Inset(tiling->TilingDataForTesting().border_texels(),
2244 tiling->TilingDataForTesting().border_texels());
2246 ActivateTree();
2248 // Make a pending tree with an invalidated raster tile 0,0.
2249 SetupPendingTreeWithInvalidation(pending_pile, first_invalidate);
2251 // Activate and make a pending tree with an invalidated raster tile 1,1.
2252 ActivateTree();
2254 SetupPendingTreeWithInvalidation(pending_pile, second_invalidate);
2256 PictureLayerTiling* pending_tiling = pending_layer_->tilings()->tiling_at(0);
2257 PictureLayerTiling* active_tiling = active_layer_->tilings()->tiling_at(0);
2259 // pending_tiling->CreateAllTilesForTesting();
2261 // Tile 0,0 should be shared, but tile 1,1 should not be.
2262 EXPECT_EQ(active_tiling->TileAt(0, 0), pending_tiling->TileAt(0, 0));
2263 EXPECT_EQ(active_tiling->TileAt(1, 0), pending_tiling->TileAt(1, 0));
2264 EXPECT_EQ(active_tiling->TileAt(0, 1), pending_tiling->TileAt(0, 1));
2265 EXPECT_NE(active_tiling->TileAt(1, 1), pending_tiling->TileAt(1, 1));
2266 EXPECT_TRUE(pending_tiling->TileAt(0, 0)->is_shared());
2267 EXPECT_TRUE(pending_tiling->TileAt(1, 0)->is_shared());
2268 EXPECT_TRUE(pending_tiling->TileAt(0, 1)->is_shared());
2269 EXPECT_FALSE(pending_tiling->TileAt(1, 1)->is_shared());
2271 // Drop the tiles on the active tree and recreate them. The same tiles
2272 // should be shared or not.
2273 active_tiling->ComputeTilePriorityRects(gfx::Rect(), 1.f, 1.0, Occlusion());
2274 EXPECT_TRUE(active_tiling->AllTilesForTesting().empty());
2275 active_tiling->CreateAllTilesForTesting();
2277 // Tile 0,0 should be shared, but tile 1,1 should not be.
2278 EXPECT_EQ(active_tiling->TileAt(0, 0), pending_tiling->TileAt(0, 0));
2279 EXPECT_EQ(active_tiling->TileAt(1, 0), pending_tiling->TileAt(1, 0));
2280 EXPECT_EQ(active_tiling->TileAt(0, 1), pending_tiling->TileAt(0, 1));
2281 EXPECT_NE(active_tiling->TileAt(1, 1), pending_tiling->TileAt(1, 1));
2282 EXPECT_TRUE(pending_tiling->TileAt(0, 0)->is_shared());
2283 EXPECT_TRUE(pending_tiling->TileAt(1, 0)->is_shared());
2284 EXPECT_TRUE(pending_tiling->TileAt(0, 1)->is_shared());
2285 EXPECT_FALSE(pending_tiling->TileAt(1, 1)->is_shared());
2288 TEST_F(PictureLayerImplTest, ShareTilesWithNoInvalidation) {
2289 SetupDefaultTrees(gfx::Size(1500, 1500));
2291 EXPECT_GE(active_layer_->num_tilings(), 1u);
2292 EXPECT_GE(pending_layer_->num_tilings(), 1u);
2294 // No invalidation, so all tiles are shared.
2295 PictureLayerTiling* active_tiling = active_layer_->tilings()->tiling_at(0);
2296 PictureLayerTiling* pending_tiling = pending_layer_->tilings()->tiling_at(0);
2297 ASSERT_TRUE(active_tiling);
2298 ASSERT_TRUE(pending_tiling);
2300 EXPECT_TRUE(active_tiling->TileAt(0, 0));
2301 EXPECT_TRUE(active_tiling->TileAt(1, 0));
2302 EXPECT_TRUE(active_tiling->TileAt(0, 1));
2303 EXPECT_TRUE(active_tiling->TileAt(1, 1));
2305 EXPECT_TRUE(pending_tiling->TileAt(0, 0));
2306 EXPECT_TRUE(pending_tiling->TileAt(1, 0));
2307 EXPECT_TRUE(pending_tiling->TileAt(0, 1));
2308 EXPECT_TRUE(pending_tiling->TileAt(1, 1));
2310 EXPECT_EQ(active_tiling->TileAt(0, 0), pending_tiling->TileAt(0, 0));
2311 EXPECT_TRUE(active_tiling->TileAt(0, 0)->is_shared());
2312 EXPECT_EQ(active_tiling->TileAt(1, 0), pending_tiling->TileAt(1, 0));
2313 EXPECT_TRUE(active_tiling->TileAt(1, 0)->is_shared());
2314 EXPECT_EQ(active_tiling->TileAt(0, 1), pending_tiling->TileAt(0, 1));
2315 EXPECT_TRUE(active_tiling->TileAt(0, 1)->is_shared());
2316 EXPECT_EQ(active_tiling->TileAt(1, 1), pending_tiling->TileAt(1, 1));
2317 EXPECT_TRUE(active_tiling->TileAt(1, 1)->is_shared());
2320 TEST_F(PictureLayerImplTest, ShareInvalidActiveTreeTiles) {
2321 gfx::Size tile_size(100, 100);
2322 gfx::Size layer_bounds(1500, 1500);
2324 scoped_refptr<FakePicturePileImpl> pending_pile =
2325 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
2326 scoped_refptr<FakePicturePileImpl> active_pile =
2327 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
2328 SetupTreesWithInvalidation(pending_pile, active_pile, gfx::Rect(1, 1));
2329 // Activate the invalidation.
2330 ActivateTree();
2331 // Make another pending tree without any invalidation in it.
2332 scoped_refptr<FakePicturePileImpl> pending_pile2 =
2333 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
2334 SetupPendingTree(pending_pile2);
2336 EXPECT_GE(active_layer_->num_tilings(), 1u);
2337 EXPECT_GE(pending_layer_->num_tilings(), 1u);
2339 // The active tree invalidation was handled by the active tiles, so they
2340 // can be shared with the pending tree.
2341 PictureLayerTiling* active_tiling = active_layer_->tilings()->tiling_at(0);
2342 PictureLayerTiling* pending_tiling = pending_layer_->tilings()->tiling_at(0);
2343 ASSERT_TRUE(active_tiling);
2344 ASSERT_TRUE(pending_tiling);
2346 EXPECT_TRUE(active_tiling->TileAt(0, 0));
2347 EXPECT_TRUE(active_tiling->TileAt(1, 0));
2348 EXPECT_TRUE(active_tiling->TileAt(0, 1));
2349 EXPECT_TRUE(active_tiling->TileAt(1, 1));
2351 EXPECT_TRUE(pending_tiling->TileAt(0, 0));
2352 EXPECT_TRUE(pending_tiling->TileAt(1, 0));
2353 EXPECT_TRUE(pending_tiling->TileAt(0, 1));
2354 EXPECT_TRUE(pending_tiling->TileAt(1, 1));
2356 EXPECT_EQ(active_tiling->TileAt(0, 0), pending_tiling->TileAt(0, 0));
2357 EXPECT_TRUE(active_tiling->TileAt(0, 0)->is_shared());
2358 EXPECT_EQ(active_tiling->TileAt(1, 0), pending_tiling->TileAt(1, 0));
2359 EXPECT_TRUE(active_tiling->TileAt(1, 0)->is_shared());
2360 EXPECT_EQ(active_tiling->TileAt(0, 1), pending_tiling->TileAt(0, 1));
2361 EXPECT_TRUE(active_tiling->TileAt(0, 1)->is_shared());
2362 EXPECT_EQ(active_tiling->TileAt(1, 1), pending_tiling->TileAt(1, 1));
2363 EXPECT_TRUE(active_tiling->TileAt(1, 1)->is_shared());
2366 TEST_F(PictureLayerImplTest, RecreateInvalidPendingTreeTiles) {
2367 // Set some invalidation on the pending tree. We should replace raster tiles
2368 // that touch this.
2369 SetupDefaultTreesWithInvalidation(gfx::Size(1500, 1500), gfx::Rect(1, 1));
2371 EXPECT_GE(active_layer_->num_tilings(), 1u);
2372 EXPECT_GE(pending_layer_->num_tilings(), 1u);
2374 // The pending tree invalidation means tiles can not be shared with the
2375 // active tree.
2376 PictureLayerTiling* active_tiling = active_layer_->tilings()->tiling_at(0);
2377 PictureLayerTiling* pending_tiling = pending_layer_->tilings()->tiling_at(0);
2378 ASSERT_TRUE(active_tiling);
2379 ASSERT_TRUE(pending_tiling);
2381 EXPECT_TRUE(active_tiling->TileAt(0, 0));
2382 EXPECT_TRUE(active_tiling->TileAt(1, 0));
2383 EXPECT_TRUE(active_tiling->TileAt(0, 1));
2384 EXPECT_TRUE(active_tiling->TileAt(1, 1));
2386 EXPECT_TRUE(pending_tiling->TileAt(0, 0));
2387 EXPECT_TRUE(pending_tiling->TileAt(1, 0));
2388 EXPECT_TRUE(pending_tiling->TileAt(0, 1));
2389 EXPECT_TRUE(pending_tiling->TileAt(1, 1));
2391 EXPECT_NE(active_tiling->TileAt(0, 0), pending_tiling->TileAt(0, 0));
2392 EXPECT_FALSE(active_tiling->TileAt(0, 0)->is_shared());
2393 EXPECT_FALSE(pending_tiling->TileAt(0, 0)->is_shared());
2394 EXPECT_EQ(active_tiling->TileAt(1, 0), pending_tiling->TileAt(1, 0));
2395 EXPECT_TRUE(active_tiling->TileAt(1, 0)->is_shared());
2396 EXPECT_EQ(active_tiling->TileAt(0, 1), pending_tiling->TileAt(0, 1));
2397 EXPECT_TRUE(active_tiling->TileAt(1, 1)->is_shared());
2398 EXPECT_EQ(active_tiling->TileAt(1, 1), pending_tiling->TileAt(1, 1));
2399 EXPECT_TRUE(active_tiling->TileAt(1, 1)->is_shared());
2402 TEST_F(PictureLayerImplTest, SyncTilingAfterGpuRasterizationToggles) {
2403 base::TimeTicks time_ticks;
2404 time_ticks += base::TimeDelta::FromMilliseconds(1);
2405 host_impl_.SetCurrentBeginFrameArgs(
2406 CreateBeginFrameArgsForTesting(BEGINFRAME_FROM_HERE, time_ticks));
2408 gfx::Size tile_size(100, 100);
2409 gfx::Size layer_bounds(10, 10);
2411 scoped_refptr<FakePicturePileImpl> pending_pile =
2412 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
2413 scoped_refptr<FakePicturePileImpl> active_pile =
2414 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
2416 SetupTrees(pending_pile, active_pile);
2418 EXPECT_TRUE(pending_layer_->tilings()->FindTilingWithScale(1.f));
2419 EXPECT_TRUE(active_layer_->tilings()->FindTilingWithScale(1.f));
2421 // Gpu rasterization is disabled by default.
2422 EXPECT_FALSE(host_impl_.use_gpu_rasterization());
2423 // Toggling the gpu rasterization clears all tilings on both trees.
2424 host_impl_.SetUseGpuRasterization(true);
2425 EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings());
2426 EXPECT_EQ(0u, active_layer_->tilings()->num_tilings());
2428 // Make sure that we can still add tiling to the pending layer,
2429 // that gets synced to the active layer.
2430 time_ticks += base::TimeDelta::FromMilliseconds(1);
2431 host_impl_.SetCurrentBeginFrameArgs(
2432 CreateBeginFrameArgsForTesting(BEGINFRAME_FROM_HERE, time_ticks));
2433 bool update_lcd_text = false;
2434 host_impl_.pending_tree()->UpdateDrawProperties(update_lcd_text);
2435 EXPECT_TRUE(pending_layer_->tilings()->FindTilingWithScale(1.f));
2437 ActivateTree();
2438 EXPECT_TRUE(active_layer_->tilings()->FindTilingWithScale(1.f));
2440 SetupPendingTree(pending_pile);
2441 EXPECT_TRUE(pending_layer_->tilings()->FindTilingWithScale(1.f));
2443 // Toggling the gpu rasterization clears all tilings on both trees.
2444 EXPECT_TRUE(host_impl_.use_gpu_rasterization());
2445 host_impl_.SetUseGpuRasterization(false);
2446 EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings());
2447 EXPECT_EQ(0u, active_layer_->tilings()->num_tilings());
2450 TEST_F(PictureLayerImplTest, HighResCreatedWhenBoundsShrink) {
2451 gfx::Size tile_size(100, 100);
2453 // Put 0.5 as high res.
2454 host_impl_.SetDeviceScaleFactor(0.5f);
2456 scoped_refptr<FakePicturePileImpl> pending_pile =
2457 FakePicturePileImpl::CreateFilledPile(tile_size, gfx::Size(10, 10));
2458 SetupPendingTree(pending_pile);
2460 // Sanity checks.
2461 EXPECT_EQ(1u, pending_layer_->tilings()->num_tilings());
2462 EXPECT_TRUE(pending_layer_->tilings()->FindTilingWithScale(0.5f));
2464 ActivateTree();
2466 // Now, set the bounds to be 1x1, so that minimum contents scale becomes 1.
2467 pending_pile =
2468 FakePicturePileImpl::CreateFilledPile(tile_size, gfx::Size(1, 1));
2469 SetupPendingTree(pending_pile);
2471 // Another sanity check.
2472 EXPECT_EQ(1.f, pending_layer_->MinimumContentsScale());
2474 // Since the MinContentsScale is 1, the 0.5 tiling should be replaced by a 1.0
2475 // tiling.
2476 SetupDrawPropertiesAndUpdateTiles(pending_layer_, 0.5f, 1.f, 1.f, 1.f, false);
2478 EXPECT_EQ(1u, pending_layer_->tilings()->num_tilings());
2479 PictureLayerTiling* tiling =
2480 pending_layer_->tilings()->FindTilingWithScale(1.0f);
2481 ASSERT_TRUE(tiling);
2482 EXPECT_EQ(HIGH_RESOLUTION, tiling->resolution());
2485 TEST_F(PictureLayerImplTest, LowResTilingWithoutGpuRasterization) {
2486 gfx::Size default_tile_size(host_impl_.settings().default_tile_size);
2487 gfx::Size layer_bounds(default_tile_size.width() * 4,
2488 default_tile_size.height() * 4);
2490 host_impl_.SetUseGpuRasterization(false);
2492 SetupDefaultTrees(layer_bounds);
2493 EXPECT_FALSE(host_impl_.use_gpu_rasterization());
2494 // Should have a low-res and a high-res tiling.
2495 EXPECT_EQ(2u, pending_layer_->tilings()->num_tilings());
2498 TEST_F(PictureLayerImplTest, NoLowResTilingWithGpuRasterization) {
2499 gfx::Size default_tile_size(host_impl_.settings().default_tile_size);
2500 gfx::Size layer_bounds(default_tile_size.width() * 4,
2501 default_tile_size.height() * 4);
2503 host_impl_.SetUseGpuRasterization(true);
2505 SetupDefaultTrees(layer_bounds);
2506 EXPECT_TRUE(host_impl_.use_gpu_rasterization());
2507 // Should only have the high-res tiling.
2508 EXPECT_EQ(1u, pending_layer_->tilings()->num_tilings());
2511 TEST_F(PictureLayerImplTest, NoTilingIfDoesNotDrawContent) {
2512 // Set up layers with tilings.
2513 SetupDefaultTrees(gfx::Size(10, 10));
2514 SetContentsScaleOnBothLayers(1.f, 1.f, 1.f, 1.f, false);
2515 pending_layer_->PushPropertiesTo(active_layer_);
2516 EXPECT_TRUE(pending_layer_->DrawsContent());
2517 EXPECT_TRUE(pending_layer_->CanHaveTilings());
2518 EXPECT_GE(pending_layer_->num_tilings(), 0u);
2519 EXPECT_GE(active_layer_->num_tilings(), 0u);
2521 // Set content to false, which should make CanHaveTilings return false.
2522 pending_layer_->SetDrawsContent(false);
2523 EXPECT_FALSE(pending_layer_->DrawsContent());
2524 EXPECT_FALSE(pending_layer_->CanHaveTilings());
2526 // No tilings should be pushed to active layer.
2527 pending_layer_->PushPropertiesTo(active_layer_);
2528 EXPECT_EQ(0u, active_layer_->num_tilings());
2531 TEST_F(PictureLayerImplTest, FirstTilingDuringPinch) {
2532 SetupDefaultTrees(gfx::Size(10, 10));
2534 // We start with a tiling at scale 1.
2535 EXPECT_EQ(1.f, pending_layer_->HighResTiling()->contents_scale());
2537 // When we scale up by 2.3, we get a new tiling that is a power of 2, in this
2538 // case 4.
2539 host_impl_.PinchGestureBegin();
2540 float high_res_scale = 2.3f;
2541 SetContentsScaleOnBothLayers(high_res_scale, 1.f, 1.f, 1.f, false);
2542 EXPECT_EQ(4.f, pending_layer_->HighResTiling()->contents_scale());
2545 TEST_F(PictureLayerImplTest, PinchingTooSmall) {
2546 SetupDefaultTrees(gfx::Size(10, 10));
2548 // We start with a tiling at scale 1.
2549 EXPECT_EQ(1.f, pending_layer_->HighResTiling()->contents_scale());
2551 host_impl_.PinchGestureBegin();
2552 float high_res_scale = 0.0001f;
2553 EXPECT_LT(high_res_scale, pending_layer_->MinimumContentsScale());
2555 SetContentsScaleOnBothLayers(high_res_scale, 1.f, high_res_scale, 1.f, false);
2556 EXPECT_FLOAT_EQ(pending_layer_->MinimumContentsScale(),
2557 pending_layer_->HighResTiling()->contents_scale());
2560 TEST_F(PictureLayerImplTest, PinchingTooSmallWithContentsScale) {
2561 SetupDefaultTrees(gfx::Size(10, 10));
2563 ResetTilingsAndRasterScales();
2565 float contents_scale = 0.15f;
2566 SetContentsScaleOnBothLayers(contents_scale, 1.f, 1.f, 1.f, false);
2568 ASSERT_GE(pending_layer_->num_tilings(), 0u);
2569 EXPECT_FLOAT_EQ(contents_scale,
2570 pending_layer_->HighResTiling()->contents_scale());
2572 host_impl_.PinchGestureBegin();
2574 float page_scale = 0.0001f;
2575 EXPECT_LT(page_scale * contents_scale,
2576 pending_layer_->MinimumContentsScale());
2578 SetContentsScaleOnBothLayers(contents_scale * page_scale, 1.f, page_scale,
2579 1.f, false);
2580 ASSERT_GE(pending_layer_->num_tilings(), 0u);
2581 EXPECT_FLOAT_EQ(pending_layer_->MinimumContentsScale(),
2582 pending_layer_->HighResTiling()->contents_scale());
2585 class DeferredInitPictureLayerImplTest : public PictureLayerImplTest {
2586 public:
2587 void InitializeRenderer() override {
2588 bool delegated_rendering = false;
2589 host_impl_.InitializeRenderer(FakeOutputSurface::CreateDeferredGL(
2590 scoped_ptr<SoftwareOutputDevice>(new SoftwareOutputDevice),
2591 delegated_rendering));
2594 void SetUp() override {
2595 PictureLayerImplTest::SetUp();
2597 // Create some default active and pending trees.
2598 gfx::Size tile_size(100, 100);
2599 gfx::Size layer_bounds(400, 400);
2601 scoped_refptr<FakePicturePileImpl> pending_pile =
2602 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
2603 scoped_refptr<FakePicturePileImpl> active_pile =
2604 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
2606 SetupTrees(pending_pile, active_pile);
2610 // This test is really a LayerTreeHostImpl test, in that it makes sure
2611 // that trees need update draw properties after deferred initialization.
2612 // However, this is also a regression test for PictureLayerImpl in that
2613 // not having this update will cause a crash.
2614 TEST_F(DeferredInitPictureLayerImplTest, PreventUpdateTilesDuringLostContext) {
2615 host_impl_.pending_tree()->UpdateDrawProperties(true);
2616 host_impl_.active_tree()->UpdateDrawProperties(false);
2617 EXPECT_FALSE(host_impl_.pending_tree()->needs_update_draw_properties());
2618 EXPECT_FALSE(host_impl_.active_tree()->needs_update_draw_properties());
2620 FakeOutputSurface* fake_output_surface =
2621 static_cast<FakeOutputSurface*>(host_impl_.output_surface());
2622 ASSERT_TRUE(fake_output_surface->InitializeAndSetContext3d(
2623 TestContextProvider::Create(), TestContextProvider::Create()));
2625 // These will crash PictureLayerImpl if this is not true.
2626 ASSERT_TRUE(host_impl_.pending_tree()->needs_update_draw_properties());
2627 ASSERT_TRUE(host_impl_.active_tree()->needs_update_draw_properties());
2628 host_impl_.active_tree()->UpdateDrawProperties(false);
2631 TEST_F(PictureLayerImplTest, HighResTilingDuringAnimationForCpuRasterization) {
2632 gfx::Size viewport_size(1000, 1000);
2633 host_impl_.SetViewportSize(viewport_size);
2635 gfx::Size layer_bounds(100, 100);
2636 SetupDefaultTrees(layer_bounds);
2638 float contents_scale = 1.f;
2639 float device_scale = 1.f;
2640 float page_scale = 1.f;
2641 float maximum_animation_scale = 1.f;
2642 bool animating_transform = false;
2644 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 1.f);
2646 // Since we're CPU-rasterizing, starting an animation should cause tiling
2647 // resolution to get set to the maximum animation scale factor.
2648 animating_transform = true;
2649 maximum_animation_scale = 3.f;
2650 contents_scale = 2.f;
2652 SetContentsScaleOnBothLayers(contents_scale,
2653 device_scale,
2654 page_scale,
2655 maximum_animation_scale,
2656 animating_transform);
2657 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 3.f);
2659 // Further changes to scale during the animation should not cause a new
2660 // high-res tiling to get created.
2661 contents_scale = 4.f;
2662 maximum_animation_scale = 5.f;
2664 SetContentsScaleOnBothLayers(contents_scale,
2665 device_scale,
2666 page_scale,
2667 maximum_animation_scale,
2668 animating_transform);
2669 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 3.f);
2671 // Once we stop animating, a new high-res tiling should be created.
2672 animating_transform = false;
2674 SetContentsScaleOnBothLayers(contents_scale,
2675 device_scale,
2676 page_scale,
2677 maximum_animation_scale,
2678 animating_transform);
2679 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 4.f);
2681 // When animating with an unknown maximum animation scale factor, a new
2682 // high-res tiling should be created at a source scale of 1.
2683 animating_transform = true;
2684 contents_scale = 2.f;
2685 maximum_animation_scale = 0.f;
2687 SetContentsScaleOnBothLayers(contents_scale,
2688 device_scale,
2689 page_scale,
2690 maximum_animation_scale,
2691 animating_transform);
2692 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), page_scale * device_scale);
2694 // Further changes to scale during the animation should not cause a new
2695 // high-res tiling to get created.
2696 contents_scale = 3.f;
2698 SetContentsScaleOnBothLayers(contents_scale,
2699 device_scale,
2700 page_scale,
2701 maximum_animation_scale,
2702 animating_transform);
2703 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), page_scale * device_scale);
2705 // Once we stop animating, a new high-res tiling should be created.
2706 animating_transform = false;
2707 contents_scale = 4.f;
2709 SetContentsScaleOnBothLayers(contents_scale,
2710 device_scale,
2711 page_scale,
2712 maximum_animation_scale,
2713 animating_transform);
2714 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 4.f);
2716 // When animating with a maxmium animation scale factor that is so large
2717 // that the layer grows larger than the viewport at this scale, a new
2718 // high-res tiling should get created at a source scale of 1, not at its
2719 // maximum scale.
2720 animating_transform = true;
2721 contents_scale = 2.f;
2722 maximum_animation_scale = 11.f;
2724 SetContentsScaleOnBothLayers(contents_scale,
2725 device_scale,
2726 page_scale,
2727 maximum_animation_scale,
2728 animating_transform);
2729 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), page_scale * device_scale);
2731 // Once we stop animating, a new high-res tiling should be created.
2732 animating_transform = false;
2733 contents_scale = 11.f;
2735 SetContentsScaleOnBothLayers(contents_scale,
2736 device_scale,
2737 page_scale,
2738 maximum_animation_scale,
2739 animating_transform);
2740 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 11.f);
2742 // When animating with a maxmium animation scale factor that is so large
2743 // that the layer grows larger than the viewport at this scale, and where
2744 // the intial source scale is < 1, a new high-res tiling should get created
2745 // at source scale 1.
2746 animating_transform = true;
2747 contents_scale = 0.1f;
2748 maximum_animation_scale = 11.f;
2750 SetContentsScaleOnBothLayers(contents_scale,
2751 device_scale,
2752 page_scale,
2753 maximum_animation_scale,
2754 animating_transform);
2755 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), device_scale * page_scale);
2757 // Once we stop animating, a new high-res tiling should be created.
2758 animating_transform = false;
2759 contents_scale = 12.f;
2761 SetContentsScaleOnBothLayers(contents_scale,
2762 device_scale,
2763 page_scale,
2764 maximum_animation_scale,
2765 animating_transform);
2766 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 12.f);
2768 // When animating toward a smaller scale, but that is still so large that the
2769 // layer grows larger than the viewport at this scale, a new high-res tiling
2770 // should get created at source scale 1.
2771 animating_transform = true;
2772 contents_scale = 11.f;
2773 maximum_animation_scale = 11.f;
2775 SetContentsScaleOnBothLayers(contents_scale,
2776 device_scale,
2777 page_scale,
2778 maximum_animation_scale,
2779 animating_transform);
2780 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), device_scale * page_scale);
2782 // Once we stop animating, a new high-res tiling should be created.
2783 animating_transform = false;
2784 contents_scale = 11.f;
2786 SetContentsScaleOnBothLayers(contents_scale,
2787 device_scale,
2788 page_scale,
2789 maximum_animation_scale,
2790 animating_transform);
2791 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 11.f);
2794 TEST_F(PictureLayerImplTest, HighResTilingDuringAnimationForGpuRasterization) {
2795 gfx::Size layer_bounds(100, 100);
2796 gfx::Size viewport_size(1000, 1000);
2797 SetupDefaultTrees(layer_bounds);
2798 host_impl_.SetViewportSize(viewport_size);
2799 host_impl_.SetUseGpuRasterization(true);
2801 float contents_scale = 1.f;
2802 float device_scale = 1.3f;
2803 float page_scale = 1.4f;
2804 float maximum_animation_scale = 1.f;
2805 bool animating_transform = false;
2807 SetContentsScaleOnBothLayers(contents_scale,
2808 device_scale,
2809 page_scale,
2810 maximum_animation_scale,
2811 animating_transform);
2812 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 1.f);
2814 // Since we're GPU-rasterizing, starting an animation should cause tiling
2815 // resolution to get set to the current contents scale.
2816 animating_transform = true;
2817 contents_scale = 2.f;
2818 maximum_animation_scale = 4.f;
2820 SetContentsScaleOnBothLayers(contents_scale,
2821 device_scale,
2822 page_scale,
2823 maximum_animation_scale,
2824 animating_transform);
2825 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 2.f);
2827 // Further changes to scale during the animation should cause a new high-res
2828 // tiling to get created.
2829 contents_scale = 3.f;
2831 SetContentsScaleOnBothLayers(contents_scale,
2832 device_scale,
2833 page_scale,
2834 maximum_animation_scale,
2835 animating_transform);
2836 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 3.f);
2838 // Since we're re-rasterizing during the animation, scales smaller than 1
2839 // should be respected.
2840 contents_scale = 0.25f;
2842 SetContentsScaleOnBothLayers(contents_scale,
2843 device_scale,
2844 page_scale,
2845 maximum_animation_scale,
2846 animating_transform);
2847 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 0.25f);
2849 // Once we stop animating, a new high-res tiling should be created.
2850 contents_scale = 4.f;
2851 animating_transform = false;
2853 SetContentsScaleOnBothLayers(contents_scale,
2854 device_scale,
2855 page_scale,
2856 maximum_animation_scale,
2857 animating_transform);
2858 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 4.f);
2861 TEST_F(PictureLayerImplTest, TilingSetRasterQueue) {
2862 base::TimeTicks time_ticks;
2863 time_ticks += base::TimeDelta::FromMilliseconds(1);
2864 host_impl_.SetCurrentBeginFrameArgs(
2865 CreateBeginFrameArgsForTesting(BEGINFRAME_FROM_HERE, time_ticks));
2867 host_impl_.SetViewportSize(gfx::Size(500, 500));
2869 gfx::Size recording_tile_size(100, 100);
2870 gfx::Size layer_bounds(1000, 1000);
2872 scoped_refptr<FakePicturePileImpl> pending_pile =
2873 FakePicturePileImpl::CreateFilledPile(recording_tile_size, layer_bounds);
2875 SetupPendingTree(pending_pile);
2876 EXPECT_EQ(2u, pending_layer_->num_tilings());
2878 std::set<Tile*> unique_tiles;
2879 bool reached_prepaint = false;
2880 int non_ideal_tile_count = 0u;
2881 int low_res_tile_count = 0u;
2882 int high_res_tile_count = 0u;
2883 int high_res_now_tiles = 0u;
2884 scoped_ptr<TilingSetRasterQueueAll> queue(new TilingSetRasterQueueAll(
2885 pending_layer_->picture_layer_tiling_set(), false));
2886 while (!queue->IsEmpty()) {
2887 Tile* tile = queue->Top();
2888 TilePriority priority = tile->priority(PENDING_TREE);
2890 EXPECT_TRUE(tile);
2892 // Non-high res tiles only get visible tiles. Also, prepaint should only
2893 // come at the end of the iteration.
2894 if (priority.resolution != HIGH_RESOLUTION) {
2895 EXPECT_EQ(TilePriority::NOW, priority.priority_bin);
2896 } else if (reached_prepaint) {
2897 EXPECT_NE(TilePriority::NOW, priority.priority_bin);
2898 } else {
2899 reached_prepaint = priority.priority_bin != TilePriority::NOW;
2900 if (!reached_prepaint)
2901 ++high_res_now_tiles;
2904 non_ideal_tile_count += priority.resolution == NON_IDEAL_RESOLUTION;
2905 low_res_tile_count += priority.resolution == LOW_RESOLUTION;
2906 high_res_tile_count += priority.resolution == HIGH_RESOLUTION;
2908 unique_tiles.insert(tile);
2909 queue->Pop();
2912 EXPECT_TRUE(reached_prepaint);
2913 EXPECT_EQ(0, non_ideal_tile_count);
2914 EXPECT_EQ(0, low_res_tile_count);
2916 // With layer size being 1000x1000 and default tile size 256x256, we expect to
2917 // see 4 now tiles out of 16 total high res tiles.
2918 EXPECT_EQ(16, high_res_tile_count);
2919 EXPECT_EQ(4, high_res_now_tiles);
2920 EXPECT_EQ(low_res_tile_count + high_res_tile_count + non_ideal_tile_count,
2921 static_cast<int>(unique_tiles.size()));
2923 scoped_ptr<TilingSetRasterQueueRequired> required_queue(
2924 new TilingSetRasterQueueRequired(
2925 pending_layer_->picture_layer_tiling_set(),
2926 RasterTilePriorityQueue::Type::REQUIRED_FOR_DRAW));
2927 EXPECT_TRUE(required_queue->IsEmpty());
2929 required_queue.reset(new TilingSetRasterQueueRequired(
2930 pending_layer_->picture_layer_tiling_set(),
2931 RasterTilePriorityQueue::Type::REQUIRED_FOR_ACTIVATION));
2932 EXPECT_FALSE(required_queue->IsEmpty());
2933 int required_for_activation_count = 0;
2934 while (!required_queue->IsEmpty()) {
2935 Tile* tile = required_queue->Top();
2936 EXPECT_TRUE(tile->required_for_activation());
2937 EXPECT_FALSE(tile->IsReadyToDraw());
2938 ++required_for_activation_count;
2939 required_queue->Pop();
2942 // All of the high res tiles should be required for activation, since there is
2943 // no active twin.
2944 EXPECT_EQ(high_res_now_tiles, required_for_activation_count);
2946 // No NOW tiles.
2947 time_ticks += base::TimeDelta::FromMilliseconds(200);
2948 host_impl_.SetCurrentBeginFrameArgs(
2949 CreateBeginFrameArgsForTesting(BEGINFRAME_FROM_HERE, time_ticks));
2951 pending_layer_->draw_properties().visible_content_rect =
2952 gfx::Rect(1100, 1100, 500, 500);
2953 bool resourceless_software_draw = false;
2954 pending_layer_->UpdateTiles(resourceless_software_draw);
2956 unique_tiles.clear();
2957 high_res_tile_count = 0u;
2958 queue.reset(new TilingSetRasterQueueAll(
2959 pending_layer_->picture_layer_tiling_set(), false));
2960 while (!queue->IsEmpty()) {
2961 Tile* tile = queue->Top();
2962 TilePriority priority = tile->priority(PENDING_TREE);
2964 EXPECT_TRUE(tile);
2966 // Non-high res tiles only get visible tiles.
2967 EXPECT_EQ(HIGH_RESOLUTION, priority.resolution);
2968 EXPECT_NE(TilePriority::NOW, priority.priority_bin);
2970 high_res_tile_count += priority.resolution == HIGH_RESOLUTION;
2972 unique_tiles.insert(tile);
2973 queue->Pop();
2976 EXPECT_EQ(16, high_res_tile_count);
2977 EXPECT_EQ(high_res_tile_count, static_cast<int>(unique_tiles.size()));
2979 time_ticks += base::TimeDelta::FromMilliseconds(200);
2980 host_impl_.SetCurrentBeginFrameArgs(
2981 CreateBeginFrameArgsForTesting(BEGINFRAME_FROM_HERE, time_ticks));
2983 pending_layer_->draw_properties().visible_content_rect =
2984 gfx::Rect(0, 0, 500, 500);
2985 pending_layer_->UpdateTiles(resourceless_software_draw);
2987 std::vector<Tile*> high_res_tiles =
2988 pending_layer_->HighResTiling()->AllTilesForTesting();
2989 for (std::vector<Tile*>::iterator tile_it = high_res_tiles.begin();
2990 tile_it != high_res_tiles.end();
2991 ++tile_it) {
2992 Tile* tile = *tile_it;
2993 TileDrawInfo& draw_info = tile->draw_info();
2994 draw_info.SetSolidColorForTesting(SK_ColorRED);
2997 non_ideal_tile_count = 0;
2998 low_res_tile_count = 0;
2999 high_res_tile_count = 0;
3000 queue.reset(new TilingSetRasterQueueAll(
3001 pending_layer_->picture_layer_tiling_set(), true));
3002 while (!queue->IsEmpty()) {
3003 Tile* tile = queue->Top();
3004 TilePriority priority = tile->priority(PENDING_TREE);
3006 EXPECT_TRUE(tile);
3008 non_ideal_tile_count += priority.resolution == NON_IDEAL_RESOLUTION;
3009 low_res_tile_count += priority.resolution == LOW_RESOLUTION;
3010 high_res_tile_count += priority.resolution == HIGH_RESOLUTION;
3011 queue->Pop();
3014 EXPECT_EQ(0, non_ideal_tile_count);
3015 EXPECT_EQ(1, low_res_tile_count);
3016 EXPECT_EQ(0, high_res_tile_count);
3019 TEST_F(PictureLayerImplTest, TilingSetRasterQueueActiveTree) {
3020 base::TimeTicks time_ticks;
3021 time_ticks += base::TimeDelta::FromMilliseconds(1);
3022 host_impl_.SetCurrentBeginFrameArgs(
3023 CreateBeginFrameArgsForTesting(BEGINFRAME_FROM_HERE, time_ticks));
3025 host_impl_.SetViewportSize(gfx::Size(500, 500));
3027 gfx::Size tile_size(100, 100);
3028 gfx::Size layer_bounds(1000, 1000);
3030 scoped_refptr<FakePicturePileImpl> pending_pile =
3031 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
3033 SetupPendingTree(pending_pile);
3034 ActivateTree();
3035 EXPECT_EQ(2u, active_layer_->num_tilings());
3037 scoped_ptr<TilingSetRasterQueueRequired> queue(
3038 new TilingSetRasterQueueRequired(
3039 active_layer_->picture_layer_tiling_set(),
3040 RasterTilePriorityQueue::Type::REQUIRED_FOR_DRAW));
3041 EXPECT_FALSE(queue->IsEmpty());
3042 while (!queue->IsEmpty()) {
3043 Tile* tile = queue->Top();
3044 EXPECT_TRUE(tile->required_for_draw());
3045 EXPECT_FALSE(tile->IsReadyToDraw());
3046 queue->Pop();
3049 queue.reset(new TilingSetRasterQueueRequired(
3050 active_layer_->picture_layer_tiling_set(),
3051 RasterTilePriorityQueue::Type::REQUIRED_FOR_ACTIVATION));
3052 EXPECT_TRUE(queue->IsEmpty());
3055 TEST_F(PictureLayerImplTest, TilingSetRasterQueueRequiredNoHighRes) {
3056 scoped_ptr<FakePicturePile> empty_recording =
3057 FakePicturePile::CreateEmptyPile(gfx::Size(256, 256),
3058 gfx::Size(1024, 1024));
3059 empty_recording->SetIsSolidColor(true);
3061 scoped_refptr<FakePicturePileImpl> pending_pile =
3062 FakePicturePileImpl::CreateFromPile(empty_recording.get(), nullptr);
3064 SetupPendingTree(pending_pile);
3065 EXPECT_FALSE(
3066 pending_layer_->picture_layer_tiling_set()->FindTilingWithResolution(
3067 HIGH_RESOLUTION));
3069 scoped_ptr<TilingSetRasterQueueRequired> queue(
3070 new TilingSetRasterQueueRequired(
3071 pending_layer_->picture_layer_tiling_set(),
3072 RasterTilePriorityQueue::Type::REQUIRED_FOR_ACTIVATION));
3073 EXPECT_TRUE(queue->IsEmpty());
3076 TEST_F(PictureLayerImplTest, TilingSetEvictionQueue) {
3077 gfx::Size tile_size(100, 100);
3078 gfx::Size layer_bounds(1000, 1000);
3079 float low_res_factor = host_impl_.settings().low_res_contents_scale_factor;
3081 host_impl_.SetViewportSize(gfx::Size(500, 500));
3083 scoped_refptr<FakePicturePileImpl> pending_pile =
3084 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
3086 // TODO(vmpstr): Add a test with tilings other than high/low res on the active
3087 // tree.
3088 SetupPendingTree(pending_pile);
3089 EXPECT_EQ(2u, pending_layer_->num_tilings());
3091 std::vector<Tile*> all_tiles;
3092 for (size_t i = 0; i < pending_layer_->num_tilings(); ++i) {
3093 PictureLayerTiling* tiling = pending_layer_->tilings()->tiling_at(i);
3094 std::vector<Tile*> tiles = tiling->AllTilesForTesting();
3095 all_tiles.insert(all_tiles.end(), tiles.begin(), tiles.end());
3098 std::set<Tile*> all_tiles_set(all_tiles.begin(), all_tiles.end());
3100 bool mark_required = false;
3101 size_t number_of_marked_tiles = 0u;
3102 size_t number_of_unmarked_tiles = 0u;
3103 for (size_t i = 0; i < pending_layer_->num_tilings(); ++i) {
3104 PictureLayerTiling* tiling = pending_layer_->tilings()->tiling_at(i);
3105 for (PictureLayerTiling::CoverageIterator iter(
3106 tiling,
3107 pending_layer_->contents_scale_x(),
3108 pending_layer_->visible_content_rect());
3109 iter;
3110 ++iter) {
3111 if (mark_required) {
3112 number_of_marked_tiles++;
3113 iter->set_required_for_activation(true);
3114 } else {
3115 number_of_unmarked_tiles++;
3117 mark_required = !mark_required;
3121 // Sanity checks.
3122 EXPECT_EQ(17u, all_tiles.size());
3123 EXPECT_EQ(17u, all_tiles_set.size());
3124 EXPECT_GT(number_of_marked_tiles, 1u);
3125 EXPECT_GT(number_of_unmarked_tiles, 1u);
3127 // Tiles don't have resources yet.
3128 scoped_ptr<TilingSetEvictionQueue> queue(new TilingSetEvictionQueue(
3129 pending_layer_->picture_layer_tiling_set(), false));
3130 EXPECT_TRUE(queue->IsEmpty());
3132 host_impl_.tile_manager()->InitializeTilesWithResourcesForTesting(all_tiles);
3134 std::set<Tile*> unique_tiles;
3135 float expected_scales[] = {low_res_factor, 1.f};
3136 size_t scale_index = 0;
3137 bool reached_visible = false;
3138 Tile* last_tile = nullptr;
3139 size_t distance_decreasing = 0;
3140 size_t distance_increasing = 0;
3141 queue.reset(new TilingSetEvictionQueue(
3142 pending_layer_->picture_layer_tiling_set(), false));
3143 while (!queue->IsEmpty()) {
3144 Tile* tile = queue->Top();
3145 if (!last_tile)
3146 last_tile = tile;
3148 EXPECT_TRUE(tile);
3150 TilePriority priority = tile->priority(PENDING_TREE);
3152 if (priority.priority_bin == TilePriority::NOW) {
3153 reached_visible = true;
3154 last_tile = tile;
3155 break;
3158 EXPECT_FALSE(tile->required_for_activation());
3160 while (std::abs(tile->contents_scale() - expected_scales[scale_index]) >
3161 std::numeric_limits<float>::epsilon()) {
3162 ++scale_index;
3163 ASSERT_LT(scale_index, arraysize(expected_scales));
3166 EXPECT_FLOAT_EQ(tile->contents_scale(), expected_scales[scale_index]);
3167 unique_tiles.insert(tile);
3169 if (tile->required_for_activation() ==
3170 last_tile->required_for_activation() &&
3171 std::abs(tile->contents_scale() - last_tile->contents_scale()) <
3172 std::numeric_limits<float>::epsilon()) {
3173 if (priority.distance_to_visible <=
3174 last_tile->priority(PENDING_TREE).distance_to_visible)
3175 ++distance_decreasing;
3176 else
3177 ++distance_increasing;
3180 last_tile = tile;
3181 queue->Pop();
3184 // 4 high res tiles are inside the viewport, the rest are evicted.
3185 EXPECT_TRUE(reached_visible);
3186 EXPECT_EQ(12u, unique_tiles.size());
3187 EXPECT_EQ(1u, distance_increasing);
3188 EXPECT_EQ(11u, distance_decreasing);
3190 scale_index = 0;
3191 bool reached_required = false;
3192 while (!queue->IsEmpty()) {
3193 Tile* tile = queue->Top();
3194 EXPECT_TRUE(tile);
3196 TilePriority priority = tile->priority(PENDING_TREE);
3197 EXPECT_EQ(TilePriority::NOW, priority.priority_bin);
3199 if (reached_required) {
3200 EXPECT_TRUE(tile->required_for_activation());
3201 } else if (tile->required_for_activation()) {
3202 reached_required = true;
3203 scale_index = 0;
3206 while (std::abs(tile->contents_scale() - expected_scales[scale_index]) >
3207 std::numeric_limits<float>::epsilon()) {
3208 ++scale_index;
3209 ASSERT_LT(scale_index, arraysize(expected_scales));
3212 EXPECT_FLOAT_EQ(tile->contents_scale(), expected_scales[scale_index]);
3213 unique_tiles.insert(tile);
3214 queue->Pop();
3217 EXPECT_TRUE(reached_required);
3218 EXPECT_EQ(all_tiles_set.size(), unique_tiles.size());
3221 TEST_F(PictureLayerImplTest, Occlusion) {
3222 gfx::Size tile_size(102, 102);
3223 gfx::Size layer_bounds(1000, 1000);
3224 gfx::Size viewport_size(1000, 1000);
3226 LayerTestCommon::LayerImplTest impl;
3227 host_impl_.SetViewportSize(viewport_size);
3229 scoped_refptr<FakePicturePileImpl> pending_pile =
3230 FakePicturePileImpl::CreateFilledPile(layer_bounds, layer_bounds);
3231 SetupPendingTreeWithFixedTileSize(pending_pile, tile_size, Region());
3232 ActivateTree();
3234 std::vector<Tile*> tiles =
3235 active_layer_->HighResTiling()->AllTilesForTesting();
3236 host_impl_.tile_manager()->InitializeTilesWithResourcesForTesting(tiles);
3239 SCOPED_TRACE("No occlusion");
3240 gfx::Rect occluded;
3241 impl.AppendQuadsWithOcclusion(active_layer_, occluded);
3243 LayerTestCommon::VerifyQuadsExactlyCoverRect(impl.quad_list(),
3244 gfx::Rect(layer_bounds));
3245 EXPECT_EQ(100u, impl.quad_list().size());
3249 SCOPED_TRACE("Full occlusion");
3250 gfx::Rect occluded(active_layer_->visible_content_rect());
3251 impl.AppendQuadsWithOcclusion(active_layer_, occluded);
3253 LayerTestCommon::VerifyQuadsExactlyCoverRect(impl.quad_list(), gfx::Rect());
3254 EXPECT_EQ(impl.quad_list().size(), 0u);
3258 SCOPED_TRACE("Partial occlusion");
3259 gfx::Rect occluded(150, 0, 200, 1000);
3260 impl.AppendQuadsWithOcclusion(active_layer_, occluded);
3262 size_t partially_occluded_count = 0;
3263 LayerTestCommon::VerifyQuadsAreOccluded(
3264 impl.quad_list(), occluded, &partially_occluded_count);
3265 // The layer outputs one quad, which is partially occluded.
3266 EXPECT_EQ(100u - 10u, impl.quad_list().size());
3267 EXPECT_EQ(10u + 10u, partially_occluded_count);
3271 TEST_F(PictureLayerImplTest, RasterScaleChangeWithoutAnimation) {
3272 gfx::Size tile_size(host_impl_.settings().default_tile_size);
3273 SetupDefaultTrees(tile_size);
3275 ResetTilingsAndRasterScales();
3277 float contents_scale = 2.f;
3278 float device_scale = 1.f;
3279 float page_scale = 1.f;
3280 float maximum_animation_scale = 1.f;
3281 bool animating_transform = false;
3283 SetContentsScaleOnBothLayers(contents_scale,
3284 device_scale,
3285 page_scale,
3286 maximum_animation_scale,
3287 animating_transform);
3288 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 2.f);
3290 // Changing the source scale without being in an animation will cause
3291 // the layer to reset its source scale to 1.f.
3292 contents_scale = 3.f;
3294 SetContentsScaleOnBothLayers(contents_scale,
3295 device_scale,
3296 page_scale,
3297 maximum_animation_scale,
3298 animating_transform);
3299 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 1.f);
3301 // Further changes to the source scale will no longer be reflected in the
3302 // contents scale.
3303 contents_scale = 0.5f;
3305 SetContentsScaleOnBothLayers(contents_scale,
3306 device_scale,
3307 page_scale,
3308 maximum_animation_scale,
3309 animating_transform);
3310 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 1.f);
3313 TEST_F(PictureLayerImplTest, LowResReadyToDrawNotEnoughToActivate) {
3314 gfx::Size tile_size(100, 100);
3315 gfx::Size layer_bounds(1000, 1000);
3317 // Make sure some tiles are not shared.
3318 gfx::Rect invalidation(gfx::Point(50, 50), tile_size);
3319 SetupDefaultTreesWithFixedTileSize(layer_bounds, tile_size, invalidation);
3321 // All pending layer tiles required are not ready.
3322 EXPECT_FALSE(host_impl_.tile_manager()->IsReadyToActivate());
3324 // Initialize all low-res tiles.
3325 pending_layer_->SetAllTilesReadyInTiling(pending_layer_->LowResTiling());
3327 // Low-res tiles should not be enough.
3328 EXPECT_FALSE(host_impl_.tile_manager()->IsReadyToActivate());
3330 // Initialize remaining tiles.
3331 pending_layer_->SetAllTilesReady();
3333 EXPECT_TRUE(host_impl_.tile_manager()->IsReadyToActivate());
3336 TEST_F(PictureLayerImplTest, HighResReadyToDrawEnoughToActivate) {
3337 gfx::Size tile_size(100, 100);
3338 gfx::Size layer_bounds(1000, 1000);
3340 // Make sure some tiles are not shared.
3341 gfx::Rect invalidation(gfx::Point(50, 50), tile_size);
3342 SetupDefaultTreesWithFixedTileSize(layer_bounds, tile_size, invalidation);
3344 // All pending layer tiles required are not ready.
3345 EXPECT_FALSE(host_impl_.tile_manager()->IsReadyToActivate());
3347 // Initialize all high-res tiles.
3348 pending_layer_->SetAllTilesReadyInTiling(pending_layer_->HighResTiling());
3350 // High-res tiles should be enough, since they cover everything visible.
3351 EXPECT_TRUE(host_impl_.tile_manager()->IsReadyToActivate());
3354 TEST_F(PictureLayerImplTest,
3355 SharedActiveHighResReadyAndPendingLowResReadyNotEnoughToActivate) {
3356 gfx::Size tile_size(100, 100);
3357 gfx::Size layer_bounds(1000, 1000);
3359 // Make sure some tiles are not shared.
3360 gfx::Rect invalidation(gfx::Point(50, 50), tile_size);
3361 SetupDefaultTreesWithFixedTileSize(layer_bounds, tile_size, invalidation);
3363 // Initialize all high-res tiles in the active layer.
3364 active_layer_->SetAllTilesReadyInTiling(active_layer_->HighResTiling());
3365 // And all the low-res tiles in the pending layer.
3366 pending_layer_->SetAllTilesReadyInTiling(pending_layer_->LowResTiling());
3368 // The unshared high-res tiles are not ready, so we cannot activate.
3369 EXPECT_FALSE(host_impl_.tile_manager()->IsReadyToActivate());
3371 // When the unshared pending high-res tiles are ready, we can activate.
3372 pending_layer_->SetAllTilesReadyInTiling(pending_layer_->HighResTiling());
3373 EXPECT_TRUE(host_impl_.tile_manager()->IsReadyToActivate());
3376 TEST_F(PictureLayerImplTest, SharedActiveHighResReadyNotEnoughToActivate) {
3377 gfx::Size tile_size(100, 100);
3378 gfx::Size layer_bounds(1000, 1000);
3380 // Make sure some tiles are not shared.
3381 gfx::Rect invalidation(gfx::Point(50, 50), tile_size);
3382 SetupDefaultTreesWithFixedTileSize(layer_bounds, tile_size, invalidation);
3384 // Initialize all high-res tiles in the active layer.
3385 active_layer_->SetAllTilesReadyInTiling(active_layer_->HighResTiling());
3387 // The unshared high-res tiles are not ready, so we cannot activate.
3388 EXPECT_FALSE(host_impl_.tile_manager()->IsReadyToActivate());
3390 // When the unshared pending high-res tiles are ready, we can activate.
3391 pending_layer_->SetAllTilesReadyInTiling(pending_layer_->HighResTiling());
3392 EXPECT_TRUE(host_impl_.tile_manager()->IsReadyToActivate());
3395 TEST_F(NoLowResPictureLayerImplTest, ManageTilingsCreatesTilings) {
3396 gfx::Size tile_size(400, 400);
3397 gfx::Size layer_bounds(1300, 1900);
3399 scoped_refptr<FakePicturePileImpl> pending_pile =
3400 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
3401 scoped_refptr<FakePicturePileImpl> active_pile =
3402 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
3404 SetupTrees(pending_pile, active_pile);
3406 float low_res_factor = host_impl_.settings().low_res_contents_scale_factor;
3407 EXPECT_LT(low_res_factor, 1.f);
3409 ResetTilingsAndRasterScales();
3411 SetupDrawPropertiesAndUpdateTiles(active_layer_,
3412 6.f, // ideal contents scale
3413 3.f, // device scale
3414 2.f, // page scale
3415 1.f, // maximum animation scale
3416 false);
3417 ASSERT_EQ(1u, active_layer_->tilings()->num_tilings());
3418 EXPECT_FLOAT_EQ(6.f,
3419 active_layer_->tilings()->tiling_at(0)->contents_scale());
3421 // If we change the page scale factor, then we should get new tilings.
3422 SetupDrawPropertiesAndUpdateTiles(active_layer_,
3423 6.6f, // ideal contents scale
3424 3.f, // device scale
3425 2.2f, // page scale
3426 1.f, // maximum animation scale
3427 false);
3428 ASSERT_EQ(2u, active_layer_->tilings()->num_tilings());
3429 EXPECT_FLOAT_EQ(6.6f,
3430 active_layer_->tilings()->tiling_at(0)->contents_scale());
3432 // If we change the device scale factor, then we should get new tilings.
3433 SetupDrawPropertiesAndUpdateTiles(active_layer_,
3434 7.26f, // ideal contents scale
3435 3.3f, // device scale
3436 2.2f, // page scale
3437 1.f, // maximum animation scale
3438 false);
3439 ASSERT_EQ(3u, active_layer_->tilings()->num_tilings());
3440 EXPECT_FLOAT_EQ(7.26f,
3441 active_layer_->tilings()->tiling_at(0)->contents_scale());
3443 // If we change the device scale factor, but end up at the same total scale
3444 // factor somehow, then we don't get new tilings.
3445 SetupDrawPropertiesAndUpdateTiles(active_layer_,
3446 7.26f, // ideal contents scale
3447 2.2f, // device scale
3448 3.3f, // page scale
3449 1.f, // maximum animation scale
3450 false);
3451 ASSERT_EQ(3u, active_layer_->tilings()->num_tilings());
3452 EXPECT_FLOAT_EQ(7.26f,
3453 active_layer_->tilings()->tiling_at(0)->contents_scale());
3456 TEST_F(NoLowResPictureLayerImplTest, PendingLayerOnlyHasHighResTiling) {
3457 gfx::Size tile_size(400, 400);
3458 gfx::Size layer_bounds(1300, 1900);
3460 scoped_refptr<FakePicturePileImpl> pending_pile =
3461 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
3462 scoped_refptr<FakePicturePileImpl> active_pile =
3463 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
3465 SetupTrees(pending_pile, active_pile);
3467 float low_res_factor = host_impl_.settings().low_res_contents_scale_factor;
3468 EXPECT_LT(low_res_factor, 1.f);
3470 ResetTilingsAndRasterScales();
3472 SetupDrawPropertiesAndUpdateTiles(pending_layer_,
3473 6.f, // ideal contents scale
3474 3.f, // device scale
3475 2.f, // page scale
3476 1.f, // maximum animation scale
3477 false);
3478 ASSERT_EQ(1u, pending_layer_->tilings()->num_tilings());
3479 EXPECT_FLOAT_EQ(6.f,
3480 pending_layer_->tilings()->tiling_at(0)->contents_scale());
3482 // If we change the page scale factor, then we should get new tilings.
3483 SetupDrawPropertiesAndUpdateTiles(pending_layer_,
3484 6.6f, // ideal contents scale
3485 3.f, // device scale
3486 2.2f, // page scale
3487 1.f, // maximum animation scale
3488 false);
3489 ASSERT_EQ(1u, pending_layer_->tilings()->num_tilings());
3490 EXPECT_FLOAT_EQ(6.6f,
3491 pending_layer_->tilings()->tiling_at(0)->contents_scale());
3493 // If we change the device scale factor, then we should get new tilings.
3494 SetupDrawPropertiesAndUpdateTiles(pending_layer_,
3495 7.26f, // ideal contents scale
3496 3.3f, // device scale
3497 2.2f, // page scale
3498 1.f, // maximum animation scale
3499 false);
3500 ASSERT_EQ(1u, pending_layer_->tilings()->num_tilings());
3501 EXPECT_FLOAT_EQ(7.26f,
3502 pending_layer_->tilings()->tiling_at(0)->contents_scale());
3504 // If we change the device scale factor, but end up at the same total scale
3505 // factor somehow, then we don't get new tilings.
3506 SetupDrawPropertiesAndUpdateTiles(pending_layer_,
3507 7.26f, // ideal contents scale
3508 2.2f, // device scale
3509 3.3f, // page scale
3510 1.f, // maximum animation scale
3511 false);
3512 ASSERT_EQ(1u, pending_layer_->tilings()->num_tilings());
3513 EXPECT_FLOAT_EQ(7.26f,
3514 pending_layer_->tilings()->tiling_at(0)->contents_scale());
3517 TEST_F(NoLowResPictureLayerImplTest, AllHighResRequiredEvenIfShared) {
3518 gfx::Size layer_bounds(400, 400);
3519 gfx::Size tile_size(100, 100);
3521 SetupDefaultTreesWithFixedTileSize(layer_bounds, tile_size, Region());
3523 Tile* some_active_tile =
3524 active_layer_->HighResTiling()->AllTilesForTesting()[0];
3525 EXPECT_FALSE(some_active_tile->IsReadyToDraw());
3527 // All tiles shared (no invalidation), so even though the active tree's
3528 // tiles aren't ready, there is nothing required.
3529 pending_layer_->HighResTiling()->UpdateAllTilePrioritiesForTesting();
3530 if (host_impl_.settings().create_low_res_tiling)
3531 pending_layer_->LowResTiling()->UpdateAllTilePrioritiesForTesting();
3533 AssertAllTilesRequired(pending_layer_->HighResTiling());
3534 if (host_impl_.settings().create_low_res_tiling)
3535 AssertNoTilesRequired(pending_layer_->LowResTiling());
3538 TEST_F(NoLowResPictureLayerImplTest, NothingRequiredIfActiveMissingTiles) {
3539 gfx::Size layer_bounds(400, 400);
3540 gfx::Size tile_size(100, 100);
3542 scoped_refptr<FakePicturePileImpl> pending_pile =
3543 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
3544 // This pile will create tilings, but has no recordings so will not create any
3545 // tiles. This is attempting to simulate scrolling past the end of recorded
3546 // content on the active layer, where the recordings are so far away that
3547 // no tiles are created.
3548 bool is_solid_color = false;
3549 scoped_refptr<FakePicturePileImpl> active_pile =
3550 FakePicturePileImpl::CreateEmptyPileThatThinksItHasRecordings(
3551 tile_size, layer_bounds, is_solid_color);
3553 SetupTreesWithFixedTileSize(pending_pile, active_pile, tile_size, Region());
3555 // Active layer has tilings, but no tiles due to missing recordings.
3556 EXPECT_TRUE(active_layer_->CanHaveTilings());
3557 EXPECT_EQ(active_layer_->tilings()->num_tilings(),
3558 host_impl_.settings().create_low_res_tiling ? 2u : 1u);
3559 EXPECT_EQ(active_layer_->HighResTiling()->AllTilesForTesting().size(), 0u);
3561 // Since the active layer has no tiles at all, the pending layer doesn't
3562 // need content in order to activate.
3563 pending_layer_->HighResTiling()->UpdateAllTilePrioritiesForTesting();
3564 if (host_impl_.settings().create_low_res_tiling)
3565 pending_layer_->LowResTiling()->UpdateAllTilePrioritiesForTesting();
3567 AssertNoTilesRequired(pending_layer_->HighResTiling());
3568 if (host_impl_.settings().create_low_res_tiling)
3569 AssertNoTilesRequired(pending_layer_->LowResTiling());
3572 TEST_F(NoLowResPictureLayerImplTest, InvalidViewportForPrioritizingTiles) {
3573 base::TimeTicks time_ticks;
3574 time_ticks += base::TimeDelta::FromMilliseconds(1);
3575 host_impl_.SetCurrentBeginFrameArgs(
3576 CreateBeginFrameArgsForTesting(BEGINFRAME_FROM_HERE, time_ticks));
3578 gfx::Size tile_size(100, 100);
3579 gfx::Size layer_bounds(400, 400);
3581 scoped_refptr<FakePicturePileImpl> pending_pile =
3582 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
3583 scoped_refptr<FakePicturePileImpl> active_pile =
3584 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
3586 SetupTreesWithInvalidation(pending_pile, active_pile, Region());
3588 SetupDrawPropertiesAndUpdateTiles(active_layer_, 1.f, 1.f, 1.f, 1.f, false);
3590 // UpdateTiles with valid viewport. Should update tile viewport.
3591 // Note viewport is considered invalid if and only if in resourceless
3592 // software draw.
3593 bool resourceless_software_draw = false;
3594 gfx::Rect viewport = gfx::Rect(layer_bounds);
3595 gfx::Transform transform;
3596 host_impl_.SetExternalDrawConstraints(transform,
3597 viewport,
3598 viewport,
3599 viewport,
3600 transform,
3601 resourceless_software_draw);
3602 active_layer_->draw_properties().visible_content_rect = viewport;
3603 active_layer_->draw_properties().screen_space_transform = transform;
3604 active_layer_->UpdateTiles(resourceless_software_draw);
3606 gfx::Rect visible_rect_for_tile_priority =
3607 active_layer_->visible_rect_for_tile_priority();
3608 EXPECT_FALSE(visible_rect_for_tile_priority.IsEmpty());
3609 gfx::Transform screen_space_transform_for_tile_priority =
3610 active_layer_->screen_space_transform();
3612 // Expand viewport and set it as invalid for prioritizing tiles.
3613 // Should update viewport and transform, but not update visible rect.
3614 time_ticks += base::TimeDelta::FromMilliseconds(200);
3615 host_impl_.SetCurrentBeginFrameArgs(
3616 CreateBeginFrameArgsForTesting(BEGINFRAME_FROM_HERE, time_ticks));
3617 resourceless_software_draw = true;
3618 viewport = gfx::ScaleToEnclosingRect(viewport, 2);
3619 transform.Translate(1.f, 1.f);
3620 active_layer_->draw_properties().visible_content_rect = viewport;
3621 active_layer_->draw_properties().screen_space_transform = transform;
3622 host_impl_.SetExternalDrawConstraints(transform,
3623 viewport,
3624 viewport,
3625 viewport,
3626 transform,
3627 resourceless_software_draw);
3628 active_layer_->UpdateTiles(resourceless_software_draw);
3630 // Transform for tile priority is updated.
3631 EXPECT_TRANSFORMATION_MATRIX_EQ(transform,
3632 active_layer_->screen_space_transform());
3633 // Visible rect for tile priority retains old value.
3634 EXPECT_EQ(visible_rect_for_tile_priority,
3635 active_layer_->visible_rect_for_tile_priority());
3637 // Keep expanded viewport but mark it valid. Should update tile viewport.
3638 time_ticks += base::TimeDelta::FromMilliseconds(200);
3639 host_impl_.SetCurrentBeginFrameArgs(
3640 CreateBeginFrameArgsForTesting(BEGINFRAME_FROM_HERE, time_ticks));
3641 resourceless_software_draw = false;
3642 host_impl_.SetExternalDrawConstraints(transform,
3643 viewport,
3644 viewport,
3645 viewport,
3646 transform,
3647 resourceless_software_draw);
3648 active_layer_->UpdateTiles(resourceless_software_draw);
3650 EXPECT_TRANSFORMATION_MATRIX_EQ(transform,
3651 active_layer_->screen_space_transform());
3652 EXPECT_EQ(viewport, active_layer_->visible_rect_for_tile_priority());
3655 TEST_F(NoLowResPictureLayerImplTest, CleanUpTilings) {
3656 gfx::Size tile_size(400, 400);
3657 gfx::Size layer_bounds(1300, 1900);
3659 scoped_refptr<FakePicturePileImpl> pending_pile =
3660 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
3661 scoped_refptr<FakePicturePileImpl> active_pile =
3662 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
3664 std::vector<PictureLayerTiling*> used_tilings;
3666 SetupTrees(pending_pile, active_pile);
3668 float low_res_factor = host_impl_.settings().low_res_contents_scale_factor;
3669 EXPECT_LT(low_res_factor, 1.f);
3671 float device_scale = 1.7f;
3672 float page_scale = 3.2f;
3673 float scale = 1.f;
3675 ResetTilingsAndRasterScales();
3677 SetContentsScaleOnBothLayers(scale, device_scale, page_scale, 1.f, false);
3678 ASSERT_EQ(1u, active_layer_->tilings()->num_tilings());
3680 // Ensure UpdateTiles won't remove any tilings. Note this is unrelated to
3681 // |used_tilings| variable, and it's here only to ensure that active_layer_
3682 // won't remove tilings before the test has a chance to verify behavior.
3683 active_layer_->MarkAllTilingsUsed();
3685 // We only have ideal tilings, so they aren't removed.
3686 used_tilings.clear();
3687 active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
3688 ASSERT_EQ(1u, active_layer_->tilings()->num_tilings());
3690 host_impl_.PinchGestureBegin();
3692 // Changing the ideal but not creating new tilings.
3693 scale *= 1.5f;
3694 page_scale *= 1.5f;
3695 SetContentsScaleOnBothLayers(scale, device_scale, page_scale, 1.f, false);
3696 ASSERT_EQ(1u, active_layer_->tilings()->num_tilings());
3698 // The tilings are still our target scale, so they aren't removed.
3699 used_tilings.clear();
3700 active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
3701 ASSERT_EQ(1u, active_layer_->tilings()->num_tilings());
3703 host_impl_.PinchGestureEnd();
3705 // Create a 1.2 scale tiling. Now we have 1.0 and 1.2 tilings. Ideal = 1.2.
3706 scale /= 4.f;
3707 page_scale /= 4.f;
3708 SetContentsScaleOnBothLayers(1.2f, device_scale, page_scale, 1.f, false);
3709 ASSERT_EQ(2u, active_layer_->tilings()->num_tilings());
3710 EXPECT_FLOAT_EQ(1.f,
3711 active_layer_->tilings()->tiling_at(1)->contents_scale());
3713 // Ensure UpdateTiles won't remove any tilings.
3714 active_layer_->MarkAllTilingsUsed();
3716 // Mark the non-ideal tilings as used. They won't be removed.
3717 used_tilings.clear();
3718 used_tilings.push_back(active_layer_->tilings()->tiling_at(1));
3719 active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
3720 ASSERT_EQ(2u, active_layer_->tilings()->num_tilings());
3722 // Now move the ideal scale to 0.5. Our target stays 1.2.
3723 SetContentsScaleOnBothLayers(0.5f, device_scale, page_scale, 1.f, false);
3725 // The high resolution tiling is between target and ideal, so is not
3726 // removed. The low res tiling for the old ideal=1.0 scale is removed.
3727 used_tilings.clear();
3728 active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
3729 ASSERT_EQ(2u, active_layer_->tilings()->num_tilings());
3731 // Now move the ideal scale to 1.0. Our target stays 1.2.
3732 SetContentsScaleOnBothLayers(1.f, device_scale, page_scale, 1.f, false);
3734 // All the tilings are between are target and the ideal, so they are not
3735 // 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.1 on the active layer. Our target stays 1.2.
3741 SetupDrawPropertiesAndUpdateTiles(
3742 active_layer_, 1.1f, device_scale, page_scale, 1.f, false);
3744 // Because the pending layer's ideal scale is still 1.0, our tilings fall
3745 // in the range [1.0,1.2] and are kept.
3746 used_tilings.clear();
3747 active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
3748 ASSERT_EQ(2u, active_layer_->tilings()->num_tilings());
3750 // Move the ideal scale on the pending layer to 1.1 as well. Our target stays
3751 // 1.2 still.
3752 SetupDrawPropertiesAndUpdateTiles(
3753 pending_layer_, 1.1f, device_scale, page_scale, 1.f, false);
3755 // Our 1.0 tiling now falls outside the range between our ideal scale and our
3756 // target raster scale. But it is in our used tilings set, so nothing is
3757 // deleted.
3758 used_tilings.clear();
3759 used_tilings.push_back(active_layer_->tilings()->tiling_at(1));
3760 active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
3761 ASSERT_EQ(2u, active_layer_->tilings()->num_tilings());
3763 // If we remove it from our used tilings set, it is outside the range to keep
3764 // so it is deleted.
3765 used_tilings.clear();
3766 active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
3767 ASSERT_EQ(1u, active_layer_->tilings()->num_tilings());
3770 TEST_F(NoLowResPictureLayerImplTest, ReleaseResources) {
3771 gfx::Size tile_size(400, 400);
3772 gfx::Size layer_bounds(1300, 1900);
3774 scoped_refptr<FakePicturePileImpl> pending_pile =
3775 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
3776 scoped_refptr<FakePicturePileImpl> active_pile =
3777 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
3779 SetupTrees(pending_pile, active_pile);
3780 EXPECT_EQ(1u, pending_layer_->tilings()->num_tilings());
3781 EXPECT_EQ(1u, active_layer_->tilings()->num_tilings());
3783 // All tilings should be removed when losing output surface.
3784 active_layer_->ReleaseResources();
3785 EXPECT_FALSE(active_layer_->tilings());
3786 active_layer_->RecreateResources();
3787 EXPECT_EQ(0u, active_layer_->tilings()->num_tilings());
3788 pending_layer_->ReleaseResources();
3789 EXPECT_FALSE(pending_layer_->tilings());
3790 pending_layer_->RecreateResources();
3791 EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings());
3793 // This should create new tilings.
3794 SetupDrawPropertiesAndUpdateTiles(pending_layer_,
3795 1.3f, // ideal contents scale
3796 2.7f, // device scale
3797 3.2f, // page scale
3798 1.f, // maximum animation scale
3799 false);
3800 EXPECT_EQ(1u, pending_layer_->tilings()->num_tilings());
3803 TEST_F(PictureLayerImplTest, SharedQuadStateContainsMaxTilingScale) {
3804 scoped_ptr<RenderPass> render_pass = RenderPass::Create();
3806 gfx::Size tile_size(400, 400);
3807 gfx::Size layer_bounds(1000, 2000);
3809 host_impl_.SetViewportSize(gfx::Size(10000, 20000));
3811 scoped_refptr<FakePicturePileImpl> pending_pile =
3812 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
3813 scoped_refptr<FakePicturePileImpl> active_pile =
3814 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
3816 SetupTrees(pending_pile, active_pile);
3818 ResetTilingsAndRasterScales();
3819 SetupDrawPropertiesAndUpdateTiles(active_layer_, 2.5f, 1.f, 1.f, 1.f, false);
3821 float max_contents_scale = active_layer_->MaximumTilingContentsScale();
3822 EXPECT_EQ(2.5f, max_contents_scale);
3824 gfx::Transform scaled_draw_transform = active_layer_->draw_transform();
3825 scaled_draw_transform.Scale(SK_MScalar1 / max_contents_scale,
3826 SK_MScalar1 / max_contents_scale);
3828 AppendQuadsData data;
3829 active_layer_->AppendQuads(render_pass.get(), &data);
3831 // SharedQuadState should have be of size 1, as we are doing AppenQuad once.
3832 EXPECT_EQ(1u, render_pass->shared_quad_state_list.size());
3833 // The content_to_target_transform should be scaled by the
3834 // MaximumTilingContentsScale on the layer.
3835 EXPECT_EQ(scaled_draw_transform.ToString(),
3836 render_pass->shared_quad_state_list.front()
3837 ->content_to_target_transform.ToString());
3838 // The content_bounds should be scaled by the
3839 // MaximumTilingContentsScale on the layer.
3840 EXPECT_EQ(
3841 gfx::Size(2500u, 5000u).ToString(),
3842 render_pass->shared_quad_state_list.front()->content_bounds.ToString());
3843 // The visible_content_rect should be scaled by the
3844 // MaximumTilingContentsScale on the layer.
3845 EXPECT_EQ(gfx::Rect(0u, 0u, 2500u, 5000u).ToString(),
3846 render_pass->shared_quad_state_list.front()
3847 ->visible_content_rect.ToString());
3850 class PictureLayerImplTestWithDelegatingRenderer : public PictureLayerImplTest {
3851 public:
3852 PictureLayerImplTestWithDelegatingRenderer() : PictureLayerImplTest() {}
3854 void InitializeRenderer() override {
3855 host_impl_.InitializeRenderer(FakeOutputSurface::CreateDelegating3d());
3859 TEST_F(PictureLayerImplTestWithDelegatingRenderer,
3860 DelegatingRendererWithTileOOM) {
3861 // This test is added for crbug.com/402321, where quad should be produced when
3862 // raster on demand is not allowed and tile is OOM.
3863 gfx::Size tile_size = host_impl_.settings().default_tile_size;
3864 gfx::Size layer_bounds(1000, 1000);
3866 // Create tiles.
3867 scoped_refptr<FakePicturePileImpl> pending_pile =
3868 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
3869 SetupPendingTree(pending_pile);
3870 pending_layer_->SetBounds(layer_bounds);
3871 ActivateTree();
3872 bool update_lcd_text = false;
3873 host_impl_.active_tree()->UpdateDrawProperties(update_lcd_text);
3874 std::vector<Tile*> tiles =
3875 active_layer_->HighResTiling()->AllTilesForTesting();
3876 host_impl_.tile_manager()->InitializeTilesWithResourcesForTesting(tiles);
3878 // Force tiles after max_tiles to be OOM. TileManager uses
3879 // GlobalStateThatImpactsTilesPriority from LayerTreeHostImpl, and we cannot
3880 // directly set state to host_impl_, so we set policy that would change the
3881 // state. We also need to update tree priority separately.
3882 GlobalStateThatImpactsTilePriority state;
3883 size_t max_tiles = 1;
3884 size_t memory_limit = max_tiles * 4 * tile_size.width() * tile_size.height();
3885 size_t resource_limit = max_tiles;
3886 ManagedMemoryPolicy policy(memory_limit,
3887 gpu::MemoryAllocation::CUTOFF_ALLOW_EVERYTHING,
3888 resource_limit);
3889 host_impl_.SetMemoryPolicy(policy);
3890 host_impl_.SetTreePriority(SAME_PRIORITY_FOR_BOTH_TREES);
3891 host_impl_.PrepareTiles();
3893 scoped_ptr<RenderPass> render_pass = RenderPass::Create();
3894 AppendQuadsData data;
3895 active_layer_->WillDraw(DRAW_MODE_HARDWARE, nullptr);
3896 active_layer_->AppendQuads(render_pass.get(), &data);
3897 active_layer_->DidDraw(nullptr);
3899 // Even when OOM, quads should be produced, and should be different material
3900 // from quads with resource.
3901 EXPECT_LT(max_tiles, render_pass->quad_list.size());
3902 EXPECT_EQ(DrawQuad::Material::TILED_CONTENT,
3903 render_pass->quad_list.front()->material);
3904 EXPECT_EQ(DrawQuad::Material::SOLID_COLOR,
3905 render_pass->quad_list.back()->material);
3908 class OcclusionTrackingSettings : public LowResTilingsSettings {
3909 public:
3910 OcclusionTrackingSettings() { use_occlusion_for_tile_prioritization = true; }
3913 class OcclusionTrackingPictureLayerImplTest : public PictureLayerImplTest {
3914 public:
3915 OcclusionTrackingPictureLayerImplTest()
3916 : PictureLayerImplTest(OcclusionTrackingSettings()) {}
3918 void VerifyEvictionConsidersOcclusion(FakePictureLayerImpl* layer,
3919 FakePictureLayerImpl* twin_layer,
3920 WhichTree tree,
3921 size_t expected_occluded_tile_count) {
3922 WhichTree twin_tree = tree == ACTIVE_TREE ? PENDING_TREE : ACTIVE_TREE;
3923 size_t occluded_tile_count = 0u;
3924 Tile* last_tile = nullptr;
3925 std::set<Tile*> shared_tiles;
3927 scoped_ptr<TilingSetEvictionQueue> queue(new TilingSetEvictionQueue(
3928 layer->picture_layer_tiling_set(), layer && twin_layer));
3929 while (!queue->IsEmpty()) {
3930 Tile* tile = queue->Top();
3931 if (!last_tile)
3932 last_tile = tile;
3933 if (tile->is_shared())
3934 EXPECT_TRUE(shared_tiles.insert(tile).second);
3936 // The only way we will encounter an occluded tile after an unoccluded
3937 // tile is if the priorty bin decreased, the tile is required for
3938 // activation, or the scale changed.
3939 bool tile_is_occluded = tile->is_occluded(tree);
3940 if (tile_is_occluded) {
3941 occluded_tile_count++;
3943 bool last_tile_is_occluded = last_tile->is_occluded(tree);
3944 if (!last_tile_is_occluded) {
3945 TilePriority::PriorityBin tile_priority_bin =
3946 tile->priority(tree).priority_bin;
3947 TilePriority::PriorityBin last_tile_priority_bin =
3948 last_tile->priority(tree).priority_bin;
3950 EXPECT_TRUE(tile_priority_bin < last_tile_priority_bin ||
3951 tile->required_for_activation() ||
3952 tile->contents_scale() != last_tile->contents_scale());
3955 last_tile = tile;
3956 queue->Pop();
3958 // Count also shared tiles which are occluded in the tree but which were
3959 // not returned by the tiling set eviction queue. Those shared tiles
3960 // shall be returned by the twin tiling set eviction queue.
3961 queue.reset(new TilingSetEvictionQueue(
3962 twin_layer->picture_layer_tiling_set(), layer && twin_layer));
3963 while (!queue->IsEmpty()) {
3964 Tile* tile = queue->Top();
3965 if (tile->is_shared()) {
3966 EXPECT_TRUE(shared_tiles.insert(tile).second);
3967 if (tile->is_occluded(tree))
3968 ++occluded_tile_count;
3969 // Check the reasons why the shared tile was not returned by
3970 // the first tiling set eviction queue.
3971 const TilePriority& combined_priority = tile->combined_priority();
3972 const TilePriority& priority = tile->priority(tree);
3973 const TilePriority& twin_priority = tile->priority(twin_tree);
3974 // Check if the shared tile was not returned by the first tiling
3975 // set eviction queue because it was out of order for the first
3976 // tiling set eviction queue but not for the twin tiling set
3977 // eviction queue.
3978 if (priority.priority_bin != twin_priority.priority_bin) {
3979 EXPECT_LT(combined_priority.priority_bin, priority.priority_bin);
3980 EXPECT_EQ(combined_priority.priority_bin, twin_priority.priority_bin);
3981 } else if (tile->is_occluded(tree) != tile->is_occluded(twin_tree)) {
3982 EXPECT_TRUE(tile->is_occluded(tree));
3983 EXPECT_FALSE(tile->is_occluded(twin_tree));
3984 EXPECT_FALSE(tile->is_occluded_combined());
3985 } else if (priority.distance_to_visible !=
3986 twin_priority.distance_to_visible) {
3987 EXPECT_LT(combined_priority.distance_to_visible,
3988 priority.distance_to_visible);
3989 EXPECT_EQ(combined_priority.distance_to_visible,
3990 twin_priority.distance_to_visible);
3991 } else {
3992 // Shared tiles having the same active and pending priorities
3993 // should be returned only by a pending tree eviction queue.
3994 EXPECT_EQ(ACTIVE_TREE, tree);
3997 queue->Pop();
3999 EXPECT_EQ(expected_occluded_tile_count, occluded_tile_count);
4003 TEST_F(OcclusionTrackingPictureLayerImplTest,
4004 OccludedTilesSkippedDuringRasterization) {
4005 base::TimeTicks time_ticks;
4006 time_ticks += base::TimeDelta::FromMilliseconds(1);
4007 host_impl_.SetCurrentBeginFrameArgs(
4008 CreateBeginFrameArgsForTesting(BEGINFRAME_FROM_HERE, time_ticks));
4010 gfx::Size tile_size(102, 102);
4011 gfx::Size layer_bounds(1000, 1000);
4012 gfx::Size viewport_size(500, 500);
4013 gfx::Point occluding_layer_position(310, 0);
4015 host_impl_.SetViewportSize(viewport_size);
4017 scoped_refptr<FakePicturePileImpl> pending_pile =
4018 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
4019 SetupPendingTreeWithFixedTileSize(pending_pile, tile_size, Region());
4021 // No occlusion.
4022 int unoccluded_tile_count = 0;
4023 scoped_ptr<TilingSetRasterQueueAll> queue(new TilingSetRasterQueueAll(
4024 pending_layer_->picture_layer_tiling_set(), false));
4025 while (!queue->IsEmpty()) {
4026 Tile* tile = queue->Top();
4028 // Occluded tiles should not be iterated over.
4029 EXPECT_FALSE(tile->is_occluded(PENDING_TREE));
4031 // Some tiles may not be visible (i.e. outside the viewport). The rest are
4032 // visible and at least partially unoccluded, verified by the above expect.
4033 bool tile_is_visible =
4034 tile->content_rect().Intersects(pending_layer_->visible_content_rect());
4035 if (tile_is_visible)
4036 unoccluded_tile_count++;
4037 queue->Pop();
4039 EXPECT_EQ(unoccluded_tile_count, 25);
4041 // Partial occlusion.
4042 pending_layer_->AddChild(LayerImpl::Create(host_impl_.pending_tree(), 1));
4043 LayerImpl* layer1 = pending_layer_->children()[0];
4044 layer1->SetBounds(layer_bounds);
4045 layer1->SetContentBounds(layer_bounds);
4046 layer1->SetDrawsContent(true);
4047 layer1->SetContentsOpaque(true);
4048 layer1->SetPosition(occluding_layer_position);
4050 time_ticks += base::TimeDelta::FromMilliseconds(200);
4051 host_impl_.SetCurrentBeginFrameArgs(
4052 CreateBeginFrameArgsForTesting(BEGINFRAME_FROM_HERE, time_ticks));
4053 bool update_lcd_text = false;
4054 host_impl_.pending_tree()->UpdateDrawProperties(update_lcd_text);
4056 unoccluded_tile_count = 0;
4057 queue.reset(new TilingSetRasterQueueAll(
4058 pending_layer_->picture_layer_tiling_set(), false));
4059 while (!queue->IsEmpty()) {
4060 Tile* tile = queue->Top();
4062 EXPECT_FALSE(tile->is_occluded(PENDING_TREE));
4064 bool tile_is_visible =
4065 tile->content_rect().Intersects(pending_layer_->visible_content_rect());
4066 if (tile_is_visible)
4067 unoccluded_tile_count++;
4068 queue->Pop();
4070 EXPECT_EQ(20, unoccluded_tile_count);
4072 // Full occlusion.
4073 layer1->SetPosition(gfx::Point(0, 0));
4075 time_ticks += base::TimeDelta::FromMilliseconds(200);
4076 host_impl_.SetCurrentBeginFrameArgs(
4077 CreateBeginFrameArgsForTesting(BEGINFRAME_FROM_HERE, time_ticks));
4078 host_impl_.pending_tree()->UpdateDrawProperties(update_lcd_text);
4080 unoccluded_tile_count = 0;
4081 queue.reset(new TilingSetRasterQueueAll(
4082 pending_layer_->picture_layer_tiling_set(), false));
4083 while (!queue->IsEmpty()) {
4084 Tile* tile = queue->Top();
4086 EXPECT_FALSE(tile->is_occluded(PENDING_TREE));
4088 bool tile_is_visible =
4089 tile->content_rect().Intersects(pending_layer_->visible_content_rect());
4090 if (tile_is_visible)
4091 unoccluded_tile_count++;
4092 queue->Pop();
4094 EXPECT_EQ(unoccluded_tile_count, 0);
4097 TEST_F(OcclusionTrackingPictureLayerImplTest,
4098 OccludedTilesNotMarkedAsRequired) {
4099 base::TimeTicks time_ticks;
4100 time_ticks += base::TimeDelta::FromMilliseconds(1);
4101 host_impl_.SetCurrentBeginFrameArgs(
4102 CreateBeginFrameArgsForTesting(BEGINFRAME_FROM_HERE, time_ticks));
4104 gfx::Size tile_size(102, 102);
4105 gfx::Size layer_bounds(1000, 1000);
4106 gfx::Size viewport_size(500, 500);
4107 gfx::Point occluding_layer_position(310, 0);
4109 host_impl_.SetViewportSize(viewport_size);
4111 scoped_refptr<FakePicturePileImpl> pending_pile =
4112 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
4113 SetupPendingTreeWithFixedTileSize(pending_pile, tile_size, Region());
4115 // No occlusion.
4116 int occluded_tile_count = 0;
4117 for (size_t i = 0; i < pending_layer_->num_tilings(); ++i) {
4118 PictureLayerTiling* tiling = pending_layer_->tilings()->tiling_at(i);
4120 occluded_tile_count = 0;
4121 for (PictureLayerTiling::CoverageIterator iter(
4122 tiling,
4123 pending_layer_->contents_scale_x(),
4124 gfx::Rect(layer_bounds));
4125 iter;
4126 ++iter) {
4127 if (!*iter)
4128 continue;
4129 const Tile* tile = *iter;
4131 // Fully occluded tiles are not required for activation.
4132 if (tile->is_occluded(PENDING_TREE)) {
4133 EXPECT_FALSE(tile->required_for_activation());
4134 occluded_tile_count++;
4137 EXPECT_EQ(occluded_tile_count, 0);
4140 // Partial occlusion.
4141 pending_layer_->AddChild(LayerImpl::Create(host_impl_.pending_tree(), 1));
4142 LayerImpl* layer1 = pending_layer_->children()[0];
4143 layer1->SetBounds(layer_bounds);
4144 layer1->SetContentBounds(layer_bounds);
4145 layer1->SetDrawsContent(true);
4146 layer1->SetContentsOpaque(true);
4147 layer1->SetPosition(occluding_layer_position);
4149 time_ticks += base::TimeDelta::FromMilliseconds(200);
4150 host_impl_.SetCurrentBeginFrameArgs(
4151 CreateBeginFrameArgsForTesting(BEGINFRAME_FROM_HERE, time_ticks));
4152 bool update_lcd_text = false;
4153 host_impl_.pending_tree()->UpdateDrawProperties(update_lcd_text);
4155 for (size_t i = 0; i < pending_layer_->num_tilings(); ++i) {
4156 PictureLayerTiling* tiling = pending_layer_->tilings()->tiling_at(i);
4157 tiling->UpdateAllTilePrioritiesForTesting();
4159 occluded_tile_count = 0;
4160 for (PictureLayerTiling::CoverageIterator iter(
4161 tiling,
4162 pending_layer_->contents_scale_x(),
4163 gfx::Rect(layer_bounds));
4164 iter;
4165 ++iter) {
4166 if (!*iter)
4167 continue;
4168 const Tile* tile = *iter;
4170 if (tile->is_occluded(PENDING_TREE)) {
4171 EXPECT_FALSE(tile->required_for_activation());
4172 occluded_tile_count++;
4175 switch (i) {
4176 case 0:
4177 EXPECT_EQ(occluded_tile_count, 5);
4178 break;
4179 case 1:
4180 EXPECT_EQ(occluded_tile_count, 2);
4181 break;
4182 default:
4183 NOTREACHED();
4187 // Full occlusion.
4188 layer1->SetPosition(gfx::PointF(0, 0));
4190 time_ticks += base::TimeDelta::FromMilliseconds(200);
4191 host_impl_.SetCurrentBeginFrameArgs(
4192 CreateBeginFrameArgsForTesting(BEGINFRAME_FROM_HERE, time_ticks));
4193 host_impl_.pending_tree()->UpdateDrawProperties(update_lcd_text);
4195 for (size_t i = 0; i < pending_layer_->num_tilings(); ++i) {
4196 PictureLayerTiling* tiling = pending_layer_->tilings()->tiling_at(i);
4197 tiling->UpdateAllTilePrioritiesForTesting();
4199 occluded_tile_count = 0;
4200 for (PictureLayerTiling::CoverageIterator iter(
4201 tiling,
4202 pending_layer_->contents_scale_x(),
4203 gfx::Rect(layer_bounds));
4204 iter;
4205 ++iter) {
4206 if (!*iter)
4207 continue;
4208 const Tile* tile = *iter;
4210 if (tile->is_occluded(PENDING_TREE)) {
4211 EXPECT_FALSE(tile->required_for_activation());
4212 occluded_tile_count++;
4215 switch (i) {
4216 case 0:
4217 EXPECT_EQ(25, occluded_tile_count);
4218 break;
4219 case 1:
4220 EXPECT_EQ(4, occluded_tile_count);
4221 break;
4222 default:
4223 NOTREACHED();
4228 TEST_F(OcclusionTrackingPictureLayerImplTest, OcclusionForDifferentScales) {
4229 base::TimeTicks time_ticks;
4230 time_ticks += base::TimeDelta::FromMilliseconds(1);
4231 host_impl_.SetCurrentBeginFrameArgs(
4232 CreateBeginFrameArgsForTesting(BEGINFRAME_FROM_HERE, time_ticks));
4234 gfx::Size tile_size(102, 102);
4235 gfx::Size layer_bounds(1000, 1000);
4236 gfx::Size viewport_size(500, 500);
4237 gfx::Point occluding_layer_position(310, 0);
4239 scoped_refptr<FakePicturePileImpl> pending_pile =
4240 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
4242 host_impl_.SetViewportSize(viewport_size);
4244 SetupPendingTreeWithFixedTileSize(pending_pile, tile_size, Region());
4245 ASSERT_TRUE(pending_layer_->CanHaveTilings());
4247 pending_layer_->AddChild(LayerImpl::Create(host_impl_.pending_tree(), 1));
4248 LayerImpl* layer1 = pending_layer_->children()[0];
4249 layer1->SetBounds(layer_bounds);
4250 layer1->SetContentBounds(layer_bounds);
4251 layer1->SetDrawsContent(true);
4252 layer1->SetContentsOpaque(true);
4253 layer1->SetPosition(occluding_layer_position);
4255 pending_layer_->tilings()->RemoveAllTilings();
4256 float low_res_factor = host_impl_.settings().low_res_contents_scale_factor;
4257 pending_layer_->AddTiling(low_res_factor);
4258 pending_layer_->AddTiling(0.3f);
4259 pending_layer_->AddTiling(0.7f);
4260 pending_layer_->AddTiling(1.0f);
4261 pending_layer_->AddTiling(2.0f);
4263 time_ticks += base::TimeDelta::FromMilliseconds(1);
4264 host_impl_.SetCurrentBeginFrameArgs(
4265 CreateBeginFrameArgsForTesting(BEGINFRAME_FROM_HERE, time_ticks));
4266 // UpdateDrawProperties with the occluding layer.
4267 bool update_lcd_text = false;
4268 host_impl_.pending_tree()->UpdateDrawProperties(update_lcd_text);
4270 EXPECT_EQ(5u, pending_layer_->num_tilings());
4272 int occluded_tile_count = 0;
4273 for (size_t i = 0; i < pending_layer_->num_tilings(); ++i) {
4274 PictureLayerTiling* tiling = pending_layer_->tilings()->tiling_at(i);
4275 tiling->UpdateAllTilePrioritiesForTesting();
4276 std::vector<Tile*> tiles = tiling->AllTilesForTesting();
4278 occluded_tile_count = 0;
4279 for (size_t j = 0; j < tiles.size(); ++j) {
4280 if (tiles[j]->is_occluded(PENDING_TREE)) {
4281 gfx::Rect scaled_content_rect = ScaleToEnclosingRect(
4282 tiles[j]->content_rect(), 1.0f / tiles[j]->contents_scale());
4283 EXPECT_GE(scaled_content_rect.x(), occluding_layer_position.x());
4284 occluded_tile_count++;
4288 switch (i) {
4289 case 0:
4290 EXPECT_EQ(occluded_tile_count, 30);
4291 break;
4292 case 1:
4293 EXPECT_EQ(occluded_tile_count, 5);
4294 break;
4295 case 2:
4296 EXPECT_EQ(occluded_tile_count, 4);
4297 break;
4298 case 4:
4299 case 3:
4300 EXPECT_EQ(occluded_tile_count, 2);
4301 break;
4302 default:
4303 NOTREACHED();
4308 TEST_F(OcclusionTrackingPictureLayerImplTest, DifferentOcclusionOnTrees) {
4309 gfx::Size tile_size(102, 102);
4310 gfx::Size layer_bounds(1000, 1000);
4311 gfx::Size viewport_size(1000, 1000);
4312 gfx::Point occluding_layer_position(310, 0);
4313 gfx::Rect invalidation_rect(230, 230, 102, 102);
4315 scoped_refptr<FakePicturePileImpl> pending_pile =
4316 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
4317 scoped_refptr<FakePicturePileImpl> active_pile =
4318 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
4320 host_impl_.SetViewportSize(viewport_size);
4321 SetupPendingTree(active_pile);
4323 // Partially occlude the active layer.
4324 pending_layer_->AddChild(LayerImpl::Create(host_impl_.pending_tree(), 2));
4325 LayerImpl* layer1 = pending_layer_->children()[0];
4326 layer1->SetBounds(layer_bounds);
4327 layer1->SetContentBounds(layer_bounds);
4328 layer1->SetDrawsContent(true);
4329 layer1->SetContentsOpaque(true);
4330 layer1->SetPosition(occluding_layer_position);
4332 ActivateTree();
4334 // Partially invalidate the pending layer.
4335 SetupPendingTreeWithInvalidation(pending_pile, invalidation_rect);
4337 for (size_t i = 0; i < pending_layer_->num_tilings(); ++i) {
4338 PictureLayerTiling* tiling = pending_layer_->tilings()->tiling_at(i);
4339 tiling->UpdateAllTilePrioritiesForTesting();
4341 for (PictureLayerTiling::CoverageIterator iter(
4342 tiling,
4343 pending_layer_->contents_scale_x(),
4344 gfx::Rect(layer_bounds));
4345 iter;
4346 ++iter) {
4347 if (!*iter)
4348 continue;
4349 const Tile* tile = *iter;
4351 // All tiles are unoccluded on the pending tree.
4352 EXPECT_FALSE(tile->is_occluded(PENDING_TREE));
4354 Tile* twin_tile = pending_layer_->GetPendingOrActiveTwinTiling(tiling)
4355 ->TileAt(iter.i(), iter.j());
4356 gfx::Rect scaled_content_rect = ScaleToEnclosingRect(
4357 tile->content_rect(), 1.0f / tile->contents_scale());
4359 if (scaled_content_rect.Intersects(invalidation_rect)) {
4360 // Tiles inside the invalidation rect are only on the pending tree.
4361 EXPECT_NE(tile, twin_tile);
4363 // Unshared tiles should be unoccluded on the active tree by default.
4364 EXPECT_FALSE(tile->is_occluded(ACTIVE_TREE));
4365 } else {
4366 // Tiles outside the invalidation rect are shared between both trees.
4367 EXPECT_EQ(tile, twin_tile);
4368 // Shared tiles are occluded on the active tree iff they lie beneath the
4369 // occluding layer.
4370 EXPECT_EQ(tile->is_occluded(ACTIVE_TREE),
4371 scaled_content_rect.x() >= occluding_layer_position.x());
4376 for (size_t i = 0; i < active_layer_->num_tilings(); ++i) {
4377 PictureLayerTiling* tiling = active_layer_->tilings()->tiling_at(i);
4379 for (PictureLayerTiling::CoverageIterator iter(
4380 tiling,
4381 active_layer_->contents_scale_x(),
4382 gfx::Rect(layer_bounds));
4383 iter;
4384 ++iter) {
4385 if (!*iter)
4386 continue;
4387 const Tile* tile = *iter;
4389 Tile* twin_tile = active_layer_->GetPendingOrActiveTwinTiling(tiling)
4390 ->TileAt(iter.i(), iter.j());
4391 gfx::Rect scaled_content_rect = ScaleToEnclosingRect(
4392 tile->content_rect(), 1.0f / tile->contents_scale());
4394 // Since we've already checked the shared tiles, only consider tiles in
4395 // the invalidation rect.
4396 if (scaled_content_rect.Intersects(invalidation_rect)) {
4397 // Tiles inside the invalidation rect are only on the active tree.
4398 EXPECT_NE(tile, twin_tile);
4400 // Unshared tiles should be unoccluded on the pending tree by default.
4401 EXPECT_FALSE(tile->is_occluded(PENDING_TREE));
4403 // Unshared tiles are occluded on the active tree iff they lie beneath
4404 // the occluding layer.
4405 EXPECT_EQ(tile->is_occluded(ACTIVE_TREE),
4406 scaled_content_rect.x() >= occluding_layer_position.x());
4412 TEST_F(OcclusionTrackingPictureLayerImplTest,
4413 OccludedTilesConsideredDuringEviction) {
4414 base::TimeTicks time_ticks;
4415 time_ticks += base::TimeDelta::FromMilliseconds(1);
4416 host_impl_.SetCurrentBeginFrameArgs(
4417 CreateBeginFrameArgsForTesting(BEGINFRAME_FROM_HERE, time_ticks));
4419 gfx::Size tile_size(102, 102);
4420 gfx::Size layer_bounds(1000, 1000);
4421 gfx::Size viewport_size(1000, 1000);
4422 gfx::Point pending_occluding_layer_position(310, 0);
4423 gfx::Point active_occluding_layer_position(0, 310);
4424 gfx::Rect invalidation_rect(230, 230, 102, 102);
4426 host_impl_.SetViewportSize(viewport_size);
4427 host_impl_.SetDeviceScaleFactor(2.f);
4429 scoped_refptr<FakePicturePileImpl> pending_pile =
4430 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
4431 scoped_refptr<FakePicturePileImpl> active_pile =
4432 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
4434 SetupPendingTreeWithFixedTileSize(active_pile, tile_size, Region());
4436 // Partially occlude the active layer.
4437 pending_layer_->AddChild(LayerImpl::Create(host_impl_.pending_tree(), 2));
4438 LayerImpl* active_occluding_layer = pending_layer_->children()[0];
4439 active_occluding_layer->SetBounds(layer_bounds);
4440 active_occluding_layer->SetContentBounds(layer_bounds);
4441 active_occluding_layer->SetDrawsContent(true);
4442 active_occluding_layer->SetContentsOpaque(true);
4443 active_occluding_layer->SetPosition(active_occluding_layer_position);
4445 ActivateTree();
4447 // Partially invalidate the pending layer. Tiles inside the invalidation rect
4448 // are not shared between trees.
4449 SetupPendingTreeWithFixedTileSize(pending_pile, tile_size, invalidation_rect);
4451 // Partially occlude the pending layer in a different way.
4452 pending_layer_->AddChild(LayerImpl::Create(host_impl_.pending_tree(), 3));
4453 LayerImpl* pending_occluding_layer = pending_layer_->children()[0];
4454 pending_occluding_layer->SetBounds(layer_bounds);
4455 pending_occluding_layer->SetContentBounds(layer_bounds);
4456 pending_occluding_layer->SetDrawsContent(true);
4457 pending_occluding_layer->SetContentsOpaque(true);
4458 pending_occluding_layer->SetPosition(pending_occluding_layer_position);
4460 EXPECT_EQ(2u, pending_layer_->num_tilings());
4461 EXPECT_EQ(2u, active_layer_->num_tilings());
4463 time_ticks += base::TimeDelta::FromMilliseconds(1);
4464 host_impl_.SetCurrentBeginFrameArgs(
4465 CreateBeginFrameArgsForTesting(BEGINFRAME_FROM_HERE, time_ticks));
4466 // UpdateDrawProperties with the occluding layer.
4467 bool update_lcd_text = false;
4468 host_impl_.pending_tree()->UpdateDrawProperties(update_lcd_text);
4470 // The expected number of occluded tiles on each of the 2 tilings for each of
4471 // the 3 tree priorities.
4472 size_t expected_occluded_tile_count_on_both[] = {9u, 1u};
4473 size_t expected_occluded_tile_count_on_active[] = {30u, 3u};
4474 size_t expected_occluded_tile_count_on_pending[] = {30u, 3u};
4476 size_t total_expected_occluded_tile_count_on_trees[] = {33u, 33u};
4478 // Verify number of occluded tiles on the pending layer for each tiling.
4479 for (size_t i = 0; i < pending_layer_->num_tilings(); ++i) {
4480 PictureLayerTiling* tiling = pending_layer_->tilings()->tiling_at(i);
4481 tiling->UpdateAllTilePrioritiesForTesting();
4483 size_t occluded_tile_count_on_pending = 0u;
4484 size_t occluded_tile_count_on_active = 0u;
4485 size_t occluded_tile_count_on_both = 0u;
4486 for (PictureLayerTiling::CoverageIterator iter(tiling, 1.f,
4487 gfx::Rect(layer_bounds));
4488 iter; ++iter) {
4489 Tile* tile = *iter;
4491 if (!tile)
4492 continue;
4493 if (tile->is_occluded(PENDING_TREE))
4494 occluded_tile_count_on_pending++;
4495 if (tile->is_occluded(ACTIVE_TREE))
4496 occluded_tile_count_on_active++;
4497 if (tile->is_occluded(PENDING_TREE) && tile->is_occluded(ACTIVE_TREE))
4498 occluded_tile_count_on_both++;
4500 EXPECT_EQ(expected_occluded_tile_count_on_pending[i],
4501 occluded_tile_count_on_pending)
4502 << tiling->contents_scale();
4503 EXPECT_EQ(expected_occluded_tile_count_on_active[i],
4504 occluded_tile_count_on_active)
4505 << tiling->contents_scale();
4506 EXPECT_EQ(expected_occluded_tile_count_on_both[i],
4507 occluded_tile_count_on_both)
4508 << tiling->contents_scale();
4511 // Verify number of occluded tiles on the active layer for each tiling.
4512 for (size_t i = 0; i < active_layer_->num_tilings(); ++i) {
4513 PictureLayerTiling* tiling = active_layer_->tilings()->tiling_at(i);
4514 tiling->UpdateAllTilePrioritiesForTesting();
4516 size_t occluded_tile_count_on_pending = 0u;
4517 size_t occluded_tile_count_on_active = 0u;
4518 size_t occluded_tile_count_on_both = 0u;
4519 for (PictureLayerTiling::CoverageIterator iter(
4520 tiling,
4521 pending_layer_->contents_scale_x(),
4522 gfx::Rect(layer_bounds));
4523 iter;
4524 ++iter) {
4525 Tile* tile = *iter;
4527 if (!tile)
4528 continue;
4529 if (tile->is_occluded(PENDING_TREE))
4530 occluded_tile_count_on_pending++;
4531 if (tile->is_occluded(ACTIVE_TREE))
4532 occluded_tile_count_on_active++;
4533 if (tile->is_occluded(PENDING_TREE) && tile->is_occluded(ACTIVE_TREE))
4534 occluded_tile_count_on_both++;
4536 EXPECT_EQ(expected_occluded_tile_count_on_pending[i],
4537 occluded_tile_count_on_pending)
4538 << i;
4539 EXPECT_EQ(expected_occluded_tile_count_on_active[i],
4540 occluded_tile_count_on_active)
4541 << i;
4542 EXPECT_EQ(expected_occluded_tile_count_on_both[i],
4543 occluded_tile_count_on_both)
4544 << i;
4547 std::vector<Tile*> all_tiles;
4548 for (size_t i = 0; i < pending_layer_->num_tilings(); ++i) {
4549 PictureLayerTiling* tiling = pending_layer_->tilings()->tiling_at(i);
4550 std::vector<Tile*> tiles = tiling->AllTilesForTesting();
4551 all_tiles.insert(all_tiles.end(), tiles.begin(), tiles.end());
4554 host_impl_.tile_manager()->InitializeTilesWithResourcesForTesting(all_tiles);
4556 VerifyEvictionConsidersOcclusion(
4557 pending_layer_, active_layer_, PENDING_TREE,
4558 total_expected_occluded_tile_count_on_trees[PENDING_TREE]);
4559 VerifyEvictionConsidersOcclusion(
4560 active_layer_, pending_layer_, ACTIVE_TREE,
4561 total_expected_occluded_tile_count_on_trees[ACTIVE_TREE]);
4563 // Repeat the tests without valid active tree priorities.
4564 active_layer_->set_has_valid_tile_priorities(false);
4565 VerifyEvictionConsidersOcclusion(
4566 pending_layer_, active_layer_, PENDING_TREE,
4567 total_expected_occluded_tile_count_on_trees[PENDING_TREE]);
4568 VerifyEvictionConsidersOcclusion(
4569 active_layer_, pending_layer_, ACTIVE_TREE, 0u);
4570 active_layer_->set_has_valid_tile_priorities(true);
4572 // Repeat the tests without valid pending tree priorities.
4573 pending_layer_->set_has_valid_tile_priorities(false);
4574 VerifyEvictionConsidersOcclusion(
4575 active_layer_, pending_layer_, ACTIVE_TREE,
4576 total_expected_occluded_tile_count_on_trees[ACTIVE_TREE]);
4577 VerifyEvictionConsidersOcclusion(
4578 pending_layer_, active_layer_, PENDING_TREE, 0u);
4579 pending_layer_->set_has_valid_tile_priorities(true);
4582 TEST_F(PictureLayerImplTest, PendingOrActiveTwinLayer) {
4583 gfx::Size tile_size(102, 102);
4584 gfx::Size layer_bounds(1000, 1000);
4586 scoped_refptr<FakePicturePileImpl> pile =
4587 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
4588 SetupPendingTree(pile);
4589 EXPECT_FALSE(pending_layer_->GetPendingOrActiveTwinLayer());
4591 ActivateTree();
4592 EXPECT_FALSE(active_layer_->GetPendingOrActiveTwinLayer());
4594 SetupPendingTree(pile);
4595 EXPECT_TRUE(pending_layer_->GetPendingOrActiveTwinLayer());
4596 EXPECT_TRUE(active_layer_->GetPendingOrActiveTwinLayer());
4597 EXPECT_EQ(pending_layer_, active_layer_->GetPendingOrActiveTwinLayer());
4598 EXPECT_EQ(active_layer_, pending_layer_->GetPendingOrActiveTwinLayer());
4600 ActivateTree();
4601 EXPECT_FALSE(active_layer_->GetPendingOrActiveTwinLayer());
4603 // Make an empty pending tree.
4604 host_impl_.CreatePendingTree();
4605 host_impl_.pending_tree()->DetachLayerTree();
4606 EXPECT_FALSE(active_layer_->GetPendingOrActiveTwinLayer());
4609 TEST_F(PictureLayerImplTest, RecycledTwinLayer) {
4610 gfx::Size tile_size(102, 102);
4611 gfx::Size layer_bounds(1000, 1000);
4613 scoped_refptr<FakePicturePileImpl> pile =
4614 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
4615 SetupPendingTree(pile);
4616 EXPECT_FALSE(pending_layer_->GetRecycledTwinLayer());
4618 ActivateTree();
4619 EXPECT_TRUE(active_layer_->GetRecycledTwinLayer());
4620 EXPECT_EQ(old_pending_layer_, active_layer_->GetRecycledTwinLayer());
4622 SetupPendingTree(pile);
4623 EXPECT_FALSE(pending_layer_->GetRecycledTwinLayer());
4624 EXPECT_FALSE(active_layer_->GetRecycledTwinLayer());
4626 ActivateTree();
4627 EXPECT_TRUE(active_layer_->GetRecycledTwinLayer());
4628 EXPECT_EQ(old_pending_layer_, active_layer_->GetRecycledTwinLayer());
4630 // Make an empty pending tree.
4631 host_impl_.CreatePendingTree();
4632 host_impl_.pending_tree()->DetachLayerTree();
4633 EXPECT_FALSE(active_layer_->GetRecycledTwinLayer());
4636 void PictureLayerImplTest::TestQuadsForSolidColor(bool test_for_solid) {
4637 base::TimeTicks time_ticks;
4638 time_ticks += base::TimeDelta::FromMilliseconds(1);
4639 host_impl_.SetCurrentBeginFrameArgs(
4640 CreateBeginFrameArgsForTesting(BEGINFRAME_FROM_HERE, time_ticks));
4642 gfx::Size tile_size(100, 100);
4643 gfx::Size layer_bounds(200, 200);
4644 gfx::Rect layer_rect(layer_bounds);
4646 FakeContentLayerClient client;
4647 scoped_refptr<PictureLayer> layer = PictureLayer::Create(&client);
4648 FakeLayerTreeHostClient host_client(FakeLayerTreeHostClient::DIRECT_3D);
4649 scoped_ptr<FakeLayerTreeHost> host = FakeLayerTreeHost::Create(&host_client);
4650 host->SetRootLayer(layer);
4651 RecordingSource* recording_source = layer->GetRecordingSourceForTesting();
4653 int frame_number = 0;
4655 client.set_fill_with_nonsolid_color(!test_for_solid);
4657 Region invalidation(layer_rect);
4658 recording_source->UpdateAndExpandInvalidation(
4659 &client, &invalidation, layer_bounds, layer_rect, frame_number++,
4660 RecordingSource::RECORD_NORMALLY);
4662 scoped_refptr<RasterSource> pending_raster_source =
4663 recording_source->CreateRasterSource(true);
4665 SetupPendingTreeWithFixedTileSize(pending_raster_source, tile_size, Region());
4666 ActivateTree();
4668 if (test_for_solid) {
4669 EXPECT_EQ(0u, active_layer_->tilings()->num_tilings());
4670 } else {
4671 ASSERT_TRUE(active_layer_->tilings());
4672 ASSERT_GT(active_layer_->tilings()->num_tilings(), 0u);
4673 std::vector<Tile*> tiles =
4674 active_layer_->tilings()->tiling_at(0)->AllTilesForTesting();
4675 EXPECT_FALSE(tiles.empty());
4676 host_impl_.tile_manager()->InitializeTilesWithResourcesForTesting(tiles);
4679 scoped_ptr<RenderPass> render_pass = RenderPass::Create();
4680 AppendQuadsData data;
4681 active_layer_->WillDraw(DRAW_MODE_SOFTWARE, nullptr);
4682 active_layer_->AppendQuads(render_pass.get(), &data);
4683 active_layer_->DidDraw(nullptr);
4685 DrawQuad::Material expected = test_for_solid
4686 ? DrawQuad::Material::SOLID_COLOR
4687 : DrawQuad::Material::TILED_CONTENT;
4688 EXPECT_EQ(expected, render_pass->quad_list.front()->material);
4691 TEST_F(PictureLayerImplTest, DrawSolidQuads) {
4692 TestQuadsForSolidColor(true);
4695 TEST_F(PictureLayerImplTest, DrawNonSolidQuads) {
4696 TestQuadsForSolidColor(false);
4699 TEST_F(PictureLayerImplTest, NonSolidToSolidNoTilings) {
4700 base::TimeTicks time_ticks;
4701 time_ticks += base::TimeDelta::FromMilliseconds(1);
4702 host_impl_.SetCurrentBeginFrameArgs(
4703 CreateBeginFrameArgsForTesting(BEGINFRAME_FROM_HERE, time_ticks));
4705 gfx::Size tile_size(100, 100);
4706 gfx::Size layer_bounds(200, 200);
4707 gfx::Rect layer_rect(layer_bounds);
4709 FakeContentLayerClient client;
4710 scoped_refptr<PictureLayer> layer = PictureLayer::Create(&client);
4711 FakeLayerTreeHostClient host_client(FakeLayerTreeHostClient::DIRECT_3D);
4712 scoped_ptr<FakeLayerTreeHost> host = FakeLayerTreeHost::Create(&host_client);
4713 host->SetRootLayer(layer);
4714 RecordingSource* recording_source = layer->GetRecordingSourceForTesting();
4716 int frame_number = 0;
4718 client.set_fill_with_nonsolid_color(true);
4720 Region invalidation1(layer_rect);
4721 recording_source->UpdateAndExpandInvalidation(
4722 &client, &invalidation1, layer_bounds, layer_rect, frame_number++,
4723 RecordingSource::RECORD_NORMALLY);
4725 scoped_refptr<RasterSource> raster_source1 =
4726 recording_source->CreateRasterSource(true);
4728 SetupPendingTree(raster_source1);
4729 ActivateTree();
4730 bool update_lcd_text = false;
4731 host_impl_.active_tree()->UpdateDrawProperties(update_lcd_text);
4733 // We've started with a solid layer that contains some tilings.
4734 ASSERT_TRUE(active_layer_->tilings());
4735 EXPECT_NE(0u, active_layer_->tilings()->num_tilings());
4737 client.set_fill_with_nonsolid_color(false);
4739 Region invalidation2(layer_rect);
4740 recording_source->UpdateAndExpandInvalidation(
4741 &client, &invalidation2, layer_bounds, layer_rect, frame_number++,
4742 RecordingSource::RECORD_NORMALLY);
4744 scoped_refptr<RasterSource> raster_source2 =
4745 recording_source->CreateRasterSource(true);
4747 SetupPendingTree(raster_source2);
4748 ActivateTree();
4750 // We've switched to a solid color, so we should end up with no tilings.
4751 ASSERT_TRUE(active_layer_->tilings());
4752 EXPECT_EQ(0u, active_layer_->tilings()->num_tilings());
4755 TEST_F(PictureLayerImplTest, ChangeInViewportAllowsTilingUpdates) {
4756 base::TimeTicks time_ticks;
4757 time_ticks += base::TimeDelta::FromMilliseconds(1);
4758 host_impl_.SetCurrentBeginFrameArgs(
4759 CreateBeginFrameArgsForTesting(BEGINFRAME_FROM_HERE, time_ticks));
4761 gfx::Size tile_size(100, 100);
4762 gfx::Size layer_bounds(400, 4000);
4764 scoped_refptr<FakePicturePileImpl> pending_pile =
4765 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
4766 scoped_refptr<FakePicturePileImpl> active_pile =
4767 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
4769 SetupTrees(pending_pile, active_pile);
4771 Region invalidation;
4772 gfx::Rect viewport = gfx::Rect(0, 0, 100, 100);
4773 gfx::Transform transform;
4775 host_impl_.SetRequiresHighResToDraw();
4777 // Update tiles.
4778 pending_layer_->draw_properties().visible_content_rect = viewport;
4779 pending_layer_->draw_properties().screen_space_transform = transform;
4780 SetupDrawPropertiesAndUpdateTiles(pending_layer_, 1.f, 1.f, 1.f, 1.f, false);
4781 pending_layer_->HighResTiling()->UpdateAllTilePrioritiesForTesting();
4783 // Ensure we can't activate.
4784 EXPECT_FALSE(host_impl_.tile_manager()->IsReadyToActivate());
4786 // Now in the same frame, move the viewport (this can happen during
4787 // animation).
4788 viewport = gfx::Rect(0, 2000, 100, 100);
4790 // Update tiles.
4791 pending_layer_->draw_properties().visible_content_rect = viewport;
4792 pending_layer_->draw_properties().screen_space_transform = transform;
4793 SetupDrawPropertiesAndUpdateTiles(pending_layer_, 1.f, 1.f, 1.f, 1.f, false);
4794 pending_layer_->HighResTiling()->UpdateAllTilePrioritiesForTesting();
4796 // Make sure all viewport tiles (viewport from the tiling) are ready to draw.
4797 std::vector<Tile*> tiles;
4798 for (PictureLayerTiling::CoverageIterator iter(
4799 pending_layer_->HighResTiling(),
4800 1.f,
4801 pending_layer_->HighResTiling()->GetCurrentVisibleRectForTesting());
4802 iter;
4803 ++iter) {
4804 if (*iter)
4805 tiles.push_back(*iter);
4808 host_impl_.tile_manager()->InitializeTilesWithResourcesForTesting(tiles);
4810 // Ensure we can activate.
4811 EXPECT_TRUE(host_impl_.tile_manager()->IsReadyToActivate());
4814 TEST_F(PictureLayerImplTest, CloneMissingRecordings) {
4815 gfx::Size tile_size(100, 100);
4816 gfx::Size layer_bounds(400, 400);
4818 scoped_refptr<FakePicturePileImpl> filled_pile =
4819 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
4821 scoped_ptr<FakePicturePile> partial_recording =
4822 FakePicturePile::CreateEmptyPile(tile_size, layer_bounds);
4823 for (int i = 1; i < partial_recording->tiling().num_tiles_x(); ++i) {
4824 for (int j = 1; j < partial_recording->tiling().num_tiles_y(); ++j)
4825 partial_recording->AddRecordingAt(i, j);
4827 scoped_refptr<FakePicturePileImpl> partial_pile =
4828 FakePicturePileImpl::CreateFromPile(partial_recording.get(), nullptr);
4830 SetupPendingTreeWithFixedTileSize(filled_pile, tile_size, Region());
4831 ActivateTree();
4833 PictureLayerTiling* pending_tiling = old_pending_layer_->HighResTiling();
4834 PictureLayerTiling* active_tiling = active_layer_->HighResTiling();
4836 // We should have all tiles in both tile sets.
4837 EXPECT_EQ(5u * 5u, pending_tiling->AllTilesForTesting().size());
4838 EXPECT_EQ(5u * 5u, active_tiling->AllTilesForTesting().size());
4840 // Now put a partially-recorded pile on the pending tree (and invalidate
4841 // everything, since the main thread PicturePile will invalidate dropped
4842 // recordings). This will cause us to be missing some tiles.
4843 SetupPendingTreeWithFixedTileSize(partial_pile, tile_size,
4844 Region(gfx::Rect(layer_bounds)));
4845 EXPECT_EQ(3u * 3u, pending_tiling->AllTilesForTesting().size());
4846 EXPECT_FALSE(pending_tiling->TileAt(0, 0));
4847 EXPECT_FALSE(pending_tiling->TileAt(1, 1));
4848 EXPECT_TRUE(pending_tiling->TileAt(2, 2));
4850 // Active is not affected yet.
4851 EXPECT_EQ(5u * 5u, active_tiling->AllTilesForTesting().size());
4853 // Activate the tree. The same tiles go missing on the active tree.
4854 ActivateTree();
4855 EXPECT_EQ(3u * 3u, active_tiling->AllTilesForTesting().size());
4856 EXPECT_FALSE(active_tiling->TileAt(0, 0));
4857 EXPECT_FALSE(active_tiling->TileAt(1, 1));
4858 EXPECT_TRUE(active_tiling->TileAt(2, 2));
4860 // Now put a full recording on the pending tree again. We'll get all our tiles
4861 // back.
4862 SetupPendingTreeWithFixedTileSize(filled_pile, tile_size,
4863 Region(gfx::Rect(layer_bounds)));
4864 EXPECT_EQ(5u * 5u, pending_tiling->AllTilesForTesting().size());
4866 // Active is not affected yet.
4867 EXPECT_EQ(3u * 3u, active_tiling->AllTilesForTesting().size());
4869 // Activate the tree. The tiles are created and shared on the active tree.
4870 ActivateTree();
4871 EXPECT_EQ(5u * 5u, active_tiling->AllTilesForTesting().size());
4872 EXPECT_TRUE(active_tiling->TileAt(0, 0)->is_shared());
4873 EXPECT_TRUE(active_tiling->TileAt(1, 1)->is_shared());
4874 EXPECT_TRUE(active_tiling->TileAt(2, 2)->is_shared());
4877 class TileSizeSettings : public ImplSidePaintingSettings {
4878 public:
4879 TileSizeSettings() {
4880 default_tile_size = gfx::Size(100, 100);
4881 max_untiled_layer_size = gfx::Size(200, 200);
4885 class TileSizeTest : public PictureLayerImplTest {
4886 public:
4887 TileSizeTest() : PictureLayerImplTest(TileSizeSettings()) {}
4890 TEST_F(TileSizeTest, TileSizes) {
4891 host_impl_.CreatePendingTree();
4893 LayerTreeImpl* pending_tree = host_impl_.pending_tree();
4894 scoped_ptr<FakePictureLayerImpl> layer =
4895 FakePictureLayerImpl::Create(pending_tree, id_);
4897 host_impl_.SetViewportSize(gfx::Size(1000, 1000));
4898 gfx::Size result;
4900 host_impl_.SetUseGpuRasterization(false);
4902 // Default tile-size for large layers.
4903 result = layer->CalculateTileSize(gfx::Size(10000, 10000));
4904 EXPECT_EQ(result.width(), 100);
4905 EXPECT_EQ(result.height(), 100);
4906 // Don't tile and round-up, when under max_untiled_layer_size.
4907 result = layer->CalculateTileSize(gfx::Size(42, 42));
4908 EXPECT_EQ(result.width(), 64);
4909 EXPECT_EQ(result.height(), 64);
4910 result = layer->CalculateTileSize(gfx::Size(191, 191));
4911 EXPECT_EQ(result.width(), 192);
4912 EXPECT_EQ(result.height(), 192);
4913 result = layer->CalculateTileSize(gfx::Size(199, 199));
4914 EXPECT_EQ(result.width(), 200);
4915 EXPECT_EQ(result.height(), 200);
4917 // Gpu-rasterization uses 25% viewport-height tiles.
4918 // The +2's below are for border texels.
4919 host_impl_.SetUseGpuRasterization(true);
4920 host_impl_.SetViewportSize(gfx::Size(2000, 2000));
4922 layer->set_gpu_raster_max_texture_size(host_impl_.device_viewport_size());
4923 result = layer->CalculateTileSize(gfx::Size(10000, 10000));
4924 EXPECT_EQ(result.width(), 2000);
4925 EXPECT_EQ(result.height(), 500 + 2);
4927 // Clamp and round-up, when smaller than viewport.
4928 // Tile-height doubles to 50% when width shrinks to <= 50%.
4929 host_impl_.SetViewportSize(gfx::Size(1000, 1000));
4930 layer->set_gpu_raster_max_texture_size(host_impl_.device_viewport_size());
4931 result = layer->CalculateTileSize(gfx::Size(447, 10000));
4932 EXPECT_EQ(result.width(), 448);
4933 EXPECT_EQ(result.height(), 500 + 2);
4935 // Largest layer is 50% of viewport width (rounded up), and
4936 // 50% of viewport in height.
4937 result = layer->CalculateTileSize(gfx::Size(447, 400));
4938 EXPECT_EQ(result.width(), 448);
4939 EXPECT_EQ(result.height(), 448);
4940 result = layer->CalculateTileSize(gfx::Size(500, 499));
4941 EXPECT_EQ(result.width(), 512);
4942 EXPECT_EQ(result.height(), 500 + 2);
4945 } // namespace
4946 } // namespace cc