blob: aa366d563ae123f2dd8974eb73b0de04ecdd3b71 [file] [log] [blame]
[email protected]d98c0242012-11-08 06:22:351// 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
[email protected]cc3cfaa2013-03-18 09:05:525#include "cc/layers/picture_layer.h"
[email protected]f117a4c2012-12-16 04:53:106
[email protected]4ea293f72014-08-13 03:03:177#include "base/auto_reset.h"
[email protected]f7837a92013-08-21 03:00:058#include "cc/layers/content_layer_client.h"
[email protected]cc3cfaa2013-03-18 09:05:529#include "cc/layers/picture_layer_impl.h"
danakj920156852015-05-18 20:22:2910#include "cc/playback/display_list_recording_source.h"
11#include "cc/playback/picture_pile.h"
jamesr9b8fda32015-03-16 19:11:0512#include "cc/trees/layer_tree_host.h"
[email protected]556fd292013-03-18 08:03:0413#include "cc/trees/layer_tree_impl.h"
[email protected]de14c2c2014-04-24 01:02:2514#include "third_party/skia/include/core/SkPictureRecorder.h"
heejin.r.chungd28506ba2014-10-23 16:36:2015#include "ui/gfx/geometry/rect_conversions.h"
[email protected]d98c0242012-11-08 06:22:3516
17namespace cc {
18
loysoa6edaaff2015-05-25 03:26:4419scoped_refptr<PictureLayer> PictureLayer::Create(const LayerSettings& settings,
20 ContentLayerClient* client) {
21 return make_scoped_refptr(new PictureLayer(settings, client));
[email protected]d98c0242012-11-08 06:22:3522}
23
loysoa6edaaff2015-05-25 03:26:4424PictureLayer::PictureLayer(const LayerSettings& settings,
25 ContentLayerClient* client)
26 : Layer(settings),
27 client_(client),
[email protected]f6d55aa2014-03-11 20:42:5328 instrumentation_object_tracker_(id()),
[email protected]36df0632014-06-12 20:26:1529 update_source_frame_number_(-1),
jackhou24229612014-12-13 23:41:0030 is_mask_(false),
31 nearest_neighbor_(false) {
[email protected]36df0632014-06-12 20:26:1532}
[email protected]d98c0242012-11-08 06:22:3533
loysoa6edaaff2015-05-25 03:26:4434PictureLayer::PictureLayer(const LayerSettings& settings,
35 ContentLayerClient* client,
danakja819c2552014-11-14 02:05:0436 scoped_ptr<RecordingSource> source)
loysoa6edaaff2015-05-25 03:26:4437 : PictureLayer(settings, client) {
danakja819c2552014-11-14 02:05:0438 recording_source_ = source.Pass();
39}
40
[email protected]d98c0242012-11-08 06:22:3541PictureLayer::~PictureLayer() {
42}
43
[email protected]8c406cda32013-03-14 16:20:3244scoped_ptr<LayerImpl> PictureLayer::CreateLayerImpl(LayerTreeImpl* tree_impl) {
aeliasd0070ba2015-01-31 13:44:4945 return PictureLayerImpl::Create(tree_impl, id(), is_mask_,
46 new LayerImpl::SyncedScrollOffset);
[email protected]d98c0242012-11-08 06:22:3547}
48
[email protected]7aba6662013-03-12 10:17:3449void PictureLayer::PushPropertiesTo(LayerImpl* base_layer) {
50 Layer::PushPropertiesTo(base_layer);
[email protected]3621e182012-11-09 22:37:0951 PictureLayerImpl* layer_impl = static_cast<PictureLayerImpl*>(base_layer);
danakja4ed6a22014-12-11 01:09:3852 // TODO(danakj): Make is_mask_ a constructor parameter for PictureLayer.
danakjd193f6b2014-12-16 00:06:2353 DCHECK_EQ(layer_impl->is_mask(), is_mask_);
[email protected]23257682013-05-17 22:53:0354
danakj45377972014-11-06 22:05:4955 int source_frame_number = layer_tree_host()->source_frame_number();
56 gfx::Size impl_bounds = layer_impl->bounds();
hendrikw9d909aa72014-11-11 20:19:5257 gfx::Size recording_source_bounds = recording_source_->GetSize();
danakj45377972014-11-06 22:05:4958
hendrikw312ee8ac2014-11-12 23:24:3459 // If update called, then recording source size must match bounds pushed to
60 // impl layer.
danakj45377972014-11-06 22:05:4961 DCHECK_IMPLIES(update_source_frame_number_ == source_frame_number,
hendrikw9d909aa72014-11-11 20:19:5262 impl_bounds == recording_source_bounds)
hendrikw312ee8ac2014-11-12 23:24:3463 << " bounds " << impl_bounds.ToString() << " recording source "
hendrikw9d909aa72014-11-11 20:19:5264 << recording_source_bounds.ToString();
danakj45377972014-11-06 22:05:4965
66 if (update_source_frame_number_ != source_frame_number &&
hendrikw9d909aa72014-11-11 20:19:5267 recording_source_bounds != impl_bounds) {
danakj45377972014-11-06 22:05:4968 // Update may not get called for the layer (if it's not in the viewport
hendrikw312ee8ac2014-11-12 23:24:3469 // for example, even though it has resized making the recording source no
70 // longer valid. In this case just destroy the recording source.
hendrikw9d909aa72014-11-11 20:19:5271 recording_source_->SetEmptyBounds();
[email protected]eda36f02013-10-23 08:15:3172 }
73
jackhou24229612014-12-13 23:41:0074 layer_impl->SetNearestNeighbor(nearest_neighbor_);
75
enneaf5bda32015-02-19 01:27:3676 // Preserve lcd text settings from the current raster source.
77 bool can_use_lcd_text = layer_impl->RasterSourceUsesLCDText();
hendrikw4e8c6322014-11-18 05:46:3278 scoped_refptr<RasterSource> raster_source =
enneaf5bda32015-02-19 01:27:3679 recording_source_->CreateRasterSource(can_use_lcd_text);
hendrikw757ba9e2015-03-23 21:13:0980 layer_impl->set_gpu_raster_max_texture_size(
81 layer_tree_host()->device_viewport_size());
danakja4ed6a22014-12-11 01:09:3882 layer_impl->UpdateRasterSource(raster_source, &recording_invalidation_,
83 nullptr);
84 DCHECK(recording_invalidation_.IsEmpty());
[email protected]3621e182012-11-09 22:37:0985}
86
[email protected]7aba6662013-03-12 10:17:3487void PictureLayer::SetLayerTreeHost(LayerTreeHost* host) {
88 Layer::SetLayerTreeHost(host);
ennebdb6fd202015-03-10 06:01:5789 if (!host)
90 return;
91
danakj4bf88ec32015-05-05 23:15:5892 const LayerTreeSettings& settings = layer_tree_host()->settings();
ennebdb6fd202015-03-10 06:01:5793 if (!recording_source_) {
danakj4bf88ec32015-05-05 23:15:5894 if (settings.use_display_lists) {
jbroman16d628c2015-05-29 20:11:5995 recording_source_.reset(
96 new DisplayListRecordingSource(settings.default_tile_grid_size));
ennebdb6fd202015-03-10 06:01:5797 } else {
danakj4bf88ec32015-05-05 23:15:5898 recording_source_.reset(new PicturePile(settings.minimum_contents_scale,
99 settings.default_tile_grid_size));
ajuma5e77f7d42014-11-27 14:19:14100 }
[email protected]f974be62013-02-28 19:12:26101 }
ennebdb6fd202015-03-10 06:01:57102 recording_source_->SetSlowdownRasterScaleFactor(
103 host->debug_state().slow_down_raster_scale_factor);
danakj4bf88ec32015-05-05 23:15:58104 recording_source_->SetGatherPixelRefs(settings.gather_pixel_refs);
[email protected]ce37a152013-01-08 17:12:33105}
106
danakj19f0c9e2014-10-11 03:24:42107void PictureLayer::SetNeedsDisplayRect(const gfx::Rect& layer_rect) {
108 if (!layer_rect.IsEmpty()) {
[email protected]7a9fc132013-01-10 00:54:58109 // Clamp invalidation to the layer bounds.
danakj19f0c9e2014-10-11 03:24:42110 pending_invalidation_.Union(
111 gfx::IntersectRects(layer_rect, gfx::Rect(bounds())));
[email protected]7a9fc132013-01-10 00:54:58112 }
[email protected]7aba6662013-03-12 10:17:34113 Layer::SetNeedsDisplayRect(layer_rect);
[email protected]3621e182012-11-09 22:37:09114}
115
danakj5f46636a2015-06-19 00:01:40116bool PictureLayer::Update() {
[email protected]abe51342013-10-28 22:04:38117 update_source_frame_number_ = layer_tree_host()->source_frame_number();
danakj5f46636a2015-06-19 00:01:40118 bool updated = Layer::Update();
[email protected]c50b997292013-08-03 18:44:30119
danakj2c8d12c2015-06-18 06:15:33120 gfx::Rect visible_layer_rect = visible_content_rect();
[email protected]a108f5db2014-07-16 06:10:27121 gfx::Size layer_size = paint_properties().bounds;
[email protected]4c1598952014-04-16 02:54:48122
[email protected]048ff152013-11-25 21:02:11123 if (last_updated_visible_content_rect_ == visible_content_rect() &&
enneaf5bda32015-02-19 01:27:36124 recording_source_->GetSize() == layer_size &&
hendrikw9d909aa72014-11-11 20:19:52125 pending_invalidation_.IsEmpty()) {
[email protected]048ff152013-11-25 21:02:11126 // Only early out if the visible content rect of this layer hasn't changed.
127 return updated;
128 }
129
enneffe57812015-02-14 02:37:20130 recording_source_->SetBackgroundColor(SafeOpaqueBackgroundColor());
131 recording_source_->SetRequiresClear(!contents_opaque() &&
132 !client_->FillsBoundsCompletely());
133
[email protected]08ac6f9e2013-12-04 05:44:09134 TRACE_EVENT1("cc", "PictureLayer::Update",
135 "source_frame_number",
136 layer_tree_host()->source_frame_number());
[email protected]8af1ce12014-06-15 12:18:36137 devtools_instrumentation::ScopedLayerTreeTask update_layer(
138 devtools_instrumentation::kUpdateLayer, id(), layer_tree_host()->id());
[email protected]08ac6f9e2013-12-04 05:44:09139
[email protected]0e630ea32012-11-28 03:29:17140 // Calling paint in WebKit can sometimes cause invalidations, so save
141 // off the invalidation prior to calling update.
hendrikw9d909aa72014-11-11 20:19:52142 pending_invalidation_.Swap(&recording_invalidation_);
[email protected]0e630ea32012-11-28 03:29:17143 pending_invalidation_.Clear();
144
[email protected]8a592802014-07-02 07:31:33145 if (layer_tree_host()->settings().record_full_layer) {
[email protected]c1079b532013-07-18 01:22:31146 // Workaround for http://crbug.com/235910 - to retain backwards compat
147 // the full page content must always be provided in the picture layer.
[email protected]a108f5db2014-07-16 06:10:27148 visible_layer_rect = gfx::Rect(layer_size);
[email protected]c1079b532013-07-18 01:22:31149 }
[email protected]8f322ba2014-06-18 23:48:18150
151 // UpdateAndExpandInvalidation will give us an invalidation that covers
152 // anything not explicitly recorded in this frame. We give this region
153 // to the impl side so that it drops tiles that may not have a recording
154 // for them.
[email protected]1d96e032014-03-25 05:59:08155 DCHECK(client_);
hendrikw9d909aa72014-11-11 20:19:52156 updated |= recording_source_->UpdateAndExpandInvalidation(
enneaf5bda32015-02-19 01:27:36157 client_, &recording_invalidation_, layer_size, visible_layer_rect,
158 update_source_frame_number_, RecordingSource::RECORD_NORMALLY);
[email protected]048ff152013-11-25 21:02:11159 last_updated_visible_content_rect_ = visible_content_rect();
160
[email protected]214c86972013-08-20 23:43:06161 if (updated) {
162 SetNeedsPushProperties();
163 } else {
hendrikw312ee8ac2014-11-12 23:24:34164 // If this invalidation did not affect the recording source, then it can be
165 // cleared as an optimization.
hendrikw9d909aa72014-11-11 20:19:52166 recording_invalidation_.Clear();
[email protected]49304bde2013-07-08 21:31:22167 }
[email protected]214c86972013-08-20 23:43:06168
[email protected]49304bde2013-07-08 21:31:22169 return updated;
[email protected]d98c0242012-11-08 06:22:35170}
171
[email protected]7aba6662013-03-12 10:17:34172void PictureLayer::SetIsMask(bool is_mask) {
hendrikw4e8c6322014-11-18 05:46:32173 is_mask_ = is_mask;
[email protected]f6776532012-12-21 20:24:33174}
175
[email protected]f7837a92013-08-21 03:00:05176skia::RefPtr<SkPicture> PictureLayer::GetPicture() const {
hendrikw312ee8ac2014-11-12 23:24:34177 // We could either flatten the RecordingSource into a single SkPicture,
[email protected]f7837a92013-08-21 03:00:05178 // or paint a fresh one depending on what we intend to do with the
179 // picture. For now we just paint a fresh one to get consistent results.
180 if (!DrawsContent())
181 return skia::RefPtr<SkPicture>();
182
pan.deng0bd2a732015-04-01 03:22:05183 gfx::Size layer_size = bounds();
184 const LayerTreeSettings& settings = layer_tree_host()->settings();
185
186 if (settings.use_display_lists) {
187 scoped_ptr<RecordingSource> recording_source;
jbroman16d628c2015-05-29 20:11:59188 recording_source.reset(
189 new DisplayListRecordingSource(settings.default_tile_grid_size));
pan.deng0bd2a732015-04-01 03:22:05190 Region recording_invalidation;
191 recording_source->UpdateAndExpandInvalidation(
192 client_, &recording_invalidation, layer_size, gfx::Rect(layer_size),
193 update_source_frame_number_, RecordingSource::RECORD_NORMALLY);
194
195 scoped_refptr<RasterSource> raster_source =
196 recording_source->CreateRasterSource(false);
197
198 return raster_source->GetFlattenedPicture();
199 }
200
201 int width = layer_size.width();
202 int height = layer_size.height();
[email protected]f7837a92013-08-21 03:00:05203
[email protected]e93fbdd2014-04-16 17:34:21204 SkPictureRecorder recorder;
kulkarni.a4015690f12014-10-10 13:50:06205 SkCanvas* canvas = recorder.beginRecording(width, height, nullptr, 0);
schenney0154bfa2015-02-05 19:46:49206 client_->PaintContents(canvas, gfx::Rect(width, height),
207 ContentLayerClient::PAINTING_BEHAVIOR_NORMAL);
thestig3523bf82015-04-08 18:49:00208 skia::RefPtr<SkPicture> picture =
209 skia::AdoptRef(recorder.endRecordingAsPicture());
[email protected]f7837a92013-08-21 03:00:05210 return picture;
211}
212
[email protected]a6c1b232014-05-05 23:53:08213bool PictureLayer::IsSuitableForGpuRasterization() const {
hendrikw9d909aa72014-11-11 20:19:52214 return recording_source_->IsSuitableForGpuRasterization();
[email protected]a6c1b232014-05-05 23:53:08215}
216
[email protected]ad63b2f2014-08-11 17:39:54217void PictureLayer::ClearClient() {
kulkarni.a4015690f12014-10-10 13:50:06218 client_ = nullptr;
[email protected]ad63b2f2014-08-11 17:39:54219 UpdateDrawsContent(HasDrawableContent());
220}
221
jackhou24229612014-12-13 23:41:00222void PictureLayer::SetNearestNeighbor(bool nearest_neighbor) {
223 if (nearest_neighbor_ == nearest_neighbor)
224 return;
225
226 nearest_neighbor_ = nearest_neighbor;
227 SetNeedsCommit();
228}
229
[email protected]ad63b2f2014-08-11 17:39:54230bool PictureLayer::HasDrawableContent() const {
231 return client_ && Layer::HasDrawableContent();
232}
233
[email protected]1a55d7dc2013-10-12 07:52:20234void PictureLayer::RunMicroBenchmark(MicroBenchmark* benchmark) {
235 benchmark->RunOnLayer(this);
236}
237
[email protected]d98c0242012-11-08 06:22:35238} // namespace cc