picture_layer_impl_unittest.cc revision 8bcbed890bc3ce4d7a057a8f32cab53fa534672e
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 <utility>
8
9#include "cc/debug/test_web_graphics_context_3d.h"
10#include "cc/layers/append_quads_data.h"
11#include "cc/layers/picture_layer.h"
12#include "cc/test/fake_content_layer_client.h"
13#include "cc/test/fake_impl_proxy.h"
14#include "cc/test/fake_layer_tree_host_impl.h"
15#include "cc/test/fake_output_surface.h"
16#include "cc/test/fake_picture_layer_impl.h"
17#include "cc/test/fake_picture_pile_impl.h"
18#include "cc/test/geometry_test_utils.h"
19#include "cc/test/impl_side_painting_settings.h"
20#include "cc/test/mock_quad_culler.h"
21#include "cc/trees/layer_tree_impl.h"
22#include "testing/gtest/include/gtest/gtest.h"
23#include "third_party/skia/include/core/SkBitmapDevice.h"
24#include "ui/gfx/rect_conversions.h"
25
26namespace cc {
27namespace {
28
29class MockCanvas : public SkCanvas {
30 public:
31  explicit MockCanvas(SkBaseDevice* device) : SkCanvas(device) {}
32
33  virtual void drawRect(const SkRect& rect, const SkPaint& paint) OVERRIDE {
34    // Capture calls before SkCanvas quickReject() kicks in.
35    rects_.push_back(rect);
36  }
37
38  std::vector<SkRect> rects_;
39};
40
41class PictureLayerImplTest : public testing::Test {
42 public:
43  PictureLayerImplTest()
44      : host_impl_(ImplSidePaintingSettings(), &proxy_), id_(7) {}
45
46  explicit PictureLayerImplTest(const LayerTreeSettings& settings)
47      : host_impl_(settings, &proxy_), id_(7) {}
48
49  virtual ~PictureLayerImplTest() {
50  }
51
52  virtual void SetUp() OVERRIDE {
53    InitializeRenderer();
54  }
55
56  virtual void InitializeRenderer() {
57    host_impl_.InitializeRenderer(CreateFakeOutputSurface());
58  }
59
60  void SetupDefaultTrees(gfx::Size layer_bounds) {
61    gfx::Size tile_size(100, 100);
62
63    scoped_refptr<FakePicturePileImpl> pending_pile =
64        FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
65    scoped_refptr<FakePicturePileImpl> active_pile =
66        FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
67
68    SetupTrees(pending_pile, active_pile);
69  }
70
71  void SetupTrees(
72      scoped_refptr<PicturePileImpl> pending_pile,
73      scoped_refptr<PicturePileImpl> active_pile) {
74    SetupPendingTree(active_pile);
75    host_impl_.ActivatePendingTree();
76
77    active_layer_ = static_cast<FakePictureLayerImpl*>(
78        host_impl_.active_tree()->LayerById(id_));
79
80    SetupPendingTree(pending_pile);
81  }
82
83  void AddDefaultTilingsWithInvalidation(const Region& invalidation) {
84    active_layer_->AddTiling(2.3f);
85    active_layer_->AddTiling(1.0f);
86    active_layer_->AddTiling(0.5f);
87    for (size_t i = 0; i < active_layer_->tilings()->num_tilings(); ++i)
88      active_layer_->tilings()->tiling_at(i)->CreateAllTilesForTesting();
89    pending_layer_->set_invalidation(invalidation);
90    for (size_t i = 0; i < pending_layer_->tilings()->num_tilings(); ++i)
91      pending_layer_->tilings()->tiling_at(i)->CreateAllTilesForTesting();
92  }
93
94  void SetupPendingTree(
95      scoped_refptr<PicturePileImpl> pile) {
96    host_impl_.CreatePendingTree();
97    LayerTreeImpl* pending_tree = host_impl_.pending_tree();
98    // Clear recycled tree.
99    pending_tree->DetachLayerTree();
100
101    scoped_ptr<FakePictureLayerImpl> pending_layer =
102        FakePictureLayerImpl::CreateWithPile(pending_tree, id_, pile);
103    pending_layer->SetDrawsContent(true);
104    pending_tree->SetRootLayer(pending_layer.PassAs<LayerImpl>());
105
106    pending_layer_ = static_cast<FakePictureLayerImpl*>(
107        host_impl_.pending_tree()->LayerById(id_));
108    pending_layer_->DoPostCommitInitializationIfNeeded();
109  }
110
111  static void VerifyAllTilesExistAndHavePile(
112      const PictureLayerTiling* tiling,
113      PicturePileImpl* pile) {
114    for (PictureLayerTiling::CoverageIterator
115             iter(tiling, tiling->contents_scale(), tiling->ContentRect());
116         iter;
117         ++iter) {
118      EXPECT_TRUE(*iter);
119      EXPECT_EQ(pile, iter->picture_pile());
120    }
121  }
122
123  void SetContentsScaleOnBothLayers(float contents_scale,
124                                    float device_scale_factor,
125                                    float page_scale_factor,
126                                    bool animating_transform) {
127    float result_scale_x, result_scale_y;
128    gfx::Size result_bounds;
129    pending_layer_->CalculateContentsScale(
130        contents_scale,
131        device_scale_factor,
132        page_scale_factor,
133        animating_transform,
134        &result_scale_x,
135        &result_scale_y,
136        &result_bounds);
137    active_layer_->CalculateContentsScale(
138        contents_scale,
139        device_scale_factor,
140        page_scale_factor,
141        animating_transform,
142        &result_scale_x,
143        &result_scale_y,
144        &result_bounds);
145  }
146
147  void ResetTilingsAndRasterScales() {
148    pending_layer_->DidLoseOutputSurface();
149    active_layer_->DidLoseOutputSurface();
150  }
151
152 protected:
153  void TestTileGridAlignmentCommon() {
154    // Layer to span 4 raster tiles in x and in y
155    ImplSidePaintingSettings settings;
156    gfx::Size layer_size(
157        settings.default_tile_size.width() * 7 / 2,
158        settings.default_tile_size.height() * 7 / 2);
159
160    scoped_refptr<FakePicturePileImpl> pending_pile =
161        FakePicturePileImpl::CreateFilledPile(layer_size, layer_size);
162    scoped_refptr<FakePicturePileImpl> active_pile =
163        FakePicturePileImpl::CreateFilledPile(layer_size, layer_size);
164
165    SetupTrees(pending_pile, active_pile);
166
167    float result_scale_x, result_scale_y;
168    gfx::Size result_bounds;
169    active_layer_->CalculateContentsScale(
170        1.f, 1.f, 1.f, false, &result_scale_x, &result_scale_y, &result_bounds);
171
172    // Add 1x1 rects at the centers of each tile, then re-record pile contents
173    active_layer_->tilings()->tiling_at(0)->CreateAllTilesForTesting();
174    std::vector<Tile*> tiles =
175        active_layer_->tilings()->tiling_at(0)->AllTilesForTesting();
176    EXPECT_EQ(16u, tiles.size());
177    std::vector<SkRect> rects;
178    std::vector<Tile*>::const_iterator tile_iter;
179    for (tile_iter = tiles.begin(); tile_iter < tiles.end(); tile_iter++) {
180      gfx::Point tile_center = (*tile_iter)->content_rect().CenterPoint();
181      gfx::Rect rect(tile_center.x(), tile_center.y(), 1, 1);
182      active_pile->add_draw_rect(rect);
183      rects.push_back(SkRect::MakeXYWH(rect.x(), rect.y(), 1, 1));
184    }
185    // Force re-record with newly injected content
186    active_pile->RemoveRecordingAt(0, 0);
187    active_pile->AddRecordingAt(0, 0);
188
189    SkBitmap store;
190    store.setConfig(SkBitmap::kNo_Config, 1000, 1000);
191    SkBitmapDevice device(store);
192
193    std::vector<SkRect>::const_iterator rect_iter = rects.begin();
194    for (tile_iter = tiles.begin(); tile_iter < tiles.end(); tile_iter++) {
195      MockCanvas mock_canvas(&device);
196      active_pile->RasterDirect(
197          &mock_canvas, (*tile_iter)->content_rect(), 1.0f, NULL);
198
199      // This test verifies that when drawing the contents of a specific tile
200      // at content scale 1.0, the playback canvas never receives content from
201      // neighboring tiles which indicates that the tile grid embedded in
202      // SkPicture is perfectly aligned with the compositor's tiles.
203      EXPECT_EQ(1u, mock_canvas.rects_.size());
204      EXPECT_RECT_EQ(*rect_iter, mock_canvas.rects_[0]);
205      rect_iter++;
206    }
207  }
208
209  FakeImplProxy proxy_;
210  FakeLayerTreeHostImpl host_impl_;
211  int id_;
212  FakePictureLayerImpl* pending_layer_;
213  FakePictureLayerImpl* active_layer_;
214
215 private:
216  DISALLOW_COPY_AND_ASSIGN(PictureLayerImplTest);
217};
218
219TEST_F(PictureLayerImplTest, TileGridAlignment) {
220  host_impl_.SetDeviceScaleFactor(1.f);
221  TestTileGridAlignmentCommon();
222}
223
224TEST_F(PictureLayerImplTest, TileGridAlignmentHiDPI) {
225  host_impl_.SetDeviceScaleFactor(2.f);
226  TestTileGridAlignmentCommon();
227}
228
229TEST_F(PictureLayerImplTest, CloneNoInvalidation) {
230  gfx::Size tile_size(100, 100);
231  gfx::Size layer_bounds(400, 400);
232
233  scoped_refptr<FakePicturePileImpl> pending_pile =
234      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
235  scoped_refptr<FakePicturePileImpl> active_pile =
236      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
237
238  SetupTrees(pending_pile, active_pile);
239
240  Region invalidation;
241  AddDefaultTilingsWithInvalidation(invalidation);
242
243  EXPECT_EQ(pending_layer_->tilings()->num_tilings(),
244            active_layer_->tilings()->num_tilings());
245
246  const PictureLayerTilingSet* tilings = pending_layer_->tilings();
247  EXPECT_GT(tilings->num_tilings(), 0u);
248  for (size_t i = 0; i < tilings->num_tilings(); ++i)
249    VerifyAllTilesExistAndHavePile(tilings->tiling_at(i), active_pile.get());
250}
251
252TEST_F(PictureLayerImplTest, SuppressUpdateTilePriorities) {
253  base::TimeTicks time_ticks;
254  host_impl_.SetCurrentFrameTimeTicks(time_ticks);
255
256  gfx::Size tile_size(100, 100);
257  gfx::Size layer_bounds(400, 400);
258
259  scoped_refptr<FakePicturePileImpl> pending_pile =
260      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
261  scoped_refptr<FakePicturePileImpl> active_pile =
262      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
263
264  SetupTrees(pending_pile, active_pile);
265
266  Region invalidation;
267  AddDefaultTilingsWithInvalidation(invalidation);
268  float dummy_contents_scale_x;
269  float dummy_contents_scale_y;
270  gfx::Size dummy_content_bounds;
271  active_layer_->CalculateContentsScale(1.f,
272                                        1.f,
273                                        1.f,
274                                        false,
275                                        &dummy_contents_scale_x,
276                                        &dummy_contents_scale_y,
277                                        &dummy_content_bounds);
278
279  EXPECT_TRUE(host_impl_.manage_tiles_needed());
280  active_layer_->UpdateTilePriorities();
281  host_impl_.ManageTiles();
282  EXPECT_FALSE(host_impl_.manage_tiles_needed());
283
284  time_ticks += base::TimeDelta::FromMilliseconds(200);
285  host_impl_.SetCurrentFrameTimeTicks(time_ticks);
286
287  // Setting this boolean should cause an early out in UpdateTilePriorities.
288  bool valid_for_tile_management = false;
289  host_impl_.SetExternalDrawConstraints(gfx::Transform(),
290                                        gfx::Rect(layer_bounds),
291                                        gfx::Rect(layer_bounds),
292                                        valid_for_tile_management);
293  active_layer_->UpdateTilePriorities();
294  EXPECT_FALSE(host_impl_.manage_tiles_needed());
295
296  time_ticks += base::TimeDelta::FromMilliseconds(200);
297  host_impl_.SetCurrentFrameTimeTicks(time_ticks);
298
299  valid_for_tile_management = true;
300  host_impl_.SetExternalDrawConstraints(gfx::Transform(),
301                                        gfx::Rect(layer_bounds),
302                                        gfx::Rect(layer_bounds),
303                                        valid_for_tile_management);
304  active_layer_->UpdateTilePriorities();
305  EXPECT_TRUE(host_impl_.manage_tiles_needed());
306}
307
308TEST_F(PictureLayerImplTest, ClonePartialInvalidation) {
309  gfx::Size tile_size(100, 100);
310  gfx::Size layer_bounds(400, 400);
311  gfx::Rect layer_invalidation(150, 200, 30, 180);
312
313  scoped_refptr<FakePicturePileImpl> pending_pile =
314      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
315  scoped_refptr<FakePicturePileImpl> active_pile =
316      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
317
318  SetupTrees(pending_pile, active_pile);
319
320  Region invalidation(layer_invalidation);
321  AddDefaultTilingsWithInvalidation(invalidation);
322
323  const PictureLayerTilingSet* tilings = pending_layer_->tilings();
324  EXPECT_GT(tilings->num_tilings(), 0u);
325  for (size_t i = 0; i < tilings->num_tilings(); ++i) {
326    const PictureLayerTiling* tiling = tilings->tiling_at(i);
327    gfx::Rect content_invalidation = gfx::ScaleToEnclosingRect(
328        layer_invalidation,
329        tiling->contents_scale());
330    for (PictureLayerTiling::CoverageIterator
331             iter(tiling,
332                  tiling->contents_scale(),
333                  tiling->ContentRect());
334         iter;
335         ++iter) {
336      EXPECT_TRUE(*iter);
337      EXPECT_FALSE(iter.geometry_rect().IsEmpty());
338      if (iter.geometry_rect().Intersects(content_invalidation))
339        EXPECT_EQ(pending_pile, iter->picture_pile());
340      else
341        EXPECT_EQ(active_pile, iter->picture_pile());
342    }
343  }
344}
345
346TEST_F(PictureLayerImplTest, CloneFullInvalidation) {
347  gfx::Size tile_size(90, 80);
348  gfx::Size layer_bounds(300, 500);
349
350  scoped_refptr<FakePicturePileImpl> pending_pile =
351      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
352  scoped_refptr<FakePicturePileImpl> active_pile =
353      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
354
355  SetupTrees(pending_pile, active_pile);
356
357  Region invalidation((gfx::Rect(layer_bounds)));
358  AddDefaultTilingsWithInvalidation(invalidation);
359
360  EXPECT_EQ(pending_layer_->tilings()->num_tilings(),
361            active_layer_->tilings()->num_tilings());
362
363  const PictureLayerTilingSet* tilings = pending_layer_->tilings();
364  EXPECT_GT(tilings->num_tilings(), 0u);
365  for (size_t i = 0; i < tilings->num_tilings(); ++i)
366    VerifyAllTilesExistAndHavePile(tilings->tiling_at(i), pending_pile.get());
367}
368
369TEST_F(PictureLayerImplTest, NoInvalidationBoundsChange) {
370  gfx::Size tile_size(90, 80);
371  gfx::Size active_layer_bounds(300, 500);
372  gfx::Size pending_layer_bounds(400, 800);
373
374  scoped_refptr<FakePicturePileImpl> pending_pile =
375      FakePicturePileImpl::CreateFilledPile(tile_size,
376                                                pending_layer_bounds);
377  scoped_refptr<FakePicturePileImpl> active_pile =
378      FakePicturePileImpl::CreateFilledPile(tile_size, active_layer_bounds);
379
380  SetupTrees(pending_pile, active_pile);
381  pending_layer_->set_fixed_tile_size(gfx::Size(100, 100));
382
383  Region invalidation;
384  AddDefaultTilingsWithInvalidation(invalidation);
385
386  const PictureLayerTilingSet* tilings = pending_layer_->tilings();
387  EXPECT_GT(tilings->num_tilings(), 0u);
388  for (size_t i = 0; i < tilings->num_tilings(); ++i) {
389    const PictureLayerTiling* tiling = tilings->tiling_at(i);
390    gfx::Rect active_content_bounds = gfx::ScaleToEnclosingRect(
391        gfx::Rect(active_layer_bounds),
392        tiling->contents_scale());
393    for (PictureLayerTiling::CoverageIterator
394             iter(tiling,
395                  tiling->contents_scale(),
396                  tiling->ContentRect());
397         iter;
398         ++iter) {
399      EXPECT_TRUE(*iter);
400      EXPECT_FALSE(iter.geometry_rect().IsEmpty());
401      std::vector<Tile*> active_tiles =
402          active_layer_->tilings()->tiling_at(i)->AllTilesForTesting();
403      std::vector<Tile*> pending_tiles = tiling->AllTilesForTesting();
404      if (iter.geometry_rect().right() >= active_content_bounds.width() ||
405          iter.geometry_rect().bottom() >= active_content_bounds.height() ||
406          active_tiles[0]->content_rect().size() !=
407              pending_tiles[0]->content_rect().size()) {
408        EXPECT_EQ(pending_pile, iter->picture_pile());
409      } else {
410        EXPECT_EQ(active_pile, iter->picture_pile());
411      }
412    }
413  }
414}
415
416TEST_F(PictureLayerImplTest, AddTilesFromNewRecording) {
417  gfx::Size tile_size(400, 400);
418  gfx::Size layer_bounds(1300, 1900);
419
420  scoped_refptr<FakePicturePileImpl> pending_pile =
421      FakePicturePileImpl::CreateEmptyPile(tile_size, layer_bounds);
422  scoped_refptr<FakePicturePileImpl> active_pile =
423      FakePicturePileImpl::CreateEmptyPile(tile_size, layer_bounds);
424
425  // Fill in some of active pile, but more of pending pile.
426  int hole_count = 0;
427  for (int x = 0; x < active_pile->tiling().num_tiles_x(); ++x) {
428    for (int y = 0; y < active_pile->tiling().num_tiles_y(); ++y) {
429      if ((x + y) % 2) {
430        pending_pile->AddRecordingAt(x, y);
431        active_pile->AddRecordingAt(x, y);
432      } else {
433        hole_count++;
434        if (hole_count % 2)
435          pending_pile->AddRecordingAt(x, y);
436      }
437    }
438  }
439
440  SetupTrees(pending_pile, active_pile);
441  Region invalidation;
442  AddDefaultTilingsWithInvalidation(invalidation);
443
444  const PictureLayerTilingSet* tilings = pending_layer_->tilings();
445  EXPECT_GT(tilings->num_tilings(), 0u);
446  for (size_t i = 0; i < tilings->num_tilings(); ++i) {
447    const PictureLayerTiling* tiling = tilings->tiling_at(i);
448
449    for (PictureLayerTiling::CoverageIterator
450             iter(tiling,
451                  tiling->contents_scale(),
452                  tiling->ContentRect());
453         iter;
454         ++iter) {
455      EXPECT_FALSE(iter.full_tile_geometry_rect().IsEmpty());
456      // Ensure there is a recording for this tile.
457      gfx::Rect layer_rect = gfx::ScaleToEnclosingRect(
458          iter.full_tile_geometry_rect(), 1.f / tiling->contents_scale());
459      layer_rect.Intersect(gfx::Rect(layer_bounds));
460
461      bool in_pending = pending_pile->recorded_region().Contains(layer_rect);
462      bool in_active = active_pile->recorded_region().Contains(layer_rect);
463
464      if (in_pending && !in_active)
465        EXPECT_EQ(pending_pile, iter->picture_pile());
466      else if (in_active)
467        EXPECT_EQ(active_pile, iter->picture_pile());
468      else
469        EXPECT_FALSE(*iter);
470    }
471  }
472}
473
474TEST_F(PictureLayerImplTest, ManageTilingsWithNoRecording) {
475  gfx::Size tile_size(400, 400);
476  gfx::Size layer_bounds(1300, 1900);
477
478  scoped_refptr<FakePicturePileImpl> pending_pile =
479      FakePicturePileImpl::CreateEmptyPile(tile_size, layer_bounds);
480  scoped_refptr<FakePicturePileImpl> active_pile =
481      FakePicturePileImpl::CreateEmptyPile(tile_size, layer_bounds);
482
483  float result_scale_x, result_scale_y;
484  gfx::Size result_bounds;
485
486  SetupTrees(pending_pile, active_pile);
487
488  pending_layer_->CalculateContentsScale(
489      1.f, 1.f, 1.f, false, &result_scale_x, &result_scale_y, &result_bounds);
490
491  EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings());
492}
493
494TEST_F(PictureLayerImplTest, ManageTilingsCreatesTilings) {
495  gfx::Size tile_size(400, 400);
496  gfx::Size layer_bounds(1300, 1900);
497
498  scoped_refptr<FakePicturePileImpl> pending_pile =
499      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
500  scoped_refptr<FakePicturePileImpl> active_pile =
501      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
502
503  float result_scale_x, result_scale_y;
504  gfx::Size result_bounds;
505
506  SetupTrees(pending_pile, active_pile);
507  EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings());
508
509  float low_res_factor = host_impl_.settings().low_res_contents_scale_factor;
510  EXPECT_LT(low_res_factor, 1.f);
511
512  pending_layer_->CalculateContentsScale(1.3f,  // ideal contents scale
513                                         1.7f,  // device scale
514                                         3.2f,  // page cale
515                                         false,
516                                         &result_scale_x,
517                                         &result_scale_y,
518                                         &result_bounds);
519  ASSERT_EQ(2u, pending_layer_->tilings()->num_tilings());
520  EXPECT_FLOAT_EQ(
521      1.3f,
522      pending_layer_->tilings()->tiling_at(0)->contents_scale());
523  EXPECT_FLOAT_EQ(
524      1.3f * low_res_factor,
525      pending_layer_->tilings()->tiling_at(1)->contents_scale());
526
527  // If we change the layer's CSS scale factor, then we should not get new
528  // tilings.
529  pending_layer_->CalculateContentsScale(1.8f,  // ideal contents scale
530                                         1.7f,  // device scale
531                                         3.2f,  // page cale
532                                         false,
533                                         &result_scale_x,
534                                         &result_scale_y,
535                                         &result_bounds);
536  ASSERT_EQ(2u, pending_layer_->tilings()->num_tilings());
537  EXPECT_FLOAT_EQ(
538      1.3f,
539      pending_layer_->tilings()->tiling_at(0)->contents_scale());
540  EXPECT_FLOAT_EQ(
541      1.3f * low_res_factor,
542      pending_layer_->tilings()->tiling_at(1)->contents_scale());
543
544  // If we change the page scale factor, then we should get new tilings.
545  pending_layer_->CalculateContentsScale(1.8f,  // ideal contents scale
546                                         1.7f,  // device scale
547                                         2.2f,  // page cale
548                                         false,
549                                         &result_scale_x,
550                                         &result_scale_y,
551                                         &result_bounds);
552  ASSERT_EQ(4u, pending_layer_->tilings()->num_tilings());
553  EXPECT_FLOAT_EQ(
554      1.8f,
555      pending_layer_->tilings()->tiling_at(0)->contents_scale());
556  EXPECT_FLOAT_EQ(
557      1.8f * low_res_factor,
558      pending_layer_->tilings()->tiling_at(2)->contents_scale());
559
560  // If we change the device scale factor, then we should get new tilings.
561  pending_layer_->CalculateContentsScale(1.9f,  // ideal contents scale
562                                         1.4f,  // device scale
563                                         2.2f,  // page cale
564                                         false,
565                                         &result_scale_x,
566                                         &result_scale_y,
567                                         &result_bounds);
568  ASSERT_EQ(6u, pending_layer_->tilings()->num_tilings());
569  EXPECT_FLOAT_EQ(
570      1.9f,
571      pending_layer_->tilings()->tiling_at(0)->contents_scale());
572  EXPECT_FLOAT_EQ(
573      1.9f * low_res_factor,
574      pending_layer_->tilings()->tiling_at(3)->contents_scale());
575
576  // If we change the device scale factor, but end up at the same total scale
577  // factor somehow, then we don't get new tilings.
578  pending_layer_->CalculateContentsScale(1.9f,  // ideal contents scale
579                                         2.2f,  // device scale
580                                         1.4f,  // page cale
581                                         false,
582                                         &result_scale_x,
583                                         &result_scale_y,
584                                         &result_bounds);
585  ASSERT_EQ(6u, pending_layer_->tilings()->num_tilings());
586  EXPECT_FLOAT_EQ(
587      1.9f,
588      pending_layer_->tilings()->tiling_at(0)->contents_scale());
589  EXPECT_FLOAT_EQ(
590      1.9f * low_res_factor,
591      pending_layer_->tilings()->tiling_at(3)->contents_scale());
592}
593
594TEST_F(PictureLayerImplTest, CleanUpTilings) {
595  gfx::Size tile_size(400, 400);
596  gfx::Size layer_bounds(1300, 1900);
597
598  scoped_refptr<FakePicturePileImpl> pending_pile =
599      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
600  scoped_refptr<FakePicturePileImpl> active_pile =
601      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
602
603  float result_scale_x, result_scale_y;
604  gfx::Size result_bounds;
605  std::vector<PictureLayerTiling*> used_tilings;
606
607  SetupTrees(pending_pile, active_pile);
608  EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings());
609
610  float low_res_factor = host_impl_.settings().low_res_contents_scale_factor;
611  EXPECT_LT(low_res_factor, 1.f);
612
613  float device_scale = 1.7f;
614  float page_scale = 3.2f;
615
616  SetContentsScaleOnBothLayers(1.f, device_scale, page_scale, false);
617  ASSERT_EQ(2u, active_layer_->tilings()->num_tilings());
618
619  // We only have ideal tilings, so they aren't removed.
620  used_tilings.clear();
621  active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
622  ASSERT_EQ(2u, active_layer_->tilings()->num_tilings());
623
624  // Changing the ideal but not creating new tilings.
625  SetContentsScaleOnBothLayers(1.5f, device_scale, page_scale, false);
626  ASSERT_EQ(2u, active_layer_->tilings()->num_tilings());
627
628  // The tilings are still our target scale, so they aren't removed.
629  used_tilings.clear();
630  active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
631  ASSERT_EQ(2u, active_layer_->tilings()->num_tilings());
632
633  // Create a 1.2 scale tiling. Now we have 1.0 and 1.2 tilings. Ideal = 1.2.
634  page_scale = 1.2f;
635  SetContentsScaleOnBothLayers(1.2f, device_scale, page_scale, false);
636  ASSERT_EQ(4u, active_layer_->tilings()->num_tilings());
637  EXPECT_FLOAT_EQ(
638      1.f,
639      active_layer_->tilings()->tiling_at(1)->contents_scale());
640  EXPECT_FLOAT_EQ(
641      1.f * low_res_factor,
642      active_layer_->tilings()->tiling_at(3)->contents_scale());
643
644  // Mark the non-ideal tilings as used. They won't be removed.
645  used_tilings.clear();
646  used_tilings.push_back(active_layer_->tilings()->tiling_at(1));
647  used_tilings.push_back(active_layer_->tilings()->tiling_at(3));
648  active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
649  ASSERT_EQ(4u, active_layer_->tilings()->num_tilings());
650
651  // Now move the ideal scale to 0.5. Our target stays 1.2.
652  SetContentsScaleOnBothLayers(0.5f, device_scale, page_scale, false);
653
654  // The high resolution tiling is between target and ideal, so is not
655  // removed.  The low res tiling for the old ideal=1.0 scale is removed.
656  used_tilings.clear();
657  active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
658  ASSERT_EQ(3u, active_layer_->tilings()->num_tilings());
659
660  // Now move the ideal scale to 1.0. Our target stays 1.2.
661  SetContentsScaleOnBothLayers(1.f, device_scale, page_scale, false);
662
663  // All the tilings are between are target and the ideal, so they are not
664  // removed.
665  used_tilings.clear();
666  active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
667  ASSERT_EQ(3u, active_layer_->tilings()->num_tilings());
668
669  // Now move the ideal scale to 1.1 on the active layer. Our target stays 1.2.
670  active_layer_->CalculateContentsScale(1.1f,
671                                        device_scale,
672                                        page_scale,
673                                        false,
674                                        &result_scale_x,
675                                        &result_scale_y,
676                                        &result_bounds);
677
678  // Because the pending layer's ideal scale is still 1.0, our tilings fall
679  // in the range [1.0,1.2] and are kept.
680  used_tilings.clear();
681  active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
682  ASSERT_EQ(3u, active_layer_->tilings()->num_tilings());
683
684  // Move the ideal scale on the pending layer to 1.1 as well. Our target stays
685  // 1.2 still.
686  pending_layer_->CalculateContentsScale(1.1f,
687                                         device_scale,
688                                         page_scale,
689                                         false,
690                                         &result_scale_x,
691                                         &result_scale_y,
692                                         &result_bounds);
693
694  // Our 1.0 tiling now falls outside the range between our ideal scale and our
695  // target raster scale. But it is in our used tilings set, so nothing is
696  // deleted.
697  used_tilings.clear();
698  used_tilings.push_back(active_layer_->tilings()->tiling_at(1));
699  active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
700  ASSERT_EQ(3u, active_layer_->tilings()->num_tilings());
701
702  // If we remove it from our used tilings set, it is outside the range to keep
703  // so it is deleted.
704  used_tilings.clear();
705  active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
706  ASSERT_EQ(2u, active_layer_->tilings()->num_tilings());
707}
708
709#define EXPECT_BOTH_EQ(expression, x)         \
710  do {                                        \
711    EXPECT_EQ(pending_layer_->expression, x); \
712    EXPECT_EQ(active_layer_->expression, x);  \
713  } while (false)
714
715TEST_F(PictureLayerImplTest, DontAddLowResDuringAnimation) {
716  // Make sure this layer covers multiple tiles, since otherwise low
717  // res won't get created because it is too small.
718  gfx::Size tile_size(host_impl_.settings().default_tile_size);
719  SetupDefaultTrees(gfx::Size(tile_size.width() + 1, tile_size.height() + 1));
720  // Avoid max untiled layer size heuristics via fixed tile size.
721  pending_layer_->set_fixed_tile_size(tile_size);
722  active_layer_->set_fixed_tile_size(tile_size);
723
724  float low_res_factor = host_impl_.settings().low_res_contents_scale_factor;
725  float contents_scale = 1.f;
726  float device_scale = 1.f;
727  float page_scale = 1.f;
728  bool animating_transform = true;
729
730  // Animating, so don't create low res even if there isn't one already.
731  SetContentsScaleOnBothLayers(
732      contents_scale, device_scale, page_scale, animating_transform);
733  EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 1.f);
734  EXPECT_BOTH_EQ(num_tilings(), 1u);
735
736  // Stop animating, low res gets created.
737  animating_transform = false;
738  SetContentsScaleOnBothLayers(
739      contents_scale, device_scale, page_scale, animating_transform);
740  EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 1.f);
741  EXPECT_BOTH_EQ(LowResTiling()->contents_scale(), low_res_factor);
742  EXPECT_BOTH_EQ(num_tilings(), 2u);
743
744  // Page scale animation, new high res, but not new low res because animating.
745  contents_scale = 2.f;
746  page_scale = 2.f;
747  animating_transform = true;
748  SetContentsScaleOnBothLayers(
749      contents_scale, device_scale, page_scale, animating_transform);
750  EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 2.f);
751  EXPECT_BOTH_EQ(LowResTiling()->contents_scale(), low_res_factor);
752  EXPECT_BOTH_EQ(num_tilings(), 3u);
753
754  // Stop animating, new low res gets created for final page scale.
755  animating_transform = false;
756  SetContentsScaleOnBothLayers(
757      contents_scale, device_scale, page_scale, animating_transform);
758  EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 2.f);
759  EXPECT_BOTH_EQ(LowResTiling()->contents_scale(), 2.f * low_res_factor);
760  EXPECT_BOTH_EQ(num_tilings(), 4u);
761}
762
763TEST_F(PictureLayerImplTest, DontAddLowResForSmallLayers) {
764  gfx::Size tile_size(host_impl_.settings().default_tile_size);
765  SetupDefaultTrees(tile_size);
766
767  float low_res_factor = host_impl_.settings().low_res_contents_scale_factor;
768  float device_scale = 1.f;
769  float page_scale = 1.f;
770  bool animating_transform = false;
771
772  // Contents exactly fit on one tile at scale 1, no low res.
773  float contents_scale = 1.f;
774  SetContentsScaleOnBothLayers(
775      contents_scale, device_scale, page_scale, animating_transform);
776  EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), contents_scale);
777  EXPECT_BOTH_EQ(num_tilings(), 1u);
778
779  ResetTilingsAndRasterScales();
780
781  // Contents that are smaller than one tile, no low res.
782  contents_scale = 0.123f;
783  SetContentsScaleOnBothLayers(
784      contents_scale, device_scale, page_scale, animating_transform);
785  EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), contents_scale);
786  EXPECT_BOTH_EQ(num_tilings(), 1u);
787
788  ResetTilingsAndRasterScales();
789
790  // Any content bounds that would create more than one tile will
791  // generate a low res tiling.
792  contents_scale = 2.5f;
793  SetContentsScaleOnBothLayers(
794      contents_scale, device_scale, page_scale, animating_transform);
795  EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), contents_scale);
796  EXPECT_BOTH_EQ(LowResTiling()->contents_scale(),
797                 contents_scale * low_res_factor);
798  EXPECT_BOTH_EQ(num_tilings(), 2u);
799
800  ResetTilingsAndRasterScales();
801
802  // Mask layers dont create low res since they always fit on one tile.
803  pending_layer_->SetIsMask(true);
804  active_layer_->SetIsMask(true);
805  SetContentsScaleOnBothLayers(
806      contents_scale, device_scale, page_scale, animating_transform);
807  EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), contents_scale);
808  EXPECT_BOTH_EQ(num_tilings(), 1u);
809}
810
811TEST_F(PictureLayerImplTest, DidLoseOutputSurface) {
812  gfx::Size tile_size(400, 400);
813  gfx::Size layer_bounds(1300, 1900);
814
815  scoped_refptr<FakePicturePileImpl> pending_pile =
816      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
817  scoped_refptr<FakePicturePileImpl> active_pile =
818      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
819
820  float result_scale_x, result_scale_y;
821  gfx::Size result_bounds;
822
823  SetupTrees(pending_pile, active_pile);
824  EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings());
825
826  pending_layer_->CalculateContentsScale(1.3f,  // ideal contents scale
827                                         2.7f,  // device scale
828                                         3.2f,  // page cale
829                                         false,
830                                         &result_scale_x,
831                                         &result_scale_y,
832                                         &result_bounds);
833  EXPECT_EQ(2u, pending_layer_->tilings()->num_tilings());
834
835  // All tilings should be removed when losing output surface.
836  active_layer_->DidLoseOutputSurface();
837  EXPECT_EQ(0u, active_layer_->tilings()->num_tilings());
838  pending_layer_->DidLoseOutputSurface();
839  EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings());
840
841  // This should create new tilings.
842  pending_layer_->CalculateContentsScale(1.3f,  // ideal contents scale
843                                         2.7f,  // device scale
844                                         3.2f,  // page cale
845                                         false,
846                                         &result_scale_x,
847                                         &result_scale_y,
848                                         &result_bounds);
849  EXPECT_EQ(2u, pending_layer_->tilings()->num_tilings());
850}
851
852TEST_F(PictureLayerImplTest, ClampTilesToToMaxTileSize) {
853  // The default max tile size is larger than 400x400.
854  gfx::Size tile_size(400, 400);
855  gfx::Size layer_bounds(5000, 5000);
856
857  scoped_refptr<FakePicturePileImpl> pending_pile =
858      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
859  scoped_refptr<FakePicturePileImpl> active_pile =
860      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
861
862  float result_scale_x, result_scale_y;
863  gfx::Size result_bounds;
864
865  SetupTrees(pending_pile, active_pile);
866  EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings());
867
868  pending_layer_->CalculateContentsScale(
869      1.f, 1.f, 1.f, false, &result_scale_x, &result_scale_y, &result_bounds);
870  ASSERT_EQ(2u, pending_layer_->tilings()->num_tilings());
871
872  pending_layer_->tilings()->tiling_at(0)->CreateAllTilesForTesting();
873
874  // The default value.
875  EXPECT_EQ(gfx::Size(256, 256).ToString(),
876            host_impl_.settings().default_tile_size.ToString());
877
878  Tile* tile = pending_layer_->tilings()->tiling_at(0)->AllTilesForTesting()[0];
879  EXPECT_EQ(gfx::Size(256, 256).ToString(),
880            tile->content_rect().size().ToString());
881
882  pending_layer_->DidLoseOutputSurface();
883
884  // Change the max texture size on the output surface context.
885  scoped_ptr<TestWebGraphicsContext3D> context =
886      TestWebGraphicsContext3D::Create();
887  context->set_max_texture_size(140);
888  host_impl_.InitializeRenderer(FakeOutputSurface::Create3d(
889      context.Pass()).PassAs<OutputSurface>());
890
891  pending_layer_->CalculateContentsScale(
892      1.f, 1.f, 1.f, false, &result_scale_x, &result_scale_y, &result_bounds);
893  ASSERT_EQ(2u, pending_layer_->tilings()->num_tilings());
894
895  pending_layer_->tilings()->tiling_at(0)->CreateAllTilesForTesting();
896
897  // Verify the tiles are not larger than the context's max texture size.
898  tile = pending_layer_->tilings()->tiling_at(0)->AllTilesForTesting()[0];
899  EXPECT_GE(140, tile->content_rect().width());
900  EXPECT_GE(140, tile->content_rect().height());
901}
902
903TEST_F(PictureLayerImplTest, ClampSingleTileToToMaxTileSize) {
904  // The default max tile size is larger than 400x400.
905  gfx::Size tile_size(400, 400);
906  gfx::Size layer_bounds(500, 500);
907
908  scoped_refptr<FakePicturePileImpl> pending_pile =
909      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
910  scoped_refptr<FakePicturePileImpl> active_pile =
911      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
912
913  float result_scale_x, result_scale_y;
914  gfx::Size result_bounds;
915
916  SetupTrees(pending_pile, active_pile);
917  EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings());
918
919  pending_layer_->CalculateContentsScale(
920      1.f, 1.f, 1.f, false, &result_scale_x, &result_scale_y, &result_bounds);
921  ASSERT_LE(1u, pending_layer_->tilings()->num_tilings());
922
923  pending_layer_->tilings()->tiling_at(0)->CreateAllTilesForTesting();
924
925  // The default value. The layer is smaller than this.
926  EXPECT_EQ(gfx::Size(512, 512).ToString(),
927            host_impl_.settings().max_untiled_layer_size.ToString());
928
929  // There should be a single tile since the layer is small.
930  PictureLayerTiling* high_res_tiling = pending_layer_->tilings()->tiling_at(0);
931  EXPECT_EQ(1u, high_res_tiling->AllTilesForTesting().size());
932
933  pending_layer_->DidLoseOutputSurface();
934
935  // Change the max texture size on the output surface context.
936  scoped_ptr<TestWebGraphicsContext3D> context =
937      TestWebGraphicsContext3D::Create();
938  context->set_max_texture_size(140);
939  host_impl_.InitializeRenderer(FakeOutputSurface::Create3d(
940      context.Pass()).PassAs<OutputSurface>());
941
942  pending_layer_->CalculateContentsScale(
943      1.f, 1.f, 1.f, false, &result_scale_x, &result_scale_y, &result_bounds);
944  ASSERT_LE(1u, pending_layer_->tilings()->num_tilings());
945
946  pending_layer_->tilings()->tiling_at(0)->CreateAllTilesForTesting();
947
948  // There should be more than one tile since the max texture size won't cover
949  // the layer.
950  high_res_tiling = pending_layer_->tilings()->tiling_at(0);
951  EXPECT_LT(1u, high_res_tiling->AllTilesForTesting().size());
952
953  // Verify the tiles are not larger than the context's max texture size.
954  Tile* tile = pending_layer_->tilings()->tiling_at(0)->AllTilesForTesting()[0];
955  EXPECT_GE(140, tile->content_rect().width());
956  EXPECT_GE(140, tile->content_rect().height());
957}
958
959TEST_F(PictureLayerImplTest, DisallowTileDrawQuads) {
960  MockQuadCuller quad_culler;
961
962  gfx::Size tile_size(400, 400);
963  gfx::Size layer_bounds(1300, 1900);
964
965  scoped_refptr<FakePicturePileImpl> pending_pile =
966      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
967  scoped_refptr<FakePicturePileImpl> active_pile =
968      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
969
970  SetupTrees(pending_pile, active_pile);
971
972  active_layer_->SetContentBounds(layer_bounds);
973  active_layer_->draw_properties().visible_content_rect =
974      gfx::Rect(layer_bounds);
975
976  gfx::Rect layer_invalidation(150, 200, 30, 180);
977  Region invalidation(layer_invalidation);
978  AddDefaultTilingsWithInvalidation(invalidation);
979
980  AppendQuadsData data;
981  active_layer_->WillDraw(DRAW_MODE_RESOURCELESS_SOFTWARE, NULL);
982  active_layer_->AppendQuads(&quad_culler, &data);
983  active_layer_->DidDraw(NULL);
984
985  ASSERT_EQ(1U, quad_culler.quad_list().size());
986  EXPECT_EQ(DrawQuad::PICTURE_CONTENT, quad_culler.quad_list()[0]->material);
987}
988
989TEST_F(PictureLayerImplTest, MarkRequiredNullTiles) {
990  gfx::Size tile_size(100, 100);
991  gfx::Size layer_bounds(1000, 1000);
992
993  scoped_refptr<FakePicturePileImpl> pending_pile =
994      FakePicturePileImpl::CreateEmptyPile(tile_size, layer_bounds);
995  // Layers with entirely empty piles can't get tilings.
996  pending_pile->AddRecordingAt(0, 0);
997
998  SetupPendingTree(pending_pile);
999
1000  ASSERT_TRUE(pending_layer_->CanHaveTilings());
1001  pending_layer_->AddTiling(1.0f);
1002  pending_layer_->AddTiling(2.0f);
1003
1004  // It should be safe to call this (and MarkVisibleResourcesAsRequired)
1005  // on a layer with no recordings.
1006  host_impl_.pending_tree()->UpdateDrawProperties();
1007  pending_layer_->MarkVisibleResourcesAsRequired();
1008}
1009
1010TEST_F(PictureLayerImplTest, MarkRequiredOffscreenTiles) {
1011  gfx::Size tile_size(100, 100);
1012  gfx::Size layer_bounds(200, 100);
1013
1014  scoped_refptr<FakePicturePileImpl> pending_pile =
1015      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1016  SetupPendingTree(pending_pile);
1017
1018  pending_layer_->set_fixed_tile_size(tile_size);
1019  ASSERT_TRUE(pending_layer_->CanHaveTilings());
1020  PictureLayerTiling* tiling = pending_layer_->AddTiling(1.f);
1021  host_impl_.pending_tree()->UpdateDrawProperties();
1022  EXPECT_EQ(tiling->resolution(), HIGH_RESOLUTION);
1023
1024  // Fake set priorities.
1025  int tile_count = 0;
1026  for (PictureLayerTiling::CoverageIterator iter(
1027           tiling,
1028           pending_layer_->contents_scale_x(),
1029           gfx::Rect(pending_layer_->visible_content_rect()));
1030       iter;
1031       ++iter) {
1032    if (!*iter)
1033      continue;
1034    Tile* tile = *iter;
1035    TilePriority priority;
1036    priority.resolution = HIGH_RESOLUTION;
1037    if (++tile_count % 2) {
1038      priority.time_to_visible_in_seconds = 0.f;
1039      priority.distance_to_visible_in_pixels = 0.f;
1040    } else {
1041      priority.time_to_visible_in_seconds = 1.f;
1042      priority.distance_to_visible_in_pixels = 1.f;
1043    }
1044    tile->SetPriority(PENDING_TREE, priority);
1045  }
1046
1047  pending_layer_->MarkVisibleResourcesAsRequired();
1048
1049  int num_visible = 0;
1050  int num_offscreen = 0;
1051
1052  for (PictureLayerTiling::CoverageIterator iter(
1053           tiling,
1054           pending_layer_->contents_scale_x(),
1055           gfx::Rect(pending_layer_->visible_content_rect()));
1056       iter;
1057       ++iter) {
1058    if (!*iter)
1059      continue;
1060    const Tile* tile = *iter;
1061    if (tile->priority(PENDING_TREE).distance_to_visible_in_pixels == 0.f) {
1062      EXPECT_TRUE(tile->required_for_activation());
1063      num_visible++;
1064    } else {
1065      EXPECT_FALSE(tile->required_for_activation());
1066      num_offscreen++;
1067    }
1068  }
1069
1070  EXPECT_GT(num_visible, 0);
1071  EXPECT_GT(num_offscreen, 0);
1072}
1073
1074TEST_F(PictureLayerImplTest, ActivateUninitializedLayer) {
1075  gfx::Size tile_size(100, 100);
1076  gfx::Size layer_bounds(400, 400);
1077  scoped_refptr<FakePicturePileImpl> pending_pile =
1078      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1079
1080  host_impl_.CreatePendingTree();
1081  LayerTreeImpl* pending_tree = host_impl_.pending_tree();
1082
1083  scoped_ptr<FakePictureLayerImpl> pending_layer =
1084      FakePictureLayerImpl::CreateWithPile(pending_tree, id_, pending_pile);
1085  pending_layer->SetDrawsContent(true);
1086  pending_tree->SetRootLayer(pending_layer.PassAs<LayerImpl>());
1087
1088  pending_layer_ = static_cast<FakePictureLayerImpl*>(
1089      host_impl_.pending_tree()->LayerById(id_));
1090
1091  // Set some state on the pending layer, make sure it is not clobbered
1092  // by a sync from the active layer.  This could happen because if the
1093  // pending layer has not been post-commit initialized it will attempt
1094  // to sync from the active layer.
1095  bool default_lcd_text_setting = pending_layer_->is_using_lcd_text();
1096  pending_layer_->force_set_lcd_text(!default_lcd_text_setting);
1097  EXPECT_TRUE(pending_layer_->needs_post_commit_initialization());
1098
1099  host_impl_.ActivatePendingTree();
1100
1101  active_layer_ = static_cast<FakePictureLayerImpl*>(
1102      host_impl_.active_tree()->LayerById(id_));
1103
1104  EXPECT_EQ(0u, active_layer_->num_tilings());
1105  EXPECT_EQ(!default_lcd_text_setting, active_layer_->is_using_lcd_text());
1106  EXPECT_FALSE(active_layer_->needs_post_commit_initialization());
1107}
1108
1109class DeferredInitPictureLayerImplTest : public PictureLayerImplTest {
1110 public:
1111  DeferredInitPictureLayerImplTest()
1112      : PictureLayerImplTest(ImplSidePaintingSettings()) {}
1113
1114  virtual void InitializeRenderer() OVERRIDE {
1115    host_impl_.InitializeRenderer(FakeOutputSurface::CreateDeferredGL(
1116        scoped_ptr<SoftwareOutputDevice>(new SoftwareOutputDevice))
1117                                      .PassAs<OutputSurface>());
1118  }
1119
1120  virtual void SetUp() OVERRIDE {
1121    PictureLayerImplTest::SetUp();
1122
1123    // Create some default active and pending trees.
1124    gfx::Size tile_size(100, 100);
1125    gfx::Size layer_bounds(400, 400);
1126
1127    scoped_refptr<FakePicturePileImpl> pending_pile =
1128        FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1129    scoped_refptr<FakePicturePileImpl> active_pile =
1130        FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1131
1132    SetupTrees(pending_pile, active_pile);
1133  }
1134};
1135
1136// This test is really a LayerTreeHostImpl test, in that it makes sure
1137// that trees need update draw properties after deferred initialization.
1138// However, this is also a regression test for PictureLayerImpl in that
1139// not having this update will cause a crash.
1140TEST_F(DeferredInitPictureLayerImplTest,
1141       PreventUpdateTilePrioritiesDuringLostContext) {
1142  host_impl_.pending_tree()->UpdateDrawProperties();
1143  host_impl_.active_tree()->UpdateDrawProperties();
1144  EXPECT_FALSE(host_impl_.pending_tree()->needs_update_draw_properties());
1145  EXPECT_FALSE(host_impl_.active_tree()->needs_update_draw_properties());
1146
1147  FakeOutputSurface* fake_output_surface =
1148      static_cast<FakeOutputSurface*>(host_impl_.output_surface());
1149  ASSERT_TRUE(fake_output_surface->InitializeAndSetContext3d(
1150      TestContextProvider::Create(), NULL));
1151
1152  // These will crash PictureLayerImpl if this is not true.
1153  ASSERT_TRUE(host_impl_.pending_tree()->needs_update_draw_properties());
1154  ASSERT_TRUE(host_impl_.active_tree()->needs_update_draw_properties());
1155  host_impl_.active_tree()->UpdateDrawProperties();
1156}
1157
1158}  // namespace
1159}  // namespace cc
1160