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/display_list_recording_source.h"
11 #include "cc/resources/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(ContentLayerClient
* client
) {
20 return make_scoped_refptr(new PictureLayer(client
));
23 PictureLayer::PictureLayer(ContentLayerClient
* client
)
25 instrumentation_object_tracker_(id()),
26 update_source_frame_number_(-1),
28 nearest_neighbor_(false) {
31 PictureLayer::PictureLayer(ContentLayerClient
* client
,
32 scoped_ptr
<RecordingSource
> source
)
33 : PictureLayer(client
) {
34 recording_source_
= source
.Pass();
37 PictureLayer::~PictureLayer() {
40 scoped_ptr
<LayerImpl
> PictureLayer::CreateLayerImpl(LayerTreeImpl
* tree_impl
) {
41 return PictureLayerImpl::Create(tree_impl
, id(), is_mask_
,
42 new LayerImpl::SyncedScrollOffset
);
45 void PictureLayer::PushPropertiesTo(LayerImpl
* base_layer
) {
46 Layer::PushPropertiesTo(base_layer
);
47 PictureLayerImpl
* layer_impl
= static_cast<PictureLayerImpl
*>(base_layer
);
48 // TODO(danakj): Make is_mask_ a constructor parameter for PictureLayer.
49 DCHECK_EQ(layer_impl
->is_mask(), is_mask_
);
51 int source_frame_number
= layer_tree_host()->source_frame_number();
52 gfx::Size impl_bounds
= layer_impl
->bounds();
53 gfx::Size recording_source_bounds
= recording_source_
->GetSize();
55 // If update called, then recording source size must match bounds pushed to
57 DCHECK_IMPLIES(update_source_frame_number_
== source_frame_number
,
58 impl_bounds
== recording_source_bounds
)
59 << " bounds " << impl_bounds
.ToString() << " recording source "
60 << recording_source_bounds
.ToString();
62 if (update_source_frame_number_
!= source_frame_number
&&
63 recording_source_bounds
!= impl_bounds
) {
64 // Update may not get called for the layer (if it's not in the viewport
65 // for example, even though it has resized making the recording source no
66 // longer valid. In this case just destroy the recording source.
67 recording_source_
->SetEmptyBounds();
70 layer_impl
->SetNearestNeighbor(nearest_neighbor_
);
72 // Preserve lcd text settings from the current raster source.
73 bool can_use_lcd_text
= layer_impl
->RasterSourceUsesLCDText();
74 scoped_refptr
<RasterSource
> raster_source
=
75 recording_source_
->CreateRasterSource(can_use_lcd_text
);
76 layer_impl
->set_gpu_raster_max_texture_size(
77 layer_tree_host()->device_viewport_size());
78 layer_impl
->UpdateRasterSource(raster_source
, &recording_invalidation_
,
80 DCHECK(recording_invalidation_
.IsEmpty());
83 void PictureLayer::SetLayerTreeHost(LayerTreeHost
* host
) {
84 Layer::SetLayerTreeHost(host
);
88 const LayerTreeSettings
& settings
= layer_tree_host()->settings();
89 if (!recording_source_
) {
90 if (settings
.use_display_lists
) {
91 recording_source_
.reset(new DisplayListRecordingSource(
92 settings
.default_tile_grid_size
,
93 settings
.use_cached_picture_in_display_list
));
95 recording_source_
.reset(new PicturePile(settings
.minimum_contents_scale
,
96 settings
.default_tile_grid_size
));
99 recording_source_
->SetSlowdownRasterScaleFactor(
100 host
->debug_state().slow_down_raster_scale_factor
);
101 recording_source_
->SetGatherPixelRefs(settings
.gather_pixel_refs
);
103 DCHECK(settings
.raster_enabled
);
106 void PictureLayer::SetNeedsDisplayRect(const gfx::Rect
& layer_rect
) {
107 if (!layer_rect
.IsEmpty()) {
108 // Clamp invalidation to the layer bounds.
109 pending_invalidation_
.Union(
110 gfx::IntersectRects(layer_rect
, gfx::Rect(bounds())));
112 Layer::SetNeedsDisplayRect(layer_rect
);
115 bool PictureLayer::Update(ResourceUpdateQueue
* queue
,
116 const OcclusionTracker
<Layer
>* occlusion
) {
117 update_source_frame_number_
= layer_tree_host()->source_frame_number();
118 bool updated
= Layer::Update(queue
, occlusion
);
120 gfx::Rect visible_layer_rect
= gfx::ScaleToEnclosingRect(
121 visible_content_rect(), 1.f
/ contents_scale_x());
122 gfx::Size layer_size
= paint_properties().bounds
;
124 if (last_updated_visible_content_rect_
== visible_content_rect() &&
125 recording_source_
->GetSize() == layer_size
&&
126 pending_invalidation_
.IsEmpty()) {
127 // Only early out if the visible content rect of this layer hasn't changed.
131 recording_source_
->SetBackgroundColor(SafeOpaqueBackgroundColor());
132 recording_source_
->SetRequiresClear(!contents_opaque() &&
133 !client_
->FillsBoundsCompletely());
135 TRACE_EVENT1("cc", "PictureLayer::Update",
136 "source_frame_number",
137 layer_tree_host()->source_frame_number());
138 devtools_instrumentation::ScopedLayerTreeTask
update_layer(
139 devtools_instrumentation::kUpdateLayer
, id(), layer_tree_host()->id());
141 // Calling paint in WebKit can sometimes cause invalidations, so save
142 // off the invalidation prior to calling update.
143 pending_invalidation_
.Swap(&recording_invalidation_
);
144 pending_invalidation_
.Clear();
146 if (layer_tree_host()->settings().record_full_layer
) {
147 // Workaround for http://crbug.com/235910 - to retain backwards compat
148 // the full page content must always be provided in the picture layer.
149 visible_layer_rect
= gfx::Rect(layer_size
);
152 // UpdateAndExpandInvalidation will give us an invalidation that covers
153 // anything not explicitly recorded in this frame. We give this region
154 // to the impl side so that it drops tiles that may not have a recording
157 updated
|= recording_source_
->UpdateAndExpandInvalidation(
158 client_
, &recording_invalidation_
, layer_size
, visible_layer_rect
,
159 update_source_frame_number_
, RecordingSource::RECORD_NORMALLY
);
160 last_updated_visible_content_rect_
= visible_content_rect();
163 SetNeedsPushProperties();
165 // If this invalidation did not affect the recording source, then it can be
166 // cleared as an optimization.
167 recording_invalidation_
.Clear();
173 void PictureLayer::SetIsMask(bool is_mask
) {
177 skia::RefPtr
<SkPicture
> PictureLayer::GetPicture() const {
178 // We could either flatten the RecordingSource into a single SkPicture,
179 // or paint a fresh one depending on what we intend to do with the
180 // picture. For now we just paint a fresh one to get consistent results.
182 return skia::RefPtr
<SkPicture
>();
184 gfx::Size layer_size
= bounds();
185 const LayerTreeSettings
& settings
= layer_tree_host()->settings();
187 if (settings
.use_display_lists
) {
188 scoped_ptr
<RecordingSource
> recording_source
;
189 recording_source
.reset(new DisplayListRecordingSource(
190 settings
.default_tile_grid_size
,
191 settings
.use_cached_picture_in_display_list
));
192 Region recording_invalidation
;
193 recording_source
->UpdateAndExpandInvalidation(
194 client_
, &recording_invalidation
, layer_size
, gfx::Rect(layer_size
),
195 update_source_frame_number_
, RecordingSource::RECORD_NORMALLY
);
197 scoped_refptr
<RasterSource
> raster_source
=
198 recording_source
->CreateRasterSource(false);
200 return raster_source
->GetFlattenedPicture();
203 int width
= layer_size
.width();
204 int height
= layer_size
.height();
206 SkPictureRecorder recorder
;
207 SkCanvas
* canvas
= recorder
.beginRecording(width
, height
, nullptr, 0);
208 client_
->PaintContents(canvas
, gfx::Rect(width
, height
),
209 ContentLayerClient::PAINTING_BEHAVIOR_NORMAL
);
210 skia::RefPtr
<SkPicture
> picture
=
211 skia::AdoptRef(recorder
.endRecordingAsPicture());
215 bool PictureLayer::IsSuitableForGpuRasterization() const {
216 return recording_source_
->IsSuitableForGpuRasterization();
219 void PictureLayer::ClearClient() {
221 UpdateDrawsContent(HasDrawableContent());
224 void PictureLayer::SetNearestNeighbor(bool nearest_neighbor
) {
225 if (nearest_neighbor_
== nearest_neighbor
)
228 nearest_neighbor_
= nearest_neighbor
;
232 bool PictureLayer::HasDrawableContent() const {
233 return client_
&& Layer::HasDrawableContent();
236 void PictureLayer::RunMicroBenchmark(MicroBenchmark
* benchmark
) {
237 benchmark
->RunOnLayer(this);