Upstream version 7.36.149.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
10 #include "base/time/time.h"
11 #include "cc/base/math_util.h"
12 #include "cc/base/util.h"
13 #include "cc/debug/debug_colors.h"
14 #include "cc/debug/micro_benchmark_impl.h"
15 #include "cc/debug/traced_value.h"
16 #include "cc/layers/append_quads_data.h"
17 #include "cc/layers/quad_sink.h"
18 #include "cc/quads/checkerboard_draw_quad.h"
19 #include "cc/quads/debug_border_draw_quad.h"
20 #include "cc/quads/picture_draw_quad.h"
21 #include "cc/quads/solid_color_draw_quad.h"
22 #include "cc/quads/tile_draw_quad.h"
23 #include "cc/resources/tile_manager.h"
24 #include "ui/gfx/quad_f.h"
25 #include "ui/gfx/rect_conversions.h"
26 #include "ui/gfx/size_conversions.h"
27
28 namespace {
29 const float kMaxScaleRatioDuringPinch = 2.0f;
30
31 // When creating a new tiling during pinch, snap to an existing
32 // tiling's scale if the desired scale is within this ratio.
33 const float kSnapToExistingTilingRatio = 0.2f;
34
35 // Estimate skewport 60 frames ahead for pre-rasterization on the CPU.
36 const float kCpuSkewportTargetTimeInFrames = 60.0f;
37
38 // Don't pre-rasterize on the GPU (except for kBackflingGuardDistancePixels in
39 // TileManager::BinFromTilePriority).
40 const float kGpuSkewportTargetTimeInFrames = 0.0f;
41 }  // namespace
42
43 namespace cc {
44
45 PictureLayerImpl::PictureLayerImpl(LayerTreeImpl* tree_impl, int id)
46     : LayerImpl(tree_impl, id),
47       twin_layer_(NULL),
48       pile_(PicturePileImpl::Create()),
49       is_mask_(false),
50       ideal_page_scale_(0.f),
51       ideal_device_scale_(0.f),
52       ideal_source_scale_(0.f),
53       ideal_contents_scale_(0.f),
54       raster_page_scale_(0.f),
55       raster_device_scale_(0.f),
56       raster_source_scale_(0.f),
57       raster_contents_scale_(0.f),
58       low_res_raster_contents_scale_(0.f),
59       raster_source_scale_is_fixed_(false),
60       was_animating_transform_to_screen_(false),
61       is_using_lcd_text_(tree_impl->settings().can_use_lcd_text),
62       needs_post_commit_initialization_(true),
63       should_update_tile_priorities_(false),
64       should_use_low_res_tiling_(tree_impl->settings().create_low_res_tiling),
65       layer_needs_to_register_itself_(true) {
66 }
67
68 PictureLayerImpl::~PictureLayerImpl() {
69   if (!layer_needs_to_register_itself_)
70     layer_tree_impl()->tile_manager()->UnregisterPictureLayerImpl(this);
71 }
72
73 const char* PictureLayerImpl::LayerTypeAsString() const {
74   return "cc::PictureLayerImpl";
75 }
76
77 scoped_ptr<LayerImpl> PictureLayerImpl::CreateLayerImpl(
78     LayerTreeImpl* tree_impl) {
79   return PictureLayerImpl::Create(tree_impl, id()).PassAs<LayerImpl>();
80 }
81
82 void PictureLayerImpl::PushPropertiesTo(LayerImpl* base_layer) {
83   // It's possible this layer was never drawn or updated (e.g. because it was
84   // a descendant of an opacity 0 layer).
85   DoPostCommitInitializationIfNeeded();
86   PictureLayerImpl* layer_impl = static_cast<PictureLayerImpl*>(base_layer);
87
88   // We have already synced the important bits from the the active layer, and
89   // we will soon swap out its tilings and use them for recycling. However,
90   // there are now tiles in this layer's tilings that were unref'd and replaced
91   // with new tiles (due to invalidation). This resets all active priorities on
92   // the to-be-recycled tiling to ensure replaced tiles don't linger and take
93   // memory (due to a stale 'active' priority).
94   if (layer_impl->tilings_)
95     layer_impl->tilings_->DidBecomeRecycled();
96
97   LayerImpl::PushPropertiesTo(base_layer);
98
99   // When the pending tree pushes to the active tree, the pending twin
100   // disappears.
101   layer_impl->twin_layer_ = NULL;
102   twin_layer_ = NULL;
103
104   layer_impl->SetIsMask(is_mask_);
105   layer_impl->pile_ = pile_;
106
107   // Tilings would be expensive to push, so we swap.
108   layer_impl->tilings_.swap(tilings_);
109
110   // Ensure that we don't have any tiles that are out of date.
111   if (tilings_)
112     tilings_->RemoveTilesInRegion(invalidation_);
113
114   layer_impl->tilings_->SetClient(layer_impl);
115   if (tilings_)
116     tilings_->SetClient(this);
117
118   layer_impl->raster_page_scale_ = raster_page_scale_;
119   layer_impl->raster_device_scale_ = raster_device_scale_;
120   layer_impl->raster_source_scale_ = raster_source_scale_;
121   layer_impl->raster_contents_scale_ = raster_contents_scale_;
122   layer_impl->low_res_raster_contents_scale_ = low_res_raster_contents_scale_;
123
124   layer_impl->UpdateLCDTextStatus(is_using_lcd_text_);
125   layer_impl->needs_post_commit_initialization_ = false;
126
127   // The invalidation on this soon-to-be-recycled layer must be cleared to
128   // mirror clearing the invalidation in PictureLayer's version of this function
129   // in case push properties is skipped.
130   layer_impl->invalidation_.Swap(&invalidation_);
131   invalidation_.Clear();
132   needs_post_commit_initialization_ = true;
133
134   // We always need to push properties.
135   // See http://crbug.com/303943
136   needs_push_properties_ = true;
137 }
138
139 void PictureLayerImpl::AppendQuads(QuadSink* quad_sink,
140                                    AppendQuadsData* append_quads_data) {
141   DCHECK(!needs_post_commit_initialization_);
142   gfx::Rect rect(visible_content_rect());
143   gfx::Rect content_rect(content_bounds());
144
145   SharedQuadState* shared_quad_state = quad_sink->CreateSharedQuadState();
146   PopulateSharedQuadState(shared_quad_state);
147
148   if (current_draw_mode_ == DRAW_MODE_RESOURCELESS_SOFTWARE) {
149     AppendDebugBorderQuad(
150         quad_sink,
151         shared_quad_state,
152         append_quads_data,
153         DebugColors::DirectPictureBorderColor(),
154         DebugColors::DirectPictureBorderWidth(layer_tree_impl()));
155
156     gfx::Rect geometry_rect = rect;
157     gfx::Rect opaque_rect = contents_opaque() ? geometry_rect : gfx::Rect();
158     gfx::Rect visible_geometry_rect =
159         quad_sink->UnoccludedContentRect(geometry_rect, draw_transform());
160     if (visible_geometry_rect.IsEmpty())
161       return;
162
163     gfx::Size texture_size = rect.size();
164     gfx::RectF texture_rect = gfx::RectF(texture_size);
165     gfx::Rect quad_content_rect = rect;
166     float contents_scale = contents_scale_x();
167
168     scoped_ptr<PictureDrawQuad> quad = PictureDrawQuad::Create();
169     quad->SetNew(shared_quad_state,
170                  geometry_rect,
171                  opaque_rect,
172                  visible_geometry_rect,
173                  texture_rect,
174                  texture_size,
175                  RGBA_8888,
176                  quad_content_rect,
177                  contents_scale,
178                  pile_);
179     quad_sink->Append(quad.PassAs<DrawQuad>());
180     append_quads_data->num_missing_tiles++;
181     return;
182   }
183
184   AppendDebugBorderQuad(quad_sink, shared_quad_state, append_quads_data);
185
186   if (ShowDebugBorders()) {
187     for (PictureLayerTilingSet::CoverageIterator iter(
188         tilings_.get(), contents_scale_x(), rect, ideal_contents_scale_);
189          iter;
190          ++iter) {
191       SkColor color;
192       float width;
193       if (*iter && iter->IsReadyToDraw()) {
194         ManagedTileState::TileVersion::Mode mode =
195             iter->GetTileVersionForDrawing().mode();
196         if (mode == ManagedTileState::TileVersion::SOLID_COLOR_MODE) {
197           color = DebugColors::SolidColorTileBorderColor();
198           width = DebugColors::SolidColorTileBorderWidth(layer_tree_impl());
199         } else if (mode == ManagedTileState::TileVersion::PICTURE_PILE_MODE) {
200           color = DebugColors::PictureTileBorderColor();
201           width = DebugColors::PictureTileBorderWidth(layer_tree_impl());
202         } else if (iter->priority(ACTIVE_TREE).resolution == HIGH_RESOLUTION) {
203           color = DebugColors::HighResTileBorderColor();
204           width = DebugColors::HighResTileBorderWidth(layer_tree_impl());
205         } else if (iter->priority(ACTIVE_TREE).resolution == LOW_RESOLUTION) {
206           color = DebugColors::LowResTileBorderColor();
207           width = DebugColors::LowResTileBorderWidth(layer_tree_impl());
208         } else if (iter->contents_scale() > contents_scale_x()) {
209           color = DebugColors::ExtraHighResTileBorderColor();
210           width = DebugColors::ExtraHighResTileBorderWidth(layer_tree_impl());
211         } else {
212           color = DebugColors::ExtraLowResTileBorderColor();
213           width = DebugColors::ExtraLowResTileBorderWidth(layer_tree_impl());
214         }
215       } else {
216         color = DebugColors::MissingTileBorderColor();
217         width = DebugColors::MissingTileBorderWidth(layer_tree_impl());
218       }
219
220       scoped_ptr<DebugBorderDrawQuad> debug_border_quad =
221           DebugBorderDrawQuad::Create();
222       gfx::Rect geometry_rect = iter.geometry_rect();
223       gfx::Rect visible_geometry_rect = geometry_rect;
224       debug_border_quad->SetNew(shared_quad_state,
225                                 geometry_rect,
226                                 visible_geometry_rect,
227                                 color,
228                                 width);
229       quad_sink->Append(debug_border_quad.PassAs<DrawQuad>());
230     }
231   }
232
233   // Keep track of the tilings that were used so that tilings that are
234   // unused can be considered for removal.
235   std::vector<PictureLayerTiling*> seen_tilings;
236
237   for (PictureLayerTilingSet::CoverageIterator iter(
238       tilings_.get(), contents_scale_x(), rect, ideal_contents_scale_);
239        iter;
240        ++iter) {
241     gfx::Rect geometry_rect = iter.geometry_rect();
242     gfx::Rect visible_geometry_rect =
243         quad_sink->UnoccludedContentRect(geometry_rect, draw_transform());
244     if (visible_geometry_rect.IsEmpty())
245       continue;
246
247     append_quads_data->visible_content_area +=
248         visible_geometry_rect.width() * visible_geometry_rect.height();
249
250     if (!*iter || !iter->IsReadyToDraw()) {
251       if (draw_checkerboard_for_missing_tiles()) {
252         scoped_ptr<CheckerboardDrawQuad> quad = CheckerboardDrawQuad::Create();
253         SkColor color = DebugColors::DefaultCheckerboardColor();
254         quad->SetNew(
255             shared_quad_state, geometry_rect, visible_geometry_rect, color);
256         quad_sink->Append(quad.PassAs<DrawQuad>());
257       } else {
258         SkColor color = SafeOpaqueBackgroundColor();
259         scoped_ptr<SolidColorDrawQuad> quad = SolidColorDrawQuad::Create();
260         quad->SetNew(shared_quad_state,
261                      geometry_rect,
262                      visible_geometry_rect,
263                      color,
264                      false);
265         quad_sink->Append(quad.PassAs<DrawQuad>());
266       }
267
268       append_quads_data->num_missing_tiles++;
269       append_quads_data->had_incomplete_tile = true;
270       append_quads_data->approximated_visible_content_area +=
271           visible_geometry_rect.width() * visible_geometry_rect.height();
272       continue;
273     }
274
275     const ManagedTileState::TileVersion& tile_version =
276         iter->GetTileVersionForDrawing();
277     scoped_ptr<DrawQuad> draw_quad;
278     switch (tile_version.mode()) {
279       case ManagedTileState::TileVersion::RESOURCE_MODE: {
280         gfx::RectF texture_rect = iter.texture_rect();
281         gfx::Rect opaque_rect = iter->opaque_rect();
282         opaque_rect.Intersect(geometry_rect);
283
284         if (iter->contents_scale() != ideal_contents_scale_)
285           append_quads_data->had_incomplete_tile = true;
286
287         scoped_ptr<TileDrawQuad> quad = TileDrawQuad::Create();
288         quad->SetNew(shared_quad_state,
289                      geometry_rect,
290                      opaque_rect,
291                      visible_geometry_rect,
292                      tile_version.get_resource_id(),
293                      texture_rect,
294                      iter.texture_size(),
295                      tile_version.contents_swizzled());
296         draw_quad = quad.PassAs<DrawQuad>();
297         break;
298       }
299       case ManagedTileState::TileVersion::PICTURE_PILE_MODE: {
300         gfx::RectF texture_rect = iter.texture_rect();
301         gfx::Rect opaque_rect = iter->opaque_rect();
302         opaque_rect.Intersect(geometry_rect);
303
304         ResourceProvider* resource_provider =
305             layer_tree_impl()->resource_provider();
306         ResourceFormat format =
307             resource_provider->memory_efficient_texture_format();
308         scoped_ptr<PictureDrawQuad> quad = PictureDrawQuad::Create();
309         quad->SetNew(shared_quad_state,
310                      geometry_rect,
311                      opaque_rect,
312                      visible_geometry_rect,
313                      texture_rect,
314                      iter.texture_size(),
315                      format,
316                      iter->content_rect(),
317                      iter->contents_scale(),
318                      pile_);
319         draw_quad = quad.PassAs<DrawQuad>();
320         break;
321       }
322       case ManagedTileState::TileVersion::SOLID_COLOR_MODE: {
323         scoped_ptr<SolidColorDrawQuad> quad = SolidColorDrawQuad::Create();
324         quad->SetNew(shared_quad_state,
325                      geometry_rect,
326                      visible_geometry_rect,
327                      tile_version.get_solid_color(),
328                      false);
329         draw_quad = quad.PassAs<DrawQuad>();
330         break;
331       }
332     }
333
334     DCHECK(draw_quad);
335     quad_sink->Append(draw_quad.Pass());
336
337     if (iter->priority(ACTIVE_TREE).resolution != HIGH_RESOLUTION) {
338       append_quads_data->approximated_visible_content_area +=
339           visible_geometry_rect.width() * visible_geometry_rect.height();
340     }
341
342     if (seen_tilings.empty() || seen_tilings.back() != iter.CurrentTiling())
343       seen_tilings.push_back(iter.CurrentTiling());
344   }
345
346   // Aggressively remove any tilings that are not seen to save memory. Note
347   // that this is at the expense of doing cause more frequent re-painting. A
348   // better scheme would be to maintain a tighter visible_content_rect for the
349   // finer tilings.
350   CleanUpTilingsOnActiveLayer(seen_tilings);
351 }
352
353 void PictureLayerImpl::DidUnregisterLayer() {
354   layer_needs_to_register_itself_ = true;
355 }
356
357 void PictureLayerImpl::UpdateTilePriorities() {
358   DCHECK(!needs_post_commit_initialization_);
359   CHECK(should_update_tile_priorities_);
360
361   if (layer_needs_to_register_itself_) {
362     layer_tree_impl()->tile_manager()->RegisterPictureLayerImpl(this);
363     layer_needs_to_register_itself_ = false;
364   }
365
366   if (layer_tree_impl()->device_viewport_valid_for_tile_management()) {
367     visible_rect_for_tile_priority_ = visible_content_rect();
368     viewport_size_for_tile_priority_ = layer_tree_impl()->DrawViewportSize();
369     screen_space_transform_for_tile_priority_ = screen_space_transform();
370   }
371
372   if (!tilings_->num_tilings())
373     return;
374
375   double current_frame_time_in_seconds =
376       (layer_tree_impl()->CurrentFrameTimeTicks() -
377        base::TimeTicks()).InSecondsF();
378
379   bool tiling_needs_update = false;
380   for (size_t i = 0; i < tilings_->num_tilings(); ++i) {
381     if (tilings_->tiling_at(i)->NeedsUpdateForFrameAtTime(
382             current_frame_time_in_seconds)) {
383       tiling_needs_update = true;
384       break;
385     }
386   }
387   if (!tiling_needs_update)
388     return;
389
390   UpdateLCDTextStatus(can_use_lcd_text());
391
392   // Use visible_content_rect, unless it's empty. If it's empty, then
393   // try to inverse project the viewport into layer space and use that.
394   gfx::Rect visible_rect_in_content_space = visible_rect_for_tile_priority_;
395   if (visible_rect_in_content_space.IsEmpty()) {
396     gfx::Transform screen_to_layer(gfx::Transform::kSkipInitialization);
397     if (screen_space_transform_for_tile_priority_.GetInverse(
398             &screen_to_layer)) {
399       visible_rect_in_content_space =
400           gfx::ToEnclosingRect(MathUtil::ProjectClippedRect(
401               screen_to_layer, gfx::Rect(viewport_size_for_tile_priority_)));
402       visible_rect_in_content_space.Intersect(gfx::Rect(content_bounds()));
403     }
404   }
405
406   WhichTree tree =
407       layer_tree_impl()->IsActiveTree() ? ACTIVE_TREE : PENDING_TREE;
408
409   tilings_->UpdateTilePriorities(tree,
410                                  visible_rect_in_content_space,
411                                  contents_scale_x(),
412                                  current_frame_time_in_seconds);
413
414   if (layer_tree_impl()->IsPendingTree())
415     MarkVisibleResourcesAsRequired();
416
417   // Tile priorities were modified.
418   layer_tree_impl()->DidModifyTilePriorities();
419 }
420
421 void PictureLayerImpl::NotifyTileInitialized(const Tile* tile) {
422   if (layer_tree_impl()->IsActiveTree()) {
423     gfx::RectF layer_damage_rect =
424         gfx::ScaleRect(tile->content_rect(), 1.f / tile->contents_scale());
425     AddDamageRect(layer_damage_rect);
426   }
427 }
428
429 void PictureLayerImpl::DidBecomeActive() {
430   LayerImpl::DidBecomeActive();
431   tilings_->DidBecomeActive();
432   layer_tree_impl()->DidModifyTilePriorities();
433 }
434
435 void PictureLayerImpl::DidBeginTracing() {
436   pile_->DidBeginTracing();
437 }
438
439 void PictureLayerImpl::ReleaseResources() {
440   if (tilings_)
441     RemoveAllTilings();
442
443   ResetRasterScale();
444
445   // To avoid an edge case after lost context where the tree is up to date but
446   // the tilings have not been managed, request an update draw properties
447   // to force tilings to get managed.
448   layer_tree_impl()->set_needs_update_draw_properties();
449 }
450
451 void PictureLayerImpl::CalculateContentsScale(
452     float ideal_contents_scale,
453     float device_scale_factor,
454     float page_scale_factor,
455     float maximum_animation_contents_scale,
456     bool animating_transform_to_screen,
457     float* contents_scale_x,
458     float* contents_scale_y,
459     gfx::Size* content_bounds) {
460   DoPostCommitInitializationIfNeeded();
461
462   // This function sets valid raster scales and manages tilings, so tile
463   // priorities can now be updated.
464   should_update_tile_priorities_ = true;
465
466   if (!CanHaveTilings()) {
467     ideal_page_scale_ = page_scale_factor;
468     ideal_device_scale_ = device_scale_factor;
469     ideal_contents_scale_ = ideal_contents_scale;
470     ideal_source_scale_ =
471         ideal_contents_scale_ / ideal_page_scale_ / ideal_device_scale_;
472     *contents_scale_x = ideal_contents_scale_;
473     *contents_scale_y = ideal_contents_scale_;
474     *content_bounds = gfx::ToCeiledSize(gfx::ScaleSize(bounds(),
475                                                        ideal_contents_scale_,
476                                                        ideal_contents_scale_));
477     return;
478   }
479
480   float min_contents_scale = MinimumContentsScale();
481   DCHECK_GT(min_contents_scale, 0.f);
482   float min_page_scale = layer_tree_impl()->min_page_scale_factor();
483   DCHECK_GT(min_page_scale, 0.f);
484   float min_device_scale = 1.f;
485   float min_source_scale =
486       min_contents_scale / min_page_scale / min_device_scale;
487
488   float ideal_page_scale = page_scale_factor;
489   float ideal_device_scale = device_scale_factor;
490   float ideal_source_scale =
491       ideal_contents_scale / ideal_page_scale / ideal_device_scale;
492
493   ideal_contents_scale_ = std::max(ideal_contents_scale, min_contents_scale);
494   ideal_page_scale_ = ideal_page_scale;
495   ideal_device_scale_ = ideal_device_scale;
496   ideal_source_scale_ = std::max(ideal_source_scale, min_source_scale);
497
498   ManageTilings(animating_transform_to_screen,
499                 maximum_animation_contents_scale);
500
501   // The content scale and bounds for a PictureLayerImpl is somewhat fictitious.
502   // There are (usually) several tilings at different scales.  However, the
503   // content bounds is the (integer!) space in which quads are generated.
504   // In order to guarantee that we can fill this integer space with any set of
505   // tilings (and then map back to floating point texture coordinates), the
506   // contents scale must be at least as large as the largest of the tilings.
507   float max_contents_scale = min_contents_scale;
508   for (size_t i = 0; i < tilings_->num_tilings(); ++i) {
509     const PictureLayerTiling* tiling = tilings_->tiling_at(i);
510     max_contents_scale = std::max(max_contents_scale, tiling->contents_scale());
511   }
512
513   *contents_scale_x = max_contents_scale;
514   *contents_scale_y = max_contents_scale;
515   *content_bounds = gfx::ToCeiledSize(
516       gfx::ScaleSize(bounds(), max_contents_scale, max_contents_scale));
517 }
518
519 skia::RefPtr<SkPicture> PictureLayerImpl::GetPicture() {
520   return pile_->GetFlattenedPicture();
521 }
522
523 scoped_refptr<Tile> PictureLayerImpl::CreateTile(PictureLayerTiling* tiling,
524                                                const gfx::Rect& content_rect) {
525   if (!pile_->CanRaster(tiling->contents_scale(), content_rect))
526     return scoped_refptr<Tile>();
527
528   int flags = 0;
529   if (is_using_lcd_text_)
530     flags |= Tile::USE_LCD_TEXT;
531   if (use_gpu_rasterization())
532     flags |= Tile::USE_GPU_RASTERIZATION;
533   return layer_tree_impl()->tile_manager()->CreateTile(
534       pile_.get(),
535       content_rect.size(),
536       content_rect,
537       contents_opaque() ? content_rect : gfx::Rect(),
538       tiling->contents_scale(),
539       id(),
540       layer_tree_impl()->source_frame_number(),
541       flags);
542 }
543
544 void PictureLayerImpl::UpdatePile(Tile* tile) {
545   tile->set_picture_pile(pile_);
546 }
547
548 const Region* PictureLayerImpl::GetInvalidation() {
549   return &invalidation_;
550 }
551
552 const PictureLayerTiling* PictureLayerImpl::GetTwinTiling(
553     const PictureLayerTiling* tiling) const {
554   if (!twin_layer_ ||
555       twin_layer_->use_gpu_rasterization() != use_gpu_rasterization())
556     return NULL;
557   for (size_t i = 0; i < twin_layer_->tilings_->num_tilings(); ++i)
558     if (twin_layer_->tilings_->tiling_at(i)->contents_scale() ==
559         tiling->contents_scale())
560       return twin_layer_->tilings_->tiling_at(i);
561   return NULL;
562 }
563
564 size_t PictureLayerImpl::GetMaxTilesForInterestArea() const {
565   return layer_tree_impl()->settings().max_tiles_for_interest_area;
566 }
567
568 float PictureLayerImpl::GetSkewportTargetTimeInSeconds() const {
569   float skewport_target_time_in_frames = use_gpu_rasterization()
570                                              ? kGpuSkewportTargetTimeInFrames
571                                              : kCpuSkewportTargetTimeInFrames;
572   return skewport_target_time_in_frames *
573          layer_tree_impl()->begin_impl_frame_interval().InSecondsF() *
574          layer_tree_impl()->settings().skewport_target_time_multiplier;
575 }
576
577 int PictureLayerImpl::GetSkewportExtrapolationLimitInContentPixels() const {
578   return layer_tree_impl()
579       ->settings()
580       .skewport_extrapolation_limit_in_content_pixels;
581 }
582
583 gfx::Size PictureLayerImpl::CalculateTileSize(
584     const gfx::Size& content_bounds) const {
585   if (is_mask_) {
586     int max_size = layer_tree_impl()->MaxTextureSize();
587     return gfx::Size(
588         std::min(max_size, content_bounds.width()),
589         std::min(max_size, content_bounds.height()));
590   }
591
592   int max_texture_size =
593       layer_tree_impl()->resource_provider()->max_texture_size();
594
595   gfx::Size default_tile_size = layer_tree_impl()->settings().default_tile_size;
596   if (use_gpu_rasterization()) {
597     // TODO(ernstm) crbug.com/365877: We need a unified way to override the
598     // default-tile-size.
599     default_tile_size =
600         gfx::Size(layer_tree_impl()->device_viewport_size().width(),
601                   layer_tree_impl()->device_viewport_size().height() / 4);
602   }
603   default_tile_size.SetToMin(gfx::Size(max_texture_size, max_texture_size));
604
605   gfx::Size max_untiled_content_size =
606       layer_tree_impl()->settings().max_untiled_layer_size;
607   max_untiled_content_size.SetToMin(
608       gfx::Size(max_texture_size, max_texture_size));
609
610   bool any_dimension_too_large =
611       content_bounds.width() > max_untiled_content_size.width() ||
612       content_bounds.height() > max_untiled_content_size.height();
613
614   bool any_dimension_one_tile =
615       content_bounds.width() <= default_tile_size.width() ||
616       content_bounds.height() <= default_tile_size.height();
617
618   // If long and skinny, tile at the max untiled content size, and clamp
619   // the smaller dimension to the content size, e.g. 1000x12 layer with
620   // 500x500 max untiled size would get 500x12 tiles.  Also do this
621   // if the layer is small.
622   if (any_dimension_one_tile || !any_dimension_too_large) {
623     int width = std::min(
624         std::max(max_untiled_content_size.width(), default_tile_size.width()),
625         content_bounds.width());
626     int height = std::min(
627         std::max(max_untiled_content_size.height(), default_tile_size.height()),
628         content_bounds.height());
629     // Round width and height up to the closest multiple of 64, or 56 if
630     // we should avoid power-of-two textures. This helps reduce the number
631     // of different textures sizes to help recycling, and also keeps all
632     // textures multiple-of-eight, which is preferred on some drivers (IMG).
633     bool avoid_pow2 =
634         layer_tree_impl()->GetRendererCapabilities().avoid_pow2_textures;
635     int round_up_to = avoid_pow2 ? 56 : 64;
636     width = RoundUp(width, round_up_to);
637     height = RoundUp(height, round_up_to);
638     return gfx::Size(width, height);
639   }
640
641   return default_tile_size;
642 }
643
644 void PictureLayerImpl::SyncFromActiveLayer(const PictureLayerImpl* other) {
645   DCHECK(!other->needs_post_commit_initialization_);
646   DCHECK(other->tilings_);
647
648   UpdateLCDTextStatus(other->is_using_lcd_text_);
649
650   if (!DrawsContent()) {
651     RemoveAllTilings();
652     return;
653   }
654
655   raster_page_scale_ = other->raster_page_scale_;
656   raster_device_scale_ = other->raster_device_scale_;
657   raster_source_scale_ = other->raster_source_scale_;
658   raster_contents_scale_ = other->raster_contents_scale_;
659   low_res_raster_contents_scale_ = other->low_res_raster_contents_scale_;
660
661   // Add synthetic invalidations for any recordings that were dropped.  As
662   // tiles are updated to point to this new pile, this will force the dropping
663   // of tiles that can no longer be rastered.  This is not ideal, but is a
664   // trade-off for memory (use the same pile as much as possible, by switching
665   // during DidBecomeActive) and for time (don't bother checking every tile
666   // during activation to see if the new pile can still raster it).
667   for (int x = 0; x < pile_->num_tiles_x(); ++x) {
668     for (int y = 0; y < pile_->num_tiles_y(); ++y) {
669       bool previously_had = other->pile_->HasRecordingAt(x, y);
670       bool now_has = pile_->HasRecordingAt(x, y);
671       if (now_has || !previously_had)
672         continue;
673       gfx::Rect layer_rect = pile_->tile_bounds(x, y);
674       invalidation_.Union(layer_rect);
675     }
676   }
677
678   // Union in the other newly exposed regions as invalid.
679   Region difference_region = Region(gfx::Rect(bounds()));
680   difference_region.Subtract(gfx::Rect(other->bounds()));
681   invalidation_.Union(difference_region);
682
683   if (CanHaveTilings()) {
684     tilings_->SyncTilings(
685         *other->tilings_, bounds(), invalidation_, MinimumContentsScale());
686   } else {
687     RemoveAllTilings();
688   }
689
690   SanityCheckTilingState();
691 }
692
693 void PictureLayerImpl::SyncTiling(
694     const PictureLayerTiling* tiling) {
695   if (!CanHaveTilingWithScale(tiling->contents_scale()))
696     return;
697   tilings_->AddTiling(tiling->contents_scale());
698
699   // If this tree needs update draw properties, then the tiling will
700   // get updated prior to drawing or activation.  If this tree does not
701   // need update draw properties, then its transforms are up to date and
702   // we can create tiles for this tiling immediately.
703   if (!layer_tree_impl()->needs_update_draw_properties() &&
704       should_update_tile_priorities_) {
705     UpdateTilePriorities();
706   }
707 }
708
709 void PictureLayerImpl::SetIsMask(bool is_mask) {
710   if (is_mask_ == is_mask)
711     return;
712   is_mask_ = is_mask;
713   if (tilings_)
714     tilings_->RemoveAllTiles();
715 }
716
717 ResourceProvider::ResourceId PictureLayerImpl::ContentsResourceId() const {
718   gfx::Rect content_rect(content_bounds());
719   float scale = contents_scale_x();
720   PictureLayerTilingSet::CoverageIterator iter(
721       tilings_.get(), scale, content_rect, ideal_contents_scale_);
722
723   // Mask resource not ready yet.
724   if (!iter || !*iter)
725     return 0;
726
727   // Masks only supported if they fit on exactly one tile.
728   if (iter.geometry_rect() != content_rect)
729     return 0;
730
731   const ManagedTileState::TileVersion& tile_version =
732       iter->GetTileVersionForDrawing();
733   if (!tile_version.IsReadyToDraw() ||
734       tile_version.mode() != ManagedTileState::TileVersion::RESOURCE_MODE)
735     return 0;
736
737   return tile_version.get_resource_id();
738 }
739
740 void PictureLayerImpl::MarkVisibleResourcesAsRequired() const {
741   DCHECK(layer_tree_impl()->IsPendingTree());
742   DCHECK(!layer_tree_impl()->needs_update_draw_properties());
743   DCHECK(ideal_contents_scale_);
744   DCHECK_GT(tilings_->num_tilings(), 0u);
745
746   // The goal of this function is to find the minimum set of tiles that need to
747   // be ready to draw in order to activate without flashing content from a
748   // higher res on the active tree to a lower res on the pending tree.
749
750   gfx::Rect rect(visible_content_rect());
751
752   float min_acceptable_scale =
753       std::min(raster_contents_scale_, ideal_contents_scale_);
754
755   if (PictureLayerImpl* twin = twin_layer_) {
756     float twin_min_acceptable_scale =
757         std::min(twin->ideal_contents_scale_, twin->raster_contents_scale_);
758     // Ignore 0 scale in case CalculateContentsScale() has never been
759     // called for active twin.
760     if (twin_min_acceptable_scale != 0.0f) {
761       min_acceptable_scale =
762           std::min(min_acceptable_scale, twin_min_acceptable_scale);
763     }
764   }
765
766   PictureLayerTiling* high_res = NULL;
767   PictureLayerTiling* low_res = NULL;
768
769   // First pass: ready to draw tiles in acceptable but non-ideal tilings are
770   // marked as required for activation so that their textures are not thrown
771   // away; any non-ready tiles are not marked as required.
772   Region missing_region = rect;
773   for (size_t i = 0; i < tilings_->num_tilings(); ++i) {
774     PictureLayerTiling* tiling = tilings_->tiling_at(i);
775     DCHECK(tiling->has_ever_been_updated());
776
777     if (tiling->resolution() == LOW_RESOLUTION) {
778       DCHECK(!low_res) << "There can only be one low res tiling";
779       low_res = tiling;
780     }
781     if (tiling->contents_scale() < min_acceptable_scale)
782       continue;
783     if (tiling->resolution() == HIGH_RESOLUTION) {
784       DCHECK(!high_res) << "There can only be one high res tiling";
785       high_res = tiling;
786       continue;
787     }
788     for (PictureLayerTiling::CoverageIterator iter(tiling,
789                                                    contents_scale_x(),
790                                                    rect);
791          iter;
792          ++iter) {
793       if (!*iter || !iter->IsReadyToDraw())
794         continue;
795
796       missing_region.Subtract(iter.geometry_rect());
797       iter->MarkRequiredForActivation();
798     }
799   }
800   DCHECK(high_res) << "There must be one high res tiling";
801
802   // If these pointers are null (because no twin, no matching tiling, or the
803   // simpification just below), then high res tiles will be required to fill any
804   // holes left by the first pass above.  If the pointers are valid, then this
805   // layer is allowed to skip any tiles that are not ready on its twin.
806   const PictureLayerTiling* twin_high_res = NULL;
807   const PictureLayerTiling* twin_low_res = NULL;
808
809   // As a simplification, only allow activating to skip twin tiles that the
810   // active layer is also missing when both this layer and its twin have 2
811   // tilings (high and low).  This avoids having to iterate/track coverage of
812   // non-ideal tilings during the last draw call on the active layer.
813   if (high_res && low_res && tilings_->num_tilings() == 2 &&
814       twin_layer_ && twin_layer_->tilings_->num_tilings() == 2) {
815     twin_low_res = GetTwinTiling(low_res);
816     if (twin_low_res)
817       twin_high_res = GetTwinTiling(high_res);
818   }
819   // If this layer and its twin have different bounds or transforms, then don't
820   // compare them and only allow activating to high res tiles, since tiles on
821   // each layer will occupy different areas of the screen.
822   if (!twin_high_res || !twin_low_res ||
823       twin_layer_->layer_tree_impl()->RequiresHighResToDraw() ||
824       bounds() != twin_layer_->bounds() ||
825       draw_properties().screen_space_transform !=
826           twin_layer_->draw_properties().screen_space_transform) {
827     twin_high_res = NULL;
828     twin_low_res = NULL;
829   }
830
831   // As a second pass, mark as required any visible high res tiles not filled in
832   // by acceptable non-ideal tiles from the first pass.
833   if (MarkVisibleTilesAsRequired(
834       high_res, twin_high_res, contents_scale_x(), rect, missing_region)) {
835     // As an optional third pass, if a high res tile was skipped because its
836     // twin was also missing, then fall back to mark low res tiles as required
837     // in case the active twin is substituting those for missing high res
838     // content.
839     MarkVisibleTilesAsRequired(
840         low_res, twin_low_res, contents_scale_x(), rect, missing_region);
841   }
842 }
843
844 bool PictureLayerImpl::MarkVisibleTilesAsRequired(
845     PictureLayerTiling* tiling,
846     const PictureLayerTiling* optional_twin_tiling,
847     float contents_scale,
848     const gfx::Rect& rect,
849     const Region& missing_region) const {
850   bool twin_had_missing_tile = false;
851   for (PictureLayerTiling::CoverageIterator iter(tiling,
852                                                  contents_scale,
853                                                  rect);
854        iter;
855        ++iter) {
856     Tile* tile = *iter;
857     // A null tile (i.e. missing recording) can just be skipped.
858     if (!tile)
859       continue;
860
861     // If the missing region doesn't cover it, this tile is fully
862     // covered by acceptable tiles at other scales.
863     if (!missing_region.Intersects(iter.geometry_rect()))
864       continue;
865
866     // If the twin tile doesn't exist (i.e. missing recording or so far away
867     // that it is outside the visible tile rect) or this tile is shared between
868     // with the twin, then this tile isn't required to prevent flashing.
869     if (optional_twin_tiling) {
870       Tile* twin_tile = optional_twin_tiling->TileAt(iter.i(), iter.j());
871       if (!twin_tile || twin_tile == tile) {
872         twin_had_missing_tile = true;
873         continue;
874       }
875     }
876
877     tile->MarkRequiredForActivation();
878   }
879   return twin_had_missing_tile;
880 }
881
882 void PictureLayerImpl::DoPostCommitInitialization() {
883   DCHECK(needs_post_commit_initialization_);
884   DCHECK(layer_tree_impl()->IsPendingTree());
885
886   if (!tilings_)
887     tilings_.reset(new PictureLayerTilingSet(this, bounds()));
888
889   DCHECK(!twin_layer_);
890   twin_layer_ = static_cast<PictureLayerImpl*>(
891       layer_tree_impl()->FindActiveTreeLayerById(id()));
892   if (twin_layer_) {
893     DCHECK(!twin_layer_->twin_layer_);
894     twin_layer_->twin_layer_ = this;
895     // If the twin has never been pushed to, do not sync from it.
896     // This can happen if this function is called during activation.
897     if (!twin_layer_->needs_post_commit_initialization_)
898       SyncFromActiveLayer(twin_layer_);
899   }
900
901   needs_post_commit_initialization_ = false;
902 }
903
904 PictureLayerTiling* PictureLayerImpl::AddTiling(float contents_scale) {
905   DCHECK(CanHaveTilingWithScale(contents_scale)) <<
906       "contents_scale: " << contents_scale;
907
908   PictureLayerTiling* tiling = tilings_->AddTiling(contents_scale);
909
910   DCHECK(pile_->HasRecordings());
911
912   if (twin_layer_ &&
913       twin_layer_->use_gpu_rasterization() == use_gpu_rasterization())
914     twin_layer_->SyncTiling(tiling);
915
916   return tiling;
917 }
918
919 void PictureLayerImpl::RemoveTiling(float contents_scale) {
920   for (size_t i = 0; i < tilings_->num_tilings(); ++i) {
921     PictureLayerTiling* tiling = tilings_->tiling_at(i);
922     if (tiling->contents_scale() == contents_scale) {
923       tilings_->Remove(tiling);
924       break;
925     }
926   }
927   if (tilings_->num_tilings() == 0)
928     ResetRasterScale();
929   SanityCheckTilingState();
930 }
931
932 void PictureLayerImpl::RemoveAllTilings() {
933   if (tilings_)
934     tilings_->RemoveAllTilings();
935   // If there are no tilings, then raster scales are no longer meaningful.
936   ResetRasterScale();
937 }
938
939 namespace {
940
941 inline float PositiveRatio(float float1, float float2) {
942   DCHECK_GT(float1, 0);
943   DCHECK_GT(float2, 0);
944   return float1 > float2 ? float1 / float2 : float2 / float1;
945 }
946
947 }  // namespace
948
949 void PictureLayerImpl::ManageTilings(bool animating_transform_to_screen,
950                                      float maximum_animation_contents_scale) {
951   DCHECK(ideal_contents_scale_);
952   DCHECK(ideal_page_scale_);
953   DCHECK(ideal_device_scale_);
954   DCHECK(ideal_source_scale_);
955   DCHECK(CanHaveTilings());
956   DCHECK(!needs_post_commit_initialization_);
957
958   bool change_target_tiling =
959       raster_page_scale_ == 0.f ||
960       raster_device_scale_ == 0.f ||
961       raster_source_scale_ == 0.f ||
962       raster_contents_scale_ == 0.f ||
963       low_res_raster_contents_scale_ == 0.f ||
964       ShouldAdjustRasterScale(animating_transform_to_screen);
965
966   if (tilings_->num_tilings() == 0) {
967     DCHECK(change_target_tiling)
968         << "A layer with no tilings shouldn't have valid raster scales";
969   }
970
971   if (change_target_tiling) {
972     RecalculateRasterScales(animating_transform_to_screen,
973                             maximum_animation_contents_scale);
974   }
975
976   was_animating_transform_to_screen_ = animating_transform_to_screen;
977
978   if (!change_target_tiling)
979     return;
980
981   PictureLayerTiling* high_res = NULL;
982   PictureLayerTiling* low_res = NULL;
983
984   PictureLayerTiling* previous_low_res = NULL;
985   for (size_t i = 0; i < tilings_->num_tilings(); ++i) {
986     PictureLayerTiling* tiling = tilings_->tiling_at(i);
987     if (tiling->contents_scale() == raster_contents_scale_)
988       high_res = tiling;
989     if (tiling->contents_scale() == low_res_raster_contents_scale_)
990       low_res = tiling;
991     if (tiling->resolution() == LOW_RESOLUTION)
992       previous_low_res = tiling;
993
994     // Reset all tilings to non-ideal until the end of this function.
995     tiling->set_resolution(NON_IDEAL_RESOLUTION);
996   }
997
998   if (!high_res) {
999     high_res = AddTiling(raster_contents_scale_);
1000     if (raster_contents_scale_ == low_res_raster_contents_scale_)
1001       low_res = high_res;
1002   }
1003
1004   // Only create new low res tilings when the transform is static.  This
1005   // prevents wastefully creating a paired low res tiling for every new high res
1006   // tiling during a pinch or a CSS animation.
1007   bool is_pinching = layer_tree_impl()->PinchGestureActive();
1008   if (ShouldHaveLowResTiling() && !is_pinching &&
1009       !animating_transform_to_screen &&
1010       !low_res && low_res != high_res)
1011     low_res = AddTiling(low_res_raster_contents_scale_);
1012
1013   // Set low-res if we have one.
1014   if (!low_res)
1015     low_res = previous_low_res;
1016   if (low_res && low_res != high_res)
1017     low_res->set_resolution(LOW_RESOLUTION);
1018
1019   // Make sure we always have one high-res (even if high == low).
1020   high_res->set_resolution(HIGH_RESOLUTION);
1021
1022   SanityCheckTilingState();
1023 }
1024
1025 bool PictureLayerImpl::ShouldAdjustRasterScale(
1026     bool animating_transform_to_screen) const {
1027   if (was_animating_transform_to_screen_ != animating_transform_to_screen)
1028     return true;
1029
1030   bool is_pinching = layer_tree_impl()->PinchGestureActive();
1031   if (is_pinching && raster_page_scale_) {
1032     // We change our raster scale when it is:
1033     // - Higher than ideal (need a lower-res tiling available)
1034     // - Too far from ideal (need a higher-res tiling available)
1035     float ratio = ideal_page_scale_ / raster_page_scale_;
1036     if (raster_page_scale_ > ideal_page_scale_ ||
1037         ratio > kMaxScaleRatioDuringPinch)
1038       return true;
1039   }
1040
1041   if (!is_pinching) {
1042     // When not pinching, match the ideal page scale factor.
1043     if (raster_page_scale_ != ideal_page_scale_)
1044       return true;
1045   }
1046
1047   // Always match the ideal device scale factor.
1048   if (raster_device_scale_ != ideal_device_scale_)
1049     return true;
1050
1051   // When the source scale changes we want to match it, but not when animating
1052   // or when we've fixed the scale in place.
1053   if (!animating_transform_to_screen && !raster_source_scale_is_fixed_ &&
1054       raster_source_scale_ != ideal_source_scale_)
1055     return true;
1056
1057   return false;
1058 }
1059
1060 float PictureLayerImpl::SnappedContentsScale(float scale) {
1061   // If a tiling exists within the max snapping ratio, snap to its scale.
1062   float snapped_contents_scale = scale;
1063   float snapped_ratio = kSnapToExistingTilingRatio;
1064   for (size_t i = 0; i < tilings_->num_tilings(); ++i) {
1065     float tiling_contents_scale = tilings_->tiling_at(i)->contents_scale();
1066     float ratio = PositiveRatio(tiling_contents_scale, scale);
1067     if (ratio < snapped_ratio) {
1068       snapped_contents_scale = tiling_contents_scale;
1069       snapped_ratio = ratio;
1070     }
1071   }
1072   return snapped_contents_scale;
1073 }
1074
1075 void PictureLayerImpl::RecalculateRasterScales(
1076     bool animating_transform_to_screen,
1077     float maximum_animation_contents_scale) {
1078   float old_raster_contents_scale = raster_contents_scale_;
1079   float old_raster_page_scale = raster_page_scale_;
1080   float old_raster_source_scale = raster_source_scale_;
1081
1082   raster_device_scale_ = ideal_device_scale_;
1083   raster_page_scale_ = ideal_page_scale_;
1084   raster_source_scale_ = ideal_source_scale_;
1085   raster_contents_scale_ = ideal_contents_scale_;
1086
1087   // If we're not animating, or leaving an animation, and the
1088   // ideal_source_scale_ changes, then things are unpredictable, and we fix
1089   // the raster_source_scale_ in place.
1090   if (old_raster_source_scale && !animating_transform_to_screen &&
1091       !was_animating_transform_to_screen_ &&
1092       old_raster_source_scale != ideal_source_scale_)
1093     raster_source_scale_is_fixed_ = true;
1094
1095   // TODO(danakj): Adjust raster source scale closer to ideal source scale at
1096   // a throttled rate. Possibly make use of invalidation_.IsEmpty() on pending
1097   // tree. This will allow CSS scale changes to get re-rastered at an
1098   // appropriate rate.
1099   if (raster_source_scale_is_fixed_) {
1100     raster_contents_scale_ /= raster_source_scale_;
1101     raster_source_scale_ = 1.f;
1102   }
1103
1104   // During pinch we completely ignore the current ideal scale, and just use
1105   // a multiple of the previous scale.
1106   // TODO(danakj): This seems crazy, we should use the current ideal, no?
1107   bool is_pinching = layer_tree_impl()->PinchGestureActive();
1108   if (is_pinching && old_raster_contents_scale) {
1109     // See ShouldAdjustRasterScale:
1110     // - When zooming out, preemptively create new tiling at lower resolution.
1111     // - When zooming in, approximate ideal using multiple of kMaxScaleRatio.
1112     bool zooming_out = old_raster_page_scale > ideal_page_scale_;
1113     float desired_contents_scale =
1114         zooming_out ? old_raster_contents_scale / kMaxScaleRatioDuringPinch
1115                     : old_raster_contents_scale * kMaxScaleRatioDuringPinch;
1116     raster_contents_scale_ = SnappedContentsScale(desired_contents_scale);
1117     raster_page_scale_ =
1118         raster_contents_scale_ / raster_device_scale_ / raster_source_scale_;
1119   }
1120
1121   raster_contents_scale_ =
1122       std::max(raster_contents_scale_, MinimumContentsScale());
1123
1124   // Since we're not re-rasterizing during animation, rasterize at the maximum
1125   // scale that will occur during the animation, if the maximum scale is
1126   // known.
1127   if (animating_transform_to_screen) {
1128     if (maximum_animation_contents_scale > 0.f) {
1129       raster_contents_scale_ =
1130           std::max(raster_contents_scale_, maximum_animation_contents_scale);
1131     } else {
1132       raster_contents_scale_ =
1133           std::max(raster_contents_scale_,
1134                    1.f * ideal_page_scale_ * ideal_device_scale_);
1135     }
1136   }
1137
1138   // If this layer would only create one tile at this content scale,
1139   // don't create a low res tiling.
1140   gfx::Size content_bounds =
1141       gfx::ToCeiledSize(gfx::ScaleSize(bounds(), raster_contents_scale_));
1142   gfx::Size tile_size = CalculateTileSize(content_bounds);
1143   if (tile_size.width() >= content_bounds.width() &&
1144       tile_size.height() >= content_bounds.height()) {
1145     low_res_raster_contents_scale_ = raster_contents_scale_;
1146     return;
1147   }
1148
1149   float low_res_factor =
1150       layer_tree_impl()->settings().low_res_contents_scale_factor;
1151   low_res_raster_contents_scale_ = std::max(
1152       raster_contents_scale_ * low_res_factor,
1153       MinimumContentsScale());
1154 }
1155
1156 void PictureLayerImpl::CleanUpTilingsOnActiveLayer(
1157     std::vector<PictureLayerTiling*> used_tilings) {
1158   DCHECK(layer_tree_impl()->IsActiveTree());
1159   if (tilings_->num_tilings() == 0)
1160     return;
1161
1162   float min_acceptable_high_res_scale = std::min(
1163       raster_contents_scale_, ideal_contents_scale_);
1164   float max_acceptable_high_res_scale = std::max(
1165       raster_contents_scale_, ideal_contents_scale_);
1166
1167   PictureLayerImpl* twin = twin_layer_;
1168   if (twin) {
1169     min_acceptable_high_res_scale = std::min(
1170         min_acceptable_high_res_scale,
1171         std::min(twin->raster_contents_scale_, twin->ideal_contents_scale_));
1172     max_acceptable_high_res_scale = std::max(
1173         max_acceptable_high_res_scale,
1174         std::max(twin->raster_contents_scale_, twin->ideal_contents_scale_));
1175   }
1176
1177   std::vector<PictureLayerTiling*> to_remove;
1178   for (size_t i = 0; i < tilings_->num_tilings(); ++i) {
1179     PictureLayerTiling* tiling = tilings_->tiling_at(i);
1180
1181     // Keep multiple high resolution tilings even if not used to help
1182     // activate earlier at non-ideal resolutions.
1183     if (tiling->contents_scale() >= min_acceptable_high_res_scale &&
1184         tiling->contents_scale() <= max_acceptable_high_res_scale)
1185       continue;
1186
1187     // Keep low resolution tilings, if the layer should have them.
1188     if (tiling->resolution() == LOW_RESOLUTION && ShouldHaveLowResTiling())
1189       continue;
1190
1191     // Don't remove tilings that are being used (and thus would cause a flash.)
1192     if (std::find(used_tilings.begin(), used_tilings.end(), tiling) !=
1193         used_tilings.end())
1194       continue;
1195
1196     to_remove.push_back(tiling);
1197   }
1198
1199   for (size_t i = 0; i < to_remove.size(); ++i) {
1200     const PictureLayerTiling* twin_tiling = GetTwinTiling(to_remove[i]);
1201     // Only remove tilings from the twin layer if they have
1202     // NON_IDEAL_RESOLUTION.
1203     if (twin_tiling && twin_tiling->resolution() == NON_IDEAL_RESOLUTION)
1204       twin->RemoveTiling(to_remove[i]->contents_scale());
1205     // TODO(enne): temporary sanity CHECK for http://crbug.com/358350
1206     CHECK_NE(HIGH_RESOLUTION, to_remove[i]->resolution());
1207     tilings_->Remove(to_remove[i]);
1208   }
1209   DCHECK_GT(tilings_->num_tilings(), 0u);
1210
1211   SanityCheckTilingState();
1212 }
1213
1214 float PictureLayerImpl::MinimumContentsScale() const {
1215   float setting_min = layer_tree_impl()->settings().minimum_contents_scale;
1216
1217   // If the contents scale is less than 1 / width (also for height),
1218   // then it will end up having less than one pixel of content in that
1219   // dimension.  Bump the minimum contents scale up in this case to prevent
1220   // this from happening.
1221   int min_dimension = std::min(bounds().width(), bounds().height());
1222   if (!min_dimension)
1223     return setting_min;
1224
1225   return std::max(1.f / min_dimension, setting_min);
1226 }
1227
1228 void PictureLayerImpl::UpdateLCDTextStatus(bool new_status) {
1229   // Once this layer is not using lcd text, don't switch back.
1230   if (!is_using_lcd_text_)
1231     return;
1232
1233   if (is_using_lcd_text_ == new_status)
1234     return;
1235
1236   is_using_lcd_text_ = new_status;
1237   tilings_->SetCanUseLCDText(is_using_lcd_text_);
1238 }
1239
1240 void PictureLayerImpl::ResetRasterScale() {
1241   raster_page_scale_ = 0.f;
1242   raster_device_scale_ = 0.f;
1243   raster_source_scale_ = 0.f;
1244   raster_contents_scale_ = 0.f;
1245   low_res_raster_contents_scale_ = 0.f;
1246   raster_source_scale_is_fixed_ = false;
1247
1248   // When raster scales aren't valid, don't update tile priorities until
1249   // this layer has been updated via UpdateDrawProperties.
1250   should_update_tile_priorities_ = false;
1251 }
1252
1253 bool PictureLayerImpl::CanHaveTilings() const {
1254   if (!DrawsContent())
1255     return false;
1256   if (!pile_->HasRecordings())
1257     return false;
1258   return true;
1259 }
1260
1261 bool PictureLayerImpl::CanHaveTilingWithScale(float contents_scale) const {
1262   if (!CanHaveTilings())
1263     return false;
1264   if (contents_scale < MinimumContentsScale())
1265     return false;
1266   return true;
1267 }
1268
1269 void PictureLayerImpl::SanityCheckTilingState() const {
1270 #if DCHECK_IS_ON
1271   if (!CanHaveTilings()) {
1272     DCHECK_EQ(0u, tilings_->num_tilings());
1273     return;
1274   }
1275   if (tilings_->num_tilings() == 0)
1276     return;
1277
1278   // MarkVisibleResourcesAsRequired depends on having exactly 1 high res
1279   // tiling to mark its tiles as being required for activation.
1280   DCHECK_EQ(1, tilings_->NumHighResTilings());
1281 #endif
1282 }
1283
1284 void PictureLayerImpl::GetDebugBorderProperties(
1285     SkColor* color,
1286     float* width) const {
1287   *color = DebugColors::TiledContentLayerBorderColor();
1288   *width = DebugColors::TiledContentLayerBorderWidth(layer_tree_impl());
1289 }
1290
1291 void PictureLayerImpl::AsValueInto(base::DictionaryValue* state) const {
1292   const_cast<PictureLayerImpl*>(this)->DoPostCommitInitializationIfNeeded();
1293   LayerImpl::AsValueInto(state);
1294   state->SetDouble("ideal_contents_scale", ideal_contents_scale_);
1295   state->SetDouble("geometry_contents_scale", contents_scale_x());
1296   state->Set("tilings", tilings_->AsValue().release());
1297   state->Set("pictures", pile_->AsValue().release());
1298   state->Set("invalidation", invalidation_.AsValue().release());
1299
1300   scoped_ptr<base::ListValue> coverage_tiles(new base::ListValue);
1301   for (PictureLayerTilingSet::CoverageIterator iter(tilings_.get(),
1302                                                     contents_scale_x(),
1303                                                     gfx::Rect(content_bounds()),
1304                                                     ideal_contents_scale_);
1305        iter;
1306        ++iter) {
1307     scoped_ptr<base::DictionaryValue> tile_data(new base::DictionaryValue);
1308     tile_data->Set("geometry_rect",
1309                    MathUtil::AsValue(iter.geometry_rect()).release());
1310     if (*iter)
1311       tile_data->Set("tile", TracedValue::CreateIDRef(*iter).release());
1312
1313     coverage_tiles->Append(tile_data.release());
1314   }
1315   state->Set("coverage_tiles", coverage_tiles.release());
1316   state->SetBoolean("is_using_lcd_text", is_using_lcd_text_);
1317   state->SetBoolean("using_gpu_rasterization", use_gpu_rasterization());
1318 }
1319
1320 size_t PictureLayerImpl::GPUMemoryUsageInBytes() const {
1321   const_cast<PictureLayerImpl*>(this)->DoPostCommitInitializationIfNeeded();
1322   return tilings_->GPUMemoryUsageInBytes();
1323 }
1324
1325 void PictureLayerImpl::RunMicroBenchmark(MicroBenchmarkImpl* benchmark) {
1326   benchmark->RunOnLayer(this);
1327 }
1328
1329 WhichTree PictureLayerImpl::GetTree() const {
1330   return layer_tree_impl()->IsActiveTree() ? ACTIVE_TREE : PENDING_TREE;
1331 }
1332
1333 bool PictureLayerImpl::IsOnActiveOrPendingTree() const {
1334   return !layer_tree_impl()->IsRecycleTree();
1335 }
1336
1337 PictureLayerImpl::LayerRasterTileIterator::LayerRasterTileIterator()
1338     : layer_(NULL) {}
1339
1340 PictureLayerImpl::LayerRasterTileIterator::LayerRasterTileIterator(
1341     PictureLayerImpl* layer,
1342     bool prioritize_low_res)
1343     : layer_(layer), current_stage_(0) {
1344   DCHECK(layer_);
1345   if (!layer_->tilings_ || !layer_->tilings_->num_tilings()) {
1346     current_stage_ = arraysize(stages_);
1347     return;
1348   }
1349
1350   WhichTree tree =
1351       layer_->layer_tree_impl()->IsActiveTree() ? ACTIVE_TREE : PENDING_TREE;
1352
1353   // Find high and low res tilings and initialize the iterators.
1354   for (size_t i = 0; i < layer_->tilings_->num_tilings(); ++i) {
1355     PictureLayerTiling* tiling = layer_->tilings_->tiling_at(i);
1356     if (tiling->resolution() == HIGH_RESOLUTION) {
1357       iterators_[HIGH_RES] =
1358           PictureLayerTiling::TilingRasterTileIterator(tiling, tree);
1359     }
1360
1361     if (tiling->resolution() == LOW_RESOLUTION) {
1362       iterators_[LOW_RES] =
1363           PictureLayerTiling::TilingRasterTileIterator(tiling, tree);
1364     }
1365   }
1366
1367   if (prioritize_low_res) {
1368     stages_[0].iterator_type = LOW_RES;
1369     stages_[0].tile_type = TilePriority::NOW;
1370
1371     stages_[1].iterator_type = HIGH_RES;
1372     stages_[1].tile_type = TilePriority::NOW;
1373   } else {
1374     stages_[0].iterator_type = HIGH_RES;
1375     stages_[0].tile_type = TilePriority::NOW;
1376
1377     stages_[1].iterator_type = LOW_RES;
1378     stages_[1].tile_type = TilePriority::NOW;
1379   }
1380
1381   stages_[2].iterator_type = HIGH_RES;
1382   stages_[2].tile_type = TilePriority::SOON;
1383
1384   stages_[3].iterator_type = HIGH_RES;
1385   stages_[3].tile_type = TilePriority::EVENTUALLY;
1386
1387   IteratorType index = stages_[current_stage_].iterator_type;
1388   TilePriority::PriorityBin tile_type = stages_[current_stage_].tile_type;
1389   if (!iterators_[index] || iterators_[index].get_type() != tile_type)
1390     ++(*this);
1391 }
1392
1393 PictureLayerImpl::LayerRasterTileIterator::~LayerRasterTileIterator() {}
1394
1395 PictureLayerImpl::LayerRasterTileIterator::operator bool() const {
1396   return layer_ && static_cast<size_t>(current_stage_) < arraysize(stages_);
1397 }
1398
1399 PictureLayerImpl::LayerRasterTileIterator&
1400 PictureLayerImpl::LayerRasterTileIterator::
1401 operator++() {
1402   IteratorType index = stages_[current_stage_].iterator_type;
1403   TilePriority::PriorityBin tile_type = stages_[current_stage_].tile_type;
1404
1405   // First advance the iterator.
1406   if (iterators_[index])
1407     ++iterators_[index];
1408
1409   if (iterators_[index] && iterators_[index].get_type() == tile_type)
1410     return *this;
1411
1412   // Next, advance the stage.
1413   int stage_count = arraysize(stages_);
1414   ++current_stage_;
1415   while (current_stage_ < stage_count) {
1416     index = stages_[current_stage_].iterator_type;
1417     tile_type = stages_[current_stage_].tile_type;
1418
1419     if (iterators_[index] && iterators_[index].get_type() == tile_type)
1420       break;
1421     ++current_stage_;
1422   }
1423   return *this;
1424 }
1425
1426 Tile* PictureLayerImpl::LayerRasterTileIterator::operator*() {
1427   DCHECK(*this);
1428
1429   IteratorType index = stages_[current_stage_].iterator_type;
1430   DCHECK(iterators_[index]);
1431   DCHECK(iterators_[index].get_type() == stages_[current_stage_].tile_type);
1432
1433   return *iterators_[index];
1434 }
1435
1436 PictureLayerImpl::LayerEvictionTileIterator::LayerEvictionTileIterator()
1437     : iterator_index_(0),
1438       iteration_stage_(TilePriority::EVENTUALLY),
1439       required_for_activation_(false),
1440       layer_(NULL) {}
1441
1442 PictureLayerImpl::LayerEvictionTileIterator::LayerEvictionTileIterator(
1443     PictureLayerImpl* layer,
1444     TreePriority tree_priority)
1445     : iterator_index_(0),
1446       iteration_stage_(TilePriority::EVENTUALLY),
1447       required_for_activation_(false),
1448       layer_(layer) {
1449   if (!layer_->tilings_ || !layer_->tilings_->num_tilings())
1450     return;
1451
1452   size_t high_res_tiling_index = layer_->tilings_->num_tilings();
1453   size_t low_res_tiling_index = layer_->tilings_->num_tilings();
1454   for (size_t i = 0; i < layer_->tilings_->num_tilings(); ++i) {
1455     PictureLayerTiling* tiling = layer_->tilings_->tiling_at(i);
1456     if (tiling->resolution() == HIGH_RESOLUTION)
1457       high_res_tiling_index = i;
1458     else if (tiling->resolution() == LOW_RESOLUTION)
1459       low_res_tiling_index = i;
1460   }
1461
1462   iterators_.reserve(layer_->tilings_->num_tilings());
1463
1464   // Higher resolution non-ideal goes first.
1465   for (size_t i = 0; i < high_res_tiling_index; ++i) {
1466     iterators_.push_back(PictureLayerTiling::TilingEvictionTileIterator(
1467         layer_->tilings_->tiling_at(i), tree_priority));
1468   }
1469
1470   // Lower resolution non-ideal goes next.
1471   for (size_t i = layer_->tilings_->num_tilings() - 1;
1472        i > high_res_tiling_index;
1473        --i) {
1474     PictureLayerTiling* tiling = layer_->tilings_->tiling_at(i);
1475     if (tiling->resolution() == LOW_RESOLUTION)
1476       continue;
1477
1478     iterators_.push_back(
1479         PictureLayerTiling::TilingEvictionTileIterator(tiling, tree_priority));
1480   }
1481
1482   // Now, put the low res tiling if we have one.
1483   if (low_res_tiling_index < layer_->tilings_->num_tilings()) {
1484     iterators_.push_back(PictureLayerTiling::TilingEvictionTileIterator(
1485         layer_->tilings_->tiling_at(low_res_tiling_index), tree_priority));
1486   }
1487
1488   // Finally, put the high res tiling if we have one.
1489   if (high_res_tiling_index < layer_->tilings_->num_tilings()) {
1490     iterators_.push_back(PictureLayerTiling::TilingEvictionTileIterator(
1491         layer_->tilings_->tiling_at(high_res_tiling_index), tree_priority));
1492   }
1493
1494   DCHECK_GT(iterators_.size(), 0u);
1495
1496   if (!iterators_[iterator_index_] ||
1497       !IsCorrectType(&iterators_[iterator_index_])) {
1498     AdvanceToNextIterator();
1499   }
1500 }
1501
1502 PictureLayerImpl::LayerEvictionTileIterator::~LayerEvictionTileIterator() {}
1503
1504 Tile* PictureLayerImpl::LayerEvictionTileIterator::operator*() {
1505   DCHECK(*this);
1506   return *iterators_[iterator_index_];
1507 }
1508
1509 PictureLayerImpl::LayerEvictionTileIterator&
1510 PictureLayerImpl::LayerEvictionTileIterator::
1511 operator++() {
1512   DCHECK(*this);
1513   ++iterators_[iterator_index_];
1514   if (!iterators_[iterator_index_] ||
1515       !IsCorrectType(&iterators_[iterator_index_])) {
1516     AdvanceToNextIterator();
1517   }
1518   return *this;
1519 }
1520
1521 void PictureLayerImpl::LayerEvictionTileIterator::AdvanceToNextIterator() {
1522   ++iterator_index_;
1523
1524   while (true) {
1525     while (iterator_index_ < iterators_.size()) {
1526       if (iterators_[iterator_index_] &&
1527           IsCorrectType(&iterators_[iterator_index_])) {
1528         return;
1529       }
1530       ++iterator_index_;
1531     }
1532
1533     // If we're NOW and required_for_activation, then this was the last pass
1534     // through the iterators.
1535     if (iteration_stage_ == TilePriority::NOW && required_for_activation_)
1536       break;
1537
1538     if (!required_for_activation_) {
1539       required_for_activation_ = true;
1540     } else {
1541       required_for_activation_ = false;
1542       iteration_stage_ =
1543           static_cast<TilePriority::PriorityBin>(iteration_stage_ - 1);
1544     }
1545     iterator_index_ = 0;
1546   }
1547 }
1548
1549 PictureLayerImpl::LayerEvictionTileIterator::operator bool() const {
1550   return iterator_index_ < iterators_.size();
1551 }
1552
1553 bool PictureLayerImpl::LayerEvictionTileIterator::IsCorrectType(
1554     PictureLayerTiling::TilingEvictionTileIterator* it) const {
1555   return it->get_type() == iteration_stage_ &&
1556          (**it)->required_for_activation() == required_for_activation_;
1557 }
1558
1559 }  // namespace cc