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