Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / cc / layers / picture_layer_impl.cc
1 // Copyright 2012 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
11 #include "base/debug/trace_event_argument.h"
12 #include "base/time/time.h"
13 #include "cc/base/math_util.h"
14 #include "cc/base/util.h"
15 #include "cc/debug/debug_colors.h"
16 #include "cc/debug/micro_benchmark_impl.h"
17 #include "cc/debug/traced_value.h"
18 #include "cc/layers/append_quads_data.h"
19 #include "cc/layers/solid_color_layer_impl.h"
20 #include "cc/output/begin_frame_args.h"
21 #include "cc/quads/checkerboard_draw_quad.h"
22 #include "cc/quads/debug_border_draw_quad.h"
23 #include "cc/quads/picture_draw_quad.h"
24 #include "cc/quads/solid_color_draw_quad.h"
25 #include "cc/quads/tile_draw_quad.h"
26 #include "cc/resources/tile_manager.h"
27 #include "cc/trees/layer_tree_impl.h"
28 #include "cc/trees/occlusion_tracker.h"
29 #include "ui/gfx/quad_f.h"
30 #include "ui/gfx/rect_conversions.h"
31 #include "ui/gfx/size_conversions.h"
32
33 namespace {
34 const float kMaxScaleRatioDuringPinch = 2.0f;
35
36 // When creating a new tiling during pinch, snap to an existing
37 // tiling's scale if the desired scale is within this ratio.
38 const float kSnapToExistingTilingRatio = 1.2f;
39
40 // Estimate skewport 60 frames ahead for pre-rasterization on the CPU.
41 const float kCpuSkewportTargetTimeInFrames = 60.0f;
42
43 // Don't pre-rasterize on the GPU (except for kBackflingGuardDistancePixels in
44 // TileManager::BinFromTilePriority).
45 const float kGpuSkewportTargetTimeInFrames = 0.0f;
46
47 }  // namespace
48
49 namespace cc {
50
51 PictureLayerImpl::Pair::Pair() : active(NULL), pending(NULL) {
52 }
53
54 PictureLayerImpl::Pair::Pair(PictureLayerImpl* active_layer,
55                              PictureLayerImpl* pending_layer)
56     : active(active_layer), pending(pending_layer) {
57 }
58
59 PictureLayerImpl::Pair::~Pair() {
60 }
61
62 PictureLayerImpl::PictureLayerImpl(LayerTreeImpl* tree_impl, int id)
63     : LayerImpl(tree_impl, id),
64       twin_layer_(NULL),
65       pile_(PicturePileImpl::Create()),
66       ideal_page_scale_(0.f),
67       ideal_device_scale_(0.f),
68       ideal_source_scale_(0.f),
69       ideal_contents_scale_(0.f),
70       raster_page_scale_(0.f),
71       raster_device_scale_(0.f),
72       raster_source_scale_(0.f),
73       raster_contents_scale_(0.f),
74       low_res_raster_contents_scale_(0.f),
75       raster_source_scale_is_fixed_(false),
76       was_screen_space_transform_animating_(false),
77       needs_post_commit_initialization_(true),
78       should_update_tile_priorities_(false) {
79   layer_tree_impl()->RegisterPictureLayerImpl(this);
80 }
81
82 PictureLayerImpl::~PictureLayerImpl() {
83   layer_tree_impl()->UnregisterPictureLayerImpl(this);
84 }
85
86 const char* PictureLayerImpl::LayerTypeAsString() const {
87   return "cc::PictureLayerImpl";
88 }
89
90 scoped_ptr<LayerImpl> PictureLayerImpl::CreateLayerImpl(
91     LayerTreeImpl* tree_impl) {
92   return PictureLayerImpl::Create(tree_impl, id()).PassAs<LayerImpl>();
93 }
94
95 void PictureLayerImpl::PushPropertiesTo(LayerImpl* base_layer) {
96   // It's possible this layer was never drawn or updated (e.g. because it was
97   // a descendant of an opacity 0 layer).
98   DoPostCommitInitializationIfNeeded();
99   PictureLayerImpl* layer_impl = static_cast<PictureLayerImpl*>(base_layer);
100
101   // We have already synced the important bits from the the active layer, and
102   // we will soon swap out its tilings and use them for recycling. However,
103   // there are now tiles in this layer's tilings that were unref'd and replaced
104   // with new tiles (due to invalidation). This resets all active priorities on
105   // the to-be-recycled tiling to ensure replaced tiles don't linger and take
106   // memory (due to a stale 'active' priority).
107   if (layer_impl->tilings_)
108     layer_impl->tilings_->DidBecomeRecycled();
109
110   LayerImpl::PushPropertiesTo(base_layer);
111
112   // When the pending tree pushes to the active tree, the pending twin
113   // becomes recycled.
114   layer_impl->twin_layer_ = NULL;
115   twin_layer_ = NULL;
116
117   layer_impl->pile_ = pile_;
118
119   DCHECK(!pile_->is_solid_color() || !tilings_->num_tilings());
120   // Tilings would be expensive to push, so we swap.
121   layer_impl->tilings_.swap(tilings_);
122   layer_impl->tilings_->SetClient(layer_impl);
123   if (tilings_)
124     tilings_->SetClient(this);
125
126   // Ensure that the recycle tree doesn't have any unshared tiles.
127   if (tilings_ && pile_->is_solid_color())
128     tilings_->RemoveAllTilings();
129
130   // Remove invalidated tiles from what will become a recycle tree.
131   if (tilings_)
132     tilings_->RemoveTilesInRegion(invalidation_);
133
134   layer_impl->raster_page_scale_ = raster_page_scale_;
135   layer_impl->raster_device_scale_ = raster_device_scale_;
136   layer_impl->raster_source_scale_ = raster_source_scale_;
137   layer_impl->raster_contents_scale_ = raster_contents_scale_;
138   layer_impl->low_res_raster_contents_scale_ = low_res_raster_contents_scale_;
139   layer_impl->needs_post_commit_initialization_ = false;
140
141   // The invalidation on this soon-to-be-recycled layer must be cleared to
142   // mirror clearing the invalidation in PictureLayer's version of this function
143   // in case push properties is skipped.
144   layer_impl->invalidation_.Swap(&invalidation_);
145   invalidation_.Clear();
146   needs_post_commit_initialization_ = true;
147
148   // We always need to push properties.
149   // See http://crbug.com/303943
150   needs_push_properties_ = true;
151 }
152
153 void PictureLayerImpl::AppendQuads(
154     RenderPass* render_pass,
155     const OcclusionTracker<LayerImpl>& occlusion_tracker,
156     AppendQuadsData* append_quads_data) {
157   DCHECK(!needs_post_commit_initialization_);
158
159   SharedQuadState* shared_quad_state =
160       render_pass->CreateAndAppendSharedQuadState();
161
162   if (pile_->is_solid_color()) {
163     PopulateSharedQuadState(shared_quad_state);
164
165     AppendDebugBorderQuad(
166         render_pass, content_bounds(), shared_quad_state, append_quads_data);
167
168     SolidColorLayerImpl::AppendSolidQuads(
169         render_pass,
170         occlusion_tracker,
171         shared_quad_state,
172         content_bounds(),
173         draw_properties().target_space_transform,
174         pile_->solid_color());
175     return;
176   }
177
178   float max_contents_scale = MaximumTilingContentsScale();
179   gfx::Transform scaled_draw_transform = draw_transform();
180   scaled_draw_transform.Scale(SK_MScalar1 / max_contents_scale,
181                               SK_MScalar1 / max_contents_scale);
182   gfx::Size scaled_content_bounds =
183       gfx::ToCeiledSize(gfx::ScaleSize(content_bounds(), max_contents_scale));
184
185   gfx::Rect scaled_visible_content_rect =
186       gfx::ScaleToEnclosingRect(visible_content_rect(), max_contents_scale);
187   scaled_visible_content_rect.Intersect(gfx::Rect(scaled_content_bounds));
188
189   Occlusion occlusion =
190       occlusion_tracker.GetCurrentOcclusionForLayer(scaled_draw_transform);
191
192   shared_quad_state->SetAll(scaled_draw_transform,
193                             scaled_content_bounds,
194                             scaled_visible_content_rect,
195                             draw_properties().clip_rect,
196                             draw_properties().is_clipped,
197                             draw_properties().opacity,
198                             blend_mode(),
199                             sorting_context_id_);
200
201   if (current_draw_mode_ == DRAW_MODE_RESOURCELESS_SOFTWARE) {
202     AppendDebugBorderQuad(
203         render_pass,
204         scaled_content_bounds,
205         shared_quad_state,
206         append_quads_data,
207         DebugColors::DirectPictureBorderColor(),
208         DebugColors::DirectPictureBorderWidth(layer_tree_impl()));
209
210     gfx::Rect geometry_rect = scaled_visible_content_rect;
211     gfx::Rect opaque_rect = contents_opaque() ? geometry_rect : gfx::Rect();
212     gfx::Rect visible_geometry_rect =
213         occlusion.GetUnoccludedContentRect(geometry_rect);
214     if (visible_geometry_rect.IsEmpty())
215       return;
216
217     gfx::Size texture_size = scaled_visible_content_rect.size();
218     gfx::RectF texture_rect = gfx::RectF(texture_size);
219     gfx::Rect quad_content_rect = scaled_visible_content_rect;
220
221     PictureDrawQuad* quad =
222         render_pass->CreateAndAppendDrawQuad<PictureDrawQuad>();
223     quad->SetNew(shared_quad_state,
224                  geometry_rect,
225                  opaque_rect,
226                  visible_geometry_rect,
227                  texture_rect,
228                  texture_size,
229                  RGBA_8888,
230                  quad_content_rect,
231                  max_contents_scale,
232                  pile_);
233     return;
234   }
235
236   AppendDebugBorderQuad(
237       render_pass, scaled_content_bounds, shared_quad_state, append_quads_data);
238
239   if (ShowDebugBorders()) {
240     for (PictureLayerTilingSet::CoverageIterator iter(
241              tilings_.get(),
242              max_contents_scale,
243              scaled_visible_content_rect,
244              ideal_contents_scale_);
245          iter;
246          ++iter) {
247       SkColor color;
248       float width;
249       if (*iter && iter->IsReadyToDraw()) {
250         ManagedTileState::TileVersion::Mode mode =
251             iter->GetTileVersionForDrawing().mode();
252         if (mode == ManagedTileState::TileVersion::SOLID_COLOR_MODE) {
253           color = DebugColors::SolidColorTileBorderColor();
254           width = DebugColors::SolidColorTileBorderWidth(layer_tree_impl());
255         } else if (mode == ManagedTileState::TileVersion::PICTURE_PILE_MODE) {
256           color = DebugColors::PictureTileBorderColor();
257           width = DebugColors::PictureTileBorderWidth(layer_tree_impl());
258         } else if (iter->priority(ACTIVE_TREE).resolution == HIGH_RESOLUTION) {
259           color = DebugColors::HighResTileBorderColor();
260           width = DebugColors::HighResTileBorderWidth(layer_tree_impl());
261         } else if (iter->priority(ACTIVE_TREE).resolution == LOW_RESOLUTION) {
262           color = DebugColors::LowResTileBorderColor();
263           width = DebugColors::LowResTileBorderWidth(layer_tree_impl());
264         } else if (iter->contents_scale() > max_contents_scale) {
265           color = DebugColors::ExtraHighResTileBorderColor();
266           width = DebugColors::ExtraHighResTileBorderWidth(layer_tree_impl());
267         } else {
268           color = DebugColors::ExtraLowResTileBorderColor();
269           width = DebugColors::ExtraLowResTileBorderWidth(layer_tree_impl());
270         }
271       } else {
272         color = DebugColors::MissingTileBorderColor();
273         width = DebugColors::MissingTileBorderWidth(layer_tree_impl());
274       }
275
276       DebugBorderDrawQuad* debug_border_quad =
277           render_pass->CreateAndAppendDrawQuad<DebugBorderDrawQuad>();
278       gfx::Rect geometry_rect = iter.geometry_rect();
279       gfx::Rect visible_geometry_rect = geometry_rect;
280       debug_border_quad->SetNew(shared_quad_state,
281                                 geometry_rect,
282                                 visible_geometry_rect,
283                                 color,
284                                 width);
285     }
286   }
287
288   // Keep track of the tilings that were used so that tilings that are
289   // unused can be considered for removal.
290   std::vector<PictureLayerTiling*> seen_tilings;
291
292   // Ignore missing tiles outside of viewport for tile priority. This is
293   // normally the same as draw viewport but can be independently overridden by
294   // embedders like Android WebView with SetExternalDrawConstraints.
295   gfx::Rect scaled_viewport_for_tile_priority = gfx::ScaleToEnclosingRect(
296       GetViewportForTilePriorityInContentSpace(), max_contents_scale);
297
298   size_t missing_tile_count = 0u;
299   size_t on_demand_missing_tile_count = 0u;
300   for (PictureLayerTilingSet::CoverageIterator iter(tilings_.get(),
301                                                     max_contents_scale,
302                                                     scaled_visible_content_rect,
303                                                     ideal_contents_scale_);
304        iter;
305        ++iter) {
306     gfx::Rect geometry_rect = iter.geometry_rect();
307     gfx::Rect opaque_rect = contents_opaque() ? geometry_rect : gfx::Rect();
308     gfx::Rect visible_geometry_rect =
309         occlusion.GetUnoccludedContentRect(geometry_rect);
310     if (visible_geometry_rect.IsEmpty())
311       continue;
312
313     append_quads_data->visible_content_area +=
314         visible_geometry_rect.width() * visible_geometry_rect.height();
315
316     bool has_draw_quad = false;
317     if (*iter && iter->IsReadyToDraw()) {
318       const ManagedTileState::TileVersion& tile_version =
319           iter->GetTileVersionForDrawing();
320       switch (tile_version.mode()) {
321         case ManagedTileState::TileVersion::RESOURCE_MODE: {
322           gfx::RectF texture_rect = iter.texture_rect();
323
324           // The raster_contents_scale_ is the best scale that the layer is
325           // trying to produce, even though it may not be ideal. Since that's
326           // the best the layer can promise in the future, consider those as
327           // complete. But if a tile is ideal scale, we don't want to consider
328           // it incomplete and trying to replace it with a tile at a worse
329           // scale.
330           if (iter->contents_scale() != raster_contents_scale_ &&
331               iter->contents_scale() != ideal_contents_scale_ &&
332               geometry_rect.Intersects(scaled_viewport_for_tile_priority)) {
333             append_quads_data->num_incomplete_tiles++;
334           }
335
336           TileDrawQuad* quad =
337               render_pass->CreateAndAppendDrawQuad<TileDrawQuad>();
338           quad->SetNew(shared_quad_state,
339                        geometry_rect,
340                        opaque_rect,
341                        visible_geometry_rect,
342                        tile_version.get_resource_id(),
343                        texture_rect,
344                        iter.texture_size(),
345                        tile_version.contents_swizzled());
346           has_draw_quad = true;
347           break;
348         }
349         case ManagedTileState::TileVersion::PICTURE_PILE_MODE: {
350           if (!layer_tree_impl()
351                    ->GetRendererCapabilities()
352                    .allow_rasterize_on_demand) {
353             ++on_demand_missing_tile_count;
354             break;
355           }
356
357           gfx::RectF texture_rect = iter.texture_rect();
358
359           ResourceProvider* resource_provider =
360               layer_tree_impl()->resource_provider();
361           ResourceFormat format =
362               resource_provider->memory_efficient_texture_format();
363           PictureDrawQuad* quad =
364               render_pass->CreateAndAppendDrawQuad<PictureDrawQuad>();
365           quad->SetNew(shared_quad_state,
366                        geometry_rect,
367                        opaque_rect,
368                        visible_geometry_rect,
369                        texture_rect,
370                        iter.texture_size(),
371                        format,
372                        iter->content_rect(),
373                        iter->contents_scale(),
374                        pile_);
375           has_draw_quad = true;
376           break;
377         }
378         case ManagedTileState::TileVersion::SOLID_COLOR_MODE: {
379           SolidColorDrawQuad* quad =
380               render_pass->CreateAndAppendDrawQuad<SolidColorDrawQuad>();
381           quad->SetNew(shared_quad_state,
382                        geometry_rect,
383                        visible_geometry_rect,
384                        tile_version.get_solid_color(),
385                        false);
386           has_draw_quad = true;
387           break;
388         }
389       }
390     }
391
392     if (!has_draw_quad) {
393       if (draw_checkerboard_for_missing_tiles()) {
394         CheckerboardDrawQuad* quad =
395             render_pass->CreateAndAppendDrawQuad<CheckerboardDrawQuad>();
396         SkColor color = DebugColors::DefaultCheckerboardColor();
397         quad->SetNew(
398             shared_quad_state, geometry_rect, visible_geometry_rect, color);
399       } else {
400         SkColor color = SafeOpaqueBackgroundColor();
401         SolidColorDrawQuad* quad =
402             render_pass->CreateAndAppendDrawQuad<SolidColorDrawQuad>();
403         quad->SetNew(shared_quad_state,
404                      geometry_rect,
405                      visible_geometry_rect,
406                      color,
407                      false);
408       }
409
410       if (geometry_rect.Intersects(scaled_viewport_for_tile_priority)) {
411         append_quads_data->num_missing_tiles++;
412         ++missing_tile_count;
413       }
414       append_quads_data->approximated_visible_content_area +=
415           visible_geometry_rect.width() * visible_geometry_rect.height();
416       continue;
417     }
418
419     if (iter->priority(ACTIVE_TREE).resolution != HIGH_RESOLUTION) {
420       append_quads_data->approximated_visible_content_area +=
421           visible_geometry_rect.width() * visible_geometry_rect.height();
422     }
423
424     if (seen_tilings.empty() || seen_tilings.back() != iter.CurrentTiling())
425       seen_tilings.push_back(iter.CurrentTiling());
426   }
427
428   if (missing_tile_count) {
429     TRACE_EVENT_INSTANT2("cc",
430                          "PictureLayerImpl::AppendQuads checkerboard",
431                          TRACE_EVENT_SCOPE_THREAD,
432                          "missing_tile_count",
433                          missing_tile_count,
434                          "on_demand_missing_tile_count",
435                          on_demand_missing_tile_count);
436   }
437
438   // Aggressively remove any tilings that are not seen to save memory. Note
439   // that this is at the expense of doing cause more frequent re-painting. A
440   // better scheme would be to maintain a tighter visible_content_rect for the
441   // finer tilings.
442   CleanUpTilingsOnActiveLayer(seen_tilings);
443 }
444
445 void PictureLayerImpl::UpdateTiles(
446     const Occlusion& occlusion_in_content_space) {
447   TRACE_EVENT0("cc", "PictureLayerImpl::UpdateTiles");
448   DCHECK_EQ(1.f, contents_scale_x());
449   DCHECK_EQ(1.f, contents_scale_y());
450
451   DoPostCommitInitializationIfNeeded();
452
453   visible_rect_for_tile_priority_ = visible_content_rect();
454   viewport_rect_for_tile_priority_ =
455       layer_tree_impl()->ViewportRectForTilePriority();
456   screen_space_transform_for_tile_priority_ = screen_space_transform();
457
458   if (!CanHaveTilings()) {
459     ideal_page_scale_ = 0.f;
460     ideal_device_scale_ = 0.f;
461     ideal_contents_scale_ = 0.f;
462     ideal_source_scale_ = 0.f;
463     SanityCheckTilingState();
464     return;
465   }
466
467   UpdateIdealScales();
468
469   DCHECK(tilings_->num_tilings() > 0 || raster_contents_scale_ == 0.f)
470       << "A layer with no tilings shouldn't have valid raster scales";
471   if (!raster_contents_scale_ || ShouldAdjustRasterScale()) {
472     RecalculateRasterScales();
473     AddTilingsForRasterScale();
474   }
475
476   DCHECK(raster_page_scale_);
477   DCHECK(raster_device_scale_);
478   DCHECK(raster_source_scale_);
479   DCHECK(raster_contents_scale_);
480   DCHECK(low_res_raster_contents_scale_);
481
482   was_screen_space_transform_animating_ =
483       draw_properties().screen_space_transform_is_animating;
484
485   should_update_tile_priorities_ = true;
486
487   UpdateTilePriorities(occlusion_in_content_space);
488
489   if (layer_tree_impl()->IsPendingTree())
490     MarkVisibleResourcesAsRequired();
491 }
492
493 void PictureLayerImpl::UpdateTilePriorities(
494     const Occlusion& occlusion_in_content_space) {
495   DCHECK(!pile_->is_solid_color() || !tilings_->num_tilings());
496
497   TRACE_EVENT0("cc", "PictureLayerImpl::UpdateTilePriorities");
498
499   double current_frame_time_in_seconds =
500       (layer_tree_impl()->CurrentBeginFrameArgs().frame_time -
501        base::TimeTicks()).InSecondsF();
502
503   bool tiling_needs_update = false;
504   for (size_t i = 0; i < tilings_->num_tilings(); ++i) {
505     if (tilings_->tiling_at(i)->NeedsUpdateForFrameAtTime(
506             current_frame_time_in_seconds)) {
507       tiling_needs_update = true;
508       break;
509     }
510   }
511   if (!tiling_needs_update)
512     return;
513
514   gfx::Rect viewport_rect_in_layer_space =
515       GetViewportForTilePriorityInContentSpace();
516   WhichTree tree =
517       layer_tree_impl()->IsActiveTree() ? ACTIVE_TREE : PENDING_TREE;
518   for (size_t i = 0; i < tilings_->num_tilings(); ++i) {
519     // Pass |occlusion_in_content_space| for |occlusion_in_layer_space| since
520     // they are the same space in picture lbayer, as contents scale is always 1.
521     tilings_->tiling_at(i)->UpdateTilePriorities(tree,
522                                                  viewport_rect_in_layer_space,
523                                                  ideal_contents_scale_,
524                                                  current_frame_time_in_seconds,
525                                                  occlusion_in_content_space);
526   }
527
528   // Tile priorities were modified.
529   layer_tree_impl()->DidModifyTilePriorities();
530 }
531
532 gfx::Rect PictureLayerImpl::GetViewportForTilePriorityInContentSpace() const {
533   // If visible_rect_for_tile_priority_ is empty or
534   // viewport_rect_for_tile_priority_ is set to be different from the device
535   // viewport, try to inverse project the viewport into layer space and use
536   // that. Otherwise just use visible_rect_for_tile_priority_
537   gfx::Rect visible_rect_in_content_space = visible_rect_for_tile_priority_;
538
539   if (visible_rect_in_content_space.IsEmpty() ||
540       layer_tree_impl()->DeviceViewport() != viewport_rect_for_tile_priority_) {
541     gfx::Transform view_to_layer(gfx::Transform::kSkipInitialization);
542
543     if (screen_space_transform_for_tile_priority_.GetInverse(&view_to_layer)) {
544       // Transform from view space to content space.
545       visible_rect_in_content_space =
546           gfx::ToEnclosingRect(MathUtil::ProjectClippedRect(
547               view_to_layer, viewport_rect_for_tile_priority_));
548     }
549   }
550   return visible_rect_in_content_space;
551 }
552
553 PictureLayerImpl* PictureLayerImpl::GetRecycledTwinLayer() {
554   // TODO(vmpstr): Maintain recycled twin as a member. crbug.com/407418
555   return static_cast<PictureLayerImpl*>(
556       layer_tree_impl()->FindRecycleTreeLayerById(id()));
557 }
558
559 void PictureLayerImpl::NotifyTileStateChanged(const Tile* tile) {
560   if (layer_tree_impl()->IsActiveTree()) {
561     gfx::RectF layer_damage_rect =
562         gfx::ScaleRect(tile->content_rect(), 1.f / tile->contents_scale());
563     AddDamageRect(layer_damage_rect);
564   }
565 }
566
567 void PictureLayerImpl::DidBecomeActive() {
568   LayerImpl::DidBecomeActive();
569   tilings_->DidBecomeActive();
570   layer_tree_impl()->DidModifyTilePriorities();
571 }
572
573 void PictureLayerImpl::DidBeginTracing() {
574   pile_->DidBeginTracing();
575 }
576
577 void PictureLayerImpl::ReleaseResources() {
578   if (tilings_)
579     RemoveAllTilings();
580
581   ResetRasterScale();
582
583   // To avoid an edge case after lost context where the tree is up to date but
584   // the tilings have not been managed, request an update draw properties
585   // to force tilings to get managed.
586   layer_tree_impl()->set_needs_update_draw_properties();
587 }
588
589 skia::RefPtr<SkPicture> PictureLayerImpl::GetPicture() {
590   return pile_->GetFlattenedPicture();
591 }
592
593 scoped_refptr<Tile> PictureLayerImpl::CreateTile(PictureLayerTiling* tiling,
594                                                const gfx::Rect& content_rect) {
595   DCHECK(!pile_->is_solid_color());
596   if (!pile_->CanRaster(tiling->contents_scale(), content_rect))
597     return scoped_refptr<Tile>();
598
599   int flags = 0;
600
601   // TODO(vmpstr): Revisit this. For now, enabling analysis means that we get as
602   // much savings on memory as we can. However, for some cases like ganesh or
603   // small layers, the amount of time we spend analyzing might not justify
604   // memory savings that we can get. Note that we don't handle solid color
605   // masks, so we shouldn't bother analyzing those.
606   // Bugs: crbug.com/397198, crbug.com/396908
607   if (!pile_->is_mask())
608     flags = Tile::USE_PICTURE_ANALYSIS;
609
610   return layer_tree_impl()->tile_manager()->CreateTile(
611       pile_.get(),
612       content_rect.size(),
613       content_rect,
614       tiling->contents_scale(),
615       id(),
616       layer_tree_impl()->source_frame_number(),
617       flags);
618 }
619
620 PicturePileImpl* PictureLayerImpl::GetPile() {
621   return pile_.get();
622 }
623
624 const Region* PictureLayerImpl::GetInvalidation() {
625   return &invalidation_;
626 }
627
628 const PictureLayerTiling* PictureLayerImpl::GetTwinTiling(
629     const PictureLayerTiling* tiling) const {
630   if (!twin_layer_)
631     return NULL;
632   return twin_layer_->tilings_->TilingAtScale(tiling->contents_scale());
633 }
634
635 PictureLayerTiling* PictureLayerImpl::GetRecycledTwinTiling(
636     const PictureLayerTiling* tiling) {
637   PictureLayerImpl* recycled_twin = GetRecycledTwinLayer();
638   if (!recycled_twin || !recycled_twin->tilings_)
639     return NULL;
640   return recycled_twin->tilings_->TilingAtScale(tiling->contents_scale());
641 }
642
643 size_t PictureLayerImpl::GetMaxTilesForInterestArea() const {
644   return layer_tree_impl()->settings().max_tiles_for_interest_area;
645 }
646
647 float PictureLayerImpl::GetSkewportTargetTimeInSeconds() const {
648   float skewport_target_time_in_frames =
649       layer_tree_impl()->use_gpu_rasterization()
650           ? kGpuSkewportTargetTimeInFrames
651           : kCpuSkewportTargetTimeInFrames;
652   return skewport_target_time_in_frames *
653          layer_tree_impl()->begin_impl_frame_interval().InSecondsF() *
654          layer_tree_impl()->settings().skewport_target_time_multiplier;
655 }
656
657 int PictureLayerImpl::GetSkewportExtrapolationLimitInContentPixels() const {
658   return layer_tree_impl()
659       ->settings()
660       .skewport_extrapolation_limit_in_content_pixels;
661 }
662
663 gfx::Size PictureLayerImpl::CalculateTileSize(
664     const gfx::Size& content_bounds) const {
665   int max_texture_size =
666       layer_tree_impl()->resource_provider()->max_texture_size();
667
668   if (pile_->is_mask()) {
669     // Masks are not tiled, so if we can't cover the whole mask with one tile,
670     // don't make any tiles at all. Returning an empty size signals this.
671     if (content_bounds.width() > max_texture_size ||
672         content_bounds.height() > max_texture_size)
673       return gfx::Size();
674     return content_bounds;
675   }
676
677   gfx::Size default_tile_size = layer_tree_impl()->settings().default_tile_size;
678   if (layer_tree_impl()->use_gpu_rasterization()) {
679     // TODO(ernstm) crbug.com/365877: We need a unified way to override the
680     // default-tile-size.
681     default_tile_size =
682         gfx::Size(layer_tree_impl()->device_viewport_size().width(),
683                   layer_tree_impl()->device_viewport_size().height() / 4);
684   }
685   default_tile_size.SetToMin(gfx::Size(max_texture_size, max_texture_size));
686
687   gfx::Size max_untiled_content_size =
688       layer_tree_impl()->settings().max_untiled_layer_size;
689   max_untiled_content_size.SetToMin(
690       gfx::Size(max_texture_size, max_texture_size));
691
692   bool any_dimension_too_large =
693       content_bounds.width() > max_untiled_content_size.width() ||
694       content_bounds.height() > max_untiled_content_size.height();
695
696   bool any_dimension_one_tile =
697       content_bounds.width() <= default_tile_size.width() ||
698       content_bounds.height() <= default_tile_size.height();
699
700   // If long and skinny, tile at the max untiled content size, and clamp
701   // the smaller dimension to the content size, e.g. 1000x12 layer with
702   // 500x500 max untiled size would get 500x12 tiles.  Also do this
703   // if the layer is small.
704   if (any_dimension_one_tile || !any_dimension_too_large) {
705     int width = std::min(
706         std::max(max_untiled_content_size.width(), default_tile_size.width()),
707         content_bounds.width());
708     int height = std::min(
709         std::max(max_untiled_content_size.height(), default_tile_size.height()),
710         content_bounds.height());
711     // Round up to the closest multiple of 64. This improves recycling and
712     // avoids odd texture sizes.
713     width = RoundUp(width, 64);
714     height = RoundUp(height, 64);
715     return gfx::Size(width, height);
716   }
717
718   return default_tile_size;
719 }
720
721 void PictureLayerImpl::SyncFromActiveLayer(const PictureLayerImpl* other) {
722   TRACE_EVENT0("cc", "SyncFromActiveLayer");
723   DCHECK(!other->needs_post_commit_initialization_);
724   DCHECK(other->tilings_);
725
726   if (!DrawsContent()) {
727     RemoveAllTilings();
728     return;
729   }
730
731   raster_page_scale_ = other->raster_page_scale_;
732   raster_device_scale_ = other->raster_device_scale_;
733   raster_source_scale_ = other->raster_source_scale_;
734   raster_contents_scale_ = other->raster_contents_scale_;
735   low_res_raster_contents_scale_ = other->low_res_raster_contents_scale_;
736
737   bool synced_high_res_tiling = false;
738   if (CanHaveTilings()) {
739     synced_high_res_tiling = tilings_->SyncTilings(
740         *other->tilings_, bounds(), invalidation_, MinimumContentsScale());
741   } else {
742     RemoveAllTilings();
743   }
744
745   // If our MinimumContentsScale has changed to prevent the twin's high res
746   // tiling from being synced, we should reset the raster scale and let it be
747   // recalculated (1) again. This can happen if our bounds shrink to the point
748   // where min contents scale grows.
749   // (1) - TODO(vmpstr) Instead of hoping that this will be recalculated, we
750   // should refactor this code a little bit and actually recalculate this.
751   // However, this is a larger undertaking, so this will work for now.
752   if (!synced_high_res_tiling)
753     ResetRasterScale();
754   else
755     SanityCheckTilingState();
756 }
757
758 void PictureLayerImpl::SyncTiling(
759     const PictureLayerTiling* tiling) {
760   if (!CanHaveTilingWithScale(tiling->contents_scale()))
761     return;
762   tilings_->AddTiling(tiling->contents_scale());
763
764   // If this tree needs update draw properties, then the tiling will
765   // get updated prior to drawing or activation.  If this tree does not
766   // need update draw properties, then its transforms are up to date and
767   // we can create tiles for this tiling immediately.
768   if (!layer_tree_impl()->needs_update_draw_properties() &&
769       should_update_tile_priorities_) {
770     // TODO(danakj): Add a DCHECK() that we are not using occlusion tracking
771     // when we stop using the pending tree in the browser compositor. If we want
772     // to support occlusion tracking here, we need to dirty the draw properties
773     // or save occlusion as a draw property.
774     UpdateTilePriorities(Occlusion());
775   }
776 }
777
778 ResourceProvider::ResourceId PictureLayerImpl::ContentsResourceId() const {
779   gfx::Rect content_rect(content_bounds());
780   PictureLayerTilingSet::CoverageIterator iter(
781       tilings_.get(), 1.f, content_rect, ideal_contents_scale_);
782
783   // Mask resource not ready yet.
784   if (!iter || !*iter)
785     return 0;
786
787   // Masks only supported if they fit on exactly one tile.
788   DCHECK(iter.geometry_rect() == content_rect)
789       << "iter rect " << iter.geometry_rect().ToString() << " content rect "
790       << content_rect.ToString();
791
792   const ManagedTileState::TileVersion& tile_version =
793       iter->GetTileVersionForDrawing();
794   if (!tile_version.IsReadyToDraw() ||
795       tile_version.mode() != ManagedTileState::TileVersion::RESOURCE_MODE)
796     return 0;
797
798   return tile_version.get_resource_id();
799 }
800
801 void PictureLayerImpl::MarkVisibleResourcesAsRequired() const {
802   DCHECK(layer_tree_impl()->IsPendingTree());
803   DCHECK(ideal_contents_scale_);
804   DCHECK_GT(tilings_->num_tilings(), 0u);
805
806   // The goal of this function is to find the minimum set of tiles that need to
807   // be ready to draw in order to activate without flashing content from a
808   // higher res on the active tree to a lower res on the pending tree.
809
810   // First, early out for layers with no visible content.
811   if (visible_content_rect().IsEmpty())
812     return;
813
814   // Only mark tiles inside the viewport for tile priority as required for
815   // activation. This viewport is normally the same as the draw viewport but
816   // can be independently overridden by embedders like Android WebView with
817   // SetExternalDrawConstraints.
818   gfx::Rect rect = GetViewportForTilePriorityInContentSpace();
819   rect.Intersect(visible_content_rect());
820
821   float min_acceptable_scale =
822       std::min(raster_contents_scale_, ideal_contents_scale_);
823
824   if (PictureLayerImpl* twin = twin_layer_) {
825     float twin_min_acceptable_scale =
826         std::min(twin->ideal_contents_scale_, twin->raster_contents_scale_);
827     // Ignore 0 scale in case CalculateContentsScale() has never been
828     // called for active twin.
829     if (twin_min_acceptable_scale != 0.0f) {
830       min_acceptable_scale =
831           std::min(min_acceptable_scale, twin_min_acceptable_scale);
832     }
833   }
834
835   PictureLayerTiling* high_res = NULL;
836   PictureLayerTiling* low_res = NULL;
837
838   // First pass: ready to draw tiles in acceptable but non-ideal tilings are
839   // marked as required for activation so that their textures are not thrown
840   // away; any non-ready tiles are not marked as required.
841   Region missing_region = rect;
842   for (size_t i = 0; i < tilings_->num_tilings(); ++i) {
843     PictureLayerTiling* tiling = tilings_->tiling_at(i);
844     DCHECK(tiling->has_ever_been_updated());
845
846     if (tiling->resolution() == LOW_RESOLUTION) {
847       DCHECK(!low_res) << "There can only be one low res tiling";
848       low_res = tiling;
849     }
850     if (tiling->contents_scale() < min_acceptable_scale)
851       continue;
852     if (tiling->resolution() == HIGH_RESOLUTION) {
853       DCHECK(!high_res) << "There can only be one high res tiling";
854       high_res = tiling;
855       continue;
856     }
857     for (PictureLayerTiling::CoverageIterator iter(tiling, 1.f, rect); iter;
858          ++iter) {
859       if (!*iter || !iter->IsReadyToDraw())
860         continue;
861
862       missing_region.Subtract(iter.geometry_rect());
863       iter->MarkRequiredForActivation();
864     }
865   }
866   DCHECK(high_res) << "There must be one high res tiling";
867
868   // If these pointers are null (because no twin, no matching tiling, or the
869   // simpification just below), then high res tiles will be required to fill any
870   // holes left by the first pass above.  If the pointers are valid, then this
871   // layer is allowed to skip any tiles that are not ready on its twin.
872   const PictureLayerTiling* twin_high_res = NULL;
873   const PictureLayerTiling* twin_low_res = NULL;
874
875   if (twin_layer_) {
876     // As a simplification, only allow activating to skip twin tiles that the
877     // active layer is also missing when both this layer and its twin have
878     // "simple" sets of tilings: only 2 tilings (high and low) or only 1 high
879     // res tiling. This avoids having to iterate/track coverage of non-ideal
880     // tilings during the last draw call on the active layer.
881     if (tilings_->num_tilings() <= 2 &&
882         twin_layer_->tilings_->num_tilings() <= tilings_->num_tilings()) {
883       twin_low_res = low_res ? GetTwinTiling(low_res) : NULL;
884       twin_high_res = high_res ? GetTwinTiling(high_res) : NULL;
885     }
886
887     // If this layer and its twin have different transforms, then don't compare
888     // them and only allow activating to high res tiles, since tiles on each
889     // layer will be in different places on screen.
890     if (twin_layer_->layer_tree_impl()->RequiresHighResToDraw() ||
891         bounds() != twin_layer_->bounds() ||
892         draw_properties().screen_space_transform !=
893             twin_layer_->draw_properties().screen_space_transform) {
894       twin_high_res = NULL;
895       twin_low_res = NULL;
896     }
897   }
898
899   // As a second pass, mark as required any visible high res tiles not filled in
900   // by acceptable non-ideal tiles from the first pass.
901   if (MarkVisibleTilesAsRequired(
902           high_res, twin_high_res, rect, missing_region)) {
903     // As an optional third pass, if a high res tile was skipped because its
904     // twin was also missing, then fall back to mark low res tiles as required
905     // in case the active twin is substituting those for missing high res
906     // content. Only suitable, when low res is enabled.
907     if (low_res) {
908       MarkVisibleTilesAsRequired(low_res, twin_low_res, rect, missing_region);
909     }
910   }
911 }
912
913 bool PictureLayerImpl::MarkVisibleTilesAsRequired(
914     PictureLayerTiling* tiling,
915     const PictureLayerTiling* optional_twin_tiling,
916     const gfx::Rect& rect,
917     const Region& missing_region) const {
918   bool twin_had_missing_tile = false;
919   for (PictureLayerTiling::CoverageIterator iter(tiling, 1.f, rect); iter;
920        ++iter) {
921     Tile* tile = *iter;
922     // A null tile (i.e. missing recording) can just be skipped.
923     if (!tile)
924       continue;
925
926     // If the tile is occluded, don't mark it as required for activation.
927     if (tile->is_occluded(PENDING_TREE))
928       continue;
929
930     // If the missing region doesn't cover it, this tile is fully
931     // covered by acceptable tiles at other scales.
932     if (!missing_region.Intersects(iter.geometry_rect()))
933       continue;
934
935     // If the twin tile doesn't exist (i.e. missing recording or so far away
936     // that it is outside the visible tile rect) or this tile is shared between
937     // with the twin, then this tile isn't required to prevent flashing.
938     if (optional_twin_tiling) {
939       Tile* twin_tile = optional_twin_tiling->TileAt(iter.i(), iter.j());
940       if (!twin_tile || twin_tile == tile) {
941         // However if the shared tile is being used on the active tree, then
942         // there's no missing content in this place, and low res is not needed.
943         if (!twin_tile || !twin_tile->IsReadyToDraw())
944           twin_had_missing_tile = true;
945         continue;
946       }
947     }
948
949     tile->MarkRequiredForActivation();
950   }
951   return twin_had_missing_tile;
952 }
953
954 void PictureLayerImpl::DoPostCommitInitialization() {
955   DCHECK(needs_post_commit_initialization_);
956   DCHECK(layer_tree_impl()->IsPendingTree());
957
958   if (!tilings_)
959     tilings_.reset(new PictureLayerTilingSet(this, bounds()));
960
961   DCHECK(!twin_layer_);
962   twin_layer_ = static_cast<PictureLayerImpl*>(
963       layer_tree_impl()->FindActiveTreeLayerById(id()));
964   if (twin_layer_) {
965     DCHECK(!twin_layer_->twin_layer_);
966     twin_layer_->twin_layer_ = this;
967     // If the twin has never been pushed to, do not sync from it.
968     // This can happen if this function is called during activation.
969     if (!twin_layer_->needs_post_commit_initialization_)
970       SyncFromActiveLayer(twin_layer_);
971   }
972
973   needs_post_commit_initialization_ = false;
974 }
975
976 PictureLayerTiling* PictureLayerImpl::AddTiling(float contents_scale) {
977   DCHECK(CanHaveTilingWithScale(contents_scale)) <<
978       "contents_scale: " << contents_scale;
979
980   PictureLayerTiling* tiling = tilings_->AddTiling(contents_scale);
981
982   DCHECK(pile_->HasRecordings());
983
984   if (twin_layer_)
985     twin_layer_->SyncTiling(tiling);
986
987   return tiling;
988 }
989
990 void PictureLayerImpl::RemoveTiling(float contents_scale) {
991   if (!tilings_ || tilings_->num_tilings() == 0)
992     return;
993
994   for (size_t i = 0; i < tilings_->num_tilings(); ++i) {
995     PictureLayerTiling* tiling = tilings_->tiling_at(i);
996     if (tiling->contents_scale() == contents_scale) {
997       tilings_->Remove(tiling);
998       break;
999     }
1000   }
1001   if (tilings_->num_tilings() == 0)
1002     ResetRasterScale();
1003   SanityCheckTilingState();
1004 }
1005
1006 void PictureLayerImpl::RemoveAllTilings() {
1007   if (tilings_)
1008     tilings_->RemoveAllTilings();
1009   // If there are no tilings, then raster scales are no longer meaningful.
1010   ResetRasterScale();
1011 }
1012
1013 namespace {
1014
1015 inline float PositiveRatio(float float1, float float2) {
1016   DCHECK_GT(float1, 0);
1017   DCHECK_GT(float2, 0);
1018   return float1 > float2 ? float1 / float2 : float2 / float1;
1019 }
1020
1021 }  // namespace
1022
1023 void PictureLayerImpl::AddTilingsForRasterScale() {
1024   PictureLayerTiling* high_res = NULL;
1025   PictureLayerTiling* low_res = NULL;
1026
1027   PictureLayerTiling* previous_low_res = NULL;
1028   for (size_t i = 0; i < tilings_->num_tilings(); ++i) {
1029     PictureLayerTiling* tiling = tilings_->tiling_at(i);
1030     if (tiling->contents_scale() == raster_contents_scale_)
1031       high_res = tiling;
1032     if (tiling->contents_scale() == low_res_raster_contents_scale_)
1033       low_res = tiling;
1034     if (tiling->resolution() == LOW_RESOLUTION)
1035       previous_low_res = tiling;
1036
1037     // Reset all tilings to non-ideal until the end of this function.
1038     tiling->set_resolution(NON_IDEAL_RESOLUTION);
1039   }
1040
1041   if (!high_res) {
1042     high_res = AddTiling(raster_contents_scale_);
1043     if (raster_contents_scale_ == low_res_raster_contents_scale_)
1044       low_res = high_res;
1045   }
1046
1047   // Only create new low res tilings when the transform is static.  This
1048   // prevents wastefully creating a paired low res tiling for every new high res
1049   // tiling during a pinch or a CSS animation.
1050   bool is_pinching = layer_tree_impl()->PinchGestureActive();
1051   if (layer_tree_impl()->create_low_res_tiling() && !is_pinching &&
1052       !draw_properties().screen_space_transform_is_animating && !low_res &&
1053       low_res != high_res)
1054     low_res = AddTiling(low_res_raster_contents_scale_);
1055
1056   // Set low-res if we have one.
1057   if (!low_res)
1058     low_res = previous_low_res;
1059   if (low_res && low_res != high_res)
1060     low_res->set_resolution(LOW_RESOLUTION);
1061
1062   // Make sure we always have one high-res (even if high == low).
1063   high_res->set_resolution(HIGH_RESOLUTION);
1064
1065   SanityCheckTilingState();
1066 }
1067
1068 bool PictureLayerImpl::ShouldAdjustRasterScale() const {
1069   if (was_screen_space_transform_animating_ !=
1070       draw_properties().screen_space_transform_is_animating)
1071     return true;
1072
1073   if (draw_properties().screen_space_transform_is_animating &&
1074       raster_contents_scale_ != ideal_contents_scale_ &&
1075       ShouldAdjustRasterScaleDuringScaleAnimations())
1076     return true;
1077
1078   bool is_pinching = layer_tree_impl()->PinchGestureActive();
1079   if (is_pinching && raster_page_scale_) {
1080     // We change our raster scale when it is:
1081     // - Higher than ideal (need a lower-res tiling available)
1082     // - Too far from ideal (need a higher-res tiling available)
1083     float ratio = ideal_page_scale_ / raster_page_scale_;
1084     if (raster_page_scale_ > ideal_page_scale_ ||
1085         ratio > kMaxScaleRatioDuringPinch)
1086       return true;
1087   }
1088
1089   if (!is_pinching) {
1090     // When not pinching, match the ideal page scale factor.
1091     if (raster_page_scale_ != ideal_page_scale_)
1092       return true;
1093   }
1094
1095   // Always match the ideal device scale factor.
1096   if (raster_device_scale_ != ideal_device_scale_)
1097     return true;
1098
1099   // When the source scale changes we want to match it, but not when animating
1100   // or when we've fixed the scale in place.
1101   if (!draw_properties().screen_space_transform_is_animating &&
1102       !raster_source_scale_is_fixed_ &&
1103       raster_source_scale_ != ideal_source_scale_)
1104     return true;
1105
1106   return false;
1107 }
1108
1109 float PictureLayerImpl::SnappedContentsScale(float scale) {
1110   // If a tiling exists within the max snapping ratio, snap to its scale.
1111   float snapped_contents_scale = scale;
1112   float snapped_ratio = kSnapToExistingTilingRatio;
1113   for (size_t i = 0; i < tilings_->num_tilings(); ++i) {
1114     float tiling_contents_scale = tilings_->tiling_at(i)->contents_scale();
1115     float ratio = PositiveRatio(tiling_contents_scale, scale);
1116     if (ratio < snapped_ratio) {
1117       snapped_contents_scale = tiling_contents_scale;
1118       snapped_ratio = ratio;
1119     }
1120   }
1121   return snapped_contents_scale;
1122 }
1123
1124 void PictureLayerImpl::RecalculateRasterScales() {
1125   float old_raster_contents_scale = raster_contents_scale_;
1126   float old_raster_page_scale = raster_page_scale_;
1127   float old_raster_source_scale = raster_source_scale_;
1128
1129   raster_device_scale_ = ideal_device_scale_;
1130   raster_page_scale_ = ideal_page_scale_;
1131   raster_source_scale_ = ideal_source_scale_;
1132   raster_contents_scale_ = ideal_contents_scale_;
1133
1134   // If we're not animating, or leaving an animation, and the
1135   // ideal_source_scale_ changes, then things are unpredictable, and we fix
1136   // the raster_source_scale_ in place.
1137   if (old_raster_source_scale &&
1138       !draw_properties().screen_space_transform_is_animating &&
1139       !was_screen_space_transform_animating_ &&
1140       old_raster_source_scale != ideal_source_scale_)
1141     raster_source_scale_is_fixed_ = true;
1142
1143   // TODO(danakj): Adjust raster source scale closer to ideal source scale at
1144   // a throttled rate. Possibly make use of invalidation_.IsEmpty() on pending
1145   // tree. This will allow CSS scale changes to get re-rastered at an
1146   // appropriate rate. (crbug.com/413636)
1147   if (raster_source_scale_is_fixed_) {
1148     raster_contents_scale_ /= raster_source_scale_;
1149     raster_source_scale_ = 1.f;
1150   }
1151
1152   // During pinch we completely ignore the current ideal scale, and just use
1153   // a multiple of the previous scale.
1154   // TODO(danakj): This seems crazy, we should use the current ideal, no?
1155   bool is_pinching = layer_tree_impl()->PinchGestureActive();
1156   if (is_pinching && old_raster_contents_scale) {
1157     // See ShouldAdjustRasterScale:
1158     // - When zooming out, preemptively create new tiling at lower resolution.
1159     // - When zooming in, approximate ideal using multiple of kMaxScaleRatio.
1160     bool zooming_out = old_raster_page_scale > ideal_page_scale_;
1161     float desired_contents_scale =
1162         zooming_out ? old_raster_contents_scale / kMaxScaleRatioDuringPinch
1163                     : old_raster_contents_scale * kMaxScaleRatioDuringPinch;
1164     raster_contents_scale_ = SnappedContentsScale(desired_contents_scale);
1165     raster_page_scale_ =
1166         raster_contents_scale_ / raster_device_scale_ / raster_source_scale_;
1167   }
1168
1169   raster_contents_scale_ =
1170       std::max(raster_contents_scale_, MinimumContentsScale());
1171
1172   // If we're not re-rasterizing during animation, rasterize at the maximum
1173   // scale that will occur during the animation, if the maximum scale is
1174   // known. However, to avoid excessive memory use, don't rasterize at a scale
1175   // at which this layer would become larger than the viewport.
1176   if (draw_properties().screen_space_transform_is_animating &&
1177       !ShouldAdjustRasterScaleDuringScaleAnimations()) {
1178     bool can_raster_at_maximum_scale = false;
1179     if (draw_properties().maximum_animation_contents_scale > 0.f) {
1180       gfx::Size bounds_at_maximum_scale = gfx::ToCeiledSize(gfx::ScaleSize(
1181           bounds(), draw_properties().maximum_animation_contents_scale));
1182       if (bounds_at_maximum_scale.GetArea() <=
1183           layer_tree_impl()->device_viewport_size().GetArea())
1184         can_raster_at_maximum_scale = true;
1185     }
1186     if (can_raster_at_maximum_scale) {
1187       raster_contents_scale_ =
1188           std::max(raster_contents_scale_,
1189                    draw_properties().maximum_animation_contents_scale);
1190     } else {
1191       raster_contents_scale_ =
1192           std::max(raster_contents_scale_,
1193                    1.f * ideal_page_scale_ * ideal_device_scale_);
1194     }
1195   }
1196
1197   // If this layer would create zero or one tiles at this content scale,
1198   // don't create a low res tiling.
1199   gfx::Size content_bounds =
1200       gfx::ToCeiledSize(gfx::ScaleSize(bounds(), raster_contents_scale_));
1201   gfx::Size tile_size = CalculateTileSize(content_bounds);
1202   bool tile_covers_bounds = tile_size.width() >= content_bounds.width() &&
1203                             tile_size.height() >= content_bounds.height();
1204   if (tile_size.IsEmpty() || tile_covers_bounds) {
1205     low_res_raster_contents_scale_ = raster_contents_scale_;
1206     return;
1207   }
1208
1209   float low_res_factor =
1210       layer_tree_impl()->settings().low_res_contents_scale_factor;
1211   low_res_raster_contents_scale_ = std::max(
1212       raster_contents_scale_ * low_res_factor,
1213       MinimumContentsScale());
1214 }
1215
1216 void PictureLayerImpl::CleanUpTilingsOnActiveLayer(
1217     std::vector<PictureLayerTiling*> used_tilings) {
1218   DCHECK(layer_tree_impl()->IsActiveTree());
1219   if (tilings_->num_tilings() == 0)
1220     return;
1221
1222   float min_acceptable_high_res_scale = std::min(
1223       raster_contents_scale_, ideal_contents_scale_);
1224   float max_acceptable_high_res_scale = std::max(
1225       raster_contents_scale_, ideal_contents_scale_);
1226   float twin_low_res_scale = 0.f;
1227
1228   PictureLayerImpl* twin = twin_layer_;
1229   if (twin && twin->CanHaveTilings()) {
1230     min_acceptable_high_res_scale = std::min(
1231         min_acceptable_high_res_scale,
1232         std::min(twin->raster_contents_scale_, twin->ideal_contents_scale_));
1233     max_acceptable_high_res_scale = std::max(
1234         max_acceptable_high_res_scale,
1235         std::max(twin->raster_contents_scale_, twin->ideal_contents_scale_));
1236
1237     for (size_t i = 0; i < twin->tilings_->num_tilings(); ++i) {
1238       PictureLayerTiling* tiling = twin->tilings_->tiling_at(i);
1239       if (tiling->resolution() == LOW_RESOLUTION)
1240         twin_low_res_scale = tiling->contents_scale();
1241     }
1242   }
1243
1244   std::vector<PictureLayerTiling*> to_remove;
1245   for (size_t i = 0; i < tilings_->num_tilings(); ++i) {
1246     PictureLayerTiling* tiling = tilings_->tiling_at(i);
1247
1248     // Keep multiple high resolution tilings even if not used to help
1249     // activate earlier at non-ideal resolutions.
1250     if (tiling->contents_scale() >= min_acceptable_high_res_scale &&
1251         tiling->contents_scale() <= max_acceptable_high_res_scale)
1252       continue;
1253
1254     // Keep low resolution tilings, if the layer should have them.
1255     if (layer_tree_impl()->create_low_res_tiling()) {
1256       if (tiling->resolution() == LOW_RESOLUTION ||
1257           tiling->contents_scale() == twin_low_res_scale)
1258         continue;
1259     }
1260
1261     // Don't remove tilings that are being used (and thus would cause a flash.)
1262     if (std::find(used_tilings.begin(), used_tilings.end(), tiling) !=
1263         used_tilings.end())
1264       continue;
1265
1266     to_remove.push_back(tiling);
1267   }
1268
1269   if (to_remove.empty())
1270     return;
1271
1272   PictureLayerImpl* recycled_twin = GetRecycledTwinLayer();
1273   // Remove tilings on this tree and the twin tree.
1274   for (size_t i = 0; i < to_remove.size(); ++i) {
1275     const PictureLayerTiling* twin_tiling = GetTwinTiling(to_remove[i]);
1276     // Only remove tilings from the twin layer if they have
1277     // NON_IDEAL_RESOLUTION.
1278     if (twin_tiling && twin_tiling->resolution() == NON_IDEAL_RESOLUTION)
1279       twin->RemoveTiling(to_remove[i]->contents_scale());
1280     // Remove the tiling from the recycle tree. Note that we ignore resolution,
1281     // since we don't need to maintain high/low res on the recycle tree.
1282     if (recycled_twin)
1283       recycled_twin->RemoveTiling(to_remove[i]->contents_scale());
1284     // TODO(enne): temporary sanity CHECK for http://crbug.com/358350
1285     CHECK_NE(HIGH_RESOLUTION, to_remove[i]->resolution());
1286     tilings_->Remove(to_remove[i]);
1287   }
1288
1289   DCHECK_GT(tilings_->num_tilings(), 0u);
1290   SanityCheckTilingState();
1291 }
1292
1293 float PictureLayerImpl::MinimumContentsScale() const {
1294   float setting_min = layer_tree_impl()->settings().minimum_contents_scale;
1295
1296   // If the contents scale is less than 1 / width (also for height),
1297   // then it will end up having less than one pixel of content in that
1298   // dimension.  Bump the minimum contents scale up in this case to prevent
1299   // this from happening.
1300   int min_dimension = std::min(bounds().width(), bounds().height());
1301   if (!min_dimension)
1302     return setting_min;
1303
1304   return std::max(1.f / min_dimension, setting_min);
1305 }
1306
1307 void PictureLayerImpl::ResetRasterScale() {
1308   raster_page_scale_ = 0.f;
1309   raster_device_scale_ = 0.f;
1310   raster_source_scale_ = 0.f;
1311   raster_contents_scale_ = 0.f;
1312   low_res_raster_contents_scale_ = 0.f;
1313   raster_source_scale_is_fixed_ = false;
1314
1315   // When raster scales aren't valid, don't update tile priorities until
1316   // this layer has been updated via UpdateDrawProperties.
1317   should_update_tile_priorities_ = false;
1318 }
1319
1320 bool PictureLayerImpl::CanHaveTilings() const {
1321   if (pile_->is_solid_color())
1322     return false;
1323   if (!DrawsContent())
1324     return false;
1325   if (!pile_->HasRecordings())
1326     return false;
1327   return true;
1328 }
1329
1330 bool PictureLayerImpl::CanHaveTilingWithScale(float contents_scale) const {
1331   if (!CanHaveTilings())
1332     return false;
1333   if (contents_scale < MinimumContentsScale())
1334     return false;
1335   return true;
1336 }
1337
1338 void PictureLayerImpl::SanityCheckTilingState() const {
1339 #if DCHECK_IS_ON
1340   // Recycle tree doesn't have any restrictions.
1341   if (layer_tree_impl()->IsRecycleTree())
1342     return;
1343
1344   if (!CanHaveTilings()) {
1345     DCHECK_EQ(0u, tilings_->num_tilings());
1346     return;
1347   }
1348   if (tilings_->num_tilings() == 0)
1349     return;
1350
1351   // MarkVisibleResourcesAsRequired depends on having exactly 1 high res
1352   // tiling to mark its tiles as being required for activation.
1353   DCHECK_EQ(1, tilings_->NumHighResTilings());
1354 #endif
1355 }
1356
1357 bool PictureLayerImpl::ShouldAdjustRasterScaleDuringScaleAnimations() const {
1358   if (!layer_tree_impl()->use_gpu_rasterization())
1359     return false;
1360
1361   // Re-rastering text at different scales using GPU rasterization causes
1362   // texture uploads for glyphs at each scale (see crbug.com/366225). To
1363   // workaround this performance issue, we don't re-rasterize layers with
1364   // text during scale animations.
1365   // TODO(ajuma): Remove this workaround once text can be efficiently
1366   // re-rastered at different scales (e.g. by using distance-field fonts).
1367   if (pile_->has_text())
1368     return false;
1369
1370   return true;
1371 }
1372
1373 float PictureLayerImpl::MaximumTilingContentsScale() const {
1374   float max_contents_scale = MinimumContentsScale();
1375   for (size_t i = 0; i < tilings_->num_tilings(); ++i) {
1376     const PictureLayerTiling* tiling = tilings_->tiling_at(i);
1377     max_contents_scale = std::max(max_contents_scale, tiling->contents_scale());
1378   }
1379   return max_contents_scale;
1380 }
1381
1382 void PictureLayerImpl::UpdateIdealScales() {
1383   DCHECK(CanHaveTilings());
1384
1385   float min_contents_scale = MinimumContentsScale();
1386   DCHECK_GT(min_contents_scale, 0.f);
1387   float min_page_scale = layer_tree_impl()->min_page_scale_factor();
1388   DCHECK_GT(min_page_scale, 0.f);
1389   float min_device_scale = 1.f;
1390   float min_source_scale =
1391       min_contents_scale / min_page_scale / min_device_scale;
1392
1393   float ideal_page_scale = draw_properties().page_scale_factor;
1394   float ideal_device_scale = draw_properties().device_scale_factor;
1395   float ideal_source_scale = draw_properties().ideal_contents_scale /
1396                              ideal_page_scale / ideal_device_scale;
1397   ideal_contents_scale_ =
1398       std::max(draw_properties().ideal_contents_scale, min_contents_scale);
1399   ideal_page_scale_ = draw_properties().page_scale_factor;
1400   ideal_device_scale_ = draw_properties().device_scale_factor;
1401   ideal_source_scale_ = std::max(ideal_source_scale, min_source_scale);
1402 }
1403
1404 void PictureLayerImpl::GetDebugBorderProperties(
1405     SkColor* color,
1406     float* width) const {
1407   *color = DebugColors::TiledContentLayerBorderColor();
1408   *width = DebugColors::TiledContentLayerBorderWidth(layer_tree_impl());
1409 }
1410
1411 void PictureLayerImpl::GetAllTilesForTracing(
1412     std::set<const Tile*>* tiles) const {
1413   if (!tilings_)
1414     return;
1415
1416   for (size_t i = 0; i < tilings_->num_tilings(); ++i)
1417     tilings_->tiling_at(i)->GetAllTilesForTracing(tiles);
1418 }
1419
1420 void PictureLayerImpl::AsValueInto(base::debug::TracedValue* state) const {
1421   const_cast<PictureLayerImpl*>(this)->DoPostCommitInitializationIfNeeded();
1422   LayerImpl::AsValueInto(state);
1423   state->SetDouble("ideal_contents_scale", ideal_contents_scale_);
1424   state->SetDouble("geometry_contents_scale", MaximumTilingContentsScale());
1425   state->BeginArray("tilings");
1426   tilings_->AsValueInto(state);
1427   state->EndArray();
1428
1429   state->BeginArray("tile_priority_rect");
1430   MathUtil::AddToTracedValue(GetViewportForTilePriorityInContentSpace(), state);
1431   state->EndArray();
1432
1433   state->BeginArray("visible_rect");
1434   MathUtil::AddToTracedValue(visible_content_rect(), state);
1435   state->EndArray();
1436
1437   state->BeginArray("pictures");
1438   pile_->AsValueInto(state);
1439   state->EndArray();
1440
1441   state->BeginArray("invalidation");
1442   invalidation_.AsValueInto(state);
1443   state->EndArray();
1444
1445   state->BeginArray("coverage_tiles");
1446   for (PictureLayerTilingSet::CoverageIterator iter(tilings_.get(),
1447                                                     1.f,
1448                                                     gfx::Rect(content_bounds()),
1449                                                     ideal_contents_scale_);
1450        iter;
1451        ++iter) {
1452     state->BeginDictionary();
1453
1454     state->BeginArray("geometry_rect");
1455     MathUtil::AddToTracedValue(iter.geometry_rect(), state);
1456     state->EndArray();
1457
1458     if (*iter)
1459       TracedValue::SetIDRef(*iter, state, "tile");
1460
1461     state->EndDictionary();
1462   }
1463   state->EndArray();
1464 }
1465
1466 size_t PictureLayerImpl::GPUMemoryUsageInBytes() const {
1467   const_cast<PictureLayerImpl*>(this)->DoPostCommitInitializationIfNeeded();
1468   return tilings_->GPUMemoryUsageInBytes();
1469 }
1470
1471 void PictureLayerImpl::RunMicroBenchmark(MicroBenchmarkImpl* benchmark) {
1472   benchmark->RunOnLayer(this);
1473 }
1474
1475 WhichTree PictureLayerImpl::GetTree() const {
1476   return layer_tree_impl()->IsActiveTree() ? ACTIVE_TREE : PENDING_TREE;
1477 }
1478
1479 bool PictureLayerImpl::IsOnActiveOrPendingTree() const {
1480   return !layer_tree_impl()->IsRecycleTree();
1481 }
1482
1483 bool PictureLayerImpl::HasValidTilePriorities() const {
1484   return IsOnActiveOrPendingTree() && IsDrawnRenderSurfaceLayerListMember();
1485 }
1486
1487 bool PictureLayerImpl::AllTilesRequiredForActivationAreReadyToDraw() const {
1488   if (!layer_tree_impl()->IsPendingTree())
1489     return true;
1490
1491   if (!HasValidTilePriorities())
1492     return true;
1493
1494   if (!tilings_)
1495     return true;
1496
1497   if (visible_content_rect().IsEmpty())
1498     return true;
1499
1500   for (size_t i = 0; i < tilings_->num_tilings(); ++i) {
1501     PictureLayerTiling* tiling = tilings_->tiling_at(i);
1502     if (tiling->resolution() != HIGH_RESOLUTION &&
1503         tiling->resolution() != LOW_RESOLUTION)
1504       continue;
1505
1506     gfx::Rect rect(visible_content_rect());
1507     for (PictureLayerTiling::CoverageIterator iter(tiling, 1.f, rect); iter;
1508          ++iter) {
1509       const Tile* tile = *iter;
1510       // A null tile (i.e. missing recording) can just be skipped.
1511       if (!tile)
1512         continue;
1513
1514       if (tile->required_for_activation() && !tile->IsReadyToDraw())
1515         return false;
1516     }
1517   }
1518
1519   return true;
1520 }
1521
1522 PictureLayerImpl::LayerRasterTileIterator::LayerRasterTileIterator()
1523     : layer_(NULL), current_stage_(arraysize(stages_)) {
1524 }
1525
1526 PictureLayerImpl::LayerRasterTileIterator::LayerRasterTileIterator(
1527     PictureLayerImpl* layer,
1528     bool prioritize_low_res)
1529     : layer_(layer), current_stage_(0) {
1530   DCHECK(layer_);
1531
1532   // Early out if the layer has no tilings.
1533   if (!layer_->tilings_ || !layer_->tilings_->num_tilings()) {
1534     current_stage_ = arraysize(stages_);
1535     return;
1536   }
1537
1538   // Tiles without valid priority are treated as having lowest priority and
1539   // never considered for raster.
1540   if (!layer_->HasValidTilePriorities()) {
1541     current_stage_ = arraysize(stages_);
1542     return;
1543   }
1544
1545   WhichTree tree = layer_->GetTree();
1546
1547   // Find high and low res tilings and initialize the iterators.
1548   for (size_t i = 0; i < layer_->tilings_->num_tilings(); ++i) {
1549     PictureLayerTiling* tiling = layer_->tilings_->tiling_at(i);
1550     if (tiling->resolution() == HIGH_RESOLUTION) {
1551       iterators_[HIGH_RES] =
1552           PictureLayerTiling::TilingRasterTileIterator(tiling, tree);
1553     }
1554
1555     if (tiling->resolution() == LOW_RESOLUTION) {
1556       iterators_[LOW_RES] =
1557           PictureLayerTiling::TilingRasterTileIterator(tiling, tree);
1558     }
1559   }
1560
1561   if (prioritize_low_res) {
1562     stages_[0].iterator_type = LOW_RES;
1563     stages_[0].tile_type = TilePriority::NOW;
1564
1565     stages_[1].iterator_type = HIGH_RES;
1566     stages_[1].tile_type = TilePriority::NOW;
1567   } else {
1568     stages_[0].iterator_type = HIGH_RES;
1569     stages_[0].tile_type = TilePriority::NOW;
1570
1571     stages_[1].iterator_type = LOW_RES;
1572     stages_[1].tile_type = TilePriority::NOW;
1573   }
1574
1575   stages_[2].iterator_type = HIGH_RES;
1576   stages_[2].tile_type = TilePriority::SOON;
1577
1578   stages_[3].iterator_type = HIGH_RES;
1579   stages_[3].tile_type = TilePriority::EVENTUALLY;
1580
1581   IteratorType index = stages_[current_stage_].iterator_type;
1582   TilePriority::PriorityBin tile_type = stages_[current_stage_].tile_type;
1583   if (!iterators_[index] || iterators_[index].get_type() != tile_type)
1584     AdvanceToNextStage();
1585 }
1586
1587 PictureLayerImpl::LayerRasterTileIterator::~LayerRasterTileIterator() {}
1588
1589 PictureLayerImpl::LayerRasterTileIterator::operator bool() const {
1590   return current_stage_ < arraysize(stages_);
1591 }
1592
1593 PictureLayerImpl::LayerRasterTileIterator&
1594 PictureLayerImpl::LayerRasterTileIterator::
1595 operator++() {
1596   IteratorType index = stages_[current_stage_].iterator_type;
1597   TilePriority::PriorityBin tile_type = stages_[current_stage_].tile_type;
1598
1599   // First advance the iterator.
1600   DCHECK(iterators_[index]);
1601   DCHECK(iterators_[index].get_type() == tile_type);
1602   ++iterators_[index];
1603
1604   if (!iterators_[index] || iterators_[index].get_type() != tile_type)
1605     AdvanceToNextStage();
1606
1607   return *this;
1608 }
1609
1610 Tile* PictureLayerImpl::LayerRasterTileIterator::operator*() {
1611   DCHECK(*this);
1612
1613   IteratorType index = stages_[current_stage_].iterator_type;
1614   DCHECK(iterators_[index]);
1615   DCHECK(iterators_[index].get_type() == stages_[current_stage_].tile_type);
1616
1617   return *iterators_[index];
1618 }
1619
1620 const Tile* PictureLayerImpl::LayerRasterTileIterator::operator*() const {
1621   DCHECK(*this);
1622
1623   IteratorType index = stages_[current_stage_].iterator_type;
1624   DCHECK(iterators_[index]);
1625   DCHECK(iterators_[index].get_type() == stages_[current_stage_].tile_type);
1626
1627   return *iterators_[index];
1628 }
1629
1630 void PictureLayerImpl::LayerRasterTileIterator::AdvanceToNextStage() {
1631   DCHECK_LT(current_stage_, arraysize(stages_));
1632   ++current_stage_;
1633   while (current_stage_ < arraysize(stages_)) {
1634     IteratorType index = stages_[current_stage_].iterator_type;
1635     TilePriority::PriorityBin tile_type = stages_[current_stage_].tile_type;
1636
1637     if (iterators_[index] && iterators_[index].get_type() == tile_type)
1638       break;
1639     ++current_stage_;
1640   }
1641 }
1642
1643 PictureLayerImpl::LayerEvictionTileIterator::LayerEvictionTileIterator()
1644     : layer_(NULL),
1645       tree_priority_(SAME_PRIORITY_FOR_BOTH_TREES),
1646       current_category_(PictureLayerTiling::EVENTUALLY),
1647       current_tiling_range_type_(PictureLayerTilingSet::HIGHER_THAN_HIGH_RES),
1648       current_tiling_(0u) {
1649 }
1650
1651 PictureLayerImpl::LayerEvictionTileIterator::LayerEvictionTileIterator(
1652     PictureLayerImpl* layer,
1653     TreePriority tree_priority)
1654     : layer_(layer),
1655       tree_priority_(tree_priority),
1656       current_category_(PictureLayerTiling::EVENTUALLY),
1657       current_tiling_range_type_(PictureLayerTilingSet::HIGHER_THAN_HIGH_RES),
1658       current_tiling_(CurrentTilingRange().start - 1u) {
1659   // TODO(vmpstr): Once tile priorities are determined by the iterators, ensure
1660   // that layers that don't have valid tile priorities have lowest priorities so
1661   // they evict their tiles first (crbug.com/381704)
1662   DCHECK(layer_->tilings_);
1663   do {
1664     if (!AdvanceToNextTiling())
1665       break;
1666
1667     current_iterator_ = PictureLayerTiling::TilingEvictionTileIterator(
1668         layer_->tilings_->tiling_at(CurrentTilingIndex()),
1669         tree_priority,
1670         current_category_);
1671   } while (!current_iterator_);
1672 }
1673
1674 PictureLayerImpl::LayerEvictionTileIterator::~LayerEvictionTileIterator() {
1675 }
1676
1677 Tile* PictureLayerImpl::LayerEvictionTileIterator::operator*() {
1678   DCHECK(*this);
1679   return *current_iterator_;
1680 }
1681
1682 const Tile* PictureLayerImpl::LayerEvictionTileIterator::operator*() const {
1683   DCHECK(*this);
1684   return *current_iterator_;
1685 }
1686
1687 PictureLayerImpl::LayerEvictionTileIterator&
1688 PictureLayerImpl::LayerEvictionTileIterator::
1689 operator++() {
1690   DCHECK(*this);
1691   ++current_iterator_;
1692   while (!current_iterator_) {
1693     if (!AdvanceToNextTiling())
1694       break;
1695
1696     current_iterator_ = PictureLayerTiling::TilingEvictionTileIterator(
1697         layer_->tilings_->tiling_at(CurrentTilingIndex()),
1698         tree_priority_,
1699         current_category_);
1700   }
1701   return *this;
1702 }
1703
1704 PictureLayerImpl::LayerEvictionTileIterator::operator bool() const {
1705   return !!current_iterator_;
1706 }
1707
1708 bool PictureLayerImpl::LayerEvictionTileIterator::AdvanceToNextCategory() {
1709   switch (current_category_) {
1710     case PictureLayerTiling::EVENTUALLY:
1711       current_category_ =
1712           PictureLayerTiling::EVENTUALLY_AND_REQUIRED_FOR_ACTIVATION;
1713       return true;
1714     case PictureLayerTiling::EVENTUALLY_AND_REQUIRED_FOR_ACTIVATION:
1715       current_category_ = PictureLayerTiling::SOON;
1716       return true;
1717     case PictureLayerTiling::SOON:
1718       current_category_ = PictureLayerTiling::SOON_AND_REQUIRED_FOR_ACTIVATION;
1719       return true;
1720     case PictureLayerTiling::SOON_AND_REQUIRED_FOR_ACTIVATION:
1721       current_category_ = PictureLayerTiling::NOW;
1722       return true;
1723     case PictureLayerTiling::NOW:
1724       current_category_ = PictureLayerTiling::NOW_AND_REQUIRED_FOR_ACTIVATION;
1725       return true;
1726     case PictureLayerTiling::NOW_AND_REQUIRED_FOR_ACTIVATION:
1727       return false;
1728   }
1729   NOTREACHED();
1730   return false;
1731 }
1732
1733 bool
1734 PictureLayerImpl::LayerEvictionTileIterator::AdvanceToNextTilingRangeType() {
1735   switch (current_tiling_range_type_) {
1736     case PictureLayerTilingSet::HIGHER_THAN_HIGH_RES:
1737       current_tiling_range_type_ = PictureLayerTilingSet::LOWER_THAN_LOW_RES;
1738       return true;
1739     case PictureLayerTilingSet::LOWER_THAN_LOW_RES:
1740       current_tiling_range_type_ =
1741           PictureLayerTilingSet::BETWEEN_HIGH_AND_LOW_RES;
1742       return true;
1743     case PictureLayerTilingSet::BETWEEN_HIGH_AND_LOW_RES:
1744       current_tiling_range_type_ = PictureLayerTilingSet::LOW_RES;
1745       return true;
1746     case PictureLayerTilingSet::LOW_RES:
1747       current_tiling_range_type_ = PictureLayerTilingSet::HIGH_RES;
1748       return true;
1749     case PictureLayerTilingSet::HIGH_RES:
1750       if (!AdvanceToNextCategory())
1751         return false;
1752
1753       current_tiling_range_type_ = PictureLayerTilingSet::HIGHER_THAN_HIGH_RES;
1754       return true;
1755   }
1756   NOTREACHED();
1757   return false;
1758 }
1759
1760 bool PictureLayerImpl::LayerEvictionTileIterator::AdvanceToNextTiling() {
1761   DCHECK_NE(current_tiling_, CurrentTilingRange().end);
1762   ++current_tiling_;
1763   while (current_tiling_ == CurrentTilingRange().end) {
1764     if (!AdvanceToNextTilingRangeType())
1765       return false;
1766
1767     current_tiling_ = CurrentTilingRange().start;
1768   }
1769   return true;
1770 }
1771
1772 PictureLayerTilingSet::TilingRange
1773 PictureLayerImpl::LayerEvictionTileIterator::CurrentTilingRange() const {
1774   return layer_->tilings_->GetTilingRange(current_tiling_range_type_);
1775 }
1776
1777 size_t PictureLayerImpl::LayerEvictionTileIterator::CurrentTilingIndex() const {
1778   DCHECK_NE(current_tiling_, CurrentTilingRange().end);
1779   switch (current_tiling_range_type_) {
1780     case PictureLayerTilingSet::HIGHER_THAN_HIGH_RES:
1781     case PictureLayerTilingSet::LOW_RES:
1782     case PictureLayerTilingSet::HIGH_RES:
1783       return current_tiling_;
1784     // Tilings in the following ranges are accessed in reverse order.
1785     case PictureLayerTilingSet::BETWEEN_HIGH_AND_LOW_RES:
1786     case PictureLayerTilingSet::LOWER_THAN_LOW_RES: {
1787       PictureLayerTilingSet::TilingRange tiling_range = CurrentTilingRange();
1788       size_t current_tiling_range_offset = current_tiling_ - tiling_range.start;
1789       return tiling_range.end - 1 - current_tiling_range_offset;
1790     }
1791   }
1792   NOTREACHED();
1793   return 0;
1794 }
1795
1796 }  // namespace cc