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.
5 #include "cc/layers/picture_layer.h"
7 #include "cc/debug/devtools_instrumentation.h"
8 #include "cc/layers/content_layer_client.h"
9 #include "cc/layers/picture_layer_impl.h"
10 #include "cc/trees/layer_tree_impl.h"
11 #include "ui/gfx/rect_conversions.h"
15 scoped_refptr<PictureLayer> PictureLayer::Create(ContentLayerClient* client) {
16 return make_scoped_refptr(new PictureLayer(client));
19 PictureLayer::PictureLayer(ContentLayerClient* client)
21 pile_(make_scoped_refptr(new PicturePile())),
22 instrumentation_object_tracker_(id()),
24 update_source_frame_number_(-1) {
27 PictureLayer::~PictureLayer() {
30 bool PictureLayer::DrawsContent() const {
31 return Layer::DrawsContent() && client_;
34 scoped_ptr<LayerImpl> PictureLayer::CreateLayerImpl(LayerTreeImpl* tree_impl) {
35 return PictureLayerImpl::Create(tree_impl, id()).PassAs<LayerImpl>();
38 void PictureLayer::PushPropertiesTo(LayerImpl* base_layer) {
39 Layer::PushPropertiesTo(base_layer);
40 PictureLayerImpl* layer_impl = static_cast<PictureLayerImpl*>(base_layer);
42 if (layer_impl->bounds().IsEmpty()) {
43 // Update may not get called for an empty layer, so resize here instead.
44 // Using layer_impl because either bounds() or paint_properties().bounds
45 // may disagree and either one could have been pushed to layer_impl.
46 pile_->Resize(gfx::Size());
47 pile_->UpdateRecordedRegion();
48 } else if (update_source_frame_number_ ==
49 layer_tree_host()->source_frame_number()) {
50 // If update called, then pile size must match bounds pushed to impl layer.
51 DCHECK_EQ(layer_impl->bounds().ToString(), pile_->size().ToString());
54 layer_impl->SetIsMask(is_mask_);
55 // Unlike other properties, invalidation must always be set on layer_impl.
56 // See PictureLayerImpl::PushPropertiesTo for more details.
57 layer_impl->invalidation_.Clear();
58 layer_impl->invalidation_.Swap(&pile_invalidation_);
59 layer_impl->pile_ = PicturePileImpl::CreateFromOther(pile_.get());
62 void PictureLayer::SetLayerTreeHost(LayerTreeHost* host) {
63 Layer::SetLayerTreeHost(host);
65 pile_->SetMinContentsScale(host->settings().minimum_contents_scale);
66 pile_->SetTileGridSize(host->settings().default_tile_size);
67 pile_->set_num_raster_threads(host->settings().num_raster_threads);
68 pile_->set_slow_down_raster_scale_factor(
69 host->debug_state().slow_down_raster_scale_factor);
70 pile_->set_show_debug_picture_borders(
71 host->debug_state().show_picture_borders);
75 void PictureLayer::SetNeedsDisplayRect(const gfx::RectF& layer_rect) {
76 gfx::Rect rect = gfx::ToEnclosedRect(layer_rect);
77 if (!rect.IsEmpty()) {
78 // Clamp invalidation to the layer bounds.
79 rect.Intersect(gfx::Rect(bounds()));
80 pending_invalidation_.Union(rect);
82 Layer::SetNeedsDisplayRect(layer_rect);
85 bool PictureLayer::Update(ResourceUpdateQueue* queue,
86 const OcclusionTracker* occlusion) {
87 // Do not early-out of this function so that PicturePile::Update has a chance
88 // to record pictures due to changing visibility of this layer.
90 TRACE_EVENT1("cc", "PictureLayer::Update",
91 "source_frame_number",
92 layer_tree_host()->source_frame_number());
94 update_source_frame_number_ = layer_tree_host()->source_frame_number();
95 bool updated = Layer::Update(queue, occlusion);
97 pile_->Resize(paint_properties().bounds);
99 // Calling paint in WebKit can sometimes cause invalidations, so save
100 // off the invalidation prior to calling update.
101 pending_invalidation_.Swap(&pile_invalidation_);
102 pending_invalidation_.Clear();
104 gfx::Rect visible_layer_rect = gfx::ScaleToEnclosingRect(
105 visible_content_rect(), 1.f / contents_scale_x());
106 if (layer_tree_host()->settings().using_synchronous_renderer_compositor) {
107 // Workaround for http://crbug.com/235910 - to retain backwards compat
108 // the full page content must always be provided in the picture layer.
109 visible_layer_rect = gfx::Rect(bounds());
111 devtools_instrumentation::ScopedLayerTask paint_layer(
112 devtools_instrumentation::kPaintLayer, id());
113 updated |= pile_->Update(client_,
114 SafeOpaqueBackgroundColor(),
118 rendering_stats_instrumentation());
120 SetNeedsPushProperties();
122 // If this invalidation did not affect the pile, then it can be cleared as
124 pile_invalidation_.Clear();
130 void PictureLayer::SetIsMask(bool is_mask) {
134 bool PictureLayer::SupportsLCDText() const {
138 skia::RefPtr<SkPicture> PictureLayer::GetPicture() const {
139 // We could either flatten the PicturePile into a single SkPicture,
140 // or paint a fresh one depending on what we intend to do with the
141 // picture. For now we just paint a fresh one to get consistent results.
143 return skia::RefPtr<SkPicture>();
145 int width = bounds().width();
146 int height = bounds().height();
149 skia::RefPtr<SkPicture> picture = skia::AdoptRef(new SkPicture);
150 SkCanvas* canvas = picture->beginRecording(width, height);
151 client_->PaintContents(canvas, gfx::Rect(width, height), &opaque);
152 picture->endRecording();
156 void PictureLayer::RunMicroBenchmark(MicroBenchmark* benchmark) {
157 benchmark->RunOnLayer(this);