Rewrite AndroidSyncSettings to be significantly simpler.
[chromium-blink-merge.git] / cc / layers / picture_layer.cc
blobe7918e220625cf3f7b90aa246deee79164b0b5cf
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_impl.h"
13 #include "third_party/skia/include/core/SkPictureRecorder.h"
14 #include "ui/gfx/geometry/rect_conversions.h"
16 namespace cc {
18 scoped_refptr<PictureLayer> PictureLayer::Create(ContentLayerClient* client) {
19 return make_scoped_refptr(new PictureLayer(client));
22 PictureLayer::PictureLayer(ContentLayerClient* client)
23 : client_(client),
24 instrumentation_object_tracker_(id()),
25 update_source_frame_number_(-1),
26 is_mask_(false),
27 nearest_neighbor_(false) {
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(), is_mask_,
41 new LayerImpl::SyncedScrollOffset);
44 void PictureLayer::PushPropertiesTo(LayerImpl* base_layer) {
45 Layer::PushPropertiesTo(base_layer);
46 PictureLayerImpl* layer_impl = static_cast<PictureLayerImpl*>(base_layer);
47 // TODO(danakj): Make is_mask_ a constructor parameter for PictureLayer.
48 DCHECK_EQ(layer_impl->is_mask(), is_mask_);
50 int source_frame_number = layer_tree_host()->source_frame_number();
51 gfx::Size impl_bounds = layer_impl->bounds();
52 gfx::Size recording_source_bounds = recording_source_->GetSize();
54 // If update called, then recording source size must match bounds pushed to
55 // impl layer.
56 DCHECK_IMPLIES(update_source_frame_number_ == source_frame_number,
57 impl_bounds == recording_source_bounds)
58 << " bounds " << impl_bounds.ToString() << " recording source "
59 << recording_source_bounds.ToString();
61 if (update_source_frame_number_ != source_frame_number &&
62 recording_source_bounds != impl_bounds) {
63 // Update may not get called for the layer (if it's not in the viewport
64 // for example, even though it has resized making the recording source no
65 // longer valid. In this case just destroy the recording source.
66 recording_source_->SetEmptyBounds();
69 layer_impl->SetNearestNeighbor(nearest_neighbor_);
71 // Preserve lcd text settings from the current raster source.
72 bool can_use_lcd_text = layer_impl->RasterSourceUsesLCDText();
73 scoped_refptr<RasterSource> raster_source =
74 recording_source_->CreateRasterSource(can_use_lcd_text);
75 layer_impl->UpdateRasterSource(raster_source, &recording_invalidation_,
76 nullptr);
77 DCHECK(recording_invalidation_.IsEmpty());
80 void PictureLayer::SetLayerTreeHost(LayerTreeHost* host) {
81 Layer::SetLayerTreeHost(host);
82 if (host) {
83 if (!recording_source_) {
84 if (host->settings().use_display_lists) {
85 recording_source_.reset(new DisplayListRecordingSource);
86 } else {
87 recording_source_.reset(
88 new PicturePile(host->settings().minimum_contents_scale,
89 host->settings().default_tile_grid_size));
92 recording_source_->SetSlowdownRasterScaleFactor(
93 host->debug_state().slow_down_raster_scale_factor);
97 void PictureLayer::SetNeedsDisplayRect(const gfx::Rect& layer_rect) {
98 if (!layer_rect.IsEmpty()) {
99 // Clamp invalidation to the layer bounds.
100 pending_invalidation_.Union(
101 gfx::IntersectRects(layer_rect, gfx::Rect(bounds())));
103 Layer::SetNeedsDisplayRect(layer_rect);
106 bool PictureLayer::Update(ResourceUpdateQueue* queue,
107 const OcclusionTracker<Layer>* occlusion) {
108 update_source_frame_number_ = layer_tree_host()->source_frame_number();
109 bool updated = Layer::Update(queue, occlusion);
111 gfx::Rect visible_layer_rect = gfx::ScaleToEnclosingRect(
112 visible_content_rect(), 1.f / contents_scale_x());
113 gfx::Size layer_size = paint_properties().bounds;
115 if (last_updated_visible_content_rect_ == visible_content_rect() &&
116 recording_source_->GetSize() == layer_size &&
117 pending_invalidation_.IsEmpty()) {
118 // Only early out if the visible content rect of this layer hasn't changed.
119 return updated;
122 recording_source_->SetBackgroundColor(SafeOpaqueBackgroundColor());
123 recording_source_->SetRequiresClear(!contents_opaque() &&
124 !client_->FillsBoundsCompletely());
126 TRACE_EVENT1("cc", "PictureLayer::Update",
127 "source_frame_number",
128 layer_tree_host()->source_frame_number());
129 devtools_instrumentation::ScopedLayerTreeTask update_layer(
130 devtools_instrumentation::kUpdateLayer, id(), layer_tree_host()->id());
132 // Calling paint in WebKit can sometimes cause invalidations, so save
133 // off the invalidation prior to calling update.
134 pending_invalidation_.Swap(&recording_invalidation_);
135 pending_invalidation_.Clear();
137 if (layer_tree_host()->settings().record_full_layer) {
138 // Workaround for http://crbug.com/235910 - to retain backwards compat
139 // the full page content must always be provided in the picture layer.
140 visible_layer_rect = gfx::Rect(layer_size);
143 // UpdateAndExpandInvalidation will give us an invalidation that covers
144 // anything not explicitly recorded in this frame. We give this region
145 // to the impl side so that it drops tiles that may not have a recording
146 // for them.
147 DCHECK(client_);
148 updated |= recording_source_->UpdateAndExpandInvalidation(
149 client_, &recording_invalidation_, layer_size, visible_layer_rect,
150 update_source_frame_number_, RecordingSource::RECORD_NORMALLY);
151 last_updated_visible_content_rect_ = visible_content_rect();
153 if (updated) {
154 SetNeedsPushProperties();
155 } else {
156 // If this invalidation did not affect the recording source, then it can be
157 // cleared as an optimization.
158 recording_invalidation_.Clear();
161 return updated;
164 void PictureLayer::SetIsMask(bool is_mask) {
165 is_mask_ = is_mask;
168 skia::RefPtr<SkPicture> PictureLayer::GetPicture() const {
169 // We could either flatten the RecordingSource into a single SkPicture,
170 // or paint a fresh one depending on what we intend to do with the
171 // picture. For now we just paint a fresh one to get consistent results.
172 if (!DrawsContent())
173 return skia::RefPtr<SkPicture>();
175 int width = bounds().width();
176 int height = bounds().height();
178 SkPictureRecorder recorder;
179 SkCanvas* canvas = recorder.beginRecording(width, height, nullptr, 0);
180 client_->PaintContents(canvas, gfx::Rect(width, height),
181 ContentLayerClient::PAINTING_BEHAVIOR_NORMAL);
182 skia::RefPtr<SkPicture> picture = skia::AdoptRef(recorder.endRecording());
183 return picture;
186 bool PictureLayer::IsSuitableForGpuRasterization() const {
187 return recording_source_->IsSuitableForGpuRasterization();
190 void PictureLayer::ClearClient() {
191 client_ = nullptr;
192 UpdateDrawsContent(HasDrawableContent());
195 void PictureLayer::SetNearestNeighbor(bool nearest_neighbor) {
196 if (nearest_neighbor_ == nearest_neighbor)
197 return;
199 nearest_neighbor_ = nearest_neighbor;
200 SetNeedsCommit();
203 bool PictureLayer::HasDrawableContent() const {
204 return client_ && Layer::HasDrawableContent();
207 void PictureLayer::RunMicroBenchmark(MicroBenchmark* benchmark) {
208 benchmark->RunOnLayer(this);
211 } // namespace cc