Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(245)

Side by Side Diff: cc/layers/picture_layer_impl_unittest.cc

Issue 1057283003: Remove parts of //cc we aren't using (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: Created 5 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « cc/layers/picture_layer_impl_perftest.cc ('k') | cc/layers/picture_layer_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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.
4
5 #include "cc/layers/picture_layer_impl.h"
6
7 #include <algorithm>
8 #include <limits>
9 #include <set>
10 #include <utility>
11
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"
36
37 namespace cc {
38 namespace {
39
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)
45
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)
51
52 class MockCanvas : public SkCanvas {
53 public:
54 explicit MockCanvas(int w, int h) : SkCanvas(w, h) {}
55
56 void onDrawRect(const SkRect& rect, const SkPaint& paint) override {
57 // Capture calls before SkCanvas quickReject() kicks in.
58 rects_.push_back(rect);
59 }
60
61 std::vector<SkRect> rects_;
62 };
63
64 class NoLowResTilingsSettings : public ImplSidePaintingSettings {};
65
66 class LowResTilingsSettings : public ImplSidePaintingSettings {
67 public:
68 LowResTilingsSettings() { create_low_res_tiling = true; }
69 };
70
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));
85 }
86
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));
96 }
97
98 ~PictureLayerImplTest() override {}
99
100 void SetUp() override { InitializeRenderer(); }
101
102 virtual void InitializeRenderer() {
103 host_impl_.InitializeRenderer(FakeOutputSurface::Create3d());
104 }
105
106 void SetupDefaultTrees(const gfx::Size& layer_bounds) {
107 gfx::Size tile_size(100, 100);
108
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);
113
114 SetupTrees(pending_pile, active_pile);
115 }
116
117 void SetupDefaultTreesWithInvalidation(const gfx::Size& layer_bounds,
118 const Region& invalidation) {
119 gfx::Size tile_size(100, 100);
120
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);
125
126 SetupTreesWithInvalidation(pending_pile, active_pile, invalidation);
127 }
128
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_));
137
138 bool update_lcd_text = false;
139 host_impl_.active_tree()->UpdateDrawProperties(update_lcd_text);
140 }
141
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);
146
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);
151
152 SetupTreesWithFixedTileSize(pending_pile, active_pile, tile_size,
153 invalidation);
154 }
155
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());
162 }
163
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);
170 }
171
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);
179 }
180
181 void SetupPendingTree(scoped_refptr<RasterSource> raster_source) {
182 SetupPendingTreeInternal(raster_source, gfx::Size(), Region());
183 }
184
185 void SetupPendingTreeWithInvalidation(
186 scoped_refptr<RasterSource> raster_source,
187 const Region& invalidation) {
188 SetupPendingTreeInternal(raster_source, gfx::Size(), invalidation);
189 }
190
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);
196 }
197
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();
204
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);
220 }
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);
226
227 pending_root->AddChild(pending_layer.Pass());
228 pending_tree->SetRootLayer(pending_root.Pass());
229
230 pending_layer_ = static_cast<FakePictureLayerImpl*>(
231 host_impl_.pending_tree()->LayerById(id_));
232
233 // Add tilings/tiles for the layer.
234 bool update_lcd_text = false;
235 host_impl_.pending_tree()->UpdateDrawProperties(update_lcd_text);
236 }
237
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);
253 }
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());
265 }
266 }
267
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);
279
280 SetupDrawPropertiesAndUpdateTiles(active_layer_,
281 contents_scale,
282 device_scale_factor,
283 page_scale_factor,
284 maximum_animation_contents_scale,
285 animating_transform);
286 }
287
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());
293
294 active_layer_->ReleaseResources();
295 EXPECT_FALSE(active_layer_->tilings());
296 active_layer_->RecreateResources();
297 EXPECT_EQ(0u, active_layer_->tilings()->num_tilings());
298 }
299
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);
305 }
306
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);
312 }
313
314 protected:
315 void TestQuadsForSolidColor(bool test_for_solid);
316
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_;
326
327 private:
328 DISALLOW_COPY_AND_ASSIGN(PictureLayerImplTest);
329 };
330
331 class NoLowResPictureLayerImplTest : public PictureLayerImplTest {
332 public:
333 NoLowResPictureLayerImplTest()
334 : PictureLayerImplTest(NoLowResTilingsSettings()) {}
335 };
336
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);
342
343 scoped_refptr<FakePicturePileImpl> pending_pile =
344 FakePicturePileImpl::CreateFilledPile(layer_size, layer_size);
345
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);
350
351 SetupTrees(pending_pile, active_pile);
352
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));
365 }
366
367 // Force re-raster with newly injected content
368 active_recording->RemoveRecordingAt(0, 0);
369 active_recording->AddRecordingAt(0, 0);
370
371 scoped_refptr<FakePicturePileImpl> updated_active_pile =
372 FakePicturePileImpl::CreateFromPile(active_recording.get(), nullptr);
373
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);
379
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++;
387 }
388 }
389
390 TEST_F(PictureLayerImplTest, CloneNoInvalidation) {
391 gfx::Size tile_size(100, 100);
392 gfx::Size layer_bounds(400, 400);
393
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);
398
399 SetupTreesWithInvalidation(pending_pile, active_pile, Region());
400
401 EXPECT_EQ(pending_layer_->tilings()->num_tilings(),
402 active_layer_->tilings()->num_tilings());
403
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());
408 }
409
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);
417
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);
422
423 SetupTreesWithInvalidation(pending_pile, active_pile, Region());
424
425 SetupDrawPropertiesAndUpdateTiles(active_layer_, 1.f, 1.f, 1.f, 1.f, false);
426
427 time_ticks += base::TimeDelta::FromMilliseconds(200);
428 host_impl_.SetCurrentBeginFrameArgs(
429 CreateBeginFrameArgsForTesting(BEGINFRAME_FROM_HERE, time_ticks));
430
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;
437
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);
446
447 gfx::Rect viewport_rect_for_tile_priority_in_view_space =
448 viewport_rect_for_tile_priority;
449
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()));
460 }
461
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));
469
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);
480
481 gfx::Transform screen_to_view(gfx::Transform::kSkipInitialization);
482 bool success = transform_for_tile_priority.GetInverse(&screen_to_view);
483 EXPECT_TRUE(success);
484
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));
492
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()));
502 }
503 }
504
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));
510
511 gfx::Size tile_size(100, 100);
512 gfx::Size layer_bounds(400, 400);
513
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);
518
519 SetupTreesWithInvalidation(pending_pile, active_pile, Region());
520
521 SetupDrawPropertiesAndUpdateTiles(active_layer_, 1.f, 1.f, 1.f, 1.f, false);
522
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);
538
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();
544
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);
562
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());
569
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);
582
583 EXPECT_TRANSFORMATION_MATRIX_EQ(transform,
584 active_layer_->screen_space_transform());
585 EXPECT_EQ(viewport, active_layer_->visible_rect_for_tile_priority());
586 }
587
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);
595
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);
600
601 SetupTreesWithInvalidation(pending_pile, active_pile, Region());
602
603 SetupDrawPropertiesAndUpdateTiles(active_layer_, 1.f, 1.f, 1.f, 1.f, false);
604
605 time_ticks += base::TimeDelta::FromMilliseconds(200);
606 host_impl_.SetCurrentBeginFrameArgs(
607 CreateBeginFrameArgsForTesting(BEGINFRAME_FROM_HERE, time_ticks));
608
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;
613
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);
619
620 EXPECT_EQ(viewport_rect_for_tile_priority,
621 active_layer_->viewport_rect_for_tile_priority_in_content_space());
622
623 time_ticks += base::TimeDelta::FromMilliseconds(200);
624 host_impl_.SetCurrentBeginFrameArgs(
625 CreateBeginFrameArgsForTesting(BEGINFRAME_FROM_HERE, time_ticks));
626
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);
631
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);
637
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());
642 }
643
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);
648
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);
655
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();
661
662 // Ensure UpdateTiles won't remove any tilings.
663 active_layer_->MarkAllTilingsUsed();
664
665 // Then setup a new pending tree and activate it.
666 SetupTreesWithFixedTileSize(pending_pile, active_pile, gfx::Size(50, 50),
667 layer_invalidation);
668
669 EXPECT_EQ(2u, pending_layer_->num_tilings());
670 EXPECT_EQ(3u, active_layer_->num_tilings());
671
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());
688 }
689 }
690
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());
711 }
712 }
713 }
714
715 TEST_F(PictureLayerImplTest, CloneFullInvalidation) {
716 gfx::Size tile_size(90, 80);
717 gfx::Size layer_bounds(300, 500);
718
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);
723
724 SetupTreesWithInvalidation(pending_pile, active_pile,
725 gfx::Rect(layer_bounds));
726
727 EXPECT_EQ(pending_layer_->tilings()->num_tilings(),
728 active_layer_->tilings()->num_tilings());
729
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());
734 }
735
736 TEST_F(PictureLayerImplTest, UpdateTilesCreatesTilings) {
737 gfx::Size tile_size(400, 400);
738 gfx::Size layer_bounds(1300, 1900);
739
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);
744
745 SetupTrees(pending_pile, active_pile);
746
747 float low_res_factor = host_impl_.settings().low_res_contents_scale_factor;
748 EXPECT_LT(low_res_factor, 1.f);
749
750 active_layer_->ReleaseResources();
751 EXPECT_FALSE(active_layer_->tilings());
752 active_layer_->RecreateResources();
753 EXPECT_EQ(0u, active_layer_->tilings()->num_tilings());
754
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());
766
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());
779
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());
792
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());
806 }
807
808 TEST_F(PictureLayerImplTest, PendingLayerOnlyHasHighAndLowResTiling) {
809 gfx::Size tile_size(400, 400);
810 gfx::Size layer_bounds(1300, 1900);
811
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);
816
817 SetupTrees(pending_pile, active_pile);
818
819 float low_res_factor = host_impl_.settings().low_res_contents_scale_factor;
820 EXPECT_LT(low_res_factor, 1.f);
821
822 pending_layer_->ReleaseResources();
823 EXPECT_FALSE(pending_layer_->tilings());
824 pending_layer_->RecreateResources();
825 EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings());
826
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());
838
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());
851
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());
864
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());
878 }
879
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);
886
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);
891
892 SetupPendingTree(valid_pile);
893 ASSERT_EQ(2u, pending_layer_->tilings()->num_tilings());
894
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());
900
901 ActivateTree();
902 EXPECT_FALSE(active_layer_->CanHaveTilings());
903 ASSERT_EQ(0u, active_layer_->tilings()->num_tilings());
904
905 SetupPendingTree(valid_pile);
906 ASSERT_EQ(2u, pending_layer_->tilings()->num_tilings());
907 ASSERT_EQ(0u, active_layer_->tilings()->num_tilings());
908 }
909
910 TEST_F(PictureLayerImplTest, ZoomOutCrash) {
911 gfx::Size tile_size(400, 400);
912 gfx::Size layer_bounds(1300, 1900);
913
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);
919
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);
929 }
930
931 TEST_F(PictureLayerImplTest, PinchGestureTilings) {
932 gfx::Size tile_size(400, 400);
933 gfx::Size layer_bounds(1300, 1900);
934
935 float low_res_factor = host_impl_.settings().low_res_contents_scale_factor;
936
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);
941
942 // Set up the high and low res tilings before pinch zoom.
943 SetupTrees(pending_pile, active_pile);
944 ResetTilingsAndRasterScales();
945
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);
951
952 // Ensure UpdateTiles won't remove any tilings.
953 active_layer_->MarkAllTilingsUsed();
954
955 // Start a pinch gesture.
956 host_impl_.PinchGestureBegin();
957
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());
968
969 // Ensure UpdateTiles won't remove any tilings.
970 active_layer_->MarkAllTilingsUsed();
971
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());
977
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());
984 }
985
986 TEST_F(PictureLayerImplTest, SnappedTilingDuringZoom) {
987 gfx::Size tile_size(300, 300);
988 gfx::Size layer_bounds(2600, 3800);
989
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);
994
995 SetupTrees(pending_pile, active_pile);
996
997 ResetTilingsAndRasterScales();
998 EXPECT_EQ(0u, active_layer_->tilings()->num_tilings());
999
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());
1007
1008 // Ensure UpdateTiles won't remove any tilings.
1009 active_layer_->MarkAllTilingsUsed();
1010
1011 // Start a pinch gesture.
1012 host_impl_.PinchGestureBegin();
1013
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());
1024
1025 // Ensure UpdateTiles won't remove any tilings.
1026 active_layer_->MarkAllTilingsUsed();
1027
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());
1032
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());
1037
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());
1044 }
1045
1046 TEST_F(PictureLayerImplTest, CleanUpTilings) {
1047 gfx::Size tile_size(400, 400);
1048 gfx::Size layer_bounds(1300, 1900);
1049
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);
1054
1055 std::vector<PictureLayerTiling*> used_tilings;
1056
1057 float low_res_factor = host_impl_.settings().low_res_contents_scale_factor;
1058 EXPECT_LT(low_res_factor, 1.f);
1059
1060 float scale = 1.f;
1061 float page_scale = 1.f;
1062
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());
1066
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();
1071
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());
1076
1077 host_impl_.PinchGestureBegin();
1078
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());
1084
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());
1089
1090 host_impl_.PinchGestureEnd();
1091
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());
1103
1104 // Ensure UpdateTiles won't remove any tilings.
1105 active_layer_->MarkAllTilingsUsed();
1106
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());
1113
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);
1116
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());
1122
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);
1125
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());
1131
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);
1135
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());
1141
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);
1146
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());
1154
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());
1160 }
1161
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());
1169
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;
1176
1177 ResetTilingsAndRasterScales();
1178
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);
1187
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);
1198
1199 // Ensure UpdateTiles won't remove any tilings.
1200 active_layer_->MarkAllTilingsUsed();
1201
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());
1219
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());
1231 }
1232
1233 TEST_F(PictureLayerImplTest, DontAddLowResForSmallLayers) {
1234 gfx::Size layer_bounds(host_impl_.settings().default_tile_size);
1235 gfx::Size tile_size(100, 100);
1236
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);
1241
1242 SetupTrees(pending_pile, active_pile);
1243
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;
1249
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);
1259
1260 ResetTilingsAndRasterScales();
1261
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);
1271
1272 ResetTilingsAndRasterScales();
1273
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);
1286
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);
1294
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);
1300 }
1301
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));
1307
1308 gfx::Size tile_size(100, 100);
1309 gfx::Size layer_bounds(1000, 1000);
1310
1311 scoped_refptr<FakePicturePileImpl> valid_pile =
1312 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1313 SetupPendingTree(valid_pile);
1314
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);
1323
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);
1329
1330 FakePictureLayerImpl* pending_mask =
1331 static_cast<FakePictureLayerImpl*>(pending_layer_->mask_layer());
1332
1333 EXPECT_EQ(1.f, pending_mask->HighResTiling()->contents_scale());
1334 EXPECT_EQ(1u, pending_mask->num_tilings());
1335
1336 host_impl_.tile_manager()->InitializeTilesWithResourcesForTesting(
1337 pending_mask->HighResTiling()->AllTilesForTesting());
1338
1339 ActivateTree();
1340
1341 FakePictureLayerImpl* active_mask =
1342 static_cast<FakePictureLayerImpl*>(active_layer_->mask_layer());
1343
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);
1352
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);
1363
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);
1369
1370 SetupPendingTree(huge_pile);
1371 pending_mask->SetBounds(huge_bounds);
1372 pending_mask->SetContentBounds(huge_bounds);
1373 pending_mask->SetRasterSourceOnPending(huge_pile, Region());
1374
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);
1379
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());
1383
1384 host_impl_.tile_manager()->InitializeTilesWithResourcesForTesting(
1385 pending_mask->HighResTiling()->AllTilesForTesting());
1386
1387 ActivateTree();
1388
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);
1397
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);
1408
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);
1416
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);
1423
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());
1428
1429 EXPECT_FALSE(pending_mask->CanHaveTilings());
1430
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);
1435
1436 EXPECT_EQ(0u, pending_mask->num_tilings());
1437 }
1438
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));
1444
1445 gfx::Size tile_size(100, 100);
1446 gfx::Size layer_bounds(1000, 1000);
1447
1448 host_impl_.SetDeviceScaleFactor(1.3f);
1449
1450 scoped_refptr<FakePicturePileImpl> valid_pile =
1451 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1452 SetupPendingTree(valid_pile);
1453
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);
1462
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);
1468
1469 FakePictureLayerImpl* pending_mask =
1470 static_cast<FakePictureLayerImpl*>(pending_layer_->mask_layer());
1471
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());
1475
1476 host_impl_.tile_manager()->InitializeTilesWithResourcesForTesting(
1477 pending_mask->HighResTiling()->AllTilesForTesting());
1478
1479 ActivateTree();
1480
1481 FakePictureLayerImpl* active_mask =
1482 static_cast<FakePictureLayerImpl*>(active_layer_->mask_layer());
1483
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);
1494 }
1495
1496 TEST_F(PictureLayerImplTest, ReleaseResources) {
1497 gfx::Size tile_size(400, 400);
1498 gfx::Size layer_bounds(1300, 1900);
1499
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);
1504
1505 SetupTrees(pending_pile, active_pile);
1506 EXPECT_EQ(2u, pending_layer_->tilings()->num_tilings());
1507
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());
1517
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());
1526 }
1527
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);
1532
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);
1537
1538 SetupTrees(pending_pile, active_pile);
1539 EXPECT_GE(pending_layer_->tilings()->num_tilings(), 1u);
1540
1541 pending_layer_->tilings()->tiling_at(0)->CreateAllTilesForTesting();
1542
1543 // The default value.
1544 EXPECT_EQ(gfx::Size(256, 256).ToString(),
1545 host_impl_.settings().default_tile_size.ToString());
1546
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());
1550
1551 ResetTilingsAndRasterScales();
1552
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());
1560
1561 SetupDrawPropertiesAndUpdateTiles(pending_layer_, 1.f, 1.f, 1.f, 1.f, false);
1562 ASSERT_EQ(2u, pending_layer_->tilings()->num_tilings());
1563
1564 pending_layer_->tilings()->tiling_at(0)->CreateAllTilesForTesting();
1565
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());
1570 }
1571
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);
1576
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);
1581
1582 SetupTrees(pending_pile, active_pile);
1583 EXPECT_GE(pending_layer_->tilings()->num_tilings(), 1u);
1584
1585 pending_layer_->tilings()->tiling_at(0)->CreateAllTilesForTesting();
1586
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());
1590
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());
1594
1595 ResetTilingsAndRasterScales();
1596
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());
1604
1605 SetupDrawPropertiesAndUpdateTiles(pending_layer_, 1.f, 1.f, 1.f, 1.f, false);
1606 ASSERT_LE(1u, pending_layer_->tilings()->num_tilings());
1607
1608 pending_layer_->tilings()->tiling_at(0)->CreateAllTilesForTesting();
1609
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());
1614
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());
1619 }
1620
1621 TEST_F(PictureLayerImplTest, DisallowTileDrawQuads) {
1622 scoped_ptr<RenderPass> render_pass = RenderPass::Create();
1623
1624 gfx::Size tile_size(400, 400);
1625 gfx::Size layer_bounds(1300, 1900);
1626
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);
1631
1632 gfx::Rect layer_invalidation(150, 200, 30, 180);
1633 SetupTreesWithInvalidation(pending_pile, active_pile, layer_invalidation);
1634
1635 active_layer_->draw_properties().visible_content_rect =
1636 gfx::Rect(layer_bounds);
1637
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);
1642
1643 ASSERT_EQ(1U, render_pass->quad_list.size());
1644 EXPECT_EQ(DrawQuad::PICTURE_CONTENT,
1645 render_pass->quad_list.front()->material);
1646 }
1647
1648 TEST_F(PictureLayerImplTest, SolidColorLayerHasVisibleFullCoverage) {
1649 scoped_ptr<RenderPass> render_pass = RenderPass::Create();
1650
1651 gfx::Size tile_size(1000, 1000);
1652 gfx::Size layer_bounds(1500, 1500);
1653 gfx::Rect visible_rect(250, 250, 1000, 1000);
1654
1655 scoped_ptr<FakePicturePile> empty_recording =
1656 FakePicturePile::CreateEmptyPile(tile_size, layer_bounds);
1657 empty_recording->SetIsSolidColor(true);
1658
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);
1663
1664 SetupTrees(pending_pile, active_pile);
1665
1666 active_layer_->draw_properties().visible_content_rect = visible_rect;
1667
1668 AppendQuadsData data;
1669 active_layer_->WillDraw(DRAW_MODE_SOFTWARE, nullptr);
1670 active_layer_->AppendQuads(render_pass.get(), &data);
1671 active_layer_->DidDraw(nullptr);
1672
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);
1678 }
1679
1680 EXPECT_TRUE(remaining.IsEmpty());
1681 }
1682
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);
1692
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());
1697
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);
1702 }
1703
1704 TEST_F(NoLowResPictureLayerImplTest, MarkRequiredOffscreenTiles) {
1705 gfx::Size tile_size(100, 100);
1706 gfx::Size layer_bounds(200, 200);
1707
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);
1718
1719 scoped_refptr<FakePicturePileImpl> pending_pile =
1720 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1721 SetupPendingTreeWithFixedTileSize(pending_pile, tile_size, Region());
1722
1723 EXPECT_EQ(1u, pending_layer_->num_tilings());
1724 EXPECT_EQ(viewport, pending_layer_->visible_rect_for_tile_priority());
1725
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);
1731
1732 int num_visible = 0;
1733 int num_offscreen = 0;
1734
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++;
1746 }
1747 }
1748
1749 EXPECT_GT(num_visible, 0);
1750 EXPECT_GT(num_offscreen, 0);
1751 }
1752
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));
1759
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);
1764
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());
1770
1771 ASSERT_EQ(1u, pending_layer_->num_tilings());
1772 ASSERT_EQ(1.f, pending_layer_->HighResTiling()->contents_scale());
1773
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);
1790
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);
1798
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());
1804
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());
1821 }
1822 }
1823
1824 EXPECT_GT(num_inside, 0);
1825 EXPECT_GT(num_outside, 0);
1826
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;
1831
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);
1837
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());
1842 }
1843
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));
1849
1850 gfx::Size tile_size(100, 100);
1851 gfx::Size layer_bounds(200, 200);
1852
1853 scoped_refptr<FakePicturePileImpl> pending_pile =
1854 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1855
1856 SetupPendingTreeWithFixedTileSize(pending_pile, tile_size, Region());
1857 ActivateTree();
1858
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);
1863
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);
1869
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());
1875 }
1876
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));
1882
1883 gfx::Size tile_size(100, 100);
1884 gfx::Size layer_bounds(200, 200);
1885
1886 scoped_refptr<FakePicturePileImpl> pending_pile =
1887 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1888 SetupPendingTreeWithFixedTileSize(pending_pile, tile_size, Region());
1889 ActivateTree();
1890
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);
1896
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());
1901 }
1902
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));
1908
1909 gfx::Size tile_size(100, 100);
1910 gfx::Size layer_bounds(200, 200);
1911
1912 scoped_refptr<FakePicturePileImpl> pending_pile =
1913 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1914 SetupPendingTreeWithFixedTileSize(pending_pile, tile_size, Region());
1915 ActivateTree();
1916
1917 std::vector<Tile*> low_tiles =
1918 active_layer_->tilings()->tiling_at(1)->AllTilesForTesting();
1919 host_impl_.tile_manager()->InitializeTilesWithResourcesForTesting(low_tiles);
1920
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);
1926
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());
1931 }
1932
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));
1938
1939 gfx::Size tile_size(100, 100);
1940 gfx::Size layer_bounds(200, 200);
1941
1942 scoped_refptr<FakePicturePileImpl> pending_pile =
1943 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1944 SetupPendingTreeWithFixedTileSize(pending_pile, tile_size, Region());
1945 ActivateTree();
1946
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);
1952
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);
1957
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);
1963
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());
1969 }
1970
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));
1977
1978 gfx::Size tile_size(100, 100);
1979 gfx::Size layer_bounds(200, 200);
1980 gfx::Size viewport_size(400, 400);
1981
1982 host_impl_.SetViewportSize(viewport_size);
1983 host_impl_.SetDeviceScaleFactor(2.f);
1984
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());
1990
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);
1997
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);
2002
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());
2006
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());
2010
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);
2015
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);
2021
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());
2030
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());
2035 }
2036
2037 TEST_F(PictureLayerImplTest, HighResRequiredWhenUnsharedActiveAllReady) {
2038 gfx::Size layer_bounds(400, 400);
2039 gfx::Size tile_size(100, 100);
2040
2041 // No tiles shared.
2042 SetupDefaultTreesWithFixedTileSize(layer_bounds, tile_size,
2043 gfx::Rect(layer_bounds));
2044
2045 active_layer_->SetAllTilesReady();
2046
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();
2051
2052 AssertAllTilesRequired(pending_layer_->HighResTiling());
2053 AssertNoTilesRequired(pending_layer_->LowResTiling());
2054 }
2055
2056 TEST_F(PictureLayerImplTest, HighResRequiredWhenMissingHighResFlagOn) {
2057 gfx::Size layer_bounds(400, 400);
2058 gfx::Size tile_size(100, 100);
2059
2060 // All tiles shared (no invalidation).
2061 SetupDefaultTreesWithFixedTileSize(layer_bounds, tile_size, Region());
2062
2063 // Verify active tree not ready.
2064 Tile* some_active_tile =
2065 active_layer_->HighResTiling()->AllTilesForTesting()[0];
2066 EXPECT_FALSE(some_active_tile->IsReadyToDraw());
2067
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();
2071
2072 pending_layer_->HighResTiling()->UpdateAllTilePrioritiesForTesting();
2073 pending_layer_->LowResTiling()->UpdateAllTilePrioritiesForTesting();
2074
2075 AssertAllTilesRequired(pending_layer_->HighResTiling());
2076 AssertNoTilesRequired(pending_layer_->LowResTiling());
2077 }
2078
2079 TEST_F(PictureLayerImplTest, AllHighResRequiredEvenIfShared) {
2080 gfx::Size layer_bounds(400, 400);
2081 gfx::Size tile_size(100, 100);
2082
2083 SetupDefaultTreesWithFixedTileSize(layer_bounds, tile_size, Region());
2084
2085 Tile* some_active_tile =
2086 active_layer_->HighResTiling()->AllTilesForTesting()[0];
2087 EXPECT_FALSE(some_active_tile->IsReadyToDraw());
2088
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();
2093
2094 AssertAllTilesRequired(pending_layer_->HighResTiling());
2095 AssertNoTilesRequired(pending_layer_->LowResTiling());
2096 }
2097
2098 TEST_F(PictureLayerImplTest, DisallowRequiredForActivation) {
2099 gfx::Size layer_bounds(400, 400);
2100 gfx::Size tile_size(100, 100);
2101
2102 SetupDefaultTreesWithFixedTileSize(layer_bounds, tile_size, Region());
2103
2104 Tile* some_active_tile =
2105 active_layer_->HighResTiling()->AllTilesForTesting()[0];
2106 EXPECT_FALSE(some_active_tile->IsReadyToDraw());
2107
2108 pending_layer_->HighResTiling()->set_can_require_tiles_for_activation(false);
2109 pending_layer_->LowResTiling()->set_can_require_tiles_for_activation(false);
2110
2111 // If we disallow required for activation, no tiles can be required.
2112 pending_layer_->HighResTiling()->UpdateAllTilePrioritiesForTesting();
2113 pending_layer_->LowResTiling()->UpdateAllTilePrioritiesForTesting();
2114
2115 AssertNoTilesRequired(pending_layer_->HighResTiling());
2116 AssertNoTilesRequired(pending_layer_->LowResTiling());
2117 }
2118
2119 TEST_F(PictureLayerImplTest, NothingRequiredIfActiveMissingTiles) {
2120 gfx::Size layer_bounds(400, 400);
2121 gfx::Size tile_size(100, 100);
2122
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);
2133
2134 SetupTreesWithFixedTileSize(pending_pile, active_pile, tile_size, Region());
2135
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);
2140
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();
2145
2146 AssertNoTilesRequired(pending_layer_->HighResTiling());
2147 AssertNoTilesRequired(pending_layer_->LowResTiling());
2148 }
2149
2150 TEST_F(PictureLayerImplTest, HighResRequiredIfActiveCantHaveTiles) {
2151 gfx::Size layer_bounds(400, 400);
2152 gfx::Size tile_size(100, 100);
2153
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());
2159
2160 // Active layer can't have tiles.
2161 EXPECT_FALSE(active_layer_->CanHaveTilings());
2162
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();
2169
2170 AssertAllTilesRequired(pending_layer_->HighResTiling());
2171 AssertNoTilesRequired(pending_layer_->LowResTiling());
2172 }
2173
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);
2178
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);
2183
2184 SetupTreesWithFixedTileSize(pending_pile, active_pile, tile_size, Region());
2185
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();
2190
2191 AssertAllTilesRequired(pending_layer_->HighResTiling());
2192 AssertNoTilesRequired(pending_layer_->LowResTiling());
2193 }
2194
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);
2200
2201 host_impl_.CreatePendingTree();
2202 LayerTreeImpl* pending_tree = host_impl_.pending_tree();
2203
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());
2209
2210 pending_layer_ = static_cast<FakePictureLayerImpl*>(
2211 host_impl_.pending_tree()->LayerById(id_));
2212
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);
2219
2220 host_impl_.ActivateSyncTree();
2221
2222 active_layer_ = static_cast<FakePictureLayerImpl*>(
2223 host_impl_.active_tree()->LayerById(id_));
2224
2225 EXPECT_EQ(0u, active_layer_->num_tilings());
2226 EXPECT_EQ(raster_page_scale, active_layer_->raster_page_scale());
2227 }
2228
2229 TEST_F(PictureLayerImplTest, ShareTilesOnNextFrame) {
2230 gfx::Size layer_bounds(1500, 1500);
2231 gfx::Size tile_size(100, 100);
2232
2233 scoped_refptr<FakePicturePileImpl> pending_pile =
2234 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
2235
2236 SetupPendingTree(pending_pile);
2237
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());
2245
2246 ActivateTree();
2247
2248 // Make a pending tree with an invalidated raster tile 0,0.
2249 SetupPendingTreeWithInvalidation(pending_pile, first_invalidate);
2250
2251 // Activate and make a pending tree with an invalidated raster tile 1,1.
2252 ActivateTree();
2253
2254 SetupPendingTreeWithInvalidation(pending_pile, second_invalidate);
2255
2256 PictureLayerTiling* pending_tiling = pending_layer_->tilings()->tiling_at(0);
2257 PictureLayerTiling* active_tiling = active_layer_->tilings()->tiling_at(0);
2258
2259 // pending_tiling->CreateAllTilesForTesting();
2260
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());
2270
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();
2276
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());
2286 }
2287
2288 TEST_F(PictureLayerImplTest, ShareTilesWithNoInvalidation) {
2289 SetupDefaultTrees(gfx::Size(1500, 1500));
2290
2291 EXPECT_GE(active_layer_->num_tilings(), 1u);
2292 EXPECT_GE(pending_layer_->num_tilings(), 1u);
2293
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);
2299
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));
2304
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));
2309
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());
2318 }
2319
2320 TEST_F(PictureLayerImplTest, ShareInvalidActiveTreeTiles) {
2321 gfx::Size tile_size(100, 100);
2322 gfx::Size layer_bounds(1500, 1500);
2323
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);
2335
2336 EXPECT_GE(active_layer_->num_tilings(), 1u);
2337 EXPECT_GE(pending_layer_->num_tilings(), 1u);
2338
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);
2345
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));
2350
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));
2355
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());
2364 }
2365
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));
2370
2371 EXPECT_GE(active_layer_->num_tilings(), 1u);
2372 EXPECT_GE(pending_layer_->num_tilings(), 1u);
2373
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);
2380
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));
2385
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));
2390
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());
2400 }
2401
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));
2407
2408 gfx::Size tile_size(100, 100);
2409 gfx::Size layer_bounds(10, 10);
2410
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);
2415
2416 SetupTrees(pending_pile, active_pile);
2417
2418 EXPECT_TRUE(pending_layer_->tilings()->FindTilingWithScale(1.f));
2419 EXPECT_TRUE(active_layer_->tilings()->FindTilingWithScale(1.f));
2420
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());
2427
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));
2436
2437 ActivateTree();
2438 EXPECT_TRUE(active_layer_->tilings()->FindTilingWithScale(1.f));
2439
2440 SetupPendingTree(pending_pile);
2441 EXPECT_TRUE(pending_layer_->tilings()->FindTilingWithScale(1.f));
2442
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());
2448 }
2449
2450 TEST_F(PictureLayerImplTest, HighResCreatedWhenBoundsShrink) {
2451 gfx::Size tile_size(100, 100);
2452
2453 // Put 0.5 as high res.
2454 host_impl_.SetDeviceScaleFactor(0.5f);
2455
2456 scoped_refptr<FakePicturePileImpl> pending_pile =
2457 FakePicturePileImpl::CreateFilledPile(tile_size, gfx::Size(10, 10));
2458 SetupPendingTree(pending_pile);
2459
2460 // Sanity checks.
2461 EXPECT_EQ(1u, pending_layer_->tilings()->num_tilings());
2462 EXPECT_TRUE(pending_layer_->tilings()->FindTilingWithScale(0.5f));
2463
2464 ActivateTree();
2465
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);
2470
2471 // Another sanity check.
2472 EXPECT_EQ(1.f, pending_layer_->MinimumContentsScale());
2473
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);
2477
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());
2483 }
2484
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);
2489
2490 host_impl_.SetUseGpuRasterization(false);
2491
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());
2496 }
2497
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);
2502
2503 host_impl_.SetUseGpuRasterization(true);
2504
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());
2509 }
2510
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);
2520
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());
2525
2526 // No tilings should be pushed to active layer.
2527 pending_layer_->PushPropertiesTo(active_layer_);
2528 EXPECT_EQ(0u, active_layer_->num_tilings());
2529 }
2530
2531 TEST_F(PictureLayerImplTest, FirstTilingDuringPinch) {
2532 SetupDefaultTrees(gfx::Size(10, 10));
2533
2534 // We start with a tiling at scale 1.
2535 EXPECT_EQ(1.f, pending_layer_->HighResTiling()->contents_scale());
2536
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());
2543 }
2544
2545 TEST_F(PictureLayerImplTest, PinchingTooSmall) {
2546 SetupDefaultTrees(gfx::Size(10, 10));
2547
2548 // We start with a tiling at scale 1.
2549 EXPECT_EQ(1.f, pending_layer_->HighResTiling()->contents_scale());
2550
2551 host_impl_.PinchGestureBegin();
2552 float high_res_scale = 0.0001f;
2553 EXPECT_LT(high_res_scale, pending_layer_->MinimumContentsScale());
2554
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());
2558 }
2559
2560 TEST_F(PictureLayerImplTest, PinchingTooSmallWithContentsScale) {
2561 SetupDefaultTrees(gfx::Size(10, 10));
2562
2563 ResetTilingsAndRasterScales();
2564
2565 float contents_scale = 0.15f;
2566 SetContentsScaleOnBothLayers(contents_scale, 1.f, 1.f, 1.f, false);
2567
2568 ASSERT_GE(pending_layer_->num_tilings(), 0u);
2569 EXPECT_FLOAT_EQ(contents_scale,
2570 pending_layer_->HighResTiling()->contents_scale());
2571
2572 host_impl_.PinchGestureBegin();
2573
2574 float page_scale = 0.0001f;
2575 EXPECT_LT(page_scale * contents_scale,
2576 pending_layer_->MinimumContentsScale());
2577
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());
2583 }
2584
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));
2592 }
2593
2594 void SetUp() override {
2595 PictureLayerImplTest::SetUp();
2596
2597 // Create some default active and pending trees.
2598 gfx::Size tile_size(100, 100);
2599 gfx::Size layer_bounds(400, 400);
2600
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);
2605
2606 SetupTrees(pending_pile, active_pile);
2607 }
2608 };
2609
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());
2619
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()));
2624
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);
2629 }
2630
2631 TEST_F(PictureLayerImplTest, HighResTilingDuringAnimationForCpuRasterization) {
2632 gfx::Size viewport_size(1000, 1000);
2633 host_impl_.SetViewportSize(viewport_size);
2634
2635 gfx::Size layer_bounds(100, 100);
2636 SetupDefaultTrees(layer_bounds);
2637
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;
2643
2644 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 1.f);
2645
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;
2651
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);
2658
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;
2663
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);
2670
2671 // Once we stop animating, a new high-res tiling should be created.
2672 animating_transform = false;
2673
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);
2680
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;
2686
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);
2693
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;
2697
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);
2704
2705 // Once we stop animating, a new high-res tiling should be created.
2706 animating_transform = false;
2707 contents_scale = 4.f;
2708
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);
2715
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;
2723
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);
2730
2731 // Once we stop animating, a new high-res tiling should be created.
2732 animating_transform = false;
2733 contents_scale = 11.f;
2734
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);
2741
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;
2749
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);
2756
2757 // Once we stop animating, a new high-res tiling should be created.
2758 animating_transform = false;
2759 contents_scale = 12.f;
2760
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);
2767
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;
2774
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);
2781
2782 // Once we stop animating, a new high-res tiling should be created.
2783 animating_transform = false;
2784 contents_scale = 11.f;
2785
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);
2792 }
2793
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);
2800
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;
2806
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);
2813
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;
2819
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);
2826
2827 // Further changes to scale during the animation should cause a new high-res
2828 // tiling to get created.
2829 contents_scale = 3.f;
2830
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);
2837
2838 // Since we're re-rasterizing during the animation, scales smaller than 1
2839 // should be respected.
2840 contents_scale = 0.25f;
2841
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);
2848
2849 // Once we stop animating, a new high-res tiling should be created.
2850 contents_scale = 4.f;
2851 animating_transform = false;
2852
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);
2859 }
2860
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));
2866
2867 host_impl_.SetViewportSize(gfx::Size(500, 500));
2868
2869 gfx::Size recording_tile_size(100, 100);
2870 gfx::Size layer_bounds(1000, 1000);
2871
2872 scoped_refptr<FakePicturePileImpl> pending_pile =
2873 FakePicturePileImpl::CreateFilledPile(recording_tile_size, layer_bounds);
2874
2875 SetupPendingTree(pending_pile);
2876 EXPECT_EQ(2u, pending_layer_->num_tilings());
2877
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);
2889
2890 EXPECT_TRUE(tile);
2891
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;
2902 }
2903
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;
2907
2908 unique_tiles.insert(tile);
2909 queue->Pop();
2910 }
2911
2912 EXPECT_TRUE(reached_prepaint);
2913 EXPECT_EQ(0, non_ideal_tile_count);
2914 EXPECT_EQ(0, low_res_tile_count);
2915
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()));
2922
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());
2928
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();
2940 }
2941
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);
2945
2946 // No NOW tiles.
2947 time_ticks += base::TimeDelta::FromMilliseconds(200);
2948 host_impl_.SetCurrentBeginFrameArgs(
2949 CreateBeginFrameArgsForTesting(BEGINFRAME_FROM_HERE, time_ticks));
2950
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);
2955
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);
2963
2964 EXPECT_TRUE(tile);
2965
2966 // Non-high res tiles only get visible tiles.
2967 EXPECT_EQ(HIGH_RESOLUTION, priority.resolution);
2968 EXPECT_NE(TilePriority::NOW, priority.priority_bin);
2969
2970 high_res_tile_count += priority.resolution == HIGH_RESOLUTION;
2971
2972 unique_tiles.insert(tile);
2973 queue->Pop();
2974 }
2975
2976 EXPECT_EQ(16, high_res_tile_count);
2977 EXPECT_EQ(high_res_tile_count, static_cast<int>(unique_tiles.size()));
2978
2979 time_ticks += base::TimeDelta::FromMilliseconds(200);
2980 host_impl_.SetCurrentBeginFrameArgs(
2981 CreateBeginFrameArgsForTesting(BEGINFRAME_FROM_HERE, time_ticks));
2982
2983 pending_layer_->draw_properties().visible_content_rect =
2984 gfx::Rect(0, 0, 500, 500);
2985 pending_layer_->UpdateTiles(resourceless_software_draw);
2986
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);
2995 }
2996
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);
3005
3006 EXPECT_TRUE(tile);
3007
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();
3012 }
3013
3014 EXPECT_EQ(0, non_ideal_tile_count);
3015 EXPECT_EQ(1, low_res_tile_count);
3016 EXPECT_EQ(0, high_res_tile_count);
3017 }
3018
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));
3024
3025 host_impl_.SetViewportSize(gfx::Size(500, 500));
3026
3027 gfx::Size tile_size(100, 100);
3028 gfx::Size layer_bounds(1000, 1000);
3029
3030 scoped_refptr<FakePicturePileImpl> pending_pile =
3031 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
3032
3033 SetupPendingTree(pending_pile);
3034 ActivateTree();
3035 EXPECT_EQ(2u, active_layer_->num_tilings());
3036
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();
3047 }
3048
3049 queue.reset(new TilingSetRasterQueueRequired(
3050 active_layer_->picture_layer_tiling_set(),
3051 RasterTilePriorityQueue::Type::REQUIRED_FOR_ACTIVATION));
3052 EXPECT_TRUE(queue->IsEmpty());
3053 }
3054
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);
3060
3061 scoped_refptr<FakePicturePileImpl> pending_pile =
3062 FakePicturePileImpl::CreateFromPile(empty_recording.get(), nullptr);
3063
3064 SetupPendingTree(pending_pile);
3065 EXPECT_FALSE(
3066 pending_layer_->picture_layer_tiling_set()->FindTilingWithResolution(
3067 HIGH_RESOLUTION));
3068
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());
3074 }
3075
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;
3080
3081 host_impl_.SetViewportSize(gfx::Size(500, 500));
3082
3083 scoped_refptr<FakePicturePileImpl> pending_pile =
3084 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
3085
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());
3090
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());
3096 }
3097
3098 std::set<Tile*> all_tiles_set(all_tiles.begin(), all_tiles.end());
3099
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++;
3116 }
3117 mark_required = !mark_required;
3118 }
3119 }
3120
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);
3126
3127 // Tiles don't have resources yet.
3128 scoped_ptr<TilingSetEvictionQueue> queue(
3129 new TilingSetEvictionQueue(pending_layer_->picture_layer_tiling_set(),
3130 SAME_PRIORITY_FOR_BOTH_TREES, false));
3131 EXPECT_TRUE(queue->IsEmpty());
3132
3133 host_impl_.tile_manager()->InitializeTilesWithResourcesForTesting(all_tiles);
3134
3135 std::set<Tile*> unique_tiles;
3136 float expected_scales[] = {low_res_factor, 1.f};
3137 size_t scale_index = 0;
3138 bool reached_visible = false;
3139 Tile* last_tile = nullptr;
3140 size_t distance_decreasing = 0;
3141 size_t distance_increasing = 0;
3142 queue.reset(
3143 new TilingSetEvictionQueue(pending_layer_->picture_layer_tiling_set(),
3144 SAME_PRIORITY_FOR_BOTH_TREES, false));
3145 while (!queue->IsEmpty()) {
3146 Tile* tile = queue->Top();
3147 if (!last_tile)
3148 last_tile = tile;
3149
3150 EXPECT_TRUE(tile);
3151
3152 TilePriority priority = tile->priority(PENDING_TREE);
3153
3154 if (priority.priority_bin == TilePriority::NOW) {
3155 reached_visible = true;
3156 last_tile = tile;
3157 break;
3158 }
3159
3160 EXPECT_FALSE(tile->required_for_activation());
3161
3162 while (std::abs(tile->contents_scale() - expected_scales[scale_index]) >
3163 std::numeric_limits<float>::epsilon()) {
3164 ++scale_index;
3165 ASSERT_LT(scale_index, arraysize(expected_scales));
3166 }
3167
3168 EXPECT_FLOAT_EQ(tile->contents_scale(), expected_scales[scale_index]);
3169 unique_tiles.insert(tile);
3170
3171 if (tile->required_for_activation() ==
3172 last_tile->required_for_activation() &&
3173 std::abs(tile->contents_scale() - last_tile->contents_scale()) <
3174 std::numeric_limits<float>::epsilon()) {
3175 if (priority.distance_to_visible <=
3176 last_tile->priority(PENDING_TREE).distance_to_visible)
3177 ++distance_decreasing;
3178 else
3179 ++distance_increasing;
3180 }
3181
3182 last_tile = tile;
3183 queue->Pop();
3184 }
3185
3186 // 4 high res tiles are inside the viewport, the rest are evicted.
3187 EXPECT_TRUE(reached_visible);
3188 EXPECT_EQ(12u, unique_tiles.size());
3189 EXPECT_EQ(1u, distance_increasing);
3190 EXPECT_EQ(11u, distance_decreasing);
3191
3192 scale_index = 0;
3193 bool reached_required = false;
3194 while (!queue->IsEmpty()) {
3195 Tile* tile = queue->Top();
3196 EXPECT_TRUE(tile);
3197
3198 TilePriority priority = tile->priority(PENDING_TREE);
3199 EXPECT_EQ(TilePriority::NOW, priority.priority_bin);
3200
3201 if (reached_required) {
3202 EXPECT_TRUE(tile->required_for_activation());
3203 } else if (tile->required_for_activation()) {
3204 reached_required = true;
3205 scale_index = 0;
3206 }
3207
3208 while (std::abs(tile->contents_scale() - expected_scales[scale_index]) >
3209 std::numeric_limits<float>::epsilon()) {
3210 ++scale_index;
3211 ASSERT_LT(scale_index, arraysize(expected_scales));
3212 }
3213
3214 EXPECT_FLOAT_EQ(tile->contents_scale(), expected_scales[scale_index]);
3215 unique_tiles.insert(tile);
3216 queue->Pop();
3217 }
3218
3219 EXPECT_TRUE(reached_required);
3220 EXPECT_EQ(all_tiles_set.size(), unique_tiles.size());
3221 }
3222
3223 TEST_F(PictureLayerImplTest, Occlusion) {
3224 gfx::Size tile_size(102, 102);
3225 gfx::Size layer_bounds(1000, 1000);
3226 gfx::Size viewport_size(1000, 1000);
3227
3228 LayerTestCommon::LayerImplTest impl;
3229 host_impl_.SetViewportSize(viewport_size);
3230
3231 scoped_refptr<FakePicturePileImpl> pending_pile =
3232 FakePicturePileImpl::CreateFilledPile(layer_bounds, layer_bounds);
3233 SetupPendingTreeWithFixedTileSize(pending_pile, tile_size, Region());
3234 ActivateTree();
3235
3236 std::vector<Tile*> tiles =
3237 active_layer_->HighResTiling()->AllTilesForTesting();
3238 host_impl_.tile_manager()->InitializeTilesWithResourcesForTesting(tiles);
3239
3240 {
3241 SCOPED_TRACE("No occlusion");
3242 gfx::Rect occluded;
3243 impl.AppendQuadsWithOcclusion(active_layer_, occluded);
3244
3245 LayerTestCommon::VerifyQuadsExactlyCoverRect(impl.quad_list(),
3246 gfx::Rect(layer_bounds));
3247 EXPECT_EQ(100u, impl.quad_list().size());
3248 }
3249
3250 {
3251 SCOPED_TRACE("Full occlusion");
3252 gfx::Rect occluded(active_layer_->visible_content_rect());
3253 impl.AppendQuadsWithOcclusion(active_layer_, occluded);
3254
3255 LayerTestCommon::VerifyQuadsExactlyCoverRect(impl.quad_list(), gfx::Rect());
3256 EXPECT_EQ(impl.quad_list().size(), 0u);
3257 }
3258
3259 {
3260 SCOPED_TRACE("Partial occlusion");
3261 gfx::Rect occluded(150, 0, 200, 1000);
3262 impl.AppendQuadsWithOcclusion(active_layer_, occluded);
3263
3264 size_t partially_occluded_count = 0;
3265 LayerTestCommon::VerifyQuadsAreOccluded(
3266 impl.quad_list(), occluded, &partially_occluded_count);
3267 // The layer outputs one quad, which is partially occluded.
3268 EXPECT_EQ(100u - 10u, impl.quad_list().size());
3269 EXPECT_EQ(10u + 10u, partially_occluded_count);
3270 }
3271 }
3272
3273 TEST_F(PictureLayerImplTest, RasterScaleChangeWithoutAnimation) {
3274 gfx::Size tile_size(host_impl_.settings().default_tile_size);
3275 SetupDefaultTrees(tile_size);
3276
3277 ResetTilingsAndRasterScales();
3278
3279 float contents_scale = 2.f;
3280 float device_scale = 1.f;
3281 float page_scale = 1.f;
3282 float maximum_animation_scale = 1.f;
3283 bool animating_transform = false;
3284
3285 SetContentsScaleOnBothLayers(contents_scale,
3286 device_scale,
3287 page_scale,
3288 maximum_animation_scale,
3289 animating_transform);
3290 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 2.f);
3291
3292 // Changing the source scale without being in an animation will cause
3293 // the layer to reset its source scale to 1.f.
3294 contents_scale = 3.f;
3295
3296 SetContentsScaleOnBothLayers(contents_scale,
3297 device_scale,
3298 page_scale,
3299 maximum_animation_scale,
3300 animating_transform);
3301 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 1.f);
3302
3303 // Further changes to the source scale will no longer be reflected in the
3304 // contents scale.
3305 contents_scale = 0.5f;
3306
3307 SetContentsScaleOnBothLayers(contents_scale,
3308 device_scale,
3309 page_scale,
3310 maximum_animation_scale,
3311 animating_transform);
3312 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 1.f);
3313 }
3314
3315 TEST_F(PictureLayerImplTest, LowResReadyToDrawNotEnoughToActivate) {
3316 gfx::Size tile_size(100, 100);
3317 gfx::Size layer_bounds(1000, 1000);
3318
3319 // Make sure some tiles are not shared.
3320 gfx::Rect invalidation(gfx::Point(50, 50), tile_size);
3321 SetupDefaultTreesWithFixedTileSize(layer_bounds, tile_size, invalidation);
3322
3323 // All pending layer tiles required are not ready.
3324 EXPECT_FALSE(host_impl_.tile_manager()->IsReadyToActivate());
3325
3326 // Initialize all low-res tiles.
3327 pending_layer_->SetAllTilesReadyInTiling(pending_layer_->LowResTiling());
3328
3329 // Low-res tiles should not be enough.
3330 EXPECT_FALSE(host_impl_.tile_manager()->IsReadyToActivate());
3331
3332 // Initialize remaining tiles.
3333 pending_layer_->SetAllTilesReady();
3334
3335 EXPECT_TRUE(host_impl_.tile_manager()->IsReadyToActivate());
3336 }
3337
3338 TEST_F(PictureLayerImplTest, HighResReadyToDrawEnoughToActivate) {
3339 gfx::Size tile_size(100, 100);
3340 gfx::Size layer_bounds(1000, 1000);
3341
3342 // Make sure some tiles are not shared.
3343 gfx::Rect invalidation(gfx::Point(50, 50), tile_size);
3344 SetupDefaultTreesWithFixedTileSize(layer_bounds, tile_size, invalidation);
3345
3346 // All pending layer tiles required are not ready.
3347 EXPECT_FALSE(host_impl_.tile_manager()->IsReadyToActivate());
3348
3349 // Initialize all high-res tiles.
3350 pending_layer_->SetAllTilesReadyInTiling(pending_layer_->HighResTiling());
3351
3352 // High-res tiles should be enough, since they cover everything visible.
3353 EXPECT_TRUE(host_impl_.tile_manager()->IsReadyToActivate());
3354 }
3355
3356 TEST_F(PictureLayerImplTest,
3357 SharedActiveHighResReadyAndPendingLowResReadyNotEnoughToActivate) {
3358 gfx::Size tile_size(100, 100);
3359 gfx::Size layer_bounds(1000, 1000);
3360
3361 // Make sure some tiles are not shared.
3362 gfx::Rect invalidation(gfx::Point(50, 50), tile_size);
3363 SetupDefaultTreesWithFixedTileSize(layer_bounds, tile_size, invalidation);
3364
3365 // Initialize all high-res tiles in the active layer.
3366 active_layer_->SetAllTilesReadyInTiling(active_layer_->HighResTiling());
3367 // And all the low-res tiles in the pending layer.
3368 pending_layer_->SetAllTilesReadyInTiling(pending_layer_->LowResTiling());
3369
3370 // The unshared high-res tiles are not ready, so we cannot activate.
3371 EXPECT_FALSE(host_impl_.tile_manager()->IsReadyToActivate());
3372
3373 // When the unshared pending high-res tiles are ready, we can activate.
3374 pending_layer_->SetAllTilesReadyInTiling(pending_layer_->HighResTiling());
3375 EXPECT_TRUE(host_impl_.tile_manager()->IsReadyToActivate());
3376 }
3377
3378 TEST_F(PictureLayerImplTest, SharedActiveHighResReadyNotEnoughToActivate) {
3379 gfx::Size tile_size(100, 100);
3380 gfx::Size layer_bounds(1000, 1000);
3381
3382 // Make sure some tiles are not shared.
3383 gfx::Rect invalidation(gfx::Point(50, 50), tile_size);
3384 SetupDefaultTreesWithFixedTileSize(layer_bounds, tile_size, invalidation);
3385
3386 // Initialize all high-res tiles in the active layer.
3387 active_layer_->SetAllTilesReadyInTiling(active_layer_->HighResTiling());
3388
3389 // The unshared high-res tiles are not ready, so we cannot activate.
3390 EXPECT_FALSE(host_impl_.tile_manager()->IsReadyToActivate());
3391
3392 // When the unshared pending high-res tiles are ready, we can activate.
3393 pending_layer_->SetAllTilesReadyInTiling(pending_layer_->HighResTiling());
3394 EXPECT_TRUE(host_impl_.tile_manager()->IsReadyToActivate());
3395 }
3396
3397 TEST_F(NoLowResPictureLayerImplTest, ManageTilingsCreatesTilings) {
3398 gfx::Size tile_size(400, 400);
3399 gfx::Size layer_bounds(1300, 1900);
3400
3401 scoped_refptr<FakePicturePileImpl> pending_pile =
3402 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
3403 scoped_refptr<FakePicturePileImpl> active_pile =
3404 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
3405
3406 SetupTrees(pending_pile, active_pile);
3407
3408 float low_res_factor = host_impl_.settings().low_res_contents_scale_factor;
3409 EXPECT_LT(low_res_factor, 1.f);
3410
3411 ResetTilingsAndRasterScales();
3412
3413 SetupDrawPropertiesAndUpdateTiles(active_layer_,
3414 6.f, // ideal contents scale
3415 3.f, // device scale
3416 2.f, // page scale
3417 1.f, // maximum animation scale
3418 false);
3419 ASSERT_EQ(1u, active_layer_->tilings()->num_tilings());
3420 EXPECT_FLOAT_EQ(6.f,
3421 active_layer_->tilings()->tiling_at(0)->contents_scale());
3422
3423 // If we change the page scale factor, then we should get new tilings.
3424 SetupDrawPropertiesAndUpdateTiles(active_layer_,
3425 6.6f, // ideal contents scale
3426 3.f, // device scale
3427 2.2f, // page scale
3428 1.f, // maximum animation scale
3429 false);
3430 ASSERT_EQ(2u, active_layer_->tilings()->num_tilings());
3431 EXPECT_FLOAT_EQ(6.6f,
3432 active_layer_->tilings()->tiling_at(0)->contents_scale());
3433
3434 // If we change the device scale factor, then we should get new tilings.
3435 SetupDrawPropertiesAndUpdateTiles(active_layer_,
3436 7.26f, // ideal contents scale
3437 3.3f, // device scale
3438 2.2f, // page scale
3439 1.f, // maximum animation scale
3440 false);
3441 ASSERT_EQ(3u, active_layer_->tilings()->num_tilings());
3442 EXPECT_FLOAT_EQ(7.26f,
3443 active_layer_->tilings()->tiling_at(0)->contents_scale());
3444
3445 // If we change the device scale factor, but end up at the same total scale
3446 // factor somehow, then we don't get new tilings.
3447 SetupDrawPropertiesAndUpdateTiles(active_layer_,
3448 7.26f, // ideal contents scale
3449 2.2f, // device scale
3450 3.3f, // page scale
3451 1.f, // maximum animation scale
3452 false);
3453 ASSERT_EQ(3u, active_layer_->tilings()->num_tilings());
3454 EXPECT_FLOAT_EQ(7.26f,
3455 active_layer_->tilings()->tiling_at(0)->contents_scale());
3456 }
3457
3458 TEST_F(NoLowResPictureLayerImplTest, PendingLayerOnlyHasHighResTiling) {
3459 gfx::Size tile_size(400, 400);
3460 gfx::Size layer_bounds(1300, 1900);
3461
3462 scoped_refptr<FakePicturePileImpl> pending_pile =
3463 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
3464 scoped_refptr<FakePicturePileImpl> active_pile =
3465 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
3466
3467 SetupTrees(pending_pile, active_pile);
3468
3469 float low_res_factor = host_impl_.settings().low_res_contents_scale_factor;
3470 EXPECT_LT(low_res_factor, 1.f);
3471
3472 ResetTilingsAndRasterScales();
3473
3474 SetupDrawPropertiesAndUpdateTiles(pending_layer_,
3475 6.f, // ideal contents scale
3476 3.f, // device scale
3477 2.f, // page scale
3478 1.f, // maximum animation scale
3479 false);
3480 ASSERT_EQ(1u, pending_layer_->tilings()->num_tilings());
3481 EXPECT_FLOAT_EQ(6.f,
3482 pending_layer_->tilings()->tiling_at(0)->contents_scale());
3483
3484 // If we change the page scale factor, then we should get new tilings.
3485 SetupDrawPropertiesAndUpdateTiles(pending_layer_,
3486 6.6f, // ideal contents scale
3487 3.f, // device scale
3488 2.2f, // page scale
3489 1.f, // maximum animation scale
3490 false);
3491 ASSERT_EQ(1u, pending_layer_->tilings()->num_tilings());
3492 EXPECT_FLOAT_EQ(6.6f,
3493 pending_layer_->tilings()->tiling_at(0)->contents_scale());
3494
3495 // If we change the device scale factor, then we should get new tilings.
3496 SetupDrawPropertiesAndUpdateTiles(pending_layer_,
3497 7.26f, // ideal contents scale
3498 3.3f, // device scale
3499 2.2f, // page scale
3500 1.f, // maximum animation scale
3501 false);
3502 ASSERT_EQ(1u, pending_layer_->tilings()->num_tilings());
3503 EXPECT_FLOAT_EQ(7.26f,
3504 pending_layer_->tilings()->tiling_at(0)->contents_scale());
3505
3506 // If we change the device scale factor, but end up at the same total scale
3507 // factor somehow, then we don't get new tilings.
3508 SetupDrawPropertiesAndUpdateTiles(pending_layer_,
3509 7.26f, // ideal contents scale
3510 2.2f, // device scale
3511 3.3f, // page scale
3512 1.f, // maximum animation scale
3513 false);
3514 ASSERT_EQ(1u, pending_layer_->tilings()->num_tilings());
3515 EXPECT_FLOAT_EQ(7.26f,
3516 pending_layer_->tilings()->tiling_at(0)->contents_scale());
3517 }
3518
3519 TEST_F(NoLowResPictureLayerImplTest, AllHighResRequiredEvenIfShared) {
3520 gfx::Size layer_bounds(400, 400);
3521 gfx::Size tile_size(100, 100);
3522
3523 SetupDefaultTreesWithFixedTileSize(layer_bounds, tile_size, Region());
3524
3525 Tile* some_active_tile =
3526 active_layer_->HighResTiling()->AllTilesForTesting()[0];
3527 EXPECT_FALSE(some_active_tile->IsReadyToDraw());
3528
3529 // All tiles shared (no invalidation), so even though the active tree's
3530 // tiles aren't ready, there is nothing required.
3531 pending_layer_->HighResTiling()->UpdateAllTilePrioritiesForTesting();
3532 if (host_impl_.settings().create_low_res_tiling)
3533 pending_layer_->LowResTiling()->UpdateAllTilePrioritiesForTesting();
3534
3535 AssertAllTilesRequired(pending_layer_->HighResTiling());
3536 if (host_impl_.settings().create_low_res_tiling)
3537 AssertNoTilesRequired(pending_layer_->LowResTiling());
3538 }
3539
3540 TEST_F(NoLowResPictureLayerImplTest, NothingRequiredIfActiveMissingTiles) {
3541 gfx::Size layer_bounds(400, 400);
3542 gfx::Size tile_size(100, 100);
3543
3544 scoped_refptr<FakePicturePileImpl> pending_pile =
3545 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
3546 // This pile will create tilings, but has no recordings so will not create any
3547 // tiles. This is attempting to simulate scrolling past the end of recorded
3548 // content on the active layer, where the recordings are so far away that
3549 // no tiles are created.
3550 bool is_solid_color = false;
3551 scoped_refptr<FakePicturePileImpl> active_pile =
3552 FakePicturePileImpl::CreateEmptyPileThatThinksItHasRecordings(
3553 tile_size, layer_bounds, is_solid_color);
3554
3555 SetupTreesWithFixedTileSize(pending_pile, active_pile, tile_size, Region());
3556
3557 // Active layer has tilings, but no tiles due to missing recordings.
3558 EXPECT_TRUE(active_layer_->CanHaveTilings());
3559 EXPECT_EQ(active_layer_->tilings()->num_tilings(),
3560 host_impl_.settings().create_low_res_tiling ? 2u : 1u);
3561 EXPECT_EQ(active_layer_->HighResTiling()->AllTilesForTesting().size(), 0u);
3562
3563 // Since the active layer has no tiles at all, the pending layer doesn't
3564 // need content in order to activate.
3565 pending_layer_->HighResTiling()->UpdateAllTilePrioritiesForTesting();
3566 if (host_impl_.settings().create_low_res_tiling)
3567 pending_layer_->LowResTiling()->UpdateAllTilePrioritiesForTesting();
3568
3569 AssertNoTilesRequired(pending_layer_->HighResTiling());
3570 if (host_impl_.settings().create_low_res_tiling)
3571 AssertNoTilesRequired(pending_layer_->LowResTiling());
3572 }
3573
3574 TEST_F(NoLowResPictureLayerImplTest, InvalidViewportForPrioritizingTiles) {
3575 base::TimeTicks time_ticks;
3576 time_ticks += base::TimeDelta::FromMilliseconds(1);
3577 host_impl_.SetCurrentBeginFrameArgs(
3578 CreateBeginFrameArgsForTesting(BEGINFRAME_FROM_HERE, time_ticks));
3579
3580 gfx::Size tile_size(100, 100);
3581 gfx::Size layer_bounds(400, 400);
3582
3583 scoped_refptr<FakePicturePileImpl> pending_pile =
3584 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
3585 scoped_refptr<FakePicturePileImpl> active_pile =
3586 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
3587
3588 SetupTreesWithInvalidation(pending_pile, active_pile, Region());
3589
3590 SetupDrawPropertiesAndUpdateTiles(active_layer_, 1.f, 1.f, 1.f, 1.f, false);
3591
3592 // UpdateTiles with valid viewport. Should update tile viewport.
3593 // Note viewport is considered invalid if and only if in resourceless
3594 // software draw.
3595 bool resourceless_software_draw = false;
3596 gfx::Rect viewport = gfx::Rect(layer_bounds);
3597 gfx::Transform transform;
3598 host_impl_.SetExternalDrawConstraints(transform,
3599 viewport,
3600 viewport,
3601 viewport,
3602 transform,
3603 resourceless_software_draw);
3604 active_layer_->draw_properties().visible_content_rect = viewport;
3605 active_layer_->draw_properties().screen_space_transform = transform;
3606 active_layer_->UpdateTiles(resourceless_software_draw);
3607
3608 gfx::Rect visible_rect_for_tile_priority =
3609 active_layer_->visible_rect_for_tile_priority();
3610 EXPECT_FALSE(visible_rect_for_tile_priority.IsEmpty());
3611 gfx::Transform screen_space_transform_for_tile_priority =
3612 active_layer_->screen_space_transform();
3613
3614 // Expand viewport and set it as invalid for prioritizing tiles.
3615 // Should update viewport and transform, but not update visible rect.
3616 time_ticks += base::TimeDelta::FromMilliseconds(200);
3617 host_impl_.SetCurrentBeginFrameArgs(
3618 CreateBeginFrameArgsForTesting(BEGINFRAME_FROM_HERE, time_ticks));
3619 resourceless_software_draw = true;
3620 viewport = gfx::ScaleToEnclosingRect(viewport, 2);
3621 transform.Translate(1.f, 1.f);
3622 active_layer_->draw_properties().visible_content_rect = viewport;
3623 active_layer_->draw_properties().screen_space_transform = transform;
3624 host_impl_.SetExternalDrawConstraints(transform,
3625 viewport,
3626 viewport,
3627 viewport,
3628 transform,
3629 resourceless_software_draw);
3630 active_layer_->UpdateTiles(resourceless_software_draw);
3631
3632 // Transform for tile priority is updated.
3633 EXPECT_TRANSFORMATION_MATRIX_EQ(transform,
3634 active_layer_->screen_space_transform());
3635 // Visible rect for tile priority retains old value.
3636 EXPECT_EQ(visible_rect_for_tile_priority,
3637 active_layer_->visible_rect_for_tile_priority());
3638
3639 // Keep expanded viewport but mark it valid. Should update tile viewport.
3640 time_ticks += base::TimeDelta::FromMilliseconds(200);
3641 host_impl_.SetCurrentBeginFrameArgs(
3642 CreateBeginFrameArgsForTesting(BEGINFRAME_FROM_HERE, time_ticks));
3643 resourceless_software_draw = false;
3644 host_impl_.SetExternalDrawConstraints(transform,
3645 viewport,
3646 viewport,
3647 viewport,
3648 transform,
3649 resourceless_software_draw);
3650 active_layer_->UpdateTiles(resourceless_software_draw);
3651
3652 EXPECT_TRANSFORMATION_MATRIX_EQ(transform,
3653 active_layer_->screen_space_transform());
3654 EXPECT_EQ(viewport, active_layer_->visible_rect_for_tile_priority());
3655 }
3656
3657 TEST_F(NoLowResPictureLayerImplTest, CleanUpTilings) {
3658 gfx::Size tile_size(400, 400);
3659 gfx::Size layer_bounds(1300, 1900);
3660
3661 scoped_refptr<FakePicturePileImpl> pending_pile =
3662 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
3663 scoped_refptr<FakePicturePileImpl> active_pile =
3664 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
3665
3666 std::vector<PictureLayerTiling*> used_tilings;
3667
3668 SetupTrees(pending_pile, active_pile);
3669
3670 float low_res_factor = host_impl_.settings().low_res_contents_scale_factor;
3671 EXPECT_LT(low_res_factor, 1.f);
3672
3673 float device_scale = 1.7f;
3674 float page_scale = 3.2f;
3675 float scale = 1.f;
3676
3677 ResetTilingsAndRasterScales();
3678
3679 SetContentsScaleOnBothLayers(scale, device_scale, page_scale, 1.f, false);
3680 ASSERT_EQ(1u, active_layer_->tilings()->num_tilings());
3681
3682 // Ensure UpdateTiles won't remove any tilings. Note this is unrelated to
3683 // |used_tilings| variable, and it's here only to ensure that active_layer_
3684 // won't remove tilings before the test has a chance to verify behavior.
3685 active_layer_->MarkAllTilingsUsed();
3686
3687 // We only have ideal tilings, so they aren't removed.
3688 used_tilings.clear();
3689 active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
3690 ASSERT_EQ(1u, active_layer_->tilings()->num_tilings());
3691
3692 host_impl_.PinchGestureBegin();
3693
3694 // Changing the ideal but not creating new tilings.
3695 scale *= 1.5f;
3696 page_scale *= 1.5f;
3697 SetContentsScaleOnBothLayers(scale, device_scale, page_scale, 1.f, false);
3698 ASSERT_EQ(1u, active_layer_->tilings()->num_tilings());
3699
3700 // The tilings are still our target scale, so they aren't removed.
3701 used_tilings.clear();
3702 active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
3703 ASSERT_EQ(1u, active_layer_->tilings()->num_tilings());
3704
3705 host_impl_.PinchGestureEnd();
3706
3707 // Create a 1.2 scale tiling. Now we have 1.0 and 1.2 tilings. Ideal = 1.2.
3708 scale /= 4.f;
3709 page_scale /= 4.f;
3710 SetContentsScaleOnBothLayers(1.2f, device_scale, page_scale, 1.f, false);
3711 ASSERT_EQ(2u, active_layer_->tilings()->num_tilings());
3712 EXPECT_FLOAT_EQ(1.f,
3713 active_layer_->tilings()->tiling_at(1)->contents_scale());
3714
3715 // Ensure UpdateTiles won't remove any tilings.
3716 active_layer_->MarkAllTilingsUsed();
3717
3718 // Mark the non-ideal tilings as used. They won't be removed.
3719 used_tilings.clear();
3720 used_tilings.push_back(active_layer_->tilings()->tiling_at(1));
3721 active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
3722 ASSERT_EQ(2u, active_layer_->tilings()->num_tilings());
3723
3724 // Now move the ideal scale to 0.5. Our target stays 1.2.
3725 SetContentsScaleOnBothLayers(0.5f, device_scale, page_scale, 1.f, false);
3726
3727 // The high resolution tiling is between target and ideal, so is not
3728 // removed. The low res tiling for the old ideal=1.0 scale is removed.
3729 used_tilings.clear();
3730 active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
3731 ASSERT_EQ(2u, active_layer_->tilings()->num_tilings());
3732
3733 // Now move the ideal scale to 1.0. Our target stays 1.2.
3734 SetContentsScaleOnBothLayers(1.f, device_scale, page_scale, 1.f, false);
3735
3736 // All the tilings are between are target and the ideal, so they are not
3737 // removed.
3738 used_tilings.clear();
3739 active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
3740 ASSERT_EQ(2u, active_layer_->tilings()->num_tilings());
3741
3742 // Now move the ideal scale to 1.1 on the active layer. Our target stays 1.2.
3743 SetupDrawPropertiesAndUpdateTiles(
3744 active_layer_, 1.1f, device_scale, page_scale, 1.f, false);
3745
3746 // Because the pending layer's ideal scale is still 1.0, our tilings fall
3747 // in the range [1.0,1.2] and are kept.
3748 used_tilings.clear();
3749 active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
3750 ASSERT_EQ(2u, active_layer_->tilings()->num_tilings());
3751
3752 // Move the ideal scale on the pending layer to 1.1 as well. Our target stays
3753 // 1.2 still.
3754 SetupDrawPropertiesAndUpdateTiles(
3755 pending_layer_, 1.1f, device_scale, page_scale, 1.f, false);
3756
3757 // Our 1.0 tiling now falls outside the range between our ideal scale and our
3758 // target raster scale. But it is in our used tilings set, so nothing is
3759 // deleted.
3760 used_tilings.clear();
3761 used_tilings.push_back(active_layer_->tilings()->tiling_at(1));
3762 active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
3763 ASSERT_EQ(2u, active_layer_->tilings()->num_tilings());
3764
3765 // If we remove it from our used tilings set, it is outside the range to keep
3766 // so it is deleted.
3767 used_tilings.clear();
3768 active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
3769 ASSERT_EQ(1u, active_layer_->tilings()->num_tilings());
3770 }
3771
3772 TEST_F(NoLowResPictureLayerImplTest, ReleaseResources) {
3773 gfx::Size tile_size(400, 400);
3774 gfx::Size layer_bounds(1300, 1900);
3775
3776 scoped_refptr<FakePicturePileImpl> pending_pile =
3777 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
3778 scoped_refptr<FakePicturePileImpl> active_pile =
3779 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
3780
3781 SetupTrees(pending_pile, active_pile);
3782 EXPECT_EQ(1u, pending_layer_->tilings()->num_tilings());
3783 EXPECT_EQ(1u, active_layer_->tilings()->num_tilings());
3784
3785 // All tilings should be removed when losing output surface.
3786 active_layer_->ReleaseResources();
3787 EXPECT_FALSE(active_layer_->tilings());
3788 active_layer_->RecreateResources();
3789 EXPECT_EQ(0u, active_layer_->tilings()->num_tilings());
3790 pending_layer_->ReleaseResources();
3791 EXPECT_FALSE(pending_layer_->tilings());
3792 pending_layer_->RecreateResources();
3793 EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings());
3794
3795 // This should create new tilings.
3796 SetupDrawPropertiesAndUpdateTiles(pending_layer_,
3797 1.3f, // ideal contents scale
3798 2.7f, // device scale
3799 3.2f, // page scale
3800 1.f, // maximum animation scale
3801 false);
3802 EXPECT_EQ(1u, pending_layer_->tilings()->num_tilings());
3803 }
3804
3805 TEST_F(PictureLayerImplTest, SharedQuadStateContainsMaxTilingScale) {
3806 scoped_ptr<RenderPass> render_pass = RenderPass::Create();
3807
3808 gfx::Size tile_size(400, 400);
3809 gfx::Size layer_bounds(1000, 2000);
3810
3811 host_impl_.SetViewportSize(gfx::Size(10000, 20000));
3812
3813 scoped_refptr<FakePicturePileImpl> pending_pile =
3814 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
3815 scoped_refptr<FakePicturePileImpl> active_pile =
3816 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
3817
3818 SetupTrees(pending_pile, active_pile);
3819
3820 ResetTilingsAndRasterScales();
3821 SetupDrawPropertiesAndUpdateTiles(active_layer_, 2.5f, 1.f, 1.f, 1.f, false);
3822
3823 float max_contents_scale = active_layer_->MaximumTilingContentsScale();
3824 EXPECT_EQ(2.5f, max_contents_scale);
3825
3826 gfx::Transform scaled_draw_transform = active_layer_->draw_transform();
3827 scaled_draw_transform.Scale(SK_MScalar1 / max_contents_scale,
3828 SK_MScalar1 / max_contents_scale);
3829
3830 AppendQuadsData data;
3831 active_layer_->AppendQuads(render_pass.get(), &data);
3832
3833 // SharedQuadState should have be of size 1, as we are doing AppenQuad once.
3834 EXPECT_EQ(1u, render_pass->shared_quad_state_list.size());
3835 // The content_to_target_transform should be scaled by the
3836 // MaximumTilingContentsScale on the layer.
3837 EXPECT_EQ(scaled_draw_transform.ToString(),
3838 render_pass->shared_quad_state_list.front()
3839 ->content_to_target_transform.ToString());
3840 // The content_bounds should be scaled by the
3841 // MaximumTilingContentsScale on the layer.
3842 EXPECT_EQ(
3843 gfx::Size(2500u, 5000u).ToString(),
3844 render_pass->shared_quad_state_list.front()->content_bounds.ToString());
3845 // The visible_content_rect should be scaled by the
3846 // MaximumTilingContentsScale on the layer.
3847 EXPECT_EQ(gfx::Rect(0u, 0u, 2500u, 5000u).ToString(),
3848 render_pass->shared_quad_state_list.front()
3849 ->visible_content_rect.ToString());
3850 }
3851
3852 class PictureLayerImplTestWithDelegatingRenderer : public PictureLayerImplTest {
3853 public:
3854 PictureLayerImplTestWithDelegatingRenderer() : PictureLayerImplTest() {}
3855
3856 void InitializeRenderer() override {
3857 host_impl_.InitializeRenderer(FakeOutputSurface::CreateDelegating3d());
3858 }
3859 };
3860
3861 TEST_F(PictureLayerImplTestWithDelegatingRenderer,
3862 DelegatingRendererWithTileOOM) {
3863 // This test is added for crbug.com/402321, where quad should be produced when
3864 // raster on demand is not allowed and tile is OOM.
3865 gfx::Size tile_size = host_impl_.settings().default_tile_size;
3866 gfx::Size layer_bounds(1000, 1000);
3867
3868 // Create tiles.
3869 scoped_refptr<FakePicturePileImpl> pending_pile =
3870 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
3871 SetupPendingTree(pending_pile);
3872 pending_layer_->SetBounds(layer_bounds);
3873 ActivateTree();
3874 bool update_lcd_text = false;
3875 host_impl_.active_tree()->UpdateDrawProperties(update_lcd_text);
3876 std::vector<Tile*> tiles =
3877 active_layer_->HighResTiling()->AllTilesForTesting();
3878 host_impl_.tile_manager()->InitializeTilesWithResourcesForTesting(tiles);
3879
3880 // Force tiles after max_tiles to be OOM. TileManager uses
3881 // GlobalStateThatImpactsTilesPriority from LayerTreeHostImpl, and we cannot
3882 // directly set state to host_impl_, so we set policy that would change the
3883 // state. We also need to update tree priority separately.
3884 GlobalStateThatImpactsTilePriority state;
3885 size_t max_tiles = 1;
3886 size_t memory_limit = max_tiles * 4 * tile_size.width() * tile_size.height();
3887 size_t resource_limit = max_tiles;
3888 ManagedMemoryPolicy policy(memory_limit,
3889 gpu::MemoryAllocation::CUTOFF_ALLOW_EVERYTHING,
3890 resource_limit);
3891 host_impl_.SetMemoryPolicy(policy);
3892 host_impl_.SetTreePriority(SAME_PRIORITY_FOR_BOTH_TREES);
3893 host_impl_.PrepareTiles();
3894
3895 scoped_ptr<RenderPass> render_pass = RenderPass::Create();
3896 AppendQuadsData data;
3897 active_layer_->WillDraw(DRAW_MODE_HARDWARE, nullptr);
3898 active_layer_->AppendQuads(render_pass.get(), &data);
3899 active_layer_->DidDraw(nullptr);
3900
3901 // Even when OOM, quads should be produced, and should be different material
3902 // from quads with resource.
3903 EXPECT_LT(max_tiles, render_pass->quad_list.size());
3904 EXPECT_EQ(DrawQuad::Material::TILED_CONTENT,
3905 render_pass->quad_list.front()->material);
3906 EXPECT_EQ(DrawQuad::Material::SOLID_COLOR,
3907 render_pass->quad_list.back()->material);
3908 }
3909
3910 class OcclusionTrackingSettings : public LowResTilingsSettings {
3911 public:
3912 OcclusionTrackingSettings() { use_occlusion_for_tile_prioritization = true; }
3913 };
3914
3915 class OcclusionTrackingPictureLayerImplTest : public PictureLayerImplTest {
3916 public:
3917 OcclusionTrackingPictureLayerImplTest()
3918 : PictureLayerImplTest(OcclusionTrackingSettings()) {}
3919
3920 void VerifyEvictionConsidersOcclusion(FakePictureLayerImpl* layer,
3921 FakePictureLayerImpl* twin_layer,
3922 WhichTree tree,
3923 size_t expected_occluded_tile_count) {
3924 WhichTree twin_tree = tree == ACTIVE_TREE ? PENDING_TREE : ACTIVE_TREE;
3925 for (int priority_count = 0; priority_count <= LAST_TREE_PRIORITY;
3926 ++priority_count) {
3927 TreePriority tree_priority = static_cast<TreePriority>(priority_count);
3928 size_t occluded_tile_count = 0u;
3929 Tile* last_tile = nullptr;
3930 std::set<Tile*> shared_tiles;
3931
3932 scoped_ptr<TilingSetEvictionQueue> queue(
3933 new TilingSetEvictionQueue(layer->picture_layer_tiling_set(),
3934 tree_priority, layer && twin_layer));
3935 while (!queue->IsEmpty()) {
3936 Tile* tile = queue->Top();
3937 if (!last_tile)
3938 last_tile = tile;
3939 if (tile->is_shared())
3940 EXPECT_TRUE(shared_tiles.insert(tile).second);
3941
3942 // The only way we will encounter an occluded tile after an unoccluded
3943 // tile is if the priorty bin decreased, the tile is required for
3944 // activation, or the scale changed.
3945 bool tile_is_occluded = tile->is_occluded(tree);
3946 if (tile_is_occluded) {
3947 occluded_tile_count++;
3948
3949 bool last_tile_is_occluded = last_tile->is_occluded(tree);
3950 if (!last_tile_is_occluded) {
3951 TilePriority::PriorityBin tile_priority_bin =
3952 tile->priority(tree).priority_bin;
3953 TilePriority::PriorityBin last_tile_priority_bin =
3954 last_tile->priority(tree).priority_bin;
3955
3956 EXPECT_TRUE(
3957 (tile_priority_bin < last_tile_priority_bin) ||
3958 tile->required_for_activation() ||
3959 (tile->contents_scale() != last_tile->contents_scale()));
3960 }
3961 }
3962 last_tile = tile;
3963 queue->Pop();
3964 }
3965 // Count also shared tiles which are occluded in the tree but which were
3966 // not returned by the tiling set eviction queue. Those shared tiles
3967 // shall be returned by the twin tiling set eviction queue.
3968 queue.reset(
3969 new TilingSetEvictionQueue(twin_layer->picture_layer_tiling_set(),
3970 tree_priority, layer && twin_layer));
3971 while (!queue->IsEmpty()) {
3972 Tile* tile = queue->Top();
3973 if (tile->is_shared()) {
3974 EXPECT_TRUE(shared_tiles.insert(tile).second);
3975 if (tile->is_occluded(tree))
3976 ++occluded_tile_count;
3977 // Check the reasons why the shared tile was not returned by
3978 // the first tiling set eviction queue.
3979 switch (tree_priority) {
3980 case SAME_PRIORITY_FOR_BOTH_TREES: {
3981 const TilePriority& priority = tile->priority(tree);
3982 const TilePriority& priority_for_tree_priority =
3983 tile->priority_for_tree_priority(tree_priority);
3984 const TilePriority& twin_priority = tile->priority(twin_tree);
3985 // Check if the shared tile was not returned by the first tiling
3986 // set eviction queue because it was out of order for the first
3987 // tiling set eviction queue but not for the twin tiling set
3988 // eviction queue.
3989 if (priority.priority_bin != twin_priority.priority_bin) {
3990 EXPECT_LT(priority_for_tree_priority.priority_bin,
3991 priority.priority_bin);
3992 EXPECT_EQ(priority_for_tree_priority.priority_bin,
3993 twin_priority.priority_bin);
3994 EXPECT_TRUE(priority_for_tree_priority.priority_bin <
3995 priority.priority_bin);
3996 } else if (tile->is_occluded(tree) !=
3997 tile->is_occluded(twin_tree)) {
3998 EXPECT_TRUE(tile->is_occluded(tree));
3999 EXPECT_FALSE(tile->is_occluded(twin_tree));
4000 EXPECT_FALSE(tile->is_occluded_combined());
4001 } else if (priority.distance_to_visible !=
4002 twin_priority.distance_to_visible) {
4003 EXPECT_LT(priority_for_tree_priority.distance_to_visible,
4004 priority.distance_to_visible);
4005 EXPECT_EQ(priority_for_tree_priority.distance_to_visible,
4006 twin_priority.distance_to_visible);
4007 EXPECT_TRUE(priority_for_tree_priority.distance_to_visible <
4008 priority.distance_to_visible);
4009 } else {
4010 // Shared tiles having the same active and pending priorities
4011 // should be returned only by a pending tree eviction queue.
4012 EXPECT_EQ(ACTIVE_TREE, tree);
4013 }
4014 break;
4015 }
4016 case SMOOTHNESS_TAKES_PRIORITY:
4017 // Shared tiles should be returned only by an active tree
4018 // eviction queue.
4019 EXPECT_EQ(PENDING_TREE, tree);
4020 break;
4021 case NEW_CONTENT_TAKES_PRIORITY:
4022 // Shared tiles should be returned only by a pending tree
4023 // eviction queue.
4024 EXPECT_EQ(ACTIVE_TREE, tree);
4025 break;
4026 }
4027 }
4028 queue->Pop();
4029 }
4030 EXPECT_EQ(expected_occluded_tile_count, occluded_tile_count);
4031 }
4032 }
4033 };
4034
4035 TEST_F(OcclusionTrackingPictureLayerImplTest,
4036 OccludedTilesSkippedDuringRasterization) {
4037 base::TimeTicks time_ticks;
4038 time_ticks += base::TimeDelta::FromMilliseconds(1);
4039 host_impl_.SetCurrentBeginFrameArgs(
4040 CreateBeginFrameArgsForTesting(BEGINFRAME_FROM_HERE, time_ticks));
4041
4042 gfx::Size tile_size(102, 102);
4043 gfx::Size layer_bounds(1000, 1000);
4044 gfx::Size viewport_size(500, 500);
4045 gfx::Point occluding_layer_position(310, 0);
4046
4047 host_impl_.SetViewportSize(viewport_size);
4048
4049 scoped_refptr<FakePicturePileImpl> pending_pile =
4050 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
4051 SetupPendingTreeWithFixedTileSize(pending_pile, tile_size, Region());
4052
4053 // No occlusion.
4054 int unoccluded_tile_count = 0;
4055 scoped_ptr<TilingSetRasterQueueAll> queue(new TilingSetRasterQueueAll(
4056 pending_layer_->picture_layer_tiling_set(), false));
4057 while (!queue->IsEmpty()) {
4058 Tile* tile = queue->Top();
4059
4060 // Occluded tiles should not be iterated over.
4061 EXPECT_FALSE(tile->is_occluded(PENDING_TREE));
4062
4063 // Some tiles may not be visible (i.e. outside the viewport). The rest are
4064 // visible and at least partially unoccluded, verified by the above expect.
4065 bool tile_is_visible =
4066 tile->content_rect().Intersects(pending_layer_->visible_content_rect());
4067 if (tile_is_visible)
4068 unoccluded_tile_count++;
4069 queue->Pop();
4070 }
4071 EXPECT_EQ(unoccluded_tile_count, 25);
4072
4073 // Partial occlusion.
4074 pending_layer_->AddChild(LayerImpl::Create(host_impl_.pending_tree(), 1));
4075 LayerImpl* layer1 = pending_layer_->children()[0];
4076 layer1->SetBounds(layer_bounds);
4077 layer1->SetContentBounds(layer_bounds);
4078 layer1->SetDrawsContent(true);
4079 layer1->SetContentsOpaque(true);
4080 layer1->SetPosition(occluding_layer_position);
4081
4082 time_ticks += base::TimeDelta::FromMilliseconds(200);
4083 host_impl_.SetCurrentBeginFrameArgs(
4084 CreateBeginFrameArgsForTesting(BEGINFRAME_FROM_HERE, time_ticks));
4085 bool update_lcd_text = false;
4086 host_impl_.pending_tree()->UpdateDrawProperties(update_lcd_text);
4087
4088 unoccluded_tile_count = 0;
4089 queue.reset(new TilingSetRasterQueueAll(
4090 pending_layer_->picture_layer_tiling_set(), false));
4091 while (!queue->IsEmpty()) {
4092 Tile* tile = queue->Top();
4093
4094 EXPECT_FALSE(tile->is_occluded(PENDING_TREE));
4095
4096 bool tile_is_visible =
4097 tile->content_rect().Intersects(pending_layer_->visible_content_rect());
4098 if (tile_is_visible)
4099 unoccluded_tile_count++;
4100 queue->Pop();
4101 }
4102 EXPECT_EQ(20, unoccluded_tile_count);
4103
4104 // Full occlusion.
4105 layer1->SetPosition(gfx::Point(0, 0));
4106
4107 time_ticks += base::TimeDelta::FromMilliseconds(200);
4108 host_impl_.SetCurrentBeginFrameArgs(
4109 CreateBeginFrameArgsForTesting(BEGINFRAME_FROM_HERE, time_ticks));
4110 host_impl_.pending_tree()->UpdateDrawProperties(update_lcd_text);
4111
4112 unoccluded_tile_count = 0;
4113 queue.reset(new TilingSetRasterQueueAll(
4114 pending_layer_->picture_layer_tiling_set(), false));
4115 while (!queue->IsEmpty()) {
4116 Tile* tile = queue->Top();
4117
4118 EXPECT_FALSE(tile->is_occluded(PENDING_TREE));
4119
4120 bool tile_is_visible =
4121 tile->content_rect().Intersects(pending_layer_->visible_content_rect());
4122 if (tile_is_visible)
4123 unoccluded_tile_count++;
4124 queue->Pop();
4125 }
4126 EXPECT_EQ(unoccluded_tile_count, 0);
4127 }
4128
4129 TEST_F(OcclusionTrackingPictureLayerImplTest,
4130 OccludedTilesNotMarkedAsRequired) {
4131 base::TimeTicks time_ticks;
4132 time_ticks += base::TimeDelta::FromMilliseconds(1);
4133 host_impl_.SetCurrentBeginFrameArgs(
4134 CreateBeginFrameArgsForTesting(BEGINFRAME_FROM_HERE, time_ticks));
4135
4136 gfx::Size tile_size(102, 102);
4137 gfx::Size layer_bounds(1000, 1000);
4138 gfx::Size viewport_size(500, 500);
4139 gfx::Point occluding_layer_position(310, 0);
4140
4141 host_impl_.SetViewportSize(viewport_size);
4142
4143 scoped_refptr<FakePicturePileImpl> pending_pile =
4144 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
4145 SetupPendingTreeWithFixedTileSize(pending_pile, tile_size, Region());
4146
4147 // No occlusion.
4148 int occluded_tile_count = 0;
4149 for (size_t i = 0; i < pending_layer_->num_tilings(); ++i) {
4150 PictureLayerTiling* tiling = pending_layer_->tilings()->tiling_at(i);
4151
4152 occluded_tile_count = 0;
4153 for (PictureLayerTiling::CoverageIterator iter(
4154 tiling,
4155 pending_layer_->contents_scale_x(),
4156 gfx::Rect(layer_bounds));
4157 iter;
4158 ++iter) {
4159 if (!*iter)
4160 continue;
4161 const Tile* tile = *iter;
4162
4163 // Fully occluded tiles are not required for activation.
4164 if (tile->is_occluded(PENDING_TREE)) {
4165 EXPECT_FALSE(tile->required_for_activation());
4166 occluded_tile_count++;
4167 }
4168 }
4169 EXPECT_EQ(occluded_tile_count, 0);
4170 }
4171
4172 // Partial occlusion.
4173 pending_layer_->AddChild(LayerImpl::Create(host_impl_.pending_tree(), 1));
4174 LayerImpl* layer1 = pending_layer_->children()[0];
4175 layer1->SetBounds(layer_bounds);
4176 layer1->SetContentBounds(layer_bounds);
4177 layer1->SetDrawsContent(true);
4178 layer1->SetContentsOpaque(true);
4179 layer1->SetPosition(occluding_layer_position);
4180
4181 time_ticks += base::TimeDelta::FromMilliseconds(200);
4182 host_impl_.SetCurrentBeginFrameArgs(
4183 CreateBeginFrameArgsForTesting(BEGINFRAME_FROM_HERE, time_ticks));
4184 bool update_lcd_text = false;
4185 host_impl_.pending_tree()->UpdateDrawProperties(update_lcd_text);
4186
4187 for (size_t i = 0; i < pending_layer_->num_tilings(); ++i) {
4188 PictureLayerTiling* tiling = pending_layer_->tilings()->tiling_at(i);
4189 tiling->UpdateAllTilePrioritiesForTesting();
4190
4191 occluded_tile_count = 0;
4192 for (PictureLayerTiling::CoverageIterator iter(
4193 tiling,
4194 pending_layer_->contents_scale_x(),
4195 gfx::Rect(layer_bounds));
4196 iter;
4197 ++iter) {
4198 if (!*iter)
4199 continue;
4200 const Tile* tile = *iter;
4201
4202 if (tile->is_occluded(PENDING_TREE)) {
4203 EXPECT_FALSE(tile->required_for_activation());
4204 occluded_tile_count++;
4205 }
4206 }
4207 switch (i) {
4208 case 0:
4209 EXPECT_EQ(occluded_tile_count, 5);
4210 break;
4211 case 1:
4212 EXPECT_EQ(occluded_tile_count, 2);
4213 break;
4214 default:
4215 NOTREACHED();
4216 }
4217 }
4218
4219 // Full occlusion.
4220 layer1->SetPosition(gfx::PointF(0, 0));
4221
4222 time_ticks += base::TimeDelta::FromMilliseconds(200);
4223 host_impl_.SetCurrentBeginFrameArgs(
4224 CreateBeginFrameArgsForTesting(BEGINFRAME_FROM_HERE, time_ticks));
4225 host_impl_.pending_tree()->UpdateDrawProperties(update_lcd_text);
4226
4227 for (size_t i = 0; i < pending_layer_->num_tilings(); ++i) {
4228 PictureLayerTiling* tiling = pending_layer_->tilings()->tiling_at(i);
4229 tiling->UpdateAllTilePrioritiesForTesting();
4230
4231 occluded_tile_count = 0;
4232 for (PictureLayerTiling::CoverageIterator iter(
4233 tiling,
4234 pending_layer_->contents_scale_x(),
4235 gfx::Rect(layer_bounds));
4236 iter;
4237 ++iter) {
4238 if (!*iter)
4239 continue;
4240 const Tile* tile = *iter;
4241
4242 if (tile->is_occluded(PENDING_TREE)) {
4243 EXPECT_FALSE(tile->required_for_activation());
4244 occluded_tile_count++;
4245 }
4246 }
4247 switch (i) {
4248 case 0:
4249 EXPECT_EQ(25, occluded_tile_count);
4250 break;
4251 case 1:
4252 EXPECT_EQ(4, occluded_tile_count);
4253 break;
4254 default:
4255 NOTREACHED();
4256 }
4257 }
4258 }
4259
4260 TEST_F(OcclusionTrackingPictureLayerImplTest, OcclusionForDifferentScales) {
4261 base::TimeTicks time_ticks;
4262 time_ticks += base::TimeDelta::FromMilliseconds(1);
4263 host_impl_.SetCurrentBeginFrameArgs(
4264 CreateBeginFrameArgsForTesting(BEGINFRAME_FROM_HERE, time_ticks));
4265
4266 gfx::Size tile_size(102, 102);
4267 gfx::Size layer_bounds(1000, 1000);
4268 gfx::Size viewport_size(500, 500);
4269 gfx::Point occluding_layer_position(310, 0);
4270
4271 scoped_refptr<FakePicturePileImpl> pending_pile =
4272 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
4273
4274 host_impl_.SetViewportSize(viewport_size);
4275
4276 SetupPendingTreeWithFixedTileSize(pending_pile, tile_size, Region());
4277 ASSERT_TRUE(pending_layer_->CanHaveTilings());
4278
4279 pending_layer_->AddChild(LayerImpl::Create(host_impl_.pending_tree(), 1));
4280 LayerImpl* layer1 = pending_layer_->children()[0];
4281 layer1->SetBounds(layer_bounds);
4282 layer1->SetContentBounds(layer_bounds);
4283 layer1->SetDrawsContent(true);
4284 layer1->SetContentsOpaque(true);
4285 layer1->SetPosition(occluding_layer_position);
4286
4287 pending_layer_->tilings()->RemoveAllTilings();
4288 float low_res_factor = host_impl_.settings().low_res_contents_scale_factor;
4289 pending_layer_->AddTiling(low_res_factor);
4290 pending_layer_->AddTiling(0.3f);
4291 pending_layer_->AddTiling(0.7f);
4292 pending_layer_->AddTiling(1.0f);
4293 pending_layer_->AddTiling(2.0f);
4294
4295 time_ticks += base::TimeDelta::FromMilliseconds(1);
4296 host_impl_.SetCurrentBeginFrameArgs(
4297 CreateBeginFrameArgsForTesting(BEGINFRAME_FROM_HERE, time_ticks));
4298 // UpdateDrawProperties with the occluding layer.
4299 bool update_lcd_text = false;
4300 host_impl_.pending_tree()->UpdateDrawProperties(update_lcd_text);
4301
4302 EXPECT_EQ(5u, pending_layer_->num_tilings());
4303
4304 int occluded_tile_count = 0;
4305 for (size_t i = 0; i < pending_layer_->num_tilings(); ++i) {
4306 PictureLayerTiling* tiling = pending_layer_->tilings()->tiling_at(i);
4307 tiling->UpdateAllTilePrioritiesForTesting();
4308 std::vector<Tile*> tiles = tiling->AllTilesForTesting();
4309
4310 occluded_tile_count = 0;
4311 for (size_t j = 0; j < tiles.size(); ++j) {
4312 if (tiles[j]->is_occluded(PENDING_TREE)) {
4313 gfx::Rect scaled_content_rect = ScaleToEnclosingRect(
4314 tiles[j]->content_rect(), 1.0f / tiles[j]->contents_scale());
4315 EXPECT_GE(scaled_content_rect.x(), occluding_layer_position.x());
4316 occluded_tile_count++;
4317 }
4318 }
4319
4320 switch (i) {
4321 case 0:
4322 EXPECT_EQ(occluded_tile_count, 30);
4323 break;
4324 case 1:
4325 EXPECT_EQ(occluded_tile_count, 5);
4326 break;
4327 case 2:
4328 EXPECT_EQ(occluded_tile_count, 4);
4329 break;
4330 case 4:
4331 case 3:
4332 EXPECT_EQ(occluded_tile_count, 2);
4333 break;
4334 default:
4335 NOTREACHED();
4336 }
4337 }
4338 }
4339
4340 TEST_F(OcclusionTrackingPictureLayerImplTest, DifferentOcclusionOnTrees) {
4341 gfx::Size tile_size(102, 102);
4342 gfx::Size layer_bounds(1000, 1000);
4343 gfx::Size viewport_size(1000, 1000);
4344 gfx::Point occluding_layer_position(310, 0);
4345 gfx::Rect invalidation_rect(230, 230, 102, 102);
4346
4347 scoped_refptr<FakePicturePileImpl> pending_pile =
4348 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
4349 scoped_refptr<FakePicturePileImpl> active_pile =
4350 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
4351
4352 host_impl_.SetViewportSize(viewport_size);
4353 SetupPendingTree(active_pile);
4354
4355 // Partially occlude the active layer.
4356 pending_layer_->AddChild(LayerImpl::Create(host_impl_.pending_tree(), 2));
4357 LayerImpl* layer1 = pending_layer_->children()[0];
4358 layer1->SetBounds(layer_bounds);
4359 layer1->SetContentBounds(layer_bounds);
4360 layer1->SetDrawsContent(true);
4361 layer1->SetContentsOpaque(true);
4362 layer1->SetPosition(occluding_layer_position);
4363
4364 ActivateTree();
4365
4366 // Partially invalidate the pending layer.
4367 SetupPendingTreeWithInvalidation(pending_pile, invalidation_rect);
4368
4369 for (size_t i = 0; i < pending_layer_->num_tilings(); ++i) {
4370 PictureLayerTiling* tiling = pending_layer_->tilings()->tiling_at(i);
4371 tiling->UpdateAllTilePrioritiesForTesting();
4372
4373 for (PictureLayerTiling::CoverageIterator iter(
4374 tiling,
4375 pending_layer_->contents_scale_x(),
4376 gfx::Rect(layer_bounds));
4377 iter;
4378 ++iter) {
4379 if (!*iter)
4380 continue;
4381 const Tile* tile = *iter;
4382
4383 // All tiles are unoccluded on the pending tree.
4384 EXPECT_FALSE(tile->is_occluded(PENDING_TREE));
4385
4386 Tile* twin_tile = pending_layer_->GetPendingOrActiveTwinTiling(tiling)
4387 ->TileAt(iter.i(), iter.j());
4388 gfx::Rect scaled_content_rect = ScaleToEnclosingRect(
4389 tile->content_rect(), 1.0f / tile->contents_scale());
4390
4391 if (scaled_content_rect.Intersects(invalidation_rect)) {
4392 // Tiles inside the invalidation rect are only on the pending tree.
4393 EXPECT_NE(tile, twin_tile);
4394
4395 // Unshared tiles should be unoccluded on the active tree by default.
4396 EXPECT_FALSE(tile->is_occluded(ACTIVE_TREE));
4397 } else {
4398 // Tiles outside the invalidation rect are shared between both trees.
4399 EXPECT_EQ(tile, twin_tile);
4400 // Shared tiles are occluded on the active tree iff they lie beneath the
4401 // occluding layer.
4402 EXPECT_EQ(tile->is_occluded(ACTIVE_TREE),
4403 scaled_content_rect.x() >= occluding_layer_position.x());
4404 }
4405 }
4406 }
4407
4408 for (size_t i = 0; i < active_layer_->num_tilings(); ++i) {
4409 PictureLayerTiling* tiling = active_layer_->tilings()->tiling_at(i);
4410
4411 for (PictureLayerTiling::CoverageIterator iter(
4412 tiling,
4413 active_layer_->contents_scale_x(),
4414 gfx::Rect(layer_bounds));
4415 iter;
4416 ++iter) {
4417 if (!*iter)
4418 continue;
4419 const Tile* tile = *iter;
4420
4421 Tile* twin_tile = active_layer_->GetPendingOrActiveTwinTiling(tiling)
4422 ->TileAt(iter.i(), iter.j());
4423 gfx::Rect scaled_content_rect = ScaleToEnclosingRect(
4424 tile->content_rect(), 1.0f / tile->contents_scale());
4425
4426 // Since we've already checked the shared tiles, only consider tiles in
4427 // the invalidation rect.
4428 if (scaled_content_rect.Intersects(invalidation_rect)) {
4429 // Tiles inside the invalidation rect are only on the active tree.
4430 EXPECT_NE(tile, twin_tile);
4431
4432 // Unshared tiles should be unoccluded on the pending tree by default.
4433 EXPECT_FALSE(tile->is_occluded(PENDING_TREE));
4434
4435 // Unshared tiles are occluded on the active tree iff they lie beneath
4436 // the occluding layer.
4437 EXPECT_EQ(tile->is_occluded(ACTIVE_TREE),
4438 scaled_content_rect.x() >= occluding_layer_position.x());
4439 }
4440 }
4441 }
4442 }
4443
4444 TEST_F(OcclusionTrackingPictureLayerImplTest,
4445 OccludedTilesConsideredDuringEviction) {
4446 base::TimeTicks time_ticks;
4447 time_ticks += base::TimeDelta::FromMilliseconds(1);
4448 host_impl_.SetCurrentBeginFrameArgs(
4449 CreateBeginFrameArgsForTesting(BEGINFRAME_FROM_HERE, time_ticks));
4450
4451 gfx::Size tile_size(102, 102);
4452 gfx::Size layer_bounds(1000, 1000);
4453 gfx::Size viewport_size(1000, 1000);
4454 gfx::Point pending_occluding_layer_position(310, 0);
4455 gfx::Point active_occluding_layer_position(0, 310);
4456 gfx::Rect invalidation_rect(230, 230, 102, 102);
4457
4458 host_impl_.SetViewportSize(viewport_size);
4459 host_impl_.SetDeviceScaleFactor(2.f);
4460
4461 scoped_refptr<FakePicturePileImpl> pending_pile =
4462 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
4463 scoped_refptr<FakePicturePileImpl> active_pile =
4464 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
4465
4466 SetupPendingTreeWithFixedTileSize(active_pile, tile_size, Region());
4467
4468 // Partially occlude the active layer.
4469 pending_layer_->AddChild(LayerImpl::Create(host_impl_.pending_tree(), 2));
4470 LayerImpl* active_occluding_layer = pending_layer_->children()[0];
4471 active_occluding_layer->SetBounds(layer_bounds);
4472 active_occluding_layer->SetContentBounds(layer_bounds);
4473 active_occluding_layer->SetDrawsContent(true);
4474 active_occluding_layer->SetContentsOpaque(true);
4475 active_occluding_layer->SetPosition(active_occluding_layer_position);
4476
4477 ActivateTree();
4478
4479 // Partially invalidate the pending layer. Tiles inside the invalidation rect
4480 // are not shared between trees.
4481 SetupPendingTreeWithFixedTileSize(pending_pile, tile_size, invalidation_rect);
4482
4483 // Partially occlude the pending layer in a different way.
4484 pending_layer_->AddChild(LayerImpl::Create(host_impl_.pending_tree(), 3));
4485 LayerImpl* pending_occluding_layer = pending_layer_->children()[0];
4486 pending_occluding_layer->SetBounds(layer_bounds);
4487 pending_occluding_layer->SetContentBounds(layer_bounds);
4488 pending_occluding_layer->SetDrawsContent(true);
4489 pending_occluding_layer->SetContentsOpaque(true);
4490 pending_occluding_layer->SetPosition(pending_occluding_layer_position);
4491
4492 EXPECT_EQ(2u, pending_layer_->num_tilings());
4493 EXPECT_EQ(2u, active_layer_->num_tilings());
4494
4495 time_ticks += base::TimeDelta::FromMilliseconds(1);
4496 host_impl_.SetCurrentBeginFrameArgs(
4497 CreateBeginFrameArgsForTesting(BEGINFRAME_FROM_HERE, time_ticks));
4498 // UpdateDrawProperties with the occluding layer.
4499 bool update_lcd_text = false;
4500 host_impl_.pending_tree()->UpdateDrawProperties(update_lcd_text);
4501
4502 // The expected number of occluded tiles on each of the 2 tilings for each of
4503 // the 3 tree priorities.
4504 size_t expected_occluded_tile_count_on_both[] = {9u, 1u};
4505 size_t expected_occluded_tile_count_on_active[] = {30u, 3u};
4506 size_t expected_occluded_tile_count_on_pending[] = {30u, 3u};
4507
4508 size_t total_expected_occluded_tile_count_on_trees[] = {33u, 33u};
4509
4510 // Verify number of occluded tiles on the pending layer for each tiling.
4511 for (size_t i = 0; i < pending_layer_->num_tilings(); ++i) {
4512 PictureLayerTiling* tiling = pending_layer_->tilings()->tiling_at(i);
4513 tiling->UpdateAllTilePrioritiesForTesting();
4514
4515 size_t occluded_tile_count_on_pending = 0u;
4516 size_t occluded_tile_count_on_active = 0u;
4517 size_t occluded_tile_count_on_both = 0u;
4518 for (PictureLayerTiling::CoverageIterator iter(tiling, 1.f,
4519 gfx::Rect(layer_bounds));
4520 iter; ++iter) {
4521 Tile* tile = *iter;
4522
4523 if (!tile)
4524 continue;
4525 if (tile->is_occluded(PENDING_TREE))
4526 occluded_tile_count_on_pending++;
4527 if (tile->is_occluded(ACTIVE_TREE))
4528 occluded_tile_count_on_active++;
4529 if (tile->is_occluded(PENDING_TREE) && tile->is_occluded(ACTIVE_TREE))
4530 occluded_tile_count_on_both++;
4531 }
4532 EXPECT_EQ(expected_occluded_tile_count_on_pending[i],
4533 occluded_tile_count_on_pending)
4534 << tiling->contents_scale();
4535 EXPECT_EQ(expected_occluded_tile_count_on_active[i],
4536 occluded_tile_count_on_active)
4537 << tiling->contents_scale();
4538 EXPECT_EQ(expected_occluded_tile_count_on_both[i],
4539 occluded_tile_count_on_both)
4540 << tiling->contents_scale();
4541 }
4542
4543 // Verify number of occluded tiles on the active layer for each tiling.
4544 for (size_t i = 0; i < active_layer_->num_tilings(); ++i) {
4545 PictureLayerTiling* tiling = active_layer_->tilings()->tiling_at(i);
4546 tiling->UpdateAllTilePrioritiesForTesting();
4547
4548 size_t occluded_tile_count_on_pending = 0u;
4549 size_t occluded_tile_count_on_active = 0u;
4550 size_t occluded_tile_count_on_both = 0u;
4551 for (PictureLayerTiling::CoverageIterator iter(
4552 tiling,
4553 pending_layer_->contents_scale_x(),
4554 gfx::Rect(layer_bounds));
4555 iter;
4556 ++iter) {
4557 Tile* tile = *iter;
4558
4559 if (!tile)
4560 continue;
4561 if (tile->is_occluded(PENDING_TREE))
4562 occluded_tile_count_on_pending++;
4563 if (tile->is_occluded(ACTIVE_TREE))
4564 occluded_tile_count_on_active++;
4565 if (tile->is_occluded(PENDING_TREE) && tile->is_occluded(ACTIVE_TREE))
4566 occluded_tile_count_on_both++;
4567 }
4568 EXPECT_EQ(expected_occluded_tile_count_on_pending[i],
4569 occluded_tile_count_on_pending)
4570 << i;
4571 EXPECT_EQ(expected_occluded_tile_count_on_active[i],
4572 occluded_tile_count_on_active)
4573 << i;
4574 EXPECT_EQ(expected_occluded_tile_count_on_both[i],
4575 occluded_tile_count_on_both)
4576 << i;
4577 }
4578
4579 std::vector<Tile*> all_tiles;
4580 for (size_t i = 0; i < pending_layer_->num_tilings(); ++i) {
4581 PictureLayerTiling* tiling = pending_layer_->tilings()->tiling_at(i);
4582 std::vector<Tile*> tiles = tiling->AllTilesForTesting();
4583 all_tiles.insert(all_tiles.end(), tiles.begin(), tiles.end());
4584 }
4585
4586 host_impl_.tile_manager()->InitializeTilesWithResourcesForTesting(all_tiles);
4587
4588 VerifyEvictionConsidersOcclusion(
4589 pending_layer_, active_layer_, PENDING_TREE,
4590 total_expected_occluded_tile_count_on_trees[PENDING_TREE]);
4591 VerifyEvictionConsidersOcclusion(
4592 active_layer_, pending_layer_, ACTIVE_TREE,
4593 total_expected_occluded_tile_count_on_trees[ACTIVE_TREE]);
4594
4595 // Repeat the tests without valid active tree priorities.
4596 active_layer_->set_has_valid_tile_priorities(false);
4597 VerifyEvictionConsidersOcclusion(
4598 pending_layer_, active_layer_, PENDING_TREE,
4599 total_expected_occluded_tile_count_on_trees[PENDING_TREE]);
4600 VerifyEvictionConsidersOcclusion(
4601 active_layer_, pending_layer_, ACTIVE_TREE, 0u);
4602 active_layer_->set_has_valid_tile_priorities(true);
4603
4604 // Repeat the tests without valid pending tree priorities.
4605 pending_layer_->set_has_valid_tile_priorities(false);
4606 VerifyEvictionConsidersOcclusion(
4607 active_layer_, pending_layer_, ACTIVE_TREE,
4608 total_expected_occluded_tile_count_on_trees[ACTIVE_TREE]);
4609 VerifyEvictionConsidersOcclusion(
4610 pending_layer_, active_layer_, PENDING_TREE, 0u);
4611 pending_layer_->set_has_valid_tile_priorities(true);
4612 }
4613
4614 TEST_F(PictureLayerImplTest, PendingOrActiveTwinLayer) {
4615 gfx::Size tile_size(102, 102);
4616 gfx::Size layer_bounds(1000, 1000);
4617
4618 scoped_refptr<FakePicturePileImpl> pile =
4619 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
4620 SetupPendingTree(pile);
4621 EXPECT_FALSE(pending_layer_->GetPendingOrActiveTwinLayer());
4622
4623 ActivateTree();
4624 EXPECT_FALSE(active_layer_->GetPendingOrActiveTwinLayer());
4625
4626 SetupPendingTree(pile);
4627 EXPECT_TRUE(pending_layer_->GetPendingOrActiveTwinLayer());
4628 EXPECT_TRUE(active_layer_->GetPendingOrActiveTwinLayer());
4629 EXPECT_EQ(pending_layer_, active_layer_->GetPendingOrActiveTwinLayer());
4630 EXPECT_EQ(active_layer_, pending_layer_->GetPendingOrActiveTwinLayer());
4631
4632 ActivateTree();
4633 EXPECT_FALSE(active_layer_->GetPendingOrActiveTwinLayer());
4634
4635 // Make an empty pending tree.
4636 host_impl_.CreatePendingTree();
4637 host_impl_.pending_tree()->DetachLayerTree();
4638 EXPECT_FALSE(active_layer_->GetPendingOrActiveTwinLayer());
4639 }
4640
4641 TEST_F(PictureLayerImplTest, RecycledTwinLayer) {
4642 gfx::Size tile_size(102, 102);
4643 gfx::Size layer_bounds(1000, 1000);
4644
4645 scoped_refptr<FakePicturePileImpl> pile =
4646 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
4647 SetupPendingTree(pile);
4648 EXPECT_FALSE(pending_layer_->GetRecycledTwinLayer());
4649
4650 ActivateTree();
4651 EXPECT_TRUE(active_layer_->GetRecycledTwinLayer());
4652 EXPECT_EQ(old_pending_layer_, active_layer_->GetRecycledTwinLayer());
4653
4654 SetupPendingTree(pile);
4655 EXPECT_FALSE(pending_layer_->GetRecycledTwinLayer());
4656 EXPECT_FALSE(active_layer_->GetRecycledTwinLayer());
4657
4658 ActivateTree();
4659 EXPECT_TRUE(active_layer_->GetRecycledTwinLayer());
4660 EXPECT_EQ(old_pending_layer_, active_layer_->GetRecycledTwinLayer());
4661
4662 // Make an empty pending tree.
4663 host_impl_.CreatePendingTree();
4664 host_impl_.pending_tree()->DetachLayerTree();
4665 EXPECT_FALSE(active_layer_->GetRecycledTwinLayer());
4666 }
4667
4668 void PictureLayerImplTest::TestQuadsForSolidColor(bool test_for_solid) {
4669 base::TimeTicks time_ticks;
4670 time_ticks += base::TimeDelta::FromMilliseconds(1);
4671 host_impl_.SetCurrentBeginFrameArgs(
4672 CreateBeginFrameArgsForTesting(BEGINFRAME_FROM_HERE, time_ticks));
4673
4674 gfx::Size tile_size(100, 100);
4675 gfx::Size layer_bounds(200, 200);
4676 gfx::Rect layer_rect(layer_bounds);
4677
4678 FakeContentLayerClient client;
4679 scoped_refptr<PictureLayer> layer = PictureLayer::Create(&client);
4680 FakeLayerTreeHostClient host_client(FakeLayerTreeHostClient::DIRECT_3D);
4681 scoped_ptr<FakeLayerTreeHost> host = FakeLayerTreeHost::Create(&host_client);
4682 host->SetRootLayer(layer);
4683 RecordingSource* recording_source = layer->GetRecordingSourceForTesting();
4684
4685 int frame_number = 0;
4686
4687 client.set_fill_with_nonsolid_color(!test_for_solid);
4688
4689 Region invalidation(layer_rect);
4690 recording_source->UpdateAndExpandInvalidation(
4691 &client, &invalidation, layer_bounds, layer_rect, frame_number++,
4692 RecordingSource::RECORD_NORMALLY);
4693
4694 scoped_refptr<RasterSource> pending_raster_source =
4695 recording_source->CreateRasterSource(true);
4696
4697 SetupPendingTreeWithFixedTileSize(pending_raster_source, tile_size, Region());
4698 ActivateTree();
4699
4700 if (test_for_solid) {
4701 EXPECT_EQ(0u, active_layer_->tilings()->num_tilings());
4702 } else {
4703 ASSERT_TRUE(active_layer_->tilings());
4704 ASSERT_GT(active_layer_->tilings()->num_tilings(), 0u);
4705 std::vector<Tile*> tiles =
4706 active_layer_->tilings()->tiling_at(0)->AllTilesForTesting();
4707 EXPECT_FALSE(tiles.empty());
4708 host_impl_.tile_manager()->InitializeTilesWithResourcesForTesting(tiles);
4709 }
4710
4711 scoped_ptr<RenderPass> render_pass = RenderPass::Create();
4712 AppendQuadsData data;
4713 active_layer_->WillDraw(DRAW_MODE_SOFTWARE, nullptr);
4714 active_layer_->AppendQuads(render_pass.get(), &data);
4715 active_layer_->DidDraw(nullptr);
4716
4717 DrawQuad::Material expected = test_for_solid
4718 ? DrawQuad::Material::SOLID_COLOR
4719 : DrawQuad::Material::TILED_CONTENT;
4720 EXPECT_EQ(expected, render_pass->quad_list.front()->material);
4721 }
4722
4723 TEST_F(PictureLayerImplTest, DrawSolidQuads) {
4724 TestQuadsForSolidColor(true);
4725 }
4726
4727 TEST_F(PictureLayerImplTest, DrawNonSolidQuads) {
4728 TestQuadsForSolidColor(false);
4729 }
4730
4731 TEST_F(PictureLayerImplTest, NonSolidToSolidNoTilings) {
4732 base::TimeTicks time_ticks;
4733 time_ticks += base::TimeDelta::FromMilliseconds(1);
4734 host_impl_.SetCurrentBeginFrameArgs(
4735 CreateBeginFrameArgsForTesting(BEGINFRAME_FROM_HERE, time_ticks));
4736
4737 gfx::Size tile_size(100, 100);
4738 gfx::Size layer_bounds(200, 200);
4739 gfx::Rect layer_rect(layer_bounds);
4740
4741 FakeContentLayerClient client;
4742 scoped_refptr<PictureLayer> layer = PictureLayer::Create(&client);
4743 FakeLayerTreeHostClient host_client(FakeLayerTreeHostClient::DIRECT_3D);
4744 scoped_ptr<FakeLayerTreeHost> host = FakeLayerTreeHost::Create(&host_client);
4745 host->SetRootLayer(layer);
4746 RecordingSource* recording_source = layer->GetRecordingSourceForTesting();
4747
4748 int frame_number = 0;
4749
4750 client.set_fill_with_nonsolid_color(true);
4751
4752 Region invalidation1(layer_rect);
4753 recording_source->UpdateAndExpandInvalidation(
4754 &client, &invalidation1, layer_bounds, layer_rect, frame_number++,
4755 RecordingSource::RECORD_NORMALLY);
4756
4757 scoped_refptr<RasterSource> raster_source1 =
4758 recording_source->CreateRasterSource(true);
4759
4760 SetupPendingTree(raster_source1);
4761 ActivateTree();
4762 bool update_lcd_text = false;
4763 host_impl_.active_tree()->UpdateDrawProperties(update_lcd_text);
4764
4765 // We've started with a solid layer that contains some tilings.
4766 ASSERT_TRUE(active_layer_->tilings());
4767 EXPECT_NE(0u, active_layer_->tilings()->num_tilings());
4768
4769 client.set_fill_with_nonsolid_color(false);
4770
4771 Region invalidation2(layer_rect);
4772 recording_source->UpdateAndExpandInvalidation(
4773 &client, &invalidation2, layer_bounds, layer_rect, frame_number++,
4774 RecordingSource::RECORD_NORMALLY);
4775
4776 scoped_refptr<RasterSource> raster_source2 =
4777 recording_source->CreateRasterSource(true);
4778
4779 SetupPendingTree(raster_source2);
4780 ActivateTree();
4781
4782 // We've switched to a solid color, so we should end up with no tilings.
4783 ASSERT_TRUE(active_layer_->tilings());
4784 EXPECT_EQ(0u, active_layer_->tilings()->num_tilings());
4785 }
4786
4787 TEST_F(PictureLayerImplTest, ChangeInViewportAllowsTilingUpdates) {
4788 base::TimeTicks time_ticks;
4789 time_ticks += base::TimeDelta::FromMilliseconds(1);
4790 host_impl_.SetCurrentBeginFrameArgs(
4791 CreateBeginFrameArgsForTesting(BEGINFRAME_FROM_HERE, time_ticks));
4792
4793 gfx::Size tile_size(100, 100);
4794 gfx::Size layer_bounds(400, 4000);
4795
4796 scoped_refptr<FakePicturePileImpl> pending_pile =
4797 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
4798 scoped_refptr<FakePicturePileImpl> active_pile =
4799 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
4800
4801 SetupTrees(pending_pile, active_pile);
4802
4803 Region invalidation;
4804 gfx::Rect viewport = gfx::Rect(0, 0, 100, 100);
4805 gfx::Transform transform;
4806
4807 host_impl_.SetRequiresHighResToDraw();
4808
4809 // Update tiles.
4810 pending_layer_->draw_properties().visible_content_rect = viewport;
4811 pending_layer_->draw_properties().screen_space_transform = transform;
4812 SetupDrawPropertiesAndUpdateTiles(pending_layer_, 1.f, 1.f, 1.f, 1.f, false);
4813 pending_layer_->HighResTiling()->UpdateAllTilePrioritiesForTesting();
4814
4815 // Ensure we can't activate.
4816 EXPECT_FALSE(host_impl_.tile_manager()->IsReadyToActivate());
4817
4818 // Now in the same frame, move the viewport (this can happen during
4819 // animation).
4820 viewport = gfx::Rect(0, 2000, 100, 100);
4821
4822 // Update tiles.
4823 pending_layer_->draw_properties().visible_content_rect = viewport;
4824 pending_layer_->draw_properties().screen_space_transform = transform;
4825 SetupDrawPropertiesAndUpdateTiles(pending_layer_, 1.f, 1.f, 1.f, 1.f, false);
4826 pending_layer_->HighResTiling()->UpdateAllTilePrioritiesForTesting();
4827
4828 // Make sure all viewport tiles (viewport from the tiling) are ready to draw.
4829 std::vector<Tile*> tiles;
4830 for (PictureLayerTiling::CoverageIterator iter(
4831 pending_layer_->HighResTiling(),
4832 1.f,
4833 pending_layer_->HighResTiling()->GetCurrentVisibleRectForTesting());
4834 iter;
4835 ++iter) {
4836 if (*iter)
4837 tiles.push_back(*iter);
4838 }
4839
4840 host_impl_.tile_manager()->InitializeTilesWithResourcesForTesting(tiles);
4841
4842 // Ensure we can activate.
4843 EXPECT_TRUE(host_impl_.tile_manager()->IsReadyToActivate());
4844 }
4845
4846 TEST_F(PictureLayerImplTest, CloneMissingRecordings) {
4847 gfx::Size tile_size(100, 100);
4848 gfx::Size layer_bounds(400, 400);
4849
4850 scoped_refptr<FakePicturePileImpl> filled_pile =
4851 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
4852
4853 scoped_ptr<FakePicturePile> partial_recording =
4854 FakePicturePile::CreateEmptyPile(tile_size, layer_bounds);
4855 for (int i = 1; i < partial_recording->tiling().num_tiles_x(); ++i) {
4856 for (int j = 1; j < partial_recording->tiling().num_tiles_y(); ++j)
4857 partial_recording->AddRecordingAt(i, j);
4858 }
4859 scoped_refptr<FakePicturePileImpl> partial_pile =
4860 FakePicturePileImpl::CreateFromPile(partial_recording.get(), nullptr);
4861
4862 SetupPendingTreeWithFixedTileSize(filled_pile, tile_size, Region());
4863 ActivateTree();
4864
4865 PictureLayerTiling* pending_tiling = old_pending_layer_->HighResTiling();
4866 PictureLayerTiling* active_tiling = active_layer_->HighResTiling();
4867
4868 // We should have all tiles in both tile sets.
4869 EXPECT_EQ(5u * 5u, pending_tiling->AllTilesForTesting().size());
4870 EXPECT_EQ(5u * 5u, active_tiling->AllTilesForTesting().size());
4871
4872 // Now put a partially-recorded pile on the pending tree (and invalidate
4873 // everything, since the main thread PicturePile will invalidate dropped
4874 // recordings). This will cause us to be missing some tiles.
4875 SetupPendingTreeWithFixedTileSize(partial_pile, tile_size,
4876 Region(gfx::Rect(layer_bounds)));
4877 EXPECT_EQ(3u * 3u, pending_tiling->AllTilesForTesting().size());
4878 EXPECT_FALSE(pending_tiling->TileAt(0, 0));
4879 EXPECT_FALSE(pending_tiling->TileAt(1, 1));
4880 EXPECT_TRUE(pending_tiling->TileAt(2, 2));
4881
4882 // Active is not affected yet.
4883 EXPECT_EQ(5u * 5u, active_tiling->AllTilesForTesting().size());
4884
4885 // Activate the tree. The same tiles go missing on the active tree.
4886 ActivateTree();
4887 EXPECT_EQ(3u * 3u, active_tiling->AllTilesForTesting().size());
4888 EXPECT_FALSE(active_tiling->TileAt(0, 0));
4889 EXPECT_FALSE(active_tiling->TileAt(1, 1));
4890 EXPECT_TRUE(active_tiling->TileAt(2, 2));
4891
4892 // Now put a full recording on the pending tree again. We'll get all our tiles
4893 // back.
4894 SetupPendingTreeWithFixedTileSize(filled_pile, tile_size,
4895 Region(gfx::Rect(layer_bounds)));
4896 EXPECT_EQ(5u * 5u, pending_tiling->AllTilesForTesting().size());
4897
4898 // Active is not affected yet.
4899 EXPECT_EQ(3u * 3u, active_tiling->AllTilesForTesting().size());
4900
4901 // Activate the tree. The tiles are created and shared on the active tree.
4902 ActivateTree();
4903 EXPECT_EQ(5u * 5u, active_tiling->AllTilesForTesting().size());
4904 EXPECT_TRUE(active_tiling->TileAt(0, 0)->is_shared());
4905 EXPECT_TRUE(active_tiling->TileAt(1, 1)->is_shared());
4906 EXPECT_TRUE(active_tiling->TileAt(2, 2)->is_shared());
4907 }
4908
4909 class TileSizeSettings : public ImplSidePaintingSettings {
4910 public:
4911 TileSizeSettings() {
4912 default_tile_size = gfx::Size(100, 100);
4913 max_untiled_layer_size = gfx::Size(200, 200);
4914 }
4915 };
4916
4917 class TileSizeTest : public PictureLayerImplTest {
4918 public:
4919 TileSizeTest() : PictureLayerImplTest(TileSizeSettings()) {}
4920 };
4921
4922 TEST_F(TileSizeTest, TileSizes) {
4923 host_impl_.CreatePendingTree();
4924
4925 LayerTreeImpl* pending_tree = host_impl_.pending_tree();
4926 scoped_ptr<FakePictureLayerImpl> layer =
4927 FakePictureLayerImpl::Create(pending_tree, id_);
4928
4929 host_impl_.SetViewportSize(gfx::Size(1000, 1000));
4930 gfx::Size result;
4931
4932 host_impl_.SetUseGpuRasterization(false);
4933
4934 // Default tile-size for large layers.
4935 result = layer->CalculateTileSize(gfx::Size(10000, 10000));
4936 EXPECT_EQ(result.width(), 100);
4937 EXPECT_EQ(result.height(), 100);
4938 // Don't tile and round-up, when under max_untiled_layer_size.
4939 result = layer->CalculateTileSize(gfx::Size(42, 42));
4940 EXPECT_EQ(result.width(), 64);
4941 EXPECT_EQ(result.height(), 64);
4942 result = layer->CalculateTileSize(gfx::Size(191, 191));
4943 EXPECT_EQ(result.width(), 192);
4944 EXPECT_EQ(result.height(), 192);
4945 result = layer->CalculateTileSize(gfx::Size(199, 199));
4946 EXPECT_EQ(result.width(), 200);
4947 EXPECT_EQ(result.height(), 200);
4948
4949 // Gpu-rasterization uses 25% viewport-height tiles.
4950 // The +2's below are for border texels.
4951 host_impl_.SetUseGpuRasterization(true);
4952 host_impl_.SetViewportSize(gfx::Size(2000, 2000));
4953
4954 layer->set_gpu_raster_max_texture_size(host_impl_.device_viewport_size());
4955 result = layer->CalculateTileSize(gfx::Size(10000, 10000));
4956 EXPECT_EQ(result.width(), 2000);
4957 EXPECT_EQ(result.height(), 500 + 2);
4958
4959 // Clamp and round-up, when smaller than viewport.
4960 // Tile-height doubles to 50% when width shrinks to <= 50%.
4961 host_impl_.SetViewportSize(gfx::Size(1000, 1000));
4962 layer->set_gpu_raster_max_texture_size(host_impl_.device_viewport_size());
4963 result = layer->CalculateTileSize(gfx::Size(447, 10000));
4964 EXPECT_EQ(result.width(), 448);
4965 EXPECT_EQ(result.height(), 500 + 2);
4966
4967 // Largest layer is 50% of viewport width (rounded up), and
4968 // 50% of viewport in height.
4969 result = layer->CalculateTileSize(gfx::Size(447, 400));
4970 EXPECT_EQ(result.width(), 448);
4971 EXPECT_EQ(result.height(), 448);
4972 result = layer->CalculateTileSize(gfx::Size(500, 499));
4973 EXPECT_EQ(result.width(), 512);
4974 EXPECT_EQ(result.height(), 500 + 2);
4975 }
4976
4977 } // namespace
4978 } // namespace cc
OLDNEW
« no previous file with comments | « cc/layers/picture_layer_impl_perftest.cc ('k') | cc/layers/picture_layer_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698