blob: e47f4b88d13e19d2992068bc06939abeab445031 [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]3621e182012-11-09 22:37:0910#include "ui/gfx/rect_conversions.h"
[email protected]d98c0242012-11-08 06:22:3511
12namespace cc {
13
[email protected]7aba6662013-03-12 10:17:3414scoped_refptr<PictureLayer> PictureLayer::Create(ContentLayerClient* client) {
[email protected]d98c0242012-11-08 06:22:3515 return make_scoped_refptr(new PictureLayer(client));
16}
17
[email protected]bf691c22013-03-26 21:15:0618PictureLayer::PictureLayer(ContentLayerClient* client)
[email protected]f6d55aa2014-03-11 20:42:5319 : client_(client),
20 pile_(make_scoped_refptr(new PicturePile())),
21 instrumentation_object_tracker_(id()),
22 is_mask_(false),
[email protected]6a41ea0c2014-04-10 15:12:1523 has_gpu_rasterization_hint_(TRIBOOL_UNKNOWN),
[email protected]f6d55aa2014-03-11 20:42:5324 update_source_frame_number_(-1) {}
[email protected]d98c0242012-11-08 06:22:3525
26PictureLayer::~PictureLayer() {
27}
28
[email protected]7aba6662013-03-12 10:17:3429bool PictureLayer::DrawsContent() const {
30 return Layer::DrawsContent() && client_;
[email protected]d98c0242012-11-08 06:22:3531}
32
[email protected]8c406cda32013-03-14 16:20:3233scoped_ptr<LayerImpl> PictureLayer::CreateLayerImpl(LayerTreeImpl* tree_impl) {
34 return PictureLayerImpl::Create(tree_impl, id()).PassAs<LayerImpl>();
[email protected]d98c0242012-11-08 06:22:3535}
36
[email protected]7aba6662013-03-12 10:17:3437void PictureLayer::PushPropertiesTo(LayerImpl* base_layer) {
38 Layer::PushPropertiesTo(base_layer);
[email protected]3621e182012-11-09 22:37:0939 PictureLayerImpl* layer_impl = static_cast<PictureLayerImpl*>(base_layer);
[email protected]23257682013-05-17 22:53:0340
[email protected]eda36f02013-10-23 08:15:3141 if (layer_impl->bounds().IsEmpty()) {
42 // Update may not get called for an empty layer, so resize here instead.
43 // Using layer_impl because either bounds() or paint_properties().bounds
44 // may disagree and either one could have been pushed to layer_impl.
[email protected]c736c1c202013-10-24 06:46:1945 pile_->Resize(gfx::Size());
[email protected]abe51342013-10-28 22:04:3846 } else if (update_source_frame_number_ ==
47 layer_tree_host()->source_frame_number()) {
48 // If update called, then pile size must match bounds pushed to impl layer.
49 DCHECK_EQ(layer_impl->bounds().ToString(), pile_->size().ToString());
[email protected]eda36f02013-10-23 08:15:3150 }
51
[email protected]f6776532012-12-21 20:24:3352 layer_impl->SetIsMask(is_mask_);
[email protected]6a41ea0c2014-04-10 15:12:1553 layer_impl->SetUseGpuRasterization(ShouldUseGpuRasterization());
[email protected]f6d55aa2014-03-11 20:42:5354
[email protected]90536172013-05-14 00:23:0655 // Unlike other properties, invalidation must always be set on layer_impl.
56 // See PictureLayerImpl::PushPropertiesTo for more details.
[email protected]f117a4c2012-12-16 04:53:1057 layer_impl->invalidation_.Clear();
[email protected]d002dd02013-03-27 07:40:4058 layer_impl->invalidation_.Swap(&pile_invalidation_);
[email protected]59460132013-06-26 20:04:0259 layer_impl->pile_ = PicturePileImpl::CreateFromOther(pile_.get());
[email protected]3621e182012-11-09 22:37:0960}
61
[email protected]7aba6662013-03-12 10:17:3462void PictureLayer::SetLayerTreeHost(LayerTreeHost* host) {
63 Layer::SetLayerTreeHost(host);
[email protected]f974be62013-02-28 19:12:2664 if (host) {
[email protected]8e0176d2013-03-21 03:14:5265 pile_->SetMinContentsScale(host->settings().minimum_contents_scale);
66 pile_->SetTileGridSize(host->settings().default_tile_size);
[email protected]e6ac3a72013-03-13 03:50:5367 pile_->set_slow_down_raster_scale_factor(
[email protected]846f455b2013-03-18 19:07:4168 host->debug_state().slow_down_raster_scale_factor);
[email protected]334a7722013-04-04 11:51:5869 pile_->set_show_debug_picture_borders(
70 host->debug_state().show_picture_borders);
[email protected]f974be62013-02-28 19:12:2671 }
[email protected]ce37a152013-01-08 17:12:3372}
73
[email protected]7aba6662013-03-12 10:17:3474void PictureLayer::SetNeedsDisplayRect(const gfx::RectF& layer_rect) {
[email protected]3621e182012-11-09 22:37:0975 gfx::Rect rect = gfx::ToEnclosedRect(layer_rect);
[email protected]7a9fc132013-01-10 00:54:5876 if (!rect.IsEmpty()) {
77 // Clamp invalidation to the layer bounds.
78 rect.Intersect(gfx::Rect(bounds()));
79 pending_invalidation_.Union(rect);
80 }
[email protected]7aba6662013-03-12 10:17:3481 Layer::SetNeedsDisplayRect(layer_rect);
[email protected]3621e182012-11-09 22:37:0982}
83
[email protected]c50b997292013-08-03 18:44:3084bool PictureLayer::Update(ResourceUpdateQueue* queue,
[email protected]34ba1ffb2014-03-05 06:55:0385 const OcclusionTracker<Layer>* occlusion) {
[email protected]abe51342013-10-28 22:04:3886 update_source_frame_number_ = layer_tree_host()->source_frame_number();
[email protected]c50b997292013-08-03 18:44:3087 bool updated = Layer::Update(queue, occlusion);
88
[email protected]048ff152013-11-25 21:02:1189 if (last_updated_visible_content_rect_ == visible_content_rect() &&
90 pile_->size() == paint_properties().bounds &&
91 pending_invalidation_.IsEmpty()) {
92 // Only early out if the visible content rect of this layer hasn't changed.
93 return updated;
94 }
95
[email protected]08ac6f9e2013-12-04 05:44:0996 TRACE_EVENT1("cc", "PictureLayer::Update",
97 "source_frame_number",
98 layer_tree_host()->source_frame_number());
99
[email protected]445881f2013-04-16 01:11:59100 pile_->Resize(paint_properties().bounds);
[email protected]0e630ea32012-11-28 03:29:17101
102 // Calling paint in WebKit can sometimes cause invalidations, so save
103 // off the invalidation prior to calling update.
[email protected]78ad45542013-07-19 01:48:25104 pending_invalidation_.Swap(&pile_invalidation_);
[email protected]0e630ea32012-11-28 03:29:17105 pending_invalidation_.Clear();
106
[email protected]935ba272013-05-20 03:50:21107 gfx::Rect visible_layer_rect = gfx::ScaleToEnclosingRect(
108 visible_content_rect(), 1.f / contents_scale_x());
[email protected]c1079b532013-07-18 01:22:31109 if (layer_tree_host()->settings().using_synchronous_renderer_compositor) {
110 // Workaround for http://crbug.com/235910 - to retain backwards compat
111 // the full page content must always be provided in the picture layer.
112 visible_layer_rect = gfx::Rect(bounds());
113 }
[email protected]1d96e032014-03-25 05:59:08114 DCHECK(client_);
[email protected]c50b997292013-08-03 18:44:30115 updated |= pile_->Update(client_,
116 SafeOpaqueBackgroundColor(),
117 contents_opaque(),
[email protected]1d96e032014-03-25 05:59:08118 client_->FillsBoundsCompletely(),
[email protected]c50b997292013-08-03 18:44:30119 pile_invalidation_,
120 visible_layer_rect,
[email protected]2db92252013-12-10 22:30:31121 update_source_frame_number_,
[email protected]c50b997292013-08-03 18:44:30122 rendering_stats_instrumentation());
[email protected]048ff152013-11-25 21:02:11123 last_updated_visible_content_rect_ = visible_content_rect();
124
[email protected]214c86972013-08-20 23:43:06125 if (updated) {
126 SetNeedsPushProperties();
127 } else {
[email protected]49304bde2013-07-08 21:31:22128 // If this invalidation did not affect the pile, then it can be cleared as
129 // an optimization.
130 pile_invalidation_.Clear();
131 }
[email protected]214c86972013-08-20 23:43:06132
[email protected]49304bde2013-07-08 21:31:22133 return updated;
[email protected]d98c0242012-11-08 06:22:35134}
135
[email protected]7aba6662013-03-12 10:17:34136void PictureLayer::SetIsMask(bool is_mask) {
[email protected]f6776532012-12-21 20:24:33137 is_mask_ = is_mask;
138}
139
[email protected]f6d55aa2014-03-11 20:42:53140void PictureLayer::SetHasGpuRasterizationHint(bool has_hint) {
[email protected]6a41ea0c2014-04-10 15:12:15141 switch (has_gpu_rasterization_hint_) {
142 case TRIBOOL_UNKNOWN: // Fall-through.
143 case TRIBOOL_TRUE:
144 has_gpu_rasterization_hint_ = has_hint ? TRIBOOL_TRUE : TRIBOOL_FALSE;
145 break;
146 case TRIBOOL_FALSE:
147 // GPU rasterization cannot be enabled once disabled.
148 // This is done to prevent frequent invalidations and visual flashing.
149 break;
150 default:
151 NOTREACHED();
152 }
153 // No need to set needs commit or push-properties.
154 // If only the hint changes and the layer is still valid, there is no need
155 // to invalidate the rasterization for the whole layer. If there is an
156 // invalidation (current or future) we will re-raster everything so that it
157 // is consistent across the layer.
158}
159
160bool PictureLayer::ShouldUseGpuRasterization() const {
161 switch (layer_tree_host()->settings().rasterization_site) {
162 case LayerTreeSettings::CpuRasterization:
163 return false;
164 case LayerTreeSettings::HybridRasterization:
165 return has_gpu_rasterization_hint_ == TRIBOOL_TRUE &&
166 pile_->is_suitable_for_gpu_rasterization();
167 case LayerTreeSettings::GpuRasterization:
168 return true;
169 }
170 NOTREACHED();
171 return false;
[email protected]f6d55aa2014-03-11 20:42:53172}
173
[email protected]7924c1852013-05-24 16:18:43174bool PictureLayer::SupportsLCDText() const {
175 return true;
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();
187 gfx::RectF opaque;
188
189 skia::RefPtr<SkPicture> picture = skia::AdoptRef(new SkPicture);
190 SkCanvas* canvas = picture->beginRecording(width, height);
191 client_->PaintContents(canvas, gfx::Rect(width, height), &opaque);
192 picture->endRecording();
193 return picture;
194}
195
[email protected]1a55d7dc2013-10-12 07:52:20196void PictureLayer::RunMicroBenchmark(MicroBenchmark* benchmark) {
197 benchmark->RunOnLayer(this);
198}
199
[email protected]d98c0242012-11-08 06:22:35200} // namespace cc