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 "base/auto_reset.h"
8 #include "cc/layers/content_layer_client.h"
9 #include "cc/layers/picture_layer_impl.h"
10 #include "cc/resources/picture_pile.h"
11 #include "cc/trees/layer_tree_impl.h"
12 #include "third_party/skia/include/core/SkPictureRecorder.h"
13 #include "ui/gfx/geometry/rect_conversions.h"
17 scoped_refptr
<PictureLayer
> PictureLayer::Create(ContentLayerClient
* client
) {
18 return make_scoped_refptr(new PictureLayer(client
));
21 PictureLayer::PictureLayer(ContentLayerClient
* client
)
23 recording_source_(new PicturePile
),
24 instrumentation_object_tracker_(id()),
25 update_source_frame_number_(-1),
26 can_use_lcd_text_for_update_(true),
30 PictureLayer::PictureLayer(ContentLayerClient
* client
,
31 scoped_ptr
<RecordingSource
> source
)
32 : PictureLayer(client
) {
33 recording_source_
= source
.Pass();
36 PictureLayer::~PictureLayer() {
39 scoped_ptr
<LayerImpl
> PictureLayer::CreateLayerImpl(LayerTreeImpl
* tree_impl
) {
40 return PictureLayerImpl::Create(tree_impl
, id());
43 void PictureLayer::PushPropertiesTo(LayerImpl
* base_layer
) {
44 Layer::PushPropertiesTo(base_layer
);
45 PictureLayerImpl
* layer_impl
= static_cast<PictureLayerImpl
*>(base_layer
);
47 int source_frame_number
= layer_tree_host()->source_frame_number();
48 gfx::Size impl_bounds
= layer_impl
->bounds();
49 gfx::Size recording_source_bounds
= recording_source_
->GetSize();
51 // If update called, then recording source size must match bounds pushed to
53 DCHECK_IMPLIES(update_source_frame_number_
== source_frame_number
,
54 impl_bounds
== recording_source_bounds
)
55 << " bounds " << impl_bounds
.ToString() << " recording source "
56 << recording_source_bounds
.ToString();
58 if (update_source_frame_number_
!= source_frame_number
&&
59 recording_source_bounds
!= impl_bounds
) {
60 // Update may not get called for the layer (if it's not in the viewport
61 // for example, even though it has resized making the recording source no
62 // longer valid. In this case just destroy the recording source.
63 recording_source_
->SetEmptyBounds();
66 // Unlike other properties, invalidation must always be set on layer_impl.
67 // See PictureLayerImpl::PushPropertiesTo for more details.
68 layer_impl
->invalidation_
.Clear();
69 layer_impl
->invalidation_
.Swap(&recording_invalidation_
);
70 layer_impl
->set_is_mask(is_mask_
);
71 scoped_refptr
<RasterSource
> raster_source
=
72 recording_source_
->CreateRasterSource();
73 raster_source
->SetBackgoundColor(SafeOpaqueBackgroundColor());
74 raster_source
->SetRequiresClear(!contents_opaque() &&
75 !client_
->FillsBoundsCompletely());
76 layer_impl
->UpdateRasterSource(raster_source
);
79 void PictureLayer::SetLayerTreeHost(LayerTreeHost
* host
) {
80 Layer::SetLayerTreeHost(host
);
82 recording_source_
->SetMinContentsScale(
83 host
->settings().minimum_contents_scale
);
84 recording_source_
->SetTileGridSize(host
->settings().default_tile_grid_size
);
85 recording_source_
->SetSlowdownRasterScaleFactor(
86 host
->debug_state().slow_down_raster_scale_factor
);
90 void PictureLayer::SetNeedsDisplayRect(const gfx::Rect
& layer_rect
) {
91 if (!layer_rect
.IsEmpty()) {
92 // Clamp invalidation to the layer bounds.
93 pending_invalidation_
.Union(
94 gfx::IntersectRects(layer_rect
, gfx::Rect(bounds())));
96 Layer::SetNeedsDisplayRect(layer_rect
);
99 bool PictureLayer::Update(ResourceUpdateQueue
* queue
,
100 const OcclusionTracker
<Layer
>* occlusion
) {
101 update_source_frame_number_
= layer_tree_host()->source_frame_number();
102 bool updated
= Layer::Update(queue
, occlusion
);
104 bool can_use_lcd_text_changed
= UpdateCanUseLCDText();
106 gfx::Rect visible_layer_rect
= gfx::ScaleToEnclosingRect(
107 visible_content_rect(), 1.f
/ contents_scale_x());
108 gfx::Size layer_size
= paint_properties().bounds
;
110 if (last_updated_visible_content_rect_
== visible_content_rect() &&
111 recording_source_
->GetSize() == layer_size
&& !can_use_lcd_text_changed
&&
112 pending_invalidation_
.IsEmpty()) {
113 // Only early out if the visible content rect of this layer hasn't changed.
117 TRACE_EVENT1("cc", "PictureLayer::Update",
118 "source_frame_number",
119 layer_tree_host()->source_frame_number());
120 devtools_instrumentation::ScopedLayerTreeTask
update_layer(
121 devtools_instrumentation::kUpdateLayer
, id(), layer_tree_host()->id());
123 // Calling paint in WebKit can sometimes cause invalidations, so save
124 // off the invalidation prior to calling update.
125 pending_invalidation_
.Swap(&recording_invalidation_
);
126 pending_invalidation_
.Clear();
128 if (layer_tree_host()->settings().record_full_layer
) {
129 // Workaround for http://crbug.com/235910 - to retain backwards compat
130 // the full page content must always be provided in the picture layer.
131 visible_layer_rect
= gfx::Rect(layer_size
);
134 // UpdateAndExpandInvalidation will give us an invalidation that covers
135 // anything not explicitly recorded in this frame. We give this region
136 // to the impl side so that it drops tiles that may not have a recording
139 updated
|= recording_source_
->UpdateAndExpandInvalidation(
140 client_
, &recording_invalidation_
, can_use_lcd_text_for_update_
,
141 layer_size
, visible_layer_rect
, update_source_frame_number_
,
142 Picture::RECORD_NORMALLY
);
143 last_updated_visible_content_rect_
= visible_content_rect();
146 SetNeedsPushProperties();
148 // If this invalidation did not affect the recording source, then it can be
149 // cleared as an optimization.
150 recording_invalidation_
.Clear();
156 void PictureLayer::SetIsMask(bool is_mask
) {
160 bool PictureLayer::SupportsLCDText() const {
164 bool PictureLayer::UpdateCanUseLCDText() {
165 if (!can_use_lcd_text_for_update_
)
166 return false; // Don't allow the LCD text state to change once disabled.
167 if (can_use_lcd_text_for_update_
== can_use_lcd_text())
170 can_use_lcd_text_for_update_
= can_use_lcd_text();
174 skia::RefPtr
<SkPicture
> PictureLayer::GetPicture() const {
175 // We could either flatten the RecordingSource into a single SkPicture,
176 // or paint a fresh one depending on what we intend to do with the
177 // picture. For now we just paint a fresh one to get consistent results.
179 return skia::RefPtr
<SkPicture
>();
181 int width
= bounds().width();
182 int height
= bounds().height();
184 SkPictureRecorder recorder
;
185 SkCanvas
* canvas
= recorder
.beginRecording(width
, height
, nullptr, 0);
186 client_
->PaintContents(canvas
,
187 gfx::Rect(width
, height
),
188 ContentLayerClient::GRAPHICS_CONTEXT_ENABLED
);
189 skia::RefPtr
<SkPicture
> picture
= skia::AdoptRef(recorder
.endRecording());
193 bool PictureLayer::IsSuitableForGpuRasterization() const {
194 return recording_source_
->IsSuitableForGpuRasterization();
197 void PictureLayer::ClearClient() {
199 UpdateDrawsContent(HasDrawableContent());
202 bool PictureLayer::HasDrawableContent() const {
203 return client_
&& Layer::HasDrawableContent();
206 void PictureLayer::RunMicroBenchmark(MicroBenchmark
* benchmark
) {
207 benchmark
->RunOnLayer(this);