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 "base/auto_reset.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 "third_party/skia/include/core/SkPictureRecorder.h"
12 #include "ui/gfx/rect_conversions.h"
16 scoped_refptr<PictureLayer> PictureLayer::Create(ContentLayerClient* client) {
17 return make_scoped_refptr(new PictureLayer(client));
20 PictureLayer::PictureLayer(ContentLayerClient* client)
22 pile_(make_scoped_refptr(new PicturePile())),
23 instrumentation_object_tracker_(id()),
25 update_source_frame_number_(-1),
26 can_use_lcd_text_last_frame_(can_use_lcd_text()) {
29 PictureLayer::~PictureLayer() {
32 scoped_ptr<LayerImpl> PictureLayer::CreateLayerImpl(LayerTreeImpl* tree_impl) {
33 return PictureLayerImpl::Create(tree_impl, id()).PassAs<LayerImpl>();
36 void PictureLayer::PushPropertiesTo(LayerImpl* base_layer) {
37 Layer::PushPropertiesTo(base_layer);
38 PictureLayerImpl* layer_impl = static_cast<PictureLayerImpl*>(base_layer);
40 if (layer_impl->bounds().IsEmpty()) {
41 // Update may not get called for an empty layer, so resize here instead.
42 // Using layer_impl because either bounds() or paint_properties().bounds
43 // may disagree and either one could have been pushed to layer_impl.
44 pile_->SetEmptyBounds();
45 } else if (update_source_frame_number_ ==
46 layer_tree_host()->source_frame_number()) {
47 // TODO(ernstm): This DCHECK is only valid as long as the pile's tiling_rect
48 // is identical to the layer_rect.
49 // If update called, then pile size must match bounds pushed to impl layer.
50 DCHECK_EQ(layer_impl->bounds().ToString(), pile_->tiling_size().ToString());
53 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_slow_down_raster_scale_factor(
68 host->debug_state().slow_down_raster_scale_factor);
69 pile_->set_show_debug_picture_borders(
70 host->debug_state().show_picture_borders);
74 void PictureLayer::SetNeedsDisplayRect(const gfx::RectF& layer_rect) {
75 gfx::Rect rect = gfx::ToEnclosedRect(layer_rect);
76 if (!rect.IsEmpty()) {
77 // Clamp invalidation to the layer bounds.
78 rect.Intersect(gfx::Rect(bounds()));
79 pending_invalidation_.Union(rect);
81 Layer::SetNeedsDisplayRect(layer_rect);
84 bool PictureLayer::Update(ResourceUpdateQueue* queue,
85 const OcclusionTracker<Layer>* occlusion) {
86 update_source_frame_number_ = layer_tree_host()->source_frame_number();
87 bool updated = Layer::Update(queue, occlusion);
90 base::AutoReset<bool> ignore_set_needs_commit(&ignore_set_needs_commit_,
92 UpdateCanUseLCDText();
95 gfx::Rect visible_layer_rect = gfx::ScaleToEnclosingRect(
96 visible_content_rect(), 1.f / contents_scale_x());
97 gfx::Size layer_size = paint_properties().bounds;
99 if (last_updated_visible_content_rect_ == visible_content_rect() &&
100 pile_->tiling_size() == layer_size && pending_invalidation_.IsEmpty()) {
101 // Only early out if the visible content rect of this layer hasn't changed.
105 TRACE_EVENT1("cc", "PictureLayer::Update",
106 "source_frame_number",
107 layer_tree_host()->source_frame_number());
108 devtools_instrumentation::ScopedLayerTreeTask update_layer(
109 devtools_instrumentation::kUpdateLayer, id(), layer_tree_host()->id());
111 // Calling paint in WebKit can sometimes cause invalidations, so save
112 // off the invalidation prior to calling update.
113 pending_invalidation_.Swap(&pile_invalidation_);
114 pending_invalidation_.Clear();
116 if (layer_tree_host()->settings().record_full_layer) {
117 // Workaround for http://crbug.com/235910 - to retain backwards compat
118 // the full page content must always be provided in the picture layer.
119 visible_layer_rect = gfx::Rect(layer_size);
122 // UpdateAndExpandInvalidation will give us an invalidation that covers
123 // anything not explicitly recorded in this frame. We give this region
124 // to the impl side so that it drops tiles that may not have a recording
128 pile_->UpdateAndExpandInvalidation(client_,
130 SafeOpaqueBackgroundColor(),
132 client_->FillsBoundsCompletely(),
135 update_source_frame_number_,
137 rendering_stats_instrumentation());
138 last_updated_visible_content_rect_ = visible_content_rect();
141 SetNeedsPushProperties();
143 // If this invalidation did not affect the pile, then it can be cleared as
145 pile_invalidation_.Clear();
151 void PictureLayer::SetIsMask(bool is_mask) {
155 Picture::RecordingMode PictureLayer::RecordingMode() const {
156 switch (layer_tree_host()->settings().recording_mode) {
157 case LayerTreeSettings::RecordNormally:
158 return Picture::RECORD_NORMALLY;
159 case LayerTreeSettings::RecordWithSkRecord:
160 return Picture::RECORD_WITH_SKRECORD;
163 return Picture::RECORD_NORMALLY;
166 bool PictureLayer::SupportsLCDText() const {
170 void PictureLayer::UpdateCanUseLCDText() {
171 if (can_use_lcd_text_last_frame_ == can_use_lcd_text())
174 can_use_lcd_text_last_frame_ = can_use_lcd_text();
176 client_->DidChangeLayerCanUseLCDText();
179 skia::RefPtr<SkPicture> PictureLayer::GetPicture() const {
180 // We could either flatten the PicturePile into a single SkPicture,
181 // or paint a fresh one depending on what we intend to do with the
182 // picture. For now we just paint a fresh one to get consistent results.
184 return skia::RefPtr<SkPicture>();
186 int width = bounds().width();
187 int height = bounds().height();
190 SkPictureRecorder recorder;
191 SkCanvas* canvas = recorder.beginRecording(width, height, NULL, 0);
192 client_->PaintContents(canvas,
193 gfx::Rect(width, height),
195 ContentLayerClient::GRAPHICS_CONTEXT_ENABLED);
196 skia::RefPtr<SkPicture> picture = skia::AdoptRef(recorder.endRecording());
200 bool PictureLayer::IsSuitableForGpuRasterization() const {
201 return pile_->is_suitable_for_gpu_rasterization();
204 void PictureLayer::ClearClient() {
206 UpdateDrawsContent(HasDrawableContent());
209 bool PictureLayer::HasDrawableContent() const {
210 return client_ && Layer::HasDrawableContent();
213 void PictureLayer::RunMicroBenchmark(MicroBenchmark* benchmark) {
214 benchmark->RunOnLayer(this);