picture_layer.cc revision a02191e04bc25c4935f804f2c080ae28663d096d
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 "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" 11 12namespace cc { 13 14scoped_refptr<PictureLayer> PictureLayer::Create(ContentLayerClient* client) { 15 return make_scoped_refptr(new PictureLayer(client)); 16} 17 18PictureLayer::PictureLayer(ContentLayerClient* client) 19 : client_(client), 20 pile_(make_scoped_refptr(new PicturePile())), 21 instrumentation_object_tracker_(id()), 22 is_mask_(false), 23 has_gpu_rasterization_hint_(TRIBOOL_UNKNOWN), 24 update_source_frame_number_(-1) {} 25 26PictureLayer::~PictureLayer() { 27} 28 29bool PictureLayer::DrawsContent() const { 30 return Layer::DrawsContent() && client_; 31} 32 33scoped_ptr<LayerImpl> PictureLayer::CreateLayerImpl(LayerTreeImpl* tree_impl) { 34 return PictureLayerImpl::Create(tree_impl, id()).PassAs<LayerImpl>(); 35} 36 37void PictureLayer::PushPropertiesTo(LayerImpl* base_layer) { 38 Layer::PushPropertiesTo(base_layer); 39 PictureLayerImpl* layer_impl = static_cast<PictureLayerImpl*>(base_layer); 40 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 } else if (update_source_frame_number_ == 47 layer_tree_host()->source_frame_number()) { 48 // If update called, then pile size must match bounds pushed to impl layer. 49 DCHECK_EQ(layer_impl->bounds().ToString(), pile_->size().ToString()); 50 } 51 52 layer_impl->SetIsMask(is_mask_); 53 layer_impl->SetUseGpuRasterization(ShouldUseGpuRasterization()); 54 55 // Unlike other properties, invalidation must always be set on layer_impl. 56 // See PictureLayerImpl::PushPropertiesTo for more details. 57 layer_impl->invalidation_.Clear(); 58 layer_impl->invalidation_.Swap(&pile_invalidation_); 59 layer_impl->pile_ = PicturePileImpl::CreateFromOther(pile_.get()); 60} 61 62void PictureLayer::SetLayerTreeHost(LayerTreeHost* host) { 63 Layer::SetLayerTreeHost(host); 64 if (host) { 65 pile_->SetMinContentsScale(host->settings().minimum_contents_scale); 66 pile_->SetTileGridSize(host->settings().default_tile_size); 67 pile_->set_slow_down_raster_scale_factor( 68 host->debug_state().slow_down_raster_scale_factor); 69 pile_->set_show_debug_picture_borders( 70 host->debug_state().show_picture_borders); 71 } 72} 73 74void PictureLayer::SetNeedsDisplayRect(const gfx::RectF& layer_rect) { 75 gfx::Rect rect = gfx::ToEnclosedRect(layer_rect); 76 if (!rect.IsEmpty()) { 77 // Clamp invalidation to the layer bounds. 78 rect.Intersect(gfx::Rect(bounds())); 79 pending_invalidation_.Union(rect); 80 } 81 Layer::SetNeedsDisplayRect(layer_rect); 82} 83 84bool PictureLayer::Update(ResourceUpdateQueue* queue, 85 const OcclusionTracker<Layer>* occlusion) { 86 update_source_frame_number_ = layer_tree_host()->source_frame_number(); 87 bool updated = Layer::Update(queue, occlusion); 88 89 if (last_updated_visible_content_rect_ == visible_content_rect() && 90 pile_->size() == paint_properties().bounds && 91 pending_invalidation_.IsEmpty()) { 92 // Only early out if the visible content rect of this layer hasn't changed. 93 return updated; 94 } 95 96 TRACE_EVENT1("cc", "PictureLayer::Update", 97 "source_frame_number", 98 layer_tree_host()->source_frame_number()); 99 100 pile_->Resize(paint_properties().bounds); 101 102 // Calling paint in WebKit can sometimes cause invalidations, so save 103 // off the invalidation prior to calling update. 104 pending_invalidation_.Swap(&pile_invalidation_); 105 pending_invalidation_.Clear(); 106 107 gfx::Rect visible_layer_rect = gfx::ScaleToEnclosingRect( 108 visible_content_rect(), 1.f / contents_scale_x()); 109 if (layer_tree_host()->settings().using_synchronous_renderer_compositor) { 110 // Workaround for http://crbug.com/235910 - to retain backwards compat 111 // the full page content must always be provided in the picture layer. 112 visible_layer_rect = gfx::Rect(bounds()); 113 } 114 DCHECK(client_); 115 updated |= pile_->Update(client_, 116 SafeOpaqueBackgroundColor(), 117 contents_opaque(), 118 client_->FillsBoundsCompletely(), 119 pile_invalidation_, 120 visible_layer_rect, 121 update_source_frame_number_, 122 rendering_stats_instrumentation()); 123 last_updated_visible_content_rect_ = visible_content_rect(); 124 125 if (updated) { 126 SetNeedsPushProperties(); 127 } else { 128 // If this invalidation did not affect the pile, then it can be cleared as 129 // an optimization. 130 pile_invalidation_.Clear(); 131 } 132 133 return updated; 134} 135 136void PictureLayer::SetIsMask(bool is_mask) { 137 is_mask_ = is_mask; 138} 139 140void PictureLayer::SetHasGpuRasterizationHint(bool has_hint) { 141 switch (has_gpu_rasterization_hint_) { 142 case TRIBOOL_UNKNOWN: // Fall-through. 143 case TRIBOOL_TRUE: 144 has_gpu_rasterization_hint_ = has_hint ? TRIBOOL_TRUE : TRIBOOL_FALSE; 145 break; 146 case TRIBOOL_FALSE: 147 // GPU rasterization cannot be enabled once disabled. 148 // This is done to prevent frequent invalidations and visual flashing. 149 break; 150 default: 151 NOTREACHED(); 152 } 153 // No need to set needs commit or push-properties. 154 // If only the hint changes and the layer is still valid, there is no need 155 // to invalidate the rasterization for the whole layer. If there is an 156 // invalidation (current or future) we will re-raster everything so that it 157 // is consistent across the layer. 158} 159 160bool PictureLayer::ShouldUseGpuRasterization() const { 161 switch (layer_tree_host()->settings().rasterization_site) { 162 case LayerTreeSettings::CpuRasterization: 163 return false; 164 case LayerTreeSettings::HybridRasterization: 165 return has_gpu_rasterization_hint_ == TRIBOOL_TRUE && 166 pile_->is_suitable_for_gpu_rasterization(); 167 case LayerTreeSettings::GpuRasterization: 168 return true; 169 } 170 NOTREACHED(); 171 return false; 172} 173 174bool PictureLayer::SupportsLCDText() const { 175 return true; 176} 177 178skia::RefPtr<SkPicture> PictureLayer::GetPicture() const { 179 // We could either flatten the PicturePile into a single SkPicture, 180 // or paint a fresh one depending on what we intend to do with the 181 // picture. For now we just paint a fresh one to get consistent results. 182 if (!DrawsContent()) 183 return skia::RefPtr<SkPicture>(); 184 185 int width = bounds().width(); 186 int height = bounds().height(); 187 gfx::RectF opaque; 188 189 skia::RefPtr<SkPicture> picture = skia::AdoptRef(new SkPicture); 190 SkCanvas* canvas = picture->beginRecording(width, height); 191 client_->PaintContents(canvas, gfx::Rect(width, height), &opaque); 192 picture->endRecording(); 193 return picture; 194} 195 196void PictureLayer::RunMicroBenchmark(MicroBenchmark* benchmark) { 197 benchmark->RunOnLayer(this); 198} 199 200} // namespace cc 201