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/debug/benchmark_instrumentation.h" 8 #include "cc/debug/devtools_instrumentation.h" 9 #include "cc/layers/picture_layer_impl.h" 10 #include "cc/trees/layer_tree_impl.h" 11 #include "ui/gfx/rect_conversions.h" 12 13 namespace cc { 14 15 scoped_refptr<PictureLayer> PictureLayer::Create(ContentLayerClient* client) { 16 return make_scoped_refptr(new PictureLayer(client)); 17 } 18 19 PictureLayer::PictureLayer(ContentLayerClient* client) 20 : client_(client), 21 pile_(make_scoped_refptr(new PicturePile())), 22 instrumentation_object_tracker_(id()), 23 is_mask_(false) { 24 } 25 26 PictureLayer::~PictureLayer() { 27 } 28 29 bool PictureLayer::DrawsContent() const { 30 return Layer::DrawsContent() && client_; 31 } 32 33 scoped_ptr<LayerImpl> PictureLayer::CreateLayerImpl(LayerTreeImpl* tree_impl) { 34 return PictureLayerImpl::Create(tree_impl, id()).PassAs<LayerImpl>(); 35 } 36 37 void PictureLayer::PushPropertiesTo(LayerImpl* base_layer) { 38 Layer::PushPropertiesTo(base_layer); 39 40 PictureLayerImpl* layer_impl = static_cast<PictureLayerImpl*>(base_layer); 41 // This should be first so others can use it. 42 layer_impl->UpdateTwinLayer(); 43 44 if (layer_impl->bounds().IsEmpty()) { 45 // Update may not get called for an empty layer, so resize here instead. 46 // Using layer_impl because either bounds() or paint_properties().bounds 47 // may disagree and either one could have been pushed to layer_impl. 48 pile_->Resize(layer_impl->bounds()); 49 pile_->UpdateRecordedRegion(); 50 } 51 52 if (DrawsContent()) { 53 DCHECK(paint_properties().bounds == pile_->size()); 54 } 55 56 layer_impl->SetIsMask(is_mask_); 57 layer_impl->CreateTilingSetIfNeeded(); 58 // Unlike other properties, invalidation must always be set on layer_impl. 59 // See PictureLayerImpl::PushPropertiesTo for more details. 60 layer_impl->invalidation_.Clear(); 61 layer_impl->invalidation_.Swap(&pile_invalidation_); 62 layer_impl->pile_ = PicturePileImpl::CreateFromOther(pile_.get()); 63 layer_impl->SyncFromActiveLayer(); 64 65 // PictureLayer must push properties every frame. 66 // TODO(danakj): If we can avoid requiring to do CreateTilingSetIfNeeded() and 67 // SyncFromActiveLayer() on every commit then this could go away, maybe 68 // conditionally. crbug.com/259402 69 needs_push_properties_ = true; 70 } 71 72 void PictureLayer::SetLayerTreeHost(LayerTreeHost* host) { 73 Layer::SetLayerTreeHost(host); 74 if (host) { 75 pile_->SetMinContentsScale(host->settings().minimum_contents_scale); 76 pile_->SetTileGridSize(host->settings().default_tile_size); 77 pile_->set_num_raster_threads(host->settings().num_raster_threads); 78 pile_->set_slow_down_raster_scale_factor( 79 host->debug_state().slow_down_raster_scale_factor); 80 pile_->set_show_debug_picture_borders( 81 host->debug_state().show_picture_borders); 82 } 83 } 84 85 void PictureLayer::SetNeedsDisplayRect(const gfx::RectF& layer_rect) { 86 gfx::Rect rect = gfx::ToEnclosedRect(layer_rect); 87 if (!rect.IsEmpty()) { 88 // Clamp invalidation to the layer bounds. 89 rect.Intersect(gfx::Rect(bounds())); 90 pending_invalidation_.Union(rect); 91 } 92 Layer::SetNeedsDisplayRect(layer_rect); 93 } 94 95 bool PictureLayer::Update(ResourceUpdateQueue* queue, 96 const OcclusionTracker* occlusion) { 97 // Do not early-out of this function so that PicturePile::Update has a chance 98 // to record pictures due to changing visibility of this layer. 99 100 TRACE_EVENT1(benchmark_instrumentation::kCategory, 101 benchmark_instrumentation::kPictureLayerUpdate, 102 benchmark_instrumentation::kSourceFrameNumber, 103 layer_tree_host()->source_frame_number()); 104 105 bool updated = Layer::Update(queue, occlusion); 106 107 pile_->Resize(paint_properties().bounds); 108 109 // Calling paint in WebKit can sometimes cause invalidations, so save 110 // off the invalidation prior to calling update. 111 pending_invalidation_.Swap(&pile_invalidation_); 112 pending_invalidation_.Clear(); 113 114 gfx::Rect visible_layer_rect = gfx::ScaleToEnclosingRect( 115 visible_content_rect(), 1.f / contents_scale_x()); 116 if (layer_tree_host()->settings().using_synchronous_renderer_compositor) { 117 // Workaround for http://crbug.com/235910 - to retain backwards compat 118 // the full page content must always be provided in the picture layer. 119 visible_layer_rect = gfx::Rect(bounds()); 120 } 121 devtools_instrumentation::ScopedLayerTask paint_layer( 122 devtools_instrumentation::kPaintLayer, id()); 123 updated |= pile_->Update(client_, 124 SafeOpaqueBackgroundColor(), 125 contents_opaque(), 126 pile_invalidation_, 127 visible_layer_rect, 128 rendering_stats_instrumentation()); 129 if (!updated) { 130 // If this invalidation did not affect the pile, then it can be cleared as 131 // an optimization. 132 pile_invalidation_.Clear(); 133 } 134 return updated; 135 } 136 137 void PictureLayer::SetIsMask(bool is_mask) { 138 is_mask_ = is_mask; 139 } 140 141 bool PictureLayer::SupportsLCDText() const { 142 return true; 143 } 144 145 } // namespace cc 146