1 // 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.
5 #include "cc/layers/picture_layer.h"
7 #include "cc/layers/content_layer_client.h"
8 #include "cc/layers/picture_layer_impl.h"
9 #include "cc/trees/layer_tree_impl.h"
10 #include "third_party/skia/include/core/SkPictureRecorder.h"
11 #include "ui/gfx/rect_conversions.h"
15 scoped_refptr
<PictureLayer
> PictureLayer::Create(ContentLayerClient
* client
) {
16 return make_scoped_refptr(new PictureLayer(client
));
19 PictureLayer::PictureLayer(ContentLayerClient
* client
)
21 pile_(make_scoped_refptr(new PicturePile())),
22 instrumentation_object_tracker_(id()),
24 update_source_frame_number_(-1),
25 can_use_lcd_text_last_frame_(can_use_lcd_text()) {
28 PictureLayer::~PictureLayer() {
31 bool PictureLayer::DrawsContent() const {
32 return Layer::DrawsContent() && client_
;
35 scoped_ptr
<LayerImpl
> PictureLayer::CreateLayerImpl(LayerTreeImpl
* tree_impl
) {
36 return PictureLayerImpl::Create(tree_impl
, id()).PassAs
<LayerImpl
>();
39 void PictureLayer::PushPropertiesTo(LayerImpl
* base_layer
) {
40 Layer::PushPropertiesTo(base_layer
);
41 PictureLayerImpl
* layer_impl
= static_cast<PictureLayerImpl
*>(base_layer
);
43 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.
47 pile_
->SetEmptyBounds();
48 } else if (update_source_frame_number_
==
49 layer_tree_host()->source_frame_number()) {
50 // TODO(ernstm): This DCHECK is only valid as long as the pile's tiling_rect
51 // is identical to the layer_rect.
52 // If update called, then pile size must match bounds pushed to impl layer.
53 DCHECK_EQ(layer_impl
->bounds().ToString(), pile_
->tiling_size().ToString());
56 layer_impl
->SetIsMask(is_mask_
);
58 // Unlike other properties, invalidation must always be set on layer_impl.
59 // See PictureLayerImpl::PushPropertiesTo for more details.
60 layer_impl
->invalidation_
.Clear();
61 layer_impl
->invalidation_
.Swap(&pile_invalidation_
);
62 layer_impl
->pile_
= PicturePileImpl::CreateFromOther(pile_
.get());
65 void PictureLayer::SetLayerTreeHost(LayerTreeHost
* host
) {
66 Layer::SetLayerTreeHost(host
);
68 pile_
->SetMinContentsScale(host
->settings().minimum_contents_scale
);
69 pile_
->SetTileGridSize(host
->settings().default_tile_size
);
70 pile_
->set_slow_down_raster_scale_factor(
71 host
->debug_state().slow_down_raster_scale_factor
);
72 pile_
->set_show_debug_picture_borders(
73 host
->debug_state().show_picture_borders
);
77 void PictureLayer::SetNeedsDisplayRect(const gfx::RectF
& layer_rect
) {
78 gfx::Rect rect
= gfx::ToEnclosedRect(layer_rect
);
79 if (!rect
.IsEmpty()) {
80 // Clamp invalidation to the layer bounds.
81 rect
.Intersect(gfx::Rect(bounds()));
82 pending_invalidation_
.Union(rect
);
84 Layer::SetNeedsDisplayRect(layer_rect
);
87 bool PictureLayer::Update(ResourceUpdateQueue
* queue
,
88 const OcclusionTracker
<Layer
>* occlusion
) {
89 update_source_frame_number_
= layer_tree_host()->source_frame_number();
90 bool updated
= Layer::Update(queue
, occlusion
);
92 UpdateCanUseLCDText();
94 gfx::Rect visible_layer_rect
= gfx::ScaleToEnclosingRect(
95 visible_content_rect(), 1.f
/ contents_scale_x());
96 gfx::Size layer_size
= paint_properties().bounds
;
98 if (last_updated_visible_content_rect_
== visible_content_rect() &&
99 pile_
->tiling_size() == layer_size
&& pending_invalidation_
.IsEmpty()) {
100 // Only early out if the visible content rect of this layer hasn't changed.
104 TRACE_EVENT1("cc", "PictureLayer::Update",
105 "source_frame_number",
106 layer_tree_host()->source_frame_number());
107 devtools_instrumentation::ScopedLayerTreeTask
update_layer(
108 devtools_instrumentation::kUpdateLayer
, id(), layer_tree_host()->id());
110 // Calling paint in WebKit can sometimes cause invalidations, so save
111 // off the invalidation prior to calling update.
112 pending_invalidation_
.Swap(&pile_invalidation_
);
113 pending_invalidation_
.Clear();
115 if (layer_tree_host()->settings().record_full_layer
) {
116 // Workaround for http://crbug.com/235910 - to retain backwards compat
117 // the full page content must always be provided in the picture layer.
118 visible_layer_rect
= gfx::Rect(layer_size
);
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
127 pile_
->UpdateAndExpandInvalidation(client_
,
129 SafeOpaqueBackgroundColor(),
131 client_
->FillsBoundsCompletely(),
134 update_source_frame_number_
,
136 rendering_stats_instrumentation());
137 last_updated_visible_content_rect_
= visible_content_rect();
140 SetNeedsPushProperties();
142 // If this invalidation did not affect the pile, then it can be cleared as
144 pile_invalidation_
.Clear();
150 void PictureLayer::SetIsMask(bool is_mask
) {
154 Picture::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
;
162 return Picture::RECORD_NORMALLY
;
165 bool PictureLayer::SupportsLCDText() const {
169 void PictureLayer::UpdateCanUseLCDText() {
170 if (can_use_lcd_text_last_frame_
== can_use_lcd_text())
173 can_use_lcd_text_last_frame_
= can_use_lcd_text();
175 client_
->DidChangeLayerCanUseLCDText();
178 skia::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.
183 return skia::RefPtr
<SkPicture
>();
185 int width
= bounds().width();
186 int height
= bounds().height();
189 SkPictureRecorder recorder
;
190 SkCanvas
* canvas
= recorder
.beginRecording(width
, height
, NULL
, 0);
191 client_
->PaintContents(canvas
,
192 gfx::Rect(width
, height
),
194 ContentLayerClient::GRAPHICS_CONTEXT_ENABLED
);
195 skia::RefPtr
<SkPicture
> picture
= skia::AdoptRef(recorder
.endRecording());
199 bool PictureLayer::IsSuitableForGpuRasterization() const {
200 return pile_
->is_suitable_for_gpu_rasterization();
203 void PictureLayer::RunMicroBenchmark(MicroBenchmark
* benchmark
) {
204 benchmark
->RunOnLayer(this);