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/playback/display_list_recording_source.h"
11 #include "cc/playback/picture_pile.h"
12 #include "cc/trees/layer_tree_host.h"
13 #include "cc/trees/layer_tree_impl.h"
14 #include "third_party/skia/include/core/SkPictureRecorder.h"
15 #include "ui/gfx/geometry/rect_conversions.h"
19 scoped_refptr
<PictureLayer
> PictureLayer::Create(const LayerSettings
& settings
,
20 ContentLayerClient
* client
) {
21 return make_scoped_refptr(new PictureLayer(settings
, client
));
24 PictureLayer::PictureLayer(const LayerSettings
& settings
,
25 ContentLayerClient
* client
)
28 instrumentation_object_tracker_(id()),
29 update_source_frame_number_(-1),
31 nearest_neighbor_(false) {
34 PictureLayer::PictureLayer(const LayerSettings
& settings
,
35 ContentLayerClient
* client
,
36 scoped_ptr
<RecordingSource
> source
)
37 : PictureLayer(settings
, client
) {
38 recording_source_
= source
.Pass();
41 PictureLayer::~PictureLayer() {
44 scoped_ptr
<LayerImpl
> PictureLayer::CreateLayerImpl(LayerTreeImpl
* tree_impl
) {
45 return PictureLayerImpl::Create(tree_impl
, id(), is_mask_
,
46 new LayerImpl::SyncedScrollOffset
);
49 void PictureLayer::PushPropertiesTo(LayerImpl
* base_layer
) {
50 Layer::PushPropertiesTo(base_layer
);
51 PictureLayerImpl
* layer_impl
= static_cast<PictureLayerImpl
*>(base_layer
);
52 // TODO(danakj): Make is_mask_ a constructor parameter for PictureLayer.
53 DCHECK_EQ(layer_impl
->is_mask(), is_mask_
);
55 int source_frame_number
= layer_tree_host()->source_frame_number();
56 gfx::Size impl_bounds
= layer_impl
->bounds();
57 gfx::Size recording_source_bounds
= recording_source_
->GetSize();
59 // If update called, then recording source size must match bounds pushed to
61 DCHECK_IMPLIES(update_source_frame_number_
== source_frame_number
,
62 impl_bounds
== recording_source_bounds
)
63 << " bounds " << impl_bounds
.ToString() << " recording source "
64 << recording_source_bounds
.ToString();
66 if (update_source_frame_number_
!= source_frame_number
&&
67 recording_source_bounds
!= impl_bounds
) {
68 // Update may not get called for the layer (if it's not in the viewport
69 // for example, even though it has resized making the recording source no
70 // longer valid. In this case just destroy the recording source.
71 recording_source_
->SetEmptyBounds();
74 layer_impl
->SetNearestNeighbor(nearest_neighbor_
);
76 // Preserve lcd text settings from the current raster source.
77 bool can_use_lcd_text
= layer_impl
->RasterSourceUsesLCDText();
78 scoped_refptr
<RasterSource
> raster_source
=
79 recording_source_
->CreateRasterSource(can_use_lcd_text
);
80 layer_impl
->set_gpu_raster_max_texture_size(
81 layer_tree_host()->device_viewport_size());
82 layer_impl
->UpdateRasterSource(raster_source
, &recording_invalidation_
,
84 DCHECK(recording_invalidation_
.IsEmpty());
87 void PictureLayer::SetLayerTreeHost(LayerTreeHost
* host
) {
88 Layer::SetLayerTreeHost(host
);
92 const LayerTreeSettings
& settings
= layer_tree_host()->settings();
93 if (!recording_source_
) {
94 recording_source_
.reset(
95 new DisplayListRecordingSource(settings
.default_tile_grid_size
));
97 recording_source_
->SetSlowdownRasterScaleFactor(
98 host
->debug_state().slow_down_raster_scale_factor
);
99 recording_source_
->SetGatherDiscardableImages(settings
.gather_images
);
102 void PictureLayer::SetNeedsDisplayRect(const gfx::Rect
& layer_rect
) {
103 if (!layer_rect
.IsEmpty()) {
104 // Clamp invalidation to the layer bounds.
105 pending_invalidation_
.Union(
106 gfx::IntersectRects(layer_rect
, gfx::Rect(bounds())));
108 Layer::SetNeedsDisplayRect(layer_rect
);
111 bool PictureLayer::Update() {
112 update_source_frame_number_
= layer_tree_host()->source_frame_number();
113 bool updated
= Layer::Update();
115 gfx::Rect update_rect
= visible_layer_rect();
116 gfx::Size layer_size
= paint_properties().bounds
;
118 if (last_updated_visible_layer_rect_
== update_rect
&&
119 recording_source_
->GetSize() == layer_size
&&
120 pending_invalidation_
.IsEmpty()) {
121 // Only early out if the visible content rect of this layer hasn't changed.
125 recording_source_
->SetBackgroundColor(SafeOpaqueBackgroundColor());
126 recording_source_
->SetRequiresClear(!contents_opaque() &&
127 !client_
->FillsBoundsCompletely());
129 TRACE_EVENT1("cc", "PictureLayer::Update",
130 "source_frame_number",
131 layer_tree_host()->source_frame_number());
132 devtools_instrumentation::ScopedLayerTreeTask
update_layer(
133 devtools_instrumentation::kUpdateLayer
, id(), layer_tree_host()->id());
135 // Calling paint in WebKit can sometimes cause invalidations, so save
136 // off the invalidation prior to calling update.
137 pending_invalidation_
.Swap(&recording_invalidation_
);
138 pending_invalidation_
.Clear();
140 if (layer_tree_host()->settings().record_full_layer
) {
141 // Workaround for http://crbug.com/235910 - to retain backwards compat
142 // the full page content must always be provided in the picture layer.
143 update_rect
= gfx::Rect(layer_size
);
146 // UpdateAndExpandInvalidation will give us an invalidation that covers
147 // anything not explicitly recorded in this frame. We give this region
148 // to the impl side so that it drops tiles that may not have a recording
151 updated
|= recording_source_
->UpdateAndExpandInvalidation(
152 client_
, &recording_invalidation_
, layer_size
, update_rect
,
153 update_source_frame_number_
, RecordingSource::RECORD_NORMALLY
);
154 last_updated_visible_layer_rect_
= visible_layer_rect();
157 SetNeedsPushProperties();
159 // If this invalidation did not affect the recording source, then it can be
160 // cleared as an optimization.
161 recording_invalidation_
.Clear();
167 void PictureLayer::SetIsMask(bool is_mask
) {
171 skia::RefPtr
<SkPicture
> PictureLayer::GetPicture() const {
172 // We could either flatten the RecordingSource into a single SkPicture,
173 // or paint a fresh one depending on what we intend to do with the
174 // picture. For now we just paint a fresh one to get consistent results.
176 return skia::RefPtr
<SkPicture
>();
178 gfx::Size layer_size
= bounds();
179 const LayerTreeSettings
& settings
= layer_tree_host()->settings();
181 scoped_ptr
<RecordingSource
> recording_source(
182 new DisplayListRecordingSource(settings
.default_tile_grid_size
));
183 Region recording_invalidation
;
184 recording_source
->UpdateAndExpandInvalidation(
185 client_
, &recording_invalidation
, layer_size
, gfx::Rect(layer_size
),
186 update_source_frame_number_
, RecordingSource::RECORD_NORMALLY
);
188 scoped_refptr
<RasterSource
> raster_source
=
189 recording_source
->CreateRasterSource(false);
191 return raster_source
->GetFlattenedPicture();
194 bool PictureLayer::IsSuitableForGpuRasterization() const {
195 return recording_source_
->IsSuitableForGpuRasterization();
198 void PictureLayer::ClearClient() {
200 UpdateDrawsContent(HasDrawableContent());
203 void PictureLayer::SetNearestNeighbor(bool nearest_neighbor
) {
204 if (nearest_neighbor_
== nearest_neighbor
)
207 nearest_neighbor_
= nearest_neighbor
;
211 bool PictureLayer::HasDrawableContent() const {
212 return client_
&& Layer::HasDrawableContent();
215 void PictureLayer::RunMicroBenchmark(MicroBenchmark
* benchmark
) {
216 benchmark
->RunOnLayer(this);