[email protected] | d98c024 | 2012-11-08 06:22:35 | [diff] [blame] | 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 | |
[email protected] | cc3cfaa | 2013-03-18 09:05:52 | [diff] [blame] | 5 | #include "cc/layers/picture_layer.h" |
[email protected] | f117a4c | 2012-12-16 04:53:10 | [diff] [blame] | 6 | |
[email protected] | f7837a9 | 2013-08-21 03:00:05 | [diff] [blame] | 7 | #include "cc/layers/content_layer_client.h" |
[email protected] | cc3cfaa | 2013-03-18 09:05:52 | [diff] [blame] | 8 | #include "cc/layers/picture_layer_impl.h" |
[email protected] | 556fd29 | 2013-03-18 08:03:04 | [diff] [blame] | 9 | #include "cc/trees/layer_tree_impl.h" |
[email protected] | 3621e18 | 2012-11-09 22:37:09 | [diff] [blame] | 10 | #include "ui/gfx/rect_conversions.h" |
[email protected] | d98c024 | 2012-11-08 06:22:35 | [diff] [blame] | 11 | |
| 12 | namespace cc { |
| 13 | |
[email protected] | 7aba666 | 2013-03-12 10:17:34 | [diff] [blame] | 14 | scoped_refptr<PictureLayer> PictureLayer::Create(ContentLayerClient* client) { |
[email protected] | d98c024 | 2012-11-08 06:22:35 | [diff] [blame] | 15 | return make_scoped_refptr(new PictureLayer(client)); |
| 16 | } |
| 17 | |
[email protected] | bf691c2 | 2013-03-26 21:15:06 | [diff] [blame] | 18 | PictureLayer::PictureLayer(ContentLayerClient* client) |
[email protected] | f6d55aa | 2014-03-11 20:42:53 | [diff] [blame^] | 19 | : client_(client), |
| 20 | pile_(make_scoped_refptr(new PicturePile())), |
| 21 | instrumentation_object_tracker_(id()), |
| 22 | is_mask_(false), |
| 23 | has_gpu_rasterization_hint_(false), |
| 24 | update_source_frame_number_(-1) {} |
[email protected] | d98c024 | 2012-11-08 06:22:35 | [diff] [blame] | 25 | |
| 26 | PictureLayer::~PictureLayer() { |
| 27 | } |
| 28 | |
[email protected] | 7aba666 | 2013-03-12 10:17:34 | [diff] [blame] | 29 | bool PictureLayer::DrawsContent() const { |
| 30 | return Layer::DrawsContent() && client_; |
[email protected] | d98c024 | 2012-11-08 06:22:35 | [diff] [blame] | 31 | } |
| 32 | |
[email protected] | 8c406cda3 | 2013-03-14 16:20:32 | [diff] [blame] | 33 | scoped_ptr<LayerImpl> PictureLayer::CreateLayerImpl(LayerTreeImpl* tree_impl) { |
| 34 | return PictureLayerImpl::Create(tree_impl, id()).PassAs<LayerImpl>(); |
[email protected] | d98c024 | 2012-11-08 06:22:35 | [diff] [blame] | 35 | } |
| 36 | |
[email protected] | 7aba666 | 2013-03-12 10:17:34 | [diff] [blame] | 37 | void PictureLayer::PushPropertiesTo(LayerImpl* base_layer) { |
| 38 | Layer::PushPropertiesTo(base_layer); |
[email protected] | 3621e18 | 2012-11-09 22:37:09 | [diff] [blame] | 39 | PictureLayerImpl* layer_impl = static_cast<PictureLayerImpl*>(base_layer); |
[email protected] | 2325768 | 2013-05-17 22:53:03 | [diff] [blame] | 40 | |
[email protected] | eda36f0 | 2013-10-23 08:15:31 | [diff] [blame] | 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. |
[email protected] | c736c1c20 | 2013-10-24 06:46:19 | [diff] [blame] | 45 | pile_->Resize(gfx::Size()); |
[email protected] | eda36f0 | 2013-10-23 08:15:31 | [diff] [blame] | 46 | pile_->UpdateRecordedRegion(); |
[email protected] | abe5134 | 2013-10-28 22:04:38 | [diff] [blame] | 47 | } else if (update_source_frame_number_ == |
| 48 | layer_tree_host()->source_frame_number()) { |
| 49 | // If update called, then pile size must match bounds pushed to impl layer. |
| 50 | DCHECK_EQ(layer_impl->bounds().ToString(), pile_->size().ToString()); |
[email protected] | eda36f0 | 2013-10-23 08:15:31 | [diff] [blame] | 51 | } |
| 52 | |
[email protected] | f677653 | 2012-12-21 20:24:33 | [diff] [blame] | 53 | layer_impl->SetIsMask(is_mask_); |
[email protected] | f6d55aa | 2014-03-11 20:42:53 | [diff] [blame^] | 54 | layer_impl->SetHasGpuRasterizationHint(has_gpu_rasterization_hint_); |
| 55 | |
[email protected] | 9053617 | 2013-05-14 00:23:06 | [diff] [blame] | 56 | // Unlike other properties, invalidation must always be set on layer_impl. |
| 57 | // See PictureLayerImpl::PushPropertiesTo for more details. |
[email protected] | f117a4c | 2012-12-16 04:53:10 | [diff] [blame] | 58 | layer_impl->invalidation_.Clear(); |
[email protected] | d002dd0 | 2013-03-27 07:40:40 | [diff] [blame] | 59 | layer_impl->invalidation_.Swap(&pile_invalidation_); |
[email protected] | 5946013 | 2013-06-26 20:04:02 | [diff] [blame] | 60 | layer_impl->pile_ = PicturePileImpl::CreateFromOther(pile_.get()); |
[email protected] | 3621e18 | 2012-11-09 22:37:09 | [diff] [blame] | 61 | } |
| 62 | |
[email protected] | 7aba666 | 2013-03-12 10:17:34 | [diff] [blame] | 63 | void PictureLayer::SetLayerTreeHost(LayerTreeHost* host) { |
| 64 | Layer::SetLayerTreeHost(host); |
[email protected] | f974be6 | 2013-02-28 19:12:26 | [diff] [blame] | 65 | if (host) { |
[email protected] | 8e0176d | 2013-03-21 03:14:52 | [diff] [blame] | 66 | pile_->SetMinContentsScale(host->settings().minimum_contents_scale); |
| 67 | pile_->SetTileGridSize(host->settings().default_tile_size); |
[email protected] | e6ac3a7 | 2013-03-13 03:50:53 | [diff] [blame] | 68 | pile_->set_slow_down_raster_scale_factor( |
[email protected] | 846f455b | 2013-03-18 19:07:41 | [diff] [blame] | 69 | host->debug_state().slow_down_raster_scale_factor); |
[email protected] | 334a772 | 2013-04-04 11:51:58 | [diff] [blame] | 70 | pile_->set_show_debug_picture_borders( |
| 71 | host->debug_state().show_picture_borders); |
[email protected] | f974be6 | 2013-02-28 19:12:26 | [diff] [blame] | 72 | } |
[email protected] | ce37a15 | 2013-01-08 17:12:33 | [diff] [blame] | 73 | } |
| 74 | |
[email protected] | 7aba666 | 2013-03-12 10:17:34 | [diff] [blame] | 75 | void PictureLayer::SetNeedsDisplayRect(const gfx::RectF& layer_rect) { |
[email protected] | 3621e18 | 2012-11-09 22:37:09 | [diff] [blame] | 76 | gfx::Rect rect = gfx::ToEnclosedRect(layer_rect); |
[email protected] | 7a9fc13 | 2013-01-10 00:54:58 | [diff] [blame] | 77 | if (!rect.IsEmpty()) { |
| 78 | // Clamp invalidation to the layer bounds. |
| 79 | rect.Intersect(gfx::Rect(bounds())); |
| 80 | pending_invalidation_.Union(rect); |
| 81 | } |
[email protected] | 7aba666 | 2013-03-12 10:17:34 | [diff] [blame] | 82 | Layer::SetNeedsDisplayRect(layer_rect); |
[email protected] | 3621e18 | 2012-11-09 22:37:09 | [diff] [blame] | 83 | } |
| 84 | |
[email protected] | c50b99729 | 2013-08-03 18:44:30 | [diff] [blame] | 85 | bool PictureLayer::Update(ResourceUpdateQueue* queue, |
[email protected] | 34ba1ffb | 2014-03-05 06:55:03 | [diff] [blame] | 86 | const OcclusionTracker<Layer>* occlusion) { |
[email protected] | abe5134 | 2013-10-28 22:04:38 | [diff] [blame] | 87 | update_source_frame_number_ = layer_tree_host()->source_frame_number(); |
[email protected] | c50b99729 | 2013-08-03 18:44:30 | [diff] [blame] | 88 | bool updated = Layer::Update(queue, occlusion); |
| 89 | |
[email protected] | 048ff15 | 2013-11-25 21:02:11 | [diff] [blame] | 90 | if (last_updated_visible_content_rect_ == visible_content_rect() && |
| 91 | pile_->size() == paint_properties().bounds && |
| 92 | pending_invalidation_.IsEmpty()) { |
| 93 | // Only early out if the visible content rect of this layer hasn't changed. |
| 94 | return updated; |
| 95 | } |
| 96 | |
[email protected] | 08ac6f9e | 2013-12-04 05:44:09 | [diff] [blame] | 97 | TRACE_EVENT1("cc", "PictureLayer::Update", |
| 98 | "source_frame_number", |
| 99 | layer_tree_host()->source_frame_number()); |
| 100 | |
[email protected] | 445881f | 2013-04-16 01:11:59 | [diff] [blame] | 101 | pile_->Resize(paint_properties().bounds); |
[email protected] | 0e630ea3 | 2012-11-28 03:29:17 | [diff] [blame] | 102 | |
| 103 | // Calling paint in WebKit can sometimes cause invalidations, so save |
| 104 | // off the invalidation prior to calling update. |
[email protected] | 78ad4554 | 2013-07-19 01:48:25 | [diff] [blame] | 105 | pending_invalidation_.Swap(&pile_invalidation_); |
[email protected] | 0e630ea3 | 2012-11-28 03:29:17 | [diff] [blame] | 106 | pending_invalidation_.Clear(); |
| 107 | |
[email protected] | 935ba27 | 2013-05-20 03:50:21 | [diff] [blame] | 108 | gfx::Rect visible_layer_rect = gfx::ScaleToEnclosingRect( |
| 109 | visible_content_rect(), 1.f / contents_scale_x()); |
[email protected] | c1079b53 | 2013-07-18 01:22:31 | [diff] [blame] | 110 | if (layer_tree_host()->settings().using_synchronous_renderer_compositor) { |
| 111 | // Workaround for http://crbug.com/235910 - to retain backwards compat |
| 112 | // the full page content must always be provided in the picture layer. |
| 113 | visible_layer_rect = gfx::Rect(bounds()); |
| 114 | } |
[email protected] | c50b99729 | 2013-08-03 18:44:30 | [diff] [blame] | 115 | updated |= pile_->Update(client_, |
| 116 | SafeOpaqueBackgroundColor(), |
| 117 | contents_opaque(), |
| 118 | pile_invalidation_, |
| 119 | visible_layer_rect, |
[email protected] | 2db9225 | 2013-12-10 22:30:31 | [diff] [blame] | 120 | update_source_frame_number_, |
[email protected] | c50b99729 | 2013-08-03 18:44:30 | [diff] [blame] | 121 | rendering_stats_instrumentation()); |
[email protected] | 048ff15 | 2013-11-25 21:02:11 | [diff] [blame] | 122 | last_updated_visible_content_rect_ = visible_content_rect(); |
| 123 | |
[email protected] | 214c8697 | 2013-08-20 23:43:06 | [diff] [blame] | 124 | if (updated) { |
| 125 | SetNeedsPushProperties(); |
| 126 | } else { |
[email protected] | 49304bde | 2013-07-08 21:31:22 | [diff] [blame] | 127 | // If this invalidation did not affect the pile, then it can be cleared as |
| 128 | // an optimization. |
| 129 | pile_invalidation_.Clear(); |
| 130 | } |
[email protected] | 214c8697 | 2013-08-20 23:43:06 | [diff] [blame] | 131 | |
[email protected] | 49304bde | 2013-07-08 21:31:22 | [diff] [blame] | 132 | return updated; |
[email protected] | d98c024 | 2012-11-08 06:22:35 | [diff] [blame] | 133 | } |
| 134 | |
[email protected] | 7aba666 | 2013-03-12 10:17:34 | [diff] [blame] | 135 | void PictureLayer::SetIsMask(bool is_mask) { |
[email protected] | f677653 | 2012-12-21 20:24:33 | [diff] [blame] | 136 | is_mask_ = is_mask; |
| 137 | } |
| 138 | |
[email protected] | f6d55aa | 2014-03-11 20:42:53 | [diff] [blame^] | 139 | void PictureLayer::SetHasGpuRasterizationHint(bool has_hint) { |
| 140 | DCHECK(IsPropertyChangeAllowed()); |
| 141 | if (has_gpu_rasterization_hint_ == has_hint) |
| 142 | return; |
| 143 | has_gpu_rasterization_hint_ = has_hint; |
| 144 | SetNeedsCommit(); |
| 145 | } |
| 146 | |
[email protected] | 7924c185 | 2013-05-24 16:18:43 | [diff] [blame] | 147 | bool PictureLayer::SupportsLCDText() const { |
| 148 | return true; |
| 149 | } |
| 150 | |
[email protected] | f7837a9 | 2013-08-21 03:00:05 | [diff] [blame] | 151 | skia::RefPtr<SkPicture> PictureLayer::GetPicture() const { |
| 152 | // We could either flatten the PicturePile into a single SkPicture, |
| 153 | // or paint a fresh one depending on what we intend to do with the |
| 154 | // picture. For now we just paint a fresh one to get consistent results. |
| 155 | if (!DrawsContent()) |
| 156 | return skia::RefPtr<SkPicture>(); |
| 157 | |
| 158 | int width = bounds().width(); |
| 159 | int height = bounds().height(); |
| 160 | gfx::RectF opaque; |
| 161 | |
| 162 | skia::RefPtr<SkPicture> picture = skia::AdoptRef(new SkPicture); |
| 163 | SkCanvas* canvas = picture->beginRecording(width, height); |
| 164 | client_->PaintContents(canvas, gfx::Rect(width, height), &opaque); |
| 165 | picture->endRecording(); |
| 166 | return picture; |
| 167 | } |
| 168 | |
[email protected] | 1a55d7dc | 2013-10-12 07:52:20 | [diff] [blame] | 169 | void PictureLayer::RunMicroBenchmark(MicroBenchmark* benchmark) { |
| 170 | benchmark->RunOnLayer(this); |
| 171 | } |
| 172 | |
[email protected] | d98c024 | 2012-11-08 06:22:35 | [diff] [blame] | 173 | } // namespace cc |