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 "third_party/skia/include/core/SkPictureRecorder.h"
11 #include "ui/gfx/rect_conversions.h"
15 scoped_refptr<PictureLayer> PictureLayer::Create(ContentLayerClient* client) {
16 return make_scoped_refptr(new PictureLayer(client));
19 PictureLayer::PictureLayer(ContentLayerClient* client)
21 pile_(make_scoped_refptr(new PicturePile())),
22 instrumentation_object_tracker_(id()),
24 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_->SetTilingRect(gfx::Rect());
46 } else if (update_source_frame_number_ ==
47 layer_tree_host()->source_frame_number()) {
48 // TODO(ernstm): This DCHECK is only valid as long as the pile's tiling_rect
49 // is identical to the layer_rect.
50 // If update called, then pile size must match bounds pushed to impl layer.
51 DCHECK_EQ(layer_impl->bounds().ToString(),
52 pile_->tiling_rect().size().ToString());
55 layer_impl->SetIsMask(is_mask_);
57 // Unlike other properties, invalidation must always be set on layer_impl.
58 // See PictureLayerImpl::PushPropertiesTo for more details.
59 layer_impl->invalidation_.Clear();
60 layer_impl->invalidation_.Swap(&pile_invalidation_);
61 layer_impl->pile_ = PicturePileImpl::CreateFromOther(pile_.get());
64 void PictureLayer::SetLayerTreeHost(LayerTreeHost* host) {
65 Layer::SetLayerTreeHost(host);
67 pile_->SetMinContentsScale(host->settings().minimum_contents_scale);
68 pile_->SetTileGridSize(host->settings().default_tile_size);
69 pile_->set_slow_down_raster_scale_factor(
70 host->debug_state().slow_down_raster_scale_factor);
71 pile_->set_show_debug_picture_borders(
72 host->debug_state().show_picture_borders);
76 void PictureLayer::SetNeedsDisplayRect(const gfx::RectF& layer_rect) {
77 gfx::Rect rect = gfx::ToEnclosedRect(layer_rect);
78 if (!rect.IsEmpty()) {
79 // Clamp invalidation to the layer bounds.
80 rect.Intersect(gfx::Rect(bounds()));
81 pending_invalidation_.Union(rect);
83 Layer::SetNeedsDisplayRect(layer_rect);
86 bool PictureLayer::Update(ResourceUpdateQueue* queue,
87 const OcclusionTracker<Layer>* occlusion) {
88 update_source_frame_number_ = layer_tree_host()->source_frame_number();
89 bool updated = Layer::Update(queue, occlusion);
91 gfx::Rect visible_layer_rect = gfx::ScaleToEnclosingRect(
92 visible_content_rect(), 1.f / contents_scale_x());
94 gfx::Rect layer_rect = gfx::Rect(paint_properties().bounds);
96 if (last_updated_visible_content_rect_ == visible_content_rect() &&
97 pile_->tiling_rect() == layer_rect && pending_invalidation_.IsEmpty()) {
98 // Only early out if the visible content rect of this layer hasn't changed.
102 TRACE_EVENT1("cc", "PictureLayer::Update",
103 "source_frame_number",
104 layer_tree_host()->source_frame_number());
106 pile_->SetTilingRect(layer_rect);
108 // Calling paint in WebKit can sometimes cause invalidations, so save
109 // off the invalidation prior to calling update.
110 pending_invalidation_.Swap(&pile_invalidation_);
111 pending_invalidation_.Clear();
113 if (layer_tree_host()->settings().using_synchronous_renderer_compositor) {
114 // Workaround for http://crbug.com/235910 - to retain backwards compat
115 // the full page content must always be provided in the picture layer.
116 visible_layer_rect = gfx::Rect(bounds());
119 updated |= pile_->Update(client_,
120 SafeOpaqueBackgroundColor(),
122 client_->FillsBoundsCompletely(),
125 update_source_frame_number_,
127 rendering_stats_instrumentation());
128 last_updated_visible_content_rect_ = visible_content_rect();
131 SetNeedsPushProperties();
133 // If this invalidation did not affect the pile, then it can be cleared as
135 pile_invalidation_.Clear();
141 void PictureLayer::SetIsMask(bool is_mask) {
145 Picture::RecordingMode PictureLayer::RecordingMode() const {
146 switch (layer_tree_host()->settings().recording_mode) {
147 case LayerTreeSettings::RecordNormally:
148 return Picture::RECORD_NORMALLY;
149 case LayerTreeSettings::RecordWithSkRecord:
150 return Picture::RECORD_WITH_SKRECORD;
153 return Picture::RECORD_NORMALLY;
156 bool PictureLayer::SupportsLCDText() const {
160 skia::RefPtr<SkPicture> PictureLayer::GetPicture() const {
161 // We could either flatten the PicturePile into a single SkPicture,
162 // or paint a fresh one depending on what we intend to do with the
163 // picture. For now we just paint a fresh one to get consistent results.
165 return skia::RefPtr<SkPicture>();
167 int width = bounds().width();
168 int height = bounds().height();
171 SkPictureRecorder recorder;
172 SkCanvas* canvas = recorder.beginRecording(width, height, NULL, 0);
173 client_->PaintContents(canvas,
174 gfx::Rect(width, height),
176 ContentLayerClient::GRAPHICS_CONTEXT_ENABLED);
177 skia::RefPtr<SkPicture> picture = skia::AdoptRef(recorder.endRecording());
181 bool PictureLayer::IsSuitableForGpuRasterization() const {
182 return pile_->is_suitable_for_gpu_rasterization();
185 void PictureLayer::RunMicroBenchmark(MicroBenchmark* benchmark) {
186 benchmark->RunOnLayer(this);