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. 4 5 #include "cc/layers/picture_layer.h" 6 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/trees/layer_tree_impl.h" 11 #include "third_party/skia/include/core/SkPictureRecorder.h" 12 #include "ui/gfx/rect_conversions.h" 13 14 namespace cc { 15 16 scoped_refptr<PictureLayer> PictureLayer::Create(ContentLayerClient* client) { 17 return make_scoped_refptr(new PictureLayer(client)); 18 } 19 20 PictureLayer::PictureLayer(ContentLayerClient* client) 21 : client_(client), 22 pile_(make_scoped_refptr(new PicturePile())), 23 instrumentation_object_tracker_(id()), 24 update_source_frame_number_(-1), 25 can_use_lcd_text_last_frame_(can_use_lcd_text()) { 26 } 27 28 PictureLayer::~PictureLayer() { 29 } 30 31 scoped_ptr<LayerImpl> PictureLayer::CreateLayerImpl(LayerTreeImpl* tree_impl) { 32 return PictureLayerImpl::Create(tree_impl, id()).PassAs<LayerImpl>(); 33 } 34 35 void PictureLayer::PushPropertiesTo(LayerImpl* base_layer) { 36 Layer::PushPropertiesTo(base_layer); 37 PictureLayerImpl* layer_impl = static_cast<PictureLayerImpl*>(base_layer); 38 39 if (layer_impl->bounds().IsEmpty()) { 40 // Update may not get called for an empty layer, so resize here instead. 41 // Using layer_impl because either bounds() or paint_properties().bounds 42 // may disagree and either one could have been pushed to layer_impl. 43 pile_->SetEmptyBounds(); 44 } else if (update_source_frame_number_ == 45 layer_tree_host()->source_frame_number()) { 46 // TODO(ernstm): This DCHECK is only valid as long as the pile's tiling_rect 47 // is identical to the layer_rect. 48 // If update called, then pile size must match bounds pushed to impl layer. 49 DCHECK_EQ(layer_impl->bounds().ToString(), pile_->tiling_size().ToString()); 50 } 51 52 // Unlike other properties, invalidation must always be set on layer_impl. 53 // See PictureLayerImpl::PushPropertiesTo for more details. 54 layer_impl->invalidation_.Clear(); 55 layer_impl->invalidation_.Swap(&pile_invalidation_); 56 layer_impl->UpdatePile(PicturePileImpl::CreateFromOther(pile_.get())); 57 } 58 59 void PictureLayer::SetLayerTreeHost(LayerTreeHost* host) { 60 Layer::SetLayerTreeHost(host); 61 if (host) { 62 pile_->SetMinContentsScale(host->settings().minimum_contents_scale); 63 pile_->SetTileGridSize(host->settings().default_tile_size); 64 pile_->set_slow_down_raster_scale_factor( 65 host->debug_state().slow_down_raster_scale_factor); 66 pile_->set_show_debug_picture_borders( 67 host->debug_state().show_picture_borders); 68 } 69 } 70 71 void PictureLayer::SetNeedsDisplayRect(const gfx::RectF& layer_rect) { 72 gfx::Rect rect = gfx::ToEnclosedRect(layer_rect); 73 if (!rect.IsEmpty()) { 74 // Clamp invalidation to the layer bounds. 75 rect.Intersect(gfx::Rect(bounds())); 76 pending_invalidation_.Union(rect); 77 } 78 Layer::SetNeedsDisplayRect(layer_rect); 79 } 80 81 bool PictureLayer::Update(ResourceUpdateQueue* queue, 82 const OcclusionTracker<Layer>* occlusion) { 83 update_source_frame_number_ = layer_tree_host()->source_frame_number(); 84 bool updated = Layer::Update(queue, occlusion); 85 86 { 87 base::AutoReset<bool> ignore_set_needs_commit(&ignore_set_needs_commit_, 88 true); 89 UpdateCanUseLCDText(); 90 } 91 92 gfx::Rect visible_layer_rect = gfx::ScaleToEnclosingRect( 93 visible_content_rect(), 1.f / contents_scale_x()); 94 gfx::Size layer_size = paint_properties().bounds; 95 96 if (last_updated_visible_content_rect_ == visible_content_rect() && 97 pile_->tiling_size() == layer_size && pending_invalidation_.IsEmpty()) { 98 // Only early out if the visible content rect of this layer hasn't changed. 99 return updated; 100 } 101 102 TRACE_EVENT1("cc", "PictureLayer::Update", 103 "source_frame_number", 104 layer_tree_host()->source_frame_number()); 105 devtools_instrumentation::ScopedLayerTreeTask update_layer( 106 devtools_instrumentation::kUpdateLayer, id(), layer_tree_host()->id()); 107 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(); 112 113 if (layer_tree_host()->settings().record_full_layer) { 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(layer_size); 117 } 118 119 // UpdateAndExpandInvalidation will give us an invalidation that covers 120 // anything not explicitly recorded in this frame. We give this region 121 // to the impl side so that it drops tiles that may not have a recording 122 // for them. 123 DCHECK(client_); 124 updated |= 125 pile_->UpdateAndExpandInvalidation(client_, 126 &pile_invalidation_, 127 SafeOpaqueBackgroundColor(), 128 contents_opaque(), 129 client_->FillsBoundsCompletely(), 130 layer_size, 131 visible_layer_rect, 132 update_source_frame_number_, 133 RecordingMode(), 134 rendering_stats_instrumentation()); 135 last_updated_visible_content_rect_ = visible_content_rect(); 136 137 if (updated) { 138 SetNeedsPushProperties(); 139 } else { 140 // If this invalidation did not affect the pile, then it can be cleared as 141 // an optimization. 142 pile_invalidation_.Clear(); 143 } 144 145 return updated; 146 } 147 148 void PictureLayer::SetIsMask(bool is_mask) { 149 pile_->set_is_mask(is_mask); 150 } 151 152 Picture::RecordingMode PictureLayer::RecordingMode() const { 153 switch (layer_tree_host()->settings().recording_mode) { 154 case LayerTreeSettings::RecordNormally: 155 return Picture::RECORD_NORMALLY; 156 case LayerTreeSettings::RecordWithSkRecord: 157 return Picture::RECORD_WITH_SKRECORD; 158 } 159 NOTREACHED(); 160 return Picture::RECORD_NORMALLY; 161 } 162 163 bool PictureLayer::SupportsLCDText() const { 164 return true; 165 } 166 167 void PictureLayer::UpdateCanUseLCDText() { 168 if (can_use_lcd_text_last_frame_ == can_use_lcd_text()) 169 return; 170 171 can_use_lcd_text_last_frame_ = can_use_lcd_text(); 172 if (client_) 173 client_->DidChangeLayerCanUseLCDText(); 174 } 175 176 skia::RefPtr<SkPicture> PictureLayer::GetPicture() const { 177 // We could either flatten the PicturePile into a single SkPicture, 178 // or paint a fresh one depending on what we intend to do with the 179 // picture. For now we just paint a fresh one to get consistent results. 180 if (!DrawsContent()) 181 return skia::RefPtr<SkPicture>(); 182 183 int width = bounds().width(); 184 int height = bounds().height(); 185 186 SkPictureRecorder recorder; 187 SkCanvas* canvas = recorder.beginRecording(width, height, NULL, 0); 188 client_->PaintContents(canvas, 189 gfx::Rect(width, height), 190 ContentLayerClient::GRAPHICS_CONTEXT_ENABLED); 191 skia::RefPtr<SkPicture> picture = skia::AdoptRef(recorder.endRecording()); 192 return picture; 193 } 194 195 bool PictureLayer::IsSuitableForGpuRasterization() const { 196 return pile_->is_suitable_for_gpu_rasterization(); 197 } 198 199 void PictureLayer::ClearClient() { 200 client_ = NULL; 201 UpdateDrawsContent(HasDrawableContent()); 202 } 203 204 bool PictureLayer::HasDrawableContent() const { 205 return client_ && Layer::HasDrawableContent(); 206 } 207 208 void PictureLayer::RunMicroBenchmark(MicroBenchmark* benchmark) { 209 benchmark->RunOnLayer(this); 210 } 211 212 } // namespace cc 213