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 "ui/gfx/rect_conversions.h"
14 scoped_refptr
<PictureLayer
> PictureLayer::Create(ContentLayerClient
* client
) {
15 return make_scoped_refptr(new PictureLayer(client
));
18 PictureLayer::PictureLayer(ContentLayerClient
* client
)
20 pile_(make_scoped_refptr(new PicturePile())),
21 instrumentation_object_tracker_(id()),
23 update_source_frame_number_(-1) {
26 PictureLayer::~PictureLayer() {
29 bool PictureLayer::DrawsContent() const {
30 return Layer::DrawsContent() && client_
;
33 scoped_ptr
<LayerImpl
> PictureLayer::CreateLayerImpl(LayerTreeImpl
* tree_impl
) {
34 return PictureLayerImpl::Create(tree_impl
, id()).PassAs
<LayerImpl
>();
37 void PictureLayer::PushPropertiesTo(LayerImpl
* base_layer
) {
38 Layer::PushPropertiesTo(base_layer
);
39 PictureLayerImpl
* layer_impl
= static_cast<PictureLayerImpl
*>(base_layer
);
41 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.
45 pile_
->Resize(gfx::Size());
46 pile_
->UpdateRecordedRegion();
47 } else if (update_source_frame_number_
==
48 layer_tree_host()->source_frame_number()) {
49 // If update called, then pile size must match bounds pushed to impl layer.
50 DCHECK_EQ(layer_impl
->bounds().ToString(), pile_
->size().ToString());
53 layer_impl
->SetIsMask(is_mask_
);
54 // Unlike other properties, invalidation must always be set on layer_impl.
55 // See PictureLayerImpl::PushPropertiesTo for more details.
56 layer_impl
->invalidation_
.Clear();
57 layer_impl
->invalidation_
.Swap(&pile_invalidation_
);
58 layer_impl
->pile_
= PicturePileImpl::CreateFromOther(pile_
.get());
61 void PictureLayer::SetLayerTreeHost(LayerTreeHost
* host
) {
62 Layer::SetLayerTreeHost(host
);
64 pile_
->SetMinContentsScale(host
->settings().minimum_contents_scale
);
65 pile_
->SetTileGridSize(host
->settings().default_tile_size
);
66 pile_
->set_slow_down_raster_scale_factor(
67 host
->debug_state().slow_down_raster_scale_factor
);
68 pile_
->set_show_debug_picture_borders(
69 host
->debug_state().show_picture_borders
);
73 void PictureLayer::SetNeedsDisplayRect(const gfx::RectF
& layer_rect
) {
74 gfx::Rect rect
= gfx::ToEnclosedRect(layer_rect
);
75 if (!rect
.IsEmpty()) {
76 // Clamp invalidation to the layer bounds.
77 rect
.Intersect(gfx::Rect(bounds()));
78 pending_invalidation_
.Union(rect
);
80 Layer::SetNeedsDisplayRect(layer_rect
);
83 bool PictureLayer::Update(ResourceUpdateQueue
* queue
,
84 const OcclusionTracker
* occlusion
) {
85 update_source_frame_number_
= layer_tree_host()->source_frame_number();
86 bool updated
= Layer::Update(queue
, occlusion
);
88 if (last_updated_visible_content_rect_
== visible_content_rect() &&
89 pile_
->size() == paint_properties().bounds
&&
90 pending_invalidation_
.IsEmpty()) {
91 // Only early out if the visible content rect of this layer hasn't changed.
95 TRACE_EVENT1("cc", "PictureLayer::Update",
96 "source_frame_number",
97 layer_tree_host()->source_frame_number());
99 pile_
->Resize(paint_properties().bounds
);
101 // Calling paint in WebKit can sometimes cause invalidations, so save
102 // off the invalidation prior to calling update.
103 pending_invalidation_
.Swap(&pile_invalidation_
);
104 pending_invalidation_
.Clear();
106 gfx::Rect visible_layer_rect
= gfx::ScaleToEnclosingRect(
107 visible_content_rect(), 1.f
/ contents_scale_x());
108 if (layer_tree_host()->settings().using_synchronous_renderer_compositor
) {
109 // Workaround for http://crbug.com/235910 - to retain backwards compat
110 // the full page content must always be provided in the picture layer.
111 visible_layer_rect
= gfx::Rect(bounds());
113 updated
|= pile_
->Update(client_
,
114 SafeOpaqueBackgroundColor(),
118 update_source_frame_number_
,
119 rendering_stats_instrumentation());
120 last_updated_visible_content_rect_
= visible_content_rect();
123 SetNeedsPushProperties();
125 // If this invalidation did not affect the pile, then it can be cleared as
127 pile_invalidation_
.Clear();
133 void PictureLayer::SetIsMask(bool is_mask
) {
137 bool PictureLayer::SupportsLCDText() const {
141 skia::RefPtr
<SkPicture
> PictureLayer::GetPicture() const {
142 // We could either flatten the PicturePile into a single SkPicture,
143 // or paint a fresh one depending on what we intend to do with the
144 // picture. For now we just paint a fresh one to get consistent results.
146 return skia::RefPtr
<SkPicture
>();
148 int width
= bounds().width();
149 int height
= bounds().height();
152 skia::RefPtr
<SkPicture
> picture
= skia::AdoptRef(new SkPicture
);
153 SkCanvas
* canvas
= picture
->beginRecording(width
, height
);
154 client_
->PaintContents(canvas
, gfx::Rect(width
, height
), &opaque
);
155 picture
->endRecording();
159 void PictureLayer::RunMicroBenchmark(MicroBenchmark
* benchmark
) {
160 benchmark
->RunOnLayer(this);