blob: 271f7226fceba2fee472b66dd852927165abec81 [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]f7837a92013-08-21 03:00:057#include "cc/layers/content_layer_client.h"
[email protected]cc3cfaa2013-03-18 09:05:528#include "cc/layers/picture_layer_impl.h"
[email protected]556fd292013-03-18 08:03:049#include "cc/trees/layer_tree_impl.h"
[email protected]de14c2c2014-04-24 01:02:2510#include "third_party/skia/include/core/SkPictureRecorder.h"
[email protected]3621e182012-11-09 22:37:0911#include "ui/gfx/rect_conversions.h"
[email protected]d98c0242012-11-08 06:22:3512
13namespace cc {
14
[email protected]7aba6662013-03-12 10:17:3415scoped_refptr<PictureLayer> PictureLayer::Create(ContentLayerClient* client) {
[email protected]d98c0242012-11-08 06:22:3516 return make_scoped_refptr(new PictureLayer(client));
17}
18
[email protected]bf691c22013-03-26 21:15:0619PictureLayer::PictureLayer(ContentLayerClient* client)
[email protected]f6d55aa2014-03-11 20:42:5320 : client_(client),
21 pile_(make_scoped_refptr(new PicturePile())),
22 instrumentation_object_tracker_(id()),
23 is_mask_(false),
[email protected]36df0632014-06-12 20:26:1524 update_source_frame_number_(-1),
25 can_use_lcd_text_last_frame_(can_use_lcd_text()) {
26}
[email protected]d98c0242012-11-08 06:22:3527
28PictureLayer::~PictureLayer() {
29}
30
[email protected]7aba6662013-03-12 10:17:3431bool PictureLayer::DrawsContent() const {
32 return Layer::DrawsContent() && client_;
[email protected]d98c0242012-11-08 06:22:3533}
34
[email protected]8c406cda32013-03-14 16:20:3235scoped_ptr<LayerImpl> PictureLayer::CreateLayerImpl(LayerTreeImpl* tree_impl) {
36 return PictureLayerImpl::Create(tree_impl, id()).PassAs<LayerImpl>();
[email protected]d98c0242012-11-08 06:22:3537}
38
[email protected]7aba6662013-03-12 10:17:3439void PictureLayer::PushPropertiesTo(LayerImpl* base_layer) {
40 Layer::PushPropertiesTo(base_layer);
[email protected]3621e182012-11-09 22:37:0941 PictureLayerImpl* layer_impl = static_cast<PictureLayerImpl*>(base_layer);
[email protected]23257682013-05-17 22:53:0342
[email protected]eda36f02013-10-23 08:15:3143 if (layer_impl->bounds().IsEmpty()) {
44 // Update may not get called for an empty layer, so resize here instead.
45 // Using layer_impl because either bounds() or paint_properties().bounds
46 // may disagree and either one could have been pushed to layer_impl.
[email protected]83613c72014-07-12 00:02:5447 pile_->SetEmptyBounds();
[email protected]abe51342013-10-28 22:04:3848 } else if (update_source_frame_number_ ==
49 layer_tree_host()->source_frame_number()) {
[email protected]4c1598952014-04-16 02:54:4850 // TODO(ernstm): This DCHECK is only valid as long as the pile's tiling_rect
51 // is identical to the layer_rect.
[email protected]abe51342013-10-28 22:04:3852 // If update called, then pile size must match bounds pushed to impl layer.
[email protected]4c1598952014-04-16 02:54:4853 DCHECK_EQ(layer_impl->bounds().ToString(),
54 pile_->tiling_rect().size().ToString());
[email protected]eda36f02013-10-23 08:15:3155 }
56
[email protected]f6776532012-12-21 20:24:3357 layer_impl->SetIsMask(is_mask_);
[email protected]f6d55aa2014-03-11 20:42:5358
[email protected]90536172013-05-14 00:23:0659 // Unlike other properties, invalidation must always be set on layer_impl.
60 // See PictureLayerImpl::PushPropertiesTo for more details.
[email protected]f117a4c2012-12-16 04:53:1061 layer_impl->invalidation_.Clear();
[email protected]d002dd02013-03-27 07:40:4062 layer_impl->invalidation_.Swap(&pile_invalidation_);
[email protected]59460132013-06-26 20:04:0263 layer_impl->pile_ = PicturePileImpl::CreateFromOther(pile_.get());
[email protected]3621e182012-11-09 22:37:0964}
65
[email protected]7aba6662013-03-12 10:17:3466void PictureLayer::SetLayerTreeHost(LayerTreeHost* host) {
67 Layer::SetLayerTreeHost(host);
[email protected]f974be62013-02-28 19:12:2668 if (host) {
[email protected]8e0176d2013-03-21 03:14:5269 pile_->SetMinContentsScale(host->settings().minimum_contents_scale);
70 pile_->SetTileGridSize(host->settings().default_tile_size);
[email protected]e6ac3a72013-03-13 03:50:5371 pile_->set_slow_down_raster_scale_factor(
[email protected]846f455b2013-03-18 19:07:4172 host->debug_state().slow_down_raster_scale_factor);
[email protected]334a7722013-04-04 11:51:5873 pile_->set_show_debug_picture_borders(
74 host->debug_state().show_picture_borders);
[email protected]f974be62013-02-28 19:12:2675 }
[email protected]ce37a152013-01-08 17:12:3376}
77
[email protected]7aba6662013-03-12 10:17:3478void PictureLayer::SetNeedsDisplayRect(const gfx::RectF& layer_rect) {
[email protected]3621e182012-11-09 22:37:0979 gfx::Rect rect = gfx::ToEnclosedRect(layer_rect);
[email protected]7a9fc132013-01-10 00:54:5880 if (!rect.IsEmpty()) {
81 // Clamp invalidation to the layer bounds.
82 rect.Intersect(gfx::Rect(bounds()));
83 pending_invalidation_.Union(rect);
84 }
[email protected]7aba6662013-03-12 10:17:3485 Layer::SetNeedsDisplayRect(layer_rect);
[email protected]3621e182012-11-09 22:37:0986}
87
[email protected]c50b997292013-08-03 18:44:3088bool PictureLayer::Update(ResourceUpdateQueue* queue,
[email protected]34ba1ffb2014-03-05 06:55:0389 const OcclusionTracker<Layer>* occlusion) {
[email protected]abe51342013-10-28 22:04:3890 update_source_frame_number_ = layer_tree_host()->source_frame_number();
[email protected]c50b997292013-08-03 18:44:3091 bool updated = Layer::Update(queue, occlusion);
92
[email protected]36df0632014-06-12 20:26:1593 UpdateCanUseLCDText();
94
[email protected]4c1598952014-04-16 02:54:4895 gfx::Rect visible_layer_rect = gfx::ScaleToEnclosingRect(
96 visible_content_rect(), 1.f / contents_scale_x());
97
98 gfx::Rect layer_rect = gfx::Rect(paint_properties().bounds);
99
[email protected]048ff152013-11-25 21:02:11100 if (last_updated_visible_content_rect_ == visible_content_rect() &&
[email protected]4c1598952014-04-16 02:54:48101 pile_->tiling_rect() == layer_rect && pending_invalidation_.IsEmpty()) {
[email protected]048ff152013-11-25 21:02:11102 // Only early out if the visible content rect of this layer hasn't changed.
103 return updated;
104 }
105
[email protected]08ac6f9e2013-12-04 05:44:09106 TRACE_EVENT1("cc", "PictureLayer::Update",
107 "source_frame_number",
108 layer_tree_host()->source_frame_number());
[email protected]8af1ce12014-06-15 12:18:36109 devtools_instrumentation::ScopedLayerTreeTask update_layer(
110 devtools_instrumentation::kUpdateLayer, id(), layer_tree_host()->id());
[email protected]08ac6f9e2013-12-04 05:44:09111
[email protected]0e630ea32012-11-28 03:29:17112 // Calling paint in WebKit can sometimes cause invalidations, so save
113 // off the invalidation prior to calling update.
[email protected]78ad45542013-07-19 01:48:25114 pending_invalidation_.Swap(&pile_invalidation_);
[email protected]0e630ea32012-11-28 03:29:17115 pending_invalidation_.Clear();
116
[email protected]8a592802014-07-02 07:31:33117 if (layer_tree_host()->settings().record_full_layer) {
[email protected]c1079b532013-07-18 01:22:31118 // Workaround for http://crbug.com/235910 - to retain backwards compat
119 // the full page content must always be provided in the picture layer.
120 visible_layer_rect = gfx::Rect(bounds());
121 }
[email protected]8f322ba2014-06-18 23:48:18122
123 // UpdateAndExpandInvalidation will give us an invalidation that covers
124 // anything not explicitly recorded in this frame. We give this region
125 // to the impl side so that it drops tiles that may not have a recording
126 // for them.
[email protected]1d96e032014-03-25 05:59:08127 DCHECK(client_);
[email protected]8f322ba2014-06-18 23:48:18128 updated |=
129 pile_->UpdateAndExpandInvalidation(client_,
130 &pile_invalidation_,
131 SafeOpaqueBackgroundColor(),
132 contents_opaque(),
133 client_->FillsBoundsCompletely(),
[email protected]83613c72014-07-12 00:02:54134 layer_rect,
[email protected]8f322ba2014-06-18 23:48:18135 visible_layer_rect,
136 update_source_frame_number_,
137 RecordingMode(),
138 rendering_stats_instrumentation());
[email protected]048ff152013-11-25 21:02:11139 last_updated_visible_content_rect_ = visible_content_rect();
140
[email protected]214c86972013-08-20 23:43:06141 if (updated) {
142 SetNeedsPushProperties();
143 } else {
[email protected]49304bde2013-07-08 21:31:22144 // If this invalidation did not affect the pile, then it can be cleared as
145 // an optimization.
146 pile_invalidation_.Clear();
147 }
[email protected]214c86972013-08-20 23:43:06148
[email protected]49304bde2013-07-08 21:31:22149 return updated;
[email protected]d98c0242012-11-08 06:22:35150}
151
[email protected]7aba6662013-03-12 10:17:34152void PictureLayer::SetIsMask(bool is_mask) {
[email protected]f6776532012-12-21 20:24:33153 is_mask_ = is_mask;
154}
155
[email protected]8b023d82014-05-06 13:49:46156Picture::RecordingMode PictureLayer::RecordingMode() const {
157 switch (layer_tree_host()->settings().recording_mode) {
158 case LayerTreeSettings::RecordNormally:
159 return Picture::RECORD_NORMALLY;
160 case LayerTreeSettings::RecordWithSkRecord:
161 return Picture::RECORD_WITH_SKRECORD;
162 }
163 NOTREACHED();
164 return Picture::RECORD_NORMALLY;
165}
166
[email protected]7924c1852013-05-24 16:18:43167bool PictureLayer::SupportsLCDText() const {
168 return true;
169}
170
[email protected]36df0632014-06-12 20:26:15171void PictureLayer::UpdateCanUseLCDText() {
172 if (can_use_lcd_text_last_frame_ == can_use_lcd_text())
173 return;
174
175 can_use_lcd_text_last_frame_ = can_use_lcd_text();
176 if (client_)
177 client_->DidChangeLayerCanUseLCDText();
178}
179
[email protected]f7837a92013-08-21 03:00:05180skia::RefPtr<SkPicture> PictureLayer::GetPicture() const {
181 // We could either flatten the PicturePile into a single SkPicture,
182 // or paint a fresh one depending on what we intend to do with the
183 // picture. For now we just paint a fresh one to get consistent results.
184 if (!DrawsContent())
185 return skia::RefPtr<SkPicture>();
186
187 int width = bounds().width();
188 int height = bounds().height();
189 gfx::RectF opaque;
190
[email protected]e93fbdd2014-04-16 17:34:21191 SkPictureRecorder recorder;
[email protected]de14c2c2014-04-24 01:02:25192 SkCanvas* canvas = recorder.beginRecording(width, height, NULL, 0);
[email protected]276172b2014-05-02 21:03:03193 client_->PaintContents(canvas,
194 gfx::Rect(width, height),
195 &opaque,
196 ContentLayerClient::GRAPHICS_CONTEXT_ENABLED);
[email protected]e93fbdd2014-04-16 17:34:21197 skia::RefPtr<SkPicture> picture = skia::AdoptRef(recorder.endRecording());
[email protected]f7837a92013-08-21 03:00:05198 return picture;
199}
200
[email protected]a6c1b232014-05-05 23:53:08201bool PictureLayer::IsSuitableForGpuRasterization() const {
202 return pile_->is_suitable_for_gpu_rasterization();
203}
204
[email protected]1a55d7dc2013-10-12 07:52:20205void PictureLayer::RunMicroBenchmark(MicroBenchmark* benchmark) {
206 benchmark->RunOnLayer(this);
207}
208
[email protected]d98c0242012-11-08 06:22:35209} // namespace cc