blob: 98e8dcb8e1ca619f9519eaa97a331f6927b4fe29 [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"
ajuma5e77f7d42014-11-27 14:19:1410#include "cc/resources/display_list_recording_source.h"
hendrikw312ee8ac2014-11-12 23:24:3411#include "cc/resources/picture_pile.h"
[email protected]556fd292013-03-18 08:03:0412#include "cc/trees/layer_tree_impl.h"
[email protected]de14c2c2014-04-24 01:02:2513#include "third_party/skia/include/core/SkPictureRecorder.h"
heejin.r.chungd28506ba2014-10-23 16:36:2014#include "ui/gfx/geometry/rect_conversions.h"
[email protected]d98c0242012-11-08 06:22:3515
16namespace cc {
17
[email protected]7aba6662013-03-12 10:17:3418scoped_refptr<PictureLayer> PictureLayer::Create(ContentLayerClient* client) {
[email protected]d98c0242012-11-08 06:22:3519 return make_scoped_refptr(new PictureLayer(client));
20}
21
[email protected]bf691c22013-03-26 21:15:0622PictureLayer::PictureLayer(ContentLayerClient* client)
[email protected]f6d55aa2014-03-11 20:42:5323 : client_(client),
[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),
danakj3f76ace2014-11-18 16:56:0026 can_use_lcd_text_for_update_(true),
jackhou24229612014-12-13 23:41:0027 is_mask_(false),
28 nearest_neighbor_(false) {
[email protected]36df0632014-06-12 20:26:1529}
[email protected]d98c0242012-11-08 06:22:3530
danakja819c2552014-11-14 02:05:0431PictureLayer::PictureLayer(ContentLayerClient* client,
32 scoped_ptr<RecordingSource> source)
33 : PictureLayer(client) {
34 recording_source_ = source.Pass();
35}
36
[email protected]d98c0242012-11-08 06:22:3537PictureLayer::~PictureLayer() {
38}
39
[email protected]8c406cda32013-03-14 16:20:3240scoped_ptr<LayerImpl> PictureLayer::CreateLayerImpl(LayerTreeImpl* tree_impl) {
danakja4ed6a22014-12-11 01:09:3841 return PictureLayerImpl::Create(tree_impl, id(), is_mask_);
[email protected]d98c0242012-11-08 06:22:3542}
43
[email protected]7aba6662013-03-12 10:17:3444void PictureLayer::PushPropertiesTo(LayerImpl* base_layer) {
45 Layer::PushPropertiesTo(base_layer);
[email protected]3621e182012-11-09 22:37:0946 PictureLayerImpl* layer_impl = static_cast<PictureLayerImpl*>(base_layer);
danakja4ed6a22014-12-11 01:09:3847 // TODO(danakj): Make is_mask_ a constructor parameter for PictureLayer.
danakjd193f6b2014-12-16 00:06:2348 DCHECK_EQ(layer_impl->is_mask(), is_mask_);
[email protected]23257682013-05-17 22:53:0349
danakj45377972014-11-06 22:05:4950 int source_frame_number = layer_tree_host()->source_frame_number();
51 gfx::Size impl_bounds = layer_impl->bounds();
hendrikw9d909aa72014-11-11 20:19:5252 gfx::Size recording_source_bounds = recording_source_->GetSize();
danakj45377972014-11-06 22:05:4953
hendrikw312ee8ac2014-11-12 23:24:3454 // If update called, then recording source size must match bounds pushed to
55 // impl layer.
danakj45377972014-11-06 22:05:4956 DCHECK_IMPLIES(update_source_frame_number_ == source_frame_number,
hendrikw9d909aa72014-11-11 20:19:5257 impl_bounds == recording_source_bounds)
hendrikw312ee8ac2014-11-12 23:24:3458 << " bounds " << impl_bounds.ToString() << " recording source "
hendrikw9d909aa72014-11-11 20:19:5259 << recording_source_bounds.ToString();
danakj45377972014-11-06 22:05:4960
61 if (update_source_frame_number_ != source_frame_number &&
hendrikw9d909aa72014-11-11 20:19:5262 recording_source_bounds != impl_bounds) {
danakj45377972014-11-06 22:05:4963 // Update may not get called for the layer (if it's not in the viewport
hendrikw312ee8ac2014-11-12 23:24:3464 // for example, even though it has resized making the recording source no
65 // longer valid. In this case just destroy the recording source.
hendrikw9d909aa72014-11-11 20:19:5266 recording_source_->SetEmptyBounds();
[email protected]eda36f02013-10-23 08:15:3167 }
68
jackhou24229612014-12-13 23:41:0069 layer_impl->SetNearestNeighbor(nearest_neighbor_);
70
hendrikw4e8c6322014-11-18 05:46:3271 scoped_refptr<RasterSource> raster_source =
72 recording_source_->CreateRasterSource();
73 raster_source->SetBackgoundColor(SafeOpaqueBackgroundColor());
74 raster_source->SetRequiresClear(!contents_opaque() &&
75 !client_->FillsBoundsCompletely());
danakja4ed6a22014-12-11 01:09:3876 layer_impl->UpdateRasterSource(raster_source, &recording_invalidation_,
77 nullptr);
78 DCHECK(recording_invalidation_.IsEmpty());
[email protected]3621e182012-11-09 22:37:0979}
80
[email protected]7aba6662013-03-12 10:17:3481void PictureLayer::SetLayerTreeHost(LayerTreeHost* host) {
82 Layer::SetLayerTreeHost(host);
[email protected]f974be62013-02-28 19:12:2683 if (host) {
ajuma5e77f7d42014-11-27 14:19:1484 if (!recording_source_) {
85 if (host->settings().use_display_lists) {
86 recording_source_.reset(new DisplayListRecordingSource);
87 } else {
88 recording_source_.reset(new PicturePile);
89 }
90 }
hendrikw9d909aa72014-11-11 20:19:5291 recording_source_->SetMinContentsScale(
92 host->settings().minimum_contents_scale);
93 recording_source_->SetTileGridSize(host->settings().default_tile_grid_size);
94 recording_source_->SetSlowdownRasterScaleFactor(
[email protected]846f455b2013-03-18 19:07:4195 host->debug_state().slow_down_raster_scale_factor);
[email protected]f974be62013-02-28 19:12:2696 }
[email protected]ce37a152013-01-08 17:12:3397}
98
danakj19f0c9e2014-10-11 03:24:4299void PictureLayer::SetNeedsDisplayRect(const gfx::Rect& layer_rect) {
100 if (!layer_rect.IsEmpty()) {
[email protected]7a9fc132013-01-10 00:54:58101 // Clamp invalidation to the layer bounds.
danakj19f0c9e2014-10-11 03:24:42102 pending_invalidation_.Union(
103 gfx::IntersectRects(layer_rect, gfx::Rect(bounds())));
[email protected]7a9fc132013-01-10 00:54:58104 }
[email protected]7aba6662013-03-12 10:17:34105 Layer::SetNeedsDisplayRect(layer_rect);
[email protected]3621e182012-11-09 22:37:09106}
107
[email protected]c50b997292013-08-03 18:44:30108bool PictureLayer::Update(ResourceUpdateQueue* queue,
[email protected]34ba1ffb2014-03-05 06:55:03109 const OcclusionTracker<Layer>* occlusion) {
[email protected]abe51342013-10-28 22:04:38110 update_source_frame_number_ = layer_tree_host()->source_frame_number();
[email protected]c50b997292013-08-03 18:44:30111 bool updated = Layer::Update(queue, occlusion);
112
danakj3f76ace2014-11-18 16:56:00113 bool can_use_lcd_text_changed = UpdateCanUseLCDText();
[email protected]36df0632014-06-12 20:26:15114
[email protected]4c1598952014-04-16 02:54:48115 gfx::Rect visible_layer_rect = gfx::ScaleToEnclosingRect(
116 visible_content_rect(), 1.f / contents_scale_x());
[email protected]a108f5db2014-07-16 06:10:27117 gfx::Size layer_size = paint_properties().bounds;
[email protected]4c1598952014-04-16 02:54:48118
[email protected]048ff152013-11-25 21:02:11119 if (last_updated_visible_content_rect_ == visible_content_rect() &&
danakj3f76ace2014-11-18 16:56:00120 recording_source_->GetSize() == layer_size && !can_use_lcd_text_changed &&
hendrikw9d909aa72014-11-11 20:19:52121 pending_invalidation_.IsEmpty()) {
[email protected]048ff152013-11-25 21:02:11122 // Only early out if the visible content rect of this layer hasn't changed.
123 return updated;
124 }
125
[email protected]08ac6f9e2013-12-04 05:44:09126 TRACE_EVENT1("cc", "PictureLayer::Update",
127 "source_frame_number",
128 layer_tree_host()->source_frame_number());
[email protected]8af1ce12014-06-15 12:18:36129 devtools_instrumentation::ScopedLayerTreeTask update_layer(
130 devtools_instrumentation::kUpdateLayer, id(), layer_tree_host()->id());
[email protected]08ac6f9e2013-12-04 05:44:09131
[email protected]0e630ea32012-11-28 03:29:17132 // Calling paint in WebKit can sometimes cause invalidations, so save
133 // off the invalidation prior to calling update.
hendrikw9d909aa72014-11-11 20:19:52134 pending_invalidation_.Swap(&recording_invalidation_);
[email protected]0e630ea32012-11-28 03:29:17135 pending_invalidation_.Clear();
136
[email protected]8a592802014-07-02 07:31:33137 if (layer_tree_host()->settings().record_full_layer) {
[email protected]c1079b532013-07-18 01:22:31138 // Workaround for http://crbug.com/235910 - to retain backwards compat
139 // the full page content must always be provided in the picture layer.
[email protected]a108f5db2014-07-16 06:10:27140 visible_layer_rect = gfx::Rect(layer_size);
[email protected]c1079b532013-07-18 01:22:31141 }
[email protected]8f322ba2014-06-18 23:48:18142
143 // UpdateAndExpandInvalidation will give us an invalidation that covers
144 // anything not explicitly recorded in this frame. We give this region
145 // to the impl side so that it drops tiles that may not have a recording
146 // for them.
[email protected]1d96e032014-03-25 05:59:08147 DCHECK(client_);
hendrikw9d909aa72014-11-11 20:19:52148 updated |= recording_source_->UpdateAndExpandInvalidation(
danakj3f76ace2014-11-18 16:56:00149 client_, &recording_invalidation_, can_use_lcd_text_for_update_,
150 layer_size, visible_layer_rect, update_source_frame_number_,
151 Picture::RECORD_NORMALLY);
[email protected]048ff152013-11-25 21:02:11152 last_updated_visible_content_rect_ = visible_content_rect();
153
[email protected]214c86972013-08-20 23:43:06154 if (updated) {
155 SetNeedsPushProperties();
156 } else {
hendrikw312ee8ac2014-11-12 23:24:34157 // If this invalidation did not affect the recording source, then it can be
158 // cleared as an optimization.
hendrikw9d909aa72014-11-11 20:19:52159 recording_invalidation_.Clear();
[email protected]49304bde2013-07-08 21:31:22160 }
[email protected]214c86972013-08-20 23:43:06161
[email protected]49304bde2013-07-08 21:31:22162 return updated;
[email protected]d98c0242012-11-08 06:22:35163}
164
[email protected]7aba6662013-03-12 10:17:34165void PictureLayer::SetIsMask(bool is_mask) {
hendrikw4e8c6322014-11-18 05:46:32166 is_mask_ = is_mask;
[email protected]f6776532012-12-21 20:24:33167}
168
[email protected]7924c1852013-05-24 16:18:43169bool PictureLayer::SupportsLCDText() const {
170 return true;
171}
172
danakj3f76ace2014-11-18 16:56:00173bool PictureLayer::UpdateCanUseLCDText() {
174 if (!can_use_lcd_text_for_update_)
175 return false; // Don't allow the LCD text state to change once disabled.
176 if (can_use_lcd_text_for_update_ == can_use_lcd_text())
177 return false;
[email protected]36df0632014-06-12 20:26:15178
danakj3f76ace2014-11-18 16:56:00179 can_use_lcd_text_for_update_ = can_use_lcd_text();
180 return true;
[email protected]36df0632014-06-12 20:26:15181}
182
[email protected]f7837a92013-08-21 03:00:05183skia::RefPtr<SkPicture> PictureLayer::GetPicture() const {
hendrikw312ee8ac2014-11-12 23:24:34184 // We could either flatten the RecordingSource into a single SkPicture,
[email protected]f7837a92013-08-21 03:00:05185 // or paint a fresh one depending on what we intend to do with the
186 // picture. For now we just paint a fresh one to get consistent results.
187 if (!DrawsContent())
188 return skia::RefPtr<SkPicture>();
189
190 int width = bounds().width();
191 int height = bounds().height();
[email protected]f7837a92013-08-21 03:00:05192
[email protected]e93fbdd2014-04-16 17:34:21193 SkPictureRecorder recorder;
kulkarni.a4015690f12014-10-10 13:50:06194 SkCanvas* canvas = recorder.beginRecording(width, height, nullptr, 0);
[email protected]276172b2014-05-02 21:03:03195 client_->PaintContents(canvas,
196 gfx::Rect(width, height),
[email protected]276172b2014-05-02 21:03:03197 ContentLayerClient::GRAPHICS_CONTEXT_ENABLED);
[email protected]e93fbdd2014-04-16 17:34:21198 skia::RefPtr<SkPicture> picture = skia::AdoptRef(recorder.endRecording());
[email protected]f7837a92013-08-21 03:00:05199 return picture;
200}
201
[email protected]a6c1b232014-05-05 23:53:08202bool PictureLayer::IsSuitableForGpuRasterization() const {
hendrikw9d909aa72014-11-11 20:19:52203 return recording_source_->IsSuitableForGpuRasterization();
[email protected]a6c1b232014-05-05 23:53:08204}
205
[email protected]ad63b2f2014-08-11 17:39:54206void PictureLayer::ClearClient() {
kulkarni.a4015690f12014-10-10 13:50:06207 client_ = nullptr;
[email protected]ad63b2f2014-08-11 17:39:54208 UpdateDrawsContent(HasDrawableContent());
209}
210
jackhou24229612014-12-13 23:41:00211void PictureLayer::SetNearestNeighbor(bool nearest_neighbor) {
212 if (nearest_neighbor_ == nearest_neighbor)
213 return;
214
215 nearest_neighbor_ = nearest_neighbor;
216 SetNeedsCommit();
217}
218
[email protected]ad63b2f2014-08-11 17:39:54219bool PictureLayer::HasDrawableContent() const {
220 return client_ && Layer::HasDrawableContent();
221}
222
[email protected]1a55d7dc2013-10-12 07:52:20223void PictureLayer::RunMicroBenchmark(MicroBenchmark* benchmark) {
224 benchmark->RunOnLayer(this);
225}
226
[email protected]d98c0242012-11-08 06:22:35227} // namespace cc