blob: 5d45caed48099c77a8f232e8ac0dfe9d770e2f72 [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"
hendrikw312ee8ac2014-11-12 23:24:3410#include "cc/resources/picture_pile.h"
[email protected]556fd292013-03-18 08:03:0411#include "cc/trees/layer_tree_impl.h"
[email protected]de14c2c2014-04-24 01:02:2512#include "third_party/skia/include/core/SkPictureRecorder.h"
heejin.r.chungd28506ba2014-10-23 16:36:2013#include "ui/gfx/geometry/rect_conversions.h"
[email protected]d98c0242012-11-08 06:22:3514
15namespace cc {
16
[email protected]7aba6662013-03-12 10:17:3417scoped_refptr<PictureLayer> PictureLayer::Create(ContentLayerClient* client) {
[email protected]d98c0242012-11-08 06:22:3518 return make_scoped_refptr(new PictureLayer(client));
19}
20
[email protected]bf691c22013-03-26 21:15:0621PictureLayer::PictureLayer(ContentLayerClient* client)
[email protected]f6d55aa2014-03-11 20:42:5322 : client_(client),
hendrikw9d909aa72014-11-11 20:19:5223 recording_source_(new PicturePile),
[email protected]f6d55aa2014-03-11 20:42:5324 instrumentation_object_tracker_(id()),
[email protected]36df0632014-06-12 20:26:1525 update_source_frame_number_(-1),
26 can_use_lcd_text_last_frame_(can_use_lcd_text()) {
27}
[email protected]d98c0242012-11-08 06:22:3528
29PictureLayer::~PictureLayer() {
30}
31
[email protected]8c406cda32013-03-14 16:20:3232scoped_ptr<LayerImpl> PictureLayer::CreateLayerImpl(LayerTreeImpl* tree_impl) {
danakjf446a072014-09-27 21:55:4833 return PictureLayerImpl::Create(tree_impl, id());
[email protected]d98c0242012-11-08 06:22:3534}
35
[email protected]7aba6662013-03-12 10:17:3436void PictureLayer::PushPropertiesTo(LayerImpl* base_layer) {
37 Layer::PushPropertiesTo(base_layer);
[email protected]3621e182012-11-09 22:37:0938 PictureLayerImpl* layer_impl = static_cast<PictureLayerImpl*>(base_layer);
[email protected]23257682013-05-17 22:53:0339
danakj45377972014-11-06 22:05:4940 int source_frame_number = layer_tree_host()->source_frame_number();
41 gfx::Size impl_bounds = layer_impl->bounds();
hendrikw9d909aa72014-11-11 20:19:5242 gfx::Size recording_source_bounds = recording_source_->GetSize();
danakj45377972014-11-06 22:05:4943
hendrikw312ee8ac2014-11-12 23:24:3444 // If update called, then recording source size must match bounds pushed to
45 // impl layer.
danakj45377972014-11-06 22:05:4946 DCHECK_IMPLIES(update_source_frame_number_ == source_frame_number,
hendrikw9d909aa72014-11-11 20:19:5247 impl_bounds == recording_source_bounds)
hendrikw312ee8ac2014-11-12 23:24:3448 << " bounds " << impl_bounds.ToString() << " recording source "
hendrikw9d909aa72014-11-11 20:19:5249 << recording_source_bounds.ToString();
danakj45377972014-11-06 22:05:4950
51 if (update_source_frame_number_ != source_frame_number &&
hendrikw9d909aa72014-11-11 20:19:5252 recording_source_bounds != impl_bounds) {
danakj45377972014-11-06 22:05:4953 // Update may not get called for the layer (if it's not in the viewport
hendrikw312ee8ac2014-11-12 23:24:3454 // for example, even though it has resized making the recording source no
55 // longer valid. In this case just destroy the recording source.
hendrikw9d909aa72014-11-11 20:19:5256 recording_source_->SetEmptyBounds();
[email protected]eda36f02013-10-23 08:15:3157 }
58
[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();
hendrikw9d909aa72014-11-11 20:19:5262 layer_impl->invalidation_.Swap(&recording_invalidation_);
63 layer_impl->UpdateRasterSource(recording_source_->CreateRasterSource());
[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) {
hendrikw9d909aa72014-11-11 20:19:5269 // TODO(hendrikw): Perhaps use and initialization function to do this work.
70 recording_source_->SetMinContentsScale(
71 host->settings().minimum_contents_scale);
72 recording_source_->SetTileGridSize(host->settings().default_tile_grid_size);
73 recording_source_->SetSlowdownRasterScaleFactor(
[email protected]846f455b2013-03-18 19:07:4174 host->debug_state().slow_down_raster_scale_factor);
[email protected]f974be62013-02-28 19:12:2675 }
[email protected]ce37a152013-01-08 17:12:3376}
77
danakj19f0c9e2014-10-11 03:24:4278void PictureLayer::SetNeedsDisplayRect(const gfx::Rect& layer_rect) {
79 if (!layer_rect.IsEmpty()) {
[email protected]7a9fc132013-01-10 00:54:5880 // Clamp invalidation to the layer bounds.
danakj19f0c9e2014-10-11 03:24:4281 pending_invalidation_.Union(
82 gfx::IntersectRects(layer_rect, gfx::Rect(bounds())));
[email protected]7a9fc132013-01-10 00:54:5883 }
[email protected]7aba6662013-03-12 10:17:3484 Layer::SetNeedsDisplayRect(layer_rect);
[email protected]3621e182012-11-09 22:37:0985}
86
[email protected]c50b997292013-08-03 18:44:3087bool PictureLayer::Update(ResourceUpdateQueue* queue,
[email protected]34ba1ffb2014-03-05 06:55:0388 const OcclusionTracker<Layer>* occlusion) {
[email protected]abe51342013-10-28 22:04:3889 update_source_frame_number_ = layer_tree_host()->source_frame_number();
[email protected]c50b997292013-08-03 18:44:3090 bool updated = Layer::Update(queue, occlusion);
91
[email protected]4ea293f72014-08-13 03:03:1792 {
93 base::AutoReset<bool> ignore_set_needs_commit(&ignore_set_needs_commit_,
94 true);
95 UpdateCanUseLCDText();
96 }
[email protected]36df0632014-06-12 20:26:1597
[email protected]4c1598952014-04-16 02:54:4898 gfx::Rect visible_layer_rect = gfx::ScaleToEnclosingRect(
99 visible_content_rect(), 1.f / contents_scale_x());
[email protected]a108f5db2014-07-16 06:10:27100 gfx::Size layer_size = paint_properties().bounds;
[email protected]4c1598952014-04-16 02:54:48101
[email protected]048ff152013-11-25 21:02:11102 if (last_updated_visible_content_rect_ == visible_content_rect() &&
hendrikw9d909aa72014-11-11 20:19:52103 recording_source_->GetSize() == layer_size &&
104 pending_invalidation_.IsEmpty()) {
[email protected]048ff152013-11-25 21:02:11105 // Only early out if the visible content rect of this layer hasn't changed.
106 return updated;
107 }
108
[email protected]08ac6f9e2013-12-04 05:44:09109 TRACE_EVENT1("cc", "PictureLayer::Update",
110 "source_frame_number",
111 layer_tree_host()->source_frame_number());
[email protected]8af1ce12014-06-15 12:18:36112 devtools_instrumentation::ScopedLayerTreeTask update_layer(
113 devtools_instrumentation::kUpdateLayer, id(), layer_tree_host()->id());
[email protected]08ac6f9e2013-12-04 05:44:09114
[email protected]0e630ea32012-11-28 03:29:17115 // Calling paint in WebKit can sometimes cause invalidations, so save
116 // off the invalidation prior to calling update.
hendrikw9d909aa72014-11-11 20:19:52117 pending_invalidation_.Swap(&recording_invalidation_);
[email protected]0e630ea32012-11-28 03:29:17118 pending_invalidation_.Clear();
119
[email protected]8a592802014-07-02 07:31:33120 if (layer_tree_host()->settings().record_full_layer) {
[email protected]c1079b532013-07-18 01:22:31121 // Workaround for http://crbug.com/235910 - to retain backwards compat
122 // the full page content must always be provided in the picture layer.
[email protected]a108f5db2014-07-16 06:10:27123 visible_layer_rect = gfx::Rect(layer_size);
[email protected]c1079b532013-07-18 01:22:31124 }
[email protected]8f322ba2014-06-18 23:48:18125
126 // UpdateAndExpandInvalidation will give us an invalidation that covers
127 // anything not explicitly recorded in this frame. We give this region
128 // to the impl side so that it drops tiles that may not have a recording
129 // for them.
[email protected]1d96e032014-03-25 05:59:08130 DCHECK(client_);
hendrikw9d909aa72014-11-11 20:19:52131 updated |= recording_source_->UpdateAndExpandInvalidation(
132 client_, &recording_invalidation_, SafeOpaqueBackgroundColor(),
hendrikw3f7c2342014-11-08 01:59:28133 contents_opaque(), client_->FillsBoundsCompletely(), layer_size,
134 visible_layer_rect, update_source_frame_number_,
135 Picture::RECORD_NORMALLY);
[email protected]048ff152013-11-25 21:02:11136 last_updated_visible_content_rect_ = visible_content_rect();
137
[email protected]214c86972013-08-20 23:43:06138 if (updated) {
139 SetNeedsPushProperties();
140 } else {
hendrikw312ee8ac2014-11-12 23:24:34141 // If this invalidation did not affect the recording source, then it can be
142 // cleared as an optimization.
hendrikw9d909aa72014-11-11 20:19:52143 recording_invalidation_.Clear();
[email protected]49304bde2013-07-08 21:31:22144 }
[email protected]214c86972013-08-20 23:43:06145
[email protected]49304bde2013-07-08 21:31:22146 return updated;
[email protected]d98c0242012-11-08 06:22:35147}
148
[email protected]7aba6662013-03-12 10:17:34149void PictureLayer::SetIsMask(bool is_mask) {
hendrikw9d909aa72014-11-11 20:19:52150 recording_source_->SetIsMask(is_mask);
[email protected]f6776532012-12-21 20:24:33151}
152
[email protected]7924c1852013-05-24 16:18:43153bool PictureLayer::SupportsLCDText() const {
154 return true;
155}
156
[email protected]36df0632014-06-12 20:26:15157void PictureLayer::UpdateCanUseLCDText() {
158 if (can_use_lcd_text_last_frame_ == can_use_lcd_text())
159 return;
160
161 can_use_lcd_text_last_frame_ = can_use_lcd_text();
162 if (client_)
163 client_->DidChangeLayerCanUseLCDText();
164}
165
[email protected]f7837a92013-08-21 03:00:05166skia::RefPtr<SkPicture> PictureLayer::GetPicture() const {
hendrikw312ee8ac2014-11-12 23:24:34167 // We could either flatten the RecordingSource into a single SkPicture,
[email protected]f7837a92013-08-21 03:00:05168 // or paint a fresh one depending on what we intend to do with the
169 // picture. For now we just paint a fresh one to get consistent results.
170 if (!DrawsContent())
171 return skia::RefPtr<SkPicture>();
172
173 int width = bounds().width();
174 int height = bounds().height();
[email protected]f7837a92013-08-21 03:00:05175
[email protected]e93fbdd2014-04-16 17:34:21176 SkPictureRecorder recorder;
kulkarni.a4015690f12014-10-10 13:50:06177 SkCanvas* canvas = recorder.beginRecording(width, height, nullptr, 0);
[email protected]276172b2014-05-02 21:03:03178 client_->PaintContents(canvas,
179 gfx::Rect(width, height),
[email protected]276172b2014-05-02 21:03:03180 ContentLayerClient::GRAPHICS_CONTEXT_ENABLED);
[email protected]e93fbdd2014-04-16 17:34:21181 skia::RefPtr<SkPicture> picture = skia::AdoptRef(recorder.endRecording());
[email protected]f7837a92013-08-21 03:00:05182 return picture;
183}
184
[email protected]a6c1b232014-05-05 23:53:08185bool PictureLayer::IsSuitableForGpuRasterization() const {
hendrikw9d909aa72014-11-11 20:19:52186 return recording_source_->IsSuitableForGpuRasterization();
[email protected]a6c1b232014-05-05 23:53:08187}
188
[email protected]ad63b2f2014-08-11 17:39:54189void PictureLayer::ClearClient() {
kulkarni.a4015690f12014-10-10 13:50:06190 client_ = nullptr;
[email protected]ad63b2f2014-08-11 17:39:54191 UpdateDrawsContent(HasDrawableContent());
192}
193
194bool PictureLayer::HasDrawableContent() const {
195 return client_ && Layer::HasDrawableContent();
196}
197
[email protected]1a55d7dc2013-10-12 07:52:20198void PictureLayer::RunMicroBenchmark(MicroBenchmark* benchmark) {
199 benchmark->RunOnLayer(this);
200}
201
[email protected]d98c0242012-11-08 06:22:35202} // namespace cc