Home | History | Annotate | Download | only in layers
      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.h"
      6 
      7 #include "cc/layers/content_layer_client.h"
      8 #include "cc/layers/picture_layer_impl.h"
      9 #include "cc/trees/layer_tree_impl.h"
     10 #include "ui/gfx/rect_conversions.h"
     11 
     12 namespace cc {
     13 
     14 scoped_refptr<PictureLayer> PictureLayer::Create(ContentLayerClient* client) {
     15   return make_scoped_refptr(new PictureLayer(client));
     16 }
     17 
     18 PictureLayer::PictureLayer(ContentLayerClient* client)
     19   : client_(client),
     20     pile_(make_scoped_refptr(new PicturePile())),
     21     instrumentation_object_tracker_(id()),
     22     is_mask_(false),
     23     update_source_frame_number_(-1) {
     24 }
     25 
     26 PictureLayer::~PictureLayer() {
     27 }
     28 
     29 bool PictureLayer::DrawsContent() const {
     30   return Layer::DrawsContent() && client_;
     31 }
     32 
     33 scoped_ptr<LayerImpl> PictureLayer::CreateLayerImpl(LayerTreeImpl* tree_impl) {
     34   return PictureLayerImpl::Create(tree_impl, id()).PassAs<LayerImpl>();
     35 }
     36 
     37 void PictureLayer::PushPropertiesTo(LayerImpl* base_layer) {
     38   Layer::PushPropertiesTo(base_layer);
     39   PictureLayerImpl* layer_impl = static_cast<PictureLayerImpl*>(base_layer);
     40 
     41   if (layer_impl->bounds().IsEmpty()) {
     42     // Update may not get called for an empty layer, so resize here instead.
     43     // Using layer_impl because either bounds() or paint_properties().bounds
     44     // may disagree and either one could have been pushed to layer_impl.
     45     pile_->Resize(gfx::Size());
     46   } else if (update_source_frame_number_ ==
     47              layer_tree_host()->source_frame_number()) {
     48     // If update called, then pile size must match bounds pushed to impl layer.
     49     DCHECK_EQ(layer_impl->bounds().ToString(), pile_->size().ToString());
     50   }
     51 
     52   layer_impl->SetIsMask(is_mask_);
     53   // Unlike other properties, invalidation must always be set on layer_impl.
     54   // See PictureLayerImpl::PushPropertiesTo for more details.
     55   layer_impl->invalidation_.Clear();
     56   layer_impl->invalidation_.Swap(&pile_invalidation_);
     57   layer_impl->pile_ = PicturePileImpl::CreateFromOther(pile_.get());
     58 }
     59 
     60 void PictureLayer::SetLayerTreeHost(LayerTreeHost* host) {
     61   Layer::SetLayerTreeHost(host);
     62   if (host) {
     63     pile_->SetMinContentsScale(host->settings().minimum_contents_scale);
     64     pile_->SetTileGridSize(host->settings().default_tile_size);
     65     pile_->set_num_raster_threads(host->settings().num_raster_threads);
     66     pile_->set_slow_down_raster_scale_factor(
     67         host->debug_state().slow_down_raster_scale_factor);
     68     pile_->set_show_debug_picture_borders(
     69         host->debug_state().show_picture_borders);
     70   }
     71 }
     72 
     73 void PictureLayer::SetNeedsDisplayRect(const gfx::RectF& layer_rect) {
     74   gfx::Rect rect = gfx::ToEnclosedRect(layer_rect);
     75   if (!rect.IsEmpty()) {
     76     // Clamp invalidation to the layer bounds.
     77     rect.Intersect(gfx::Rect(bounds()));
     78     pending_invalidation_.Union(rect);
     79   }
     80   Layer::SetNeedsDisplayRect(layer_rect);
     81 }
     82 
     83 bool PictureLayer::Update(ResourceUpdateQueue* queue,
     84                           const OcclusionTracker* occlusion) {
     85   update_source_frame_number_ = layer_tree_host()->source_frame_number();
     86   bool updated = Layer::Update(queue, occlusion);
     87 
     88   if (last_updated_visible_content_rect_ == visible_content_rect() &&
     89       pile_->size() == paint_properties().bounds &&
     90       pending_invalidation_.IsEmpty()) {
     91     // Only early out if the visible content rect of this layer hasn't changed.
     92     return updated;
     93   }
     94 
     95   TRACE_EVENT1("cc", "PictureLayer::Update",
     96                "source_frame_number",
     97                layer_tree_host()->source_frame_number());
     98 
     99   pile_->Resize(paint_properties().bounds);
    100 
    101   // Calling paint in WebKit can sometimes cause invalidations, so save
    102   // off the invalidation prior to calling update.
    103   pending_invalidation_.Swap(&pile_invalidation_);
    104   pending_invalidation_.Clear();
    105 
    106   gfx::Rect visible_layer_rect = gfx::ScaleToEnclosingRect(
    107       visible_content_rect(), 1.f / contents_scale_x());
    108   if (layer_tree_host()->settings().using_synchronous_renderer_compositor) {
    109     // Workaround for http://crbug.com/235910 - to retain backwards compat
    110     // the full page content must always be provided in the picture layer.
    111     visible_layer_rect = gfx::Rect(bounds());
    112   }
    113   updated |= pile_->Update(client_,
    114                            SafeOpaqueBackgroundColor(),
    115                            contents_opaque(),
    116                            pile_invalidation_,
    117                            visible_layer_rect,
    118                            update_source_frame_number_,
    119                            rendering_stats_instrumentation());
    120   last_updated_visible_content_rect_ = visible_content_rect();
    121 
    122   if (updated) {
    123     SetNeedsPushProperties();
    124   } else {
    125     // If this invalidation did not affect the pile, then it can be cleared as
    126     // an optimization.
    127     pile_invalidation_.Clear();
    128   }
    129 
    130   return updated;
    131 }
    132 
    133 void PictureLayer::SetIsMask(bool is_mask) {
    134   is_mask_ = is_mask;
    135 }
    136 
    137 bool PictureLayer::SupportsLCDText() const {
    138   return true;
    139 }
    140 
    141 skia::RefPtr<SkPicture> PictureLayer::GetPicture() const {
    142   // We could either flatten the PicturePile into a single SkPicture,
    143   // or paint a fresh one depending on what we intend to do with the
    144   // picture. For now we just paint a fresh one to get consistent results.
    145   if (!DrawsContent())
    146     return skia::RefPtr<SkPicture>();
    147 
    148   int width = bounds().width();
    149   int height = bounds().height();
    150   gfx::RectF opaque;
    151 
    152   skia::RefPtr<SkPicture> picture = skia::AdoptRef(new SkPicture);
    153   SkCanvas* canvas = picture->beginRecording(width, height);
    154   client_->PaintContents(canvas, gfx::Rect(width, height), &opaque);
    155   picture->endRecording();
    156   return picture;
    157 }
    158 
    159 void PictureLayer::RunMicroBenchmark(MicroBenchmark* benchmark) {
    160   benchmark->RunOnLayer(this);
    161 }
    162 
    163 }  // namespace cc
    164