blob: d86aef7f4910c3cc9c6b80178352e0991196dd92 [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"
[email protected]556fd292013-03-18 08:03:0410#include "cc/trees/layer_tree_impl.h"
[email protected]de14c2c2014-04-24 01:02:2511#include "third_party/skia/include/core/SkPictureRecorder.h"
bokanc79358522014-10-07 10:33:0512#include "ui/gfx/geometry/size_conversions.h"
[email protected]3621e182012-11-09 22:37:0913#include "ui/gfx/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),
23 pile_(make_scoped_refptr(new PicturePile())),
24 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
[email protected]eda36f02013-10-23 08:15:3140 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.
[email protected]83613c72014-07-12 00:02:5444 pile_->SetEmptyBounds();
[email protected]abe51342013-10-28 22:04:3845 } else if (update_source_frame_number_ ==
46 layer_tree_host()->source_frame_number()) {
[email protected]4c1598952014-04-16 02:54:4847 // TODO(ernstm): This DCHECK is only valid as long as the pile's tiling_rect
48 // is identical to the layer_rect.
[email protected]abe51342013-10-28 22:04:3849 // If update called, then pile size must match bounds pushed to impl layer.
bokanc79358522014-10-07 10:33:0550 DCHECK_EQ(gfx::ToCeiledSize(layer_impl->bounds()).ToString(),
51 pile_->tiling_size().ToString());
[email protected]eda36f02013-10-23 08:15:3152 }
53
[email protected]90536172013-05-14 00:23:0654 // Unlike other properties, invalidation must always be set on layer_impl.
55 // See PictureLayerImpl::PushPropertiesTo for more details.
[email protected]f117a4c2012-12-16 04:53:1056 layer_impl->invalidation_.Clear();
[email protected]d002dd02013-03-27 07:40:4057 layer_impl->invalidation_.Swap(&pile_invalidation_);
[email protected]59460132013-06-26 20:04:0258 layer_impl->pile_ = PicturePileImpl::CreateFromOther(pile_.get());
[email protected]3621e182012-11-09 22:37:0959}
60
[email protected]7aba6662013-03-12 10:17:3461void PictureLayer::SetLayerTreeHost(LayerTreeHost* host) {
62 Layer::SetLayerTreeHost(host);
[email protected]f974be62013-02-28 19:12:2663 if (host) {
[email protected]8e0176d2013-03-21 03:14:5264 pile_->SetMinContentsScale(host->settings().minimum_contents_scale);
65 pile_->SetTileGridSize(host->settings().default_tile_size);
[email protected]e6ac3a72013-03-13 03:50:5366 pile_->set_slow_down_raster_scale_factor(
[email protected]846f455b2013-03-18 19:07:4167 host->debug_state().slow_down_raster_scale_factor);
[email protected]334a7722013-04-04 11:51:5868 pile_->set_show_debug_picture_borders(
69 host->debug_state().show_picture_borders);
[email protected]f974be62013-02-28 19:12:2670 }
[email protected]ce37a152013-01-08 17:12:3371}
72
[email protected]7aba6662013-03-12 10:17:3473void PictureLayer::SetNeedsDisplayRect(const gfx::RectF& layer_rect) {
[email protected]3621e182012-11-09 22:37:0974 gfx::Rect rect = gfx::ToEnclosedRect(layer_rect);
[email protected]7a9fc132013-01-10 00:54:5875 if (!rect.IsEmpty()) {
76 // Clamp invalidation to the layer bounds.
77 rect.Intersect(gfx::Rect(bounds()));
78 pending_invalidation_.Union(rect);
79 }
[email protected]7aba6662013-03-12 10:17:3480 Layer::SetNeedsDisplayRect(layer_rect);
[email protected]3621e182012-11-09 22:37:0981}
82
[email protected]c50b997292013-08-03 18:44:3083bool PictureLayer::Update(ResourceUpdateQueue* queue,
[email protected]34ba1ffb2014-03-05 06:55:0384 const OcclusionTracker<Layer>* occlusion) {
[email protected]abe51342013-10-28 22:04:3885 update_source_frame_number_ = layer_tree_host()->source_frame_number();
[email protected]c50b997292013-08-03 18:44:3086 bool updated = Layer::Update(queue, occlusion);
87
[email protected]4ea293f72014-08-13 03:03:1788 {
89 base::AutoReset<bool> ignore_set_needs_commit(&ignore_set_needs_commit_,
90 true);
91 UpdateCanUseLCDText();
92 }
[email protected]36df0632014-06-12 20:26:1593
[email protected]4c1598952014-04-16 02:54:4894 gfx::Rect visible_layer_rect = gfx::ScaleToEnclosingRect(
95 visible_content_rect(), 1.f / contents_scale_x());
[email protected]a108f5db2014-07-16 06:10:2796 gfx::Size layer_size = paint_properties().bounds;
[email protected]4c1598952014-04-16 02:54:4897
[email protected]048ff152013-11-25 21:02:1198 if (last_updated_visible_content_rect_ == visible_content_rect() &&
[email protected]a108f5db2014-07-16 06:10:2799 pile_->tiling_size() == layer_size && pending_invalidation_.IsEmpty()) {
[email protected]048ff152013-11-25 21:02:11100 // Only early out if the visible content rect of this layer hasn't changed.
101 return updated;
102 }
103
[email protected]08ac6f9e2013-12-04 05:44:09104 TRACE_EVENT1("cc", "PictureLayer::Update",
105 "source_frame_number",
106 layer_tree_host()->source_frame_number());
[email protected]8af1ce12014-06-15 12:18:36107 devtools_instrumentation::ScopedLayerTreeTask update_layer(
108 devtools_instrumentation::kUpdateLayer, id(), layer_tree_host()->id());
[email protected]08ac6f9e2013-12-04 05:44:09109
[email protected]0e630ea32012-11-28 03:29:17110 // Calling paint in WebKit can sometimes cause invalidations, so save
111 // off the invalidation prior to calling update.
[email protected]78ad45542013-07-19 01:48:25112 pending_invalidation_.Swap(&pile_invalidation_);
[email protected]0e630ea32012-11-28 03:29:17113 pending_invalidation_.Clear();
114
[email protected]8a592802014-07-02 07:31:33115 if (layer_tree_host()->settings().record_full_layer) {
[email protected]c1079b532013-07-18 01:22:31116 // Workaround for http://crbug.com/235910 - to retain backwards compat
117 // the full page content must always be provided in the picture layer.
[email protected]a108f5db2014-07-16 06:10:27118 visible_layer_rect = gfx::Rect(layer_size);
[email protected]c1079b532013-07-18 01:22:31119 }
[email protected]8f322ba2014-06-18 23:48:18120
121 // UpdateAndExpandInvalidation will give us an invalidation that covers
122 // anything not explicitly recorded in this frame. We give this region
123 // to the impl side so that it drops tiles that may not have a recording
124 // for them.
[email protected]1d96e032014-03-25 05:59:08125 DCHECK(client_);
[email protected]8f322ba2014-06-18 23:48:18126 updated |=
127 pile_->UpdateAndExpandInvalidation(client_,
128 &pile_invalidation_,
129 SafeOpaqueBackgroundColor(),
130 contents_opaque(),
131 client_->FillsBoundsCompletely(),
[email protected]a108f5db2014-07-16 06:10:27132 layer_size,
[email protected]8f322ba2014-06-18 23:48:18133 visible_layer_rect,
134 update_source_frame_number_,
135 RecordingMode(),
136 rendering_stats_instrumentation());
[email protected]048ff152013-11-25 21:02:11137 last_updated_visible_content_rect_ = visible_content_rect();
138
[email protected]214c86972013-08-20 23:43:06139 if (updated) {
140 SetNeedsPushProperties();
141 } else {
[email protected]49304bde2013-07-08 21:31:22142 // If this invalidation did not affect the pile, then it can be cleared as
143 // an optimization.
144 pile_invalidation_.Clear();
145 }
[email protected]214c86972013-08-20 23:43:06146
[email protected]49304bde2013-07-08 21:31:22147 return updated;
[email protected]d98c0242012-11-08 06:22:35148}
149
[email protected]7aba6662013-03-12 10:17:34150void PictureLayer::SetIsMask(bool is_mask) {
danakj132fdfe2014-09-07 06:08:16151 pile_->set_is_mask(is_mask);
[email protected]f6776532012-12-21 20:24:33152}
153
[email protected]8b023d82014-05-06 13:49:46154Picture::RecordingMode PictureLayer::RecordingMode() const {
155 switch (layer_tree_host()->settings().recording_mode) {
156 case LayerTreeSettings::RecordNormally:
157 return Picture::RECORD_NORMALLY;
158 case LayerTreeSettings::RecordWithSkRecord:
159 return Picture::RECORD_WITH_SKRECORD;
160 }
161 NOTREACHED();
162 return Picture::RECORD_NORMALLY;
163}
164
[email protected]7924c1852013-05-24 16:18:43165bool PictureLayer::SupportsLCDText() const {
166 return true;
167}
168
[email protected]36df0632014-06-12 20:26:15169void PictureLayer::UpdateCanUseLCDText() {
170 if (can_use_lcd_text_last_frame_ == can_use_lcd_text())
171 return;
172
173 can_use_lcd_text_last_frame_ = can_use_lcd_text();
174 if (client_)
175 client_->DidChangeLayerCanUseLCDText();
176}
177
[email protected]f7837a92013-08-21 03:00:05178skia::RefPtr<SkPicture> PictureLayer::GetPicture() const {
179 // We could either flatten the PicturePile into a single SkPicture,
180 // or paint a fresh one depending on what we intend to do with the
181 // picture. For now we just paint a fresh one to get consistent results.
182 if (!DrawsContent())
183 return skia::RefPtr<SkPicture>();
184
185 int width = bounds().width();
186 int height = bounds().height();
[email protected]f7837a92013-08-21 03:00:05187
[email protected]e93fbdd2014-04-16 17:34:21188 SkPictureRecorder recorder;
[email protected]de14c2c2014-04-24 01:02:25189 SkCanvas* canvas = recorder.beginRecording(width, height, NULL, 0);
[email protected]276172b2014-05-02 21:03:03190 client_->PaintContents(canvas,
191 gfx::Rect(width, height),
[email protected]276172b2014-05-02 21:03:03192 ContentLayerClient::GRAPHICS_CONTEXT_ENABLED);
[email protected]e93fbdd2014-04-16 17:34:21193 skia::RefPtr<SkPicture> picture = skia::AdoptRef(recorder.endRecording());
[email protected]f7837a92013-08-21 03:00:05194 return picture;
195}
196
[email protected]a6c1b232014-05-05 23:53:08197bool PictureLayer::IsSuitableForGpuRasterization() const {
198 return pile_->is_suitable_for_gpu_rasterization();
199}
200
[email protected]ad63b2f2014-08-11 17:39:54201void PictureLayer::ClearClient() {
202 client_ = NULL;
203 UpdateDrawsContent(HasDrawableContent());
204}
205
206bool PictureLayer::HasDrawableContent() const {
207 return client_ && Layer::HasDrawableContent();
208}
209
[email protected]1a55d7dc2013-10-12 07:52:20210void PictureLayer::RunMicroBenchmark(MicroBenchmark* benchmark) {
211 benchmark->RunOnLayer(this);
212}
213
[email protected]d98c0242012-11-08 06:22:35214} // namespace cc