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