[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] | 4ea293f7 | 2014-08-13 03:03:17 | [diff] [blame] | 7 | #include "base/auto_reset.h" |
[email protected] | f7837a9 | 2013-08-21 03:00:05 | [diff] [blame] | 8 | #include "cc/layers/content_layer_client.h" |
[email protected] | cc3cfaa | 2013-03-18 09:05:52 | [diff] [blame] | 9 | #include "cc/layers/picture_layer_impl.h" |
ajuma | 5e77f7d4 | 2014-11-27 14:19:14 | [diff] [blame] | 10 | #include "cc/resources/display_list_recording_source.h" |
hendrikw | 312ee8ac | 2014-11-12 23:24:34 | [diff] [blame] | 11 | #include "cc/resources/picture_pile.h" |
[email protected] | 556fd29 | 2013-03-18 08:03:04 | [diff] [blame] | 12 | #include "cc/trees/layer_tree_impl.h" |
[email protected] | de14c2c | 2014-04-24 01:02:25 | [diff] [blame] | 13 | #include "third_party/skia/include/core/SkPictureRecorder.h" |
heejin.r.chung | d28506ba | 2014-10-23 16:36:20 | [diff] [blame] | 14 | #include "ui/gfx/geometry/rect_conversions.h" |
[email protected] | d98c024 | 2012-11-08 06:22:35 | [diff] [blame] | 15 | |
| 16 | namespace cc { |
| 17 | |
[email protected] | 7aba666 | 2013-03-12 10:17:34 | [diff] [blame] | 18 | scoped_refptr<PictureLayer> PictureLayer::Create(ContentLayerClient* client) { |
[email protected] | d98c024 | 2012-11-08 06:22:35 | [diff] [blame] | 19 | return make_scoped_refptr(new PictureLayer(client)); |
| 20 | } |
| 21 | |
[email protected] | bf691c2 | 2013-03-26 21:15:06 | [diff] [blame] | 22 | PictureLayer::PictureLayer(ContentLayerClient* client) |
[email protected] | f6d55aa | 2014-03-11 20:42:53 | [diff] [blame] | 23 | : client_(client), |
[email protected] | f6d55aa | 2014-03-11 20:42:53 | [diff] [blame] | 24 | instrumentation_object_tracker_(id()), |
[email protected] | 36df063 | 2014-06-12 20:26:15 | [diff] [blame] | 25 | update_source_frame_number_(-1), |
jackhou | 2422961 | 2014-12-13 23:41:00 | [diff] [blame] | 26 | is_mask_(false), |
| 27 | nearest_neighbor_(false) { |
[email protected] | 36df063 | 2014-06-12 20:26:15 | [diff] [blame] | 28 | } |
[email protected] | d98c024 | 2012-11-08 06:22:35 | [diff] [blame] | 29 | |
danakj | a819c255 | 2014-11-14 02:05:04 | [diff] [blame] | 30 | PictureLayer::PictureLayer(ContentLayerClient* client, |
| 31 | scoped_ptr<RecordingSource> source) |
| 32 | : PictureLayer(client) { |
| 33 | recording_source_ = source.Pass(); |
| 34 | } |
| 35 | |
[email protected] | d98c024 | 2012-11-08 06:22:35 | [diff] [blame] | 36 | PictureLayer::~PictureLayer() { |
| 37 | } |
| 38 | |
[email protected] | 8c406cda3 | 2013-03-14 16:20:32 | [diff] [blame] | 39 | scoped_ptr<LayerImpl> PictureLayer::CreateLayerImpl(LayerTreeImpl* tree_impl) { |
aelias | d0070ba | 2015-01-31 13:44:49 | [diff] [blame] | 40 | return PictureLayerImpl::Create(tree_impl, id(), is_mask_, |
| 41 | new LayerImpl::SyncedScrollOffset); |
[email protected] | d98c024 | 2012-11-08 06:22:35 | [diff] [blame] | 42 | } |
| 43 | |
[email protected] | 7aba666 | 2013-03-12 10:17:34 | [diff] [blame] | 44 | void PictureLayer::PushPropertiesTo(LayerImpl* base_layer) { |
| 45 | Layer::PushPropertiesTo(base_layer); |
[email protected] | 3621e18 | 2012-11-09 22:37:09 | [diff] [blame] | 46 | PictureLayerImpl* layer_impl = static_cast<PictureLayerImpl*>(base_layer); |
danakj | a4ed6a2 | 2014-12-11 01:09:38 | [diff] [blame] | 47 | // TODO(danakj): Make is_mask_ a constructor parameter for PictureLayer. |
danakj | d193f6b | 2014-12-16 00:06:23 | [diff] [blame] | 48 | DCHECK_EQ(layer_impl->is_mask(), is_mask_); |
[email protected] | 2325768 | 2013-05-17 22:53:03 | [diff] [blame] | 49 | |
danakj | 4537797 | 2014-11-06 22:05:49 | [diff] [blame] | 50 | int source_frame_number = layer_tree_host()->source_frame_number(); |
| 51 | gfx::Size impl_bounds = layer_impl->bounds(); |
hendrikw | 9d909aa7 | 2014-11-11 20:19:52 | [diff] [blame] | 52 | gfx::Size recording_source_bounds = recording_source_->GetSize(); |
danakj | 4537797 | 2014-11-06 22:05:49 | [diff] [blame] | 53 | |
hendrikw | 312ee8ac | 2014-11-12 23:24:34 | [diff] [blame] | 54 | // If update called, then recording source size must match bounds pushed to |
| 55 | // impl layer. |
danakj | 4537797 | 2014-11-06 22:05:49 | [diff] [blame] | 56 | DCHECK_IMPLIES(update_source_frame_number_ == source_frame_number, |
hendrikw | 9d909aa7 | 2014-11-11 20:19:52 | [diff] [blame] | 57 | impl_bounds == recording_source_bounds) |
hendrikw | 312ee8ac | 2014-11-12 23:24:34 | [diff] [blame] | 58 | << " bounds " << impl_bounds.ToString() << " recording source " |
hendrikw | 9d909aa7 | 2014-11-11 20:19:52 | [diff] [blame] | 59 | << recording_source_bounds.ToString(); |
danakj | 4537797 | 2014-11-06 22:05:49 | [diff] [blame] | 60 | |
| 61 | if (update_source_frame_number_ != source_frame_number && |
hendrikw | 9d909aa7 | 2014-11-11 20:19:52 | [diff] [blame] | 62 | recording_source_bounds != impl_bounds) { |
danakj | 4537797 | 2014-11-06 22:05:49 | [diff] [blame] | 63 | // Update may not get called for the layer (if it's not in the viewport |
hendrikw | 312ee8ac | 2014-11-12 23:24:34 | [diff] [blame] | 64 | // for example, even though it has resized making the recording source no |
| 65 | // longer valid. In this case just destroy the recording source. |
hendrikw | 9d909aa7 | 2014-11-11 20:19:52 | [diff] [blame] | 66 | recording_source_->SetEmptyBounds(); |
[email protected] | eda36f0 | 2013-10-23 08:15:31 | [diff] [blame] | 67 | } |
| 68 | |
jackhou | 2422961 | 2014-12-13 23:41:00 | [diff] [blame] | 69 | layer_impl->SetNearestNeighbor(nearest_neighbor_); |
| 70 | |
enne | af5bda3 | 2015-02-19 01:27:36 | [diff] [blame^] | 71 | // Preserve lcd text settings from the current raster source. |
| 72 | bool can_use_lcd_text = layer_impl->RasterSourceUsesLCDText(); |
hendrikw | 4e8c632 | 2014-11-18 05:46:32 | [diff] [blame] | 73 | scoped_refptr<RasterSource> raster_source = |
enne | af5bda3 | 2015-02-19 01:27:36 | [diff] [blame^] | 74 | recording_source_->CreateRasterSource(can_use_lcd_text); |
danakj | a4ed6a2 | 2014-12-11 01:09:38 | [diff] [blame] | 75 | layer_impl->UpdateRasterSource(raster_source, &recording_invalidation_, |
| 76 | nullptr); |
| 77 | DCHECK(recording_invalidation_.IsEmpty()); |
[email protected] | 3621e18 | 2012-11-09 22:37:09 | [diff] [blame] | 78 | } |
| 79 | |
[email protected] | 7aba666 | 2013-03-12 10:17:34 | [diff] [blame] | 80 | void PictureLayer::SetLayerTreeHost(LayerTreeHost* host) { |
| 81 | Layer::SetLayerTreeHost(host); |
[email protected] | f974be6 | 2013-02-28 19:12:26 | [diff] [blame] | 82 | if (host) { |
ajuma | 5e77f7d4 | 2014-11-27 14:19:14 | [diff] [blame] | 83 | if (!recording_source_) { |
| 84 | if (host->settings().use_display_lists) { |
| 85 | recording_source_.reset(new DisplayListRecordingSource); |
| 86 | } else { |
hendrikw | 751c4ac | 2015-01-27 00:59:26 | [diff] [blame] | 87 | recording_source_.reset( |
| 88 | new PicturePile(host->settings().minimum_contents_scale, |
| 89 | host->settings().default_tile_grid_size)); |
ajuma | 5e77f7d4 | 2014-11-27 14:19:14 | [diff] [blame] | 90 | } |
| 91 | } |
hendrikw | 9d909aa7 | 2014-11-11 20:19:52 | [diff] [blame] | 92 | recording_source_->SetSlowdownRasterScaleFactor( |
[email protected] | 846f455b | 2013-03-18 19:07:41 | [diff] [blame] | 93 | host->debug_state().slow_down_raster_scale_factor); |
[email protected] | f974be6 | 2013-02-28 19:12:26 | [diff] [blame] | 94 | } |
[email protected] | ce37a15 | 2013-01-08 17:12:33 | [diff] [blame] | 95 | } |
| 96 | |
danakj | 19f0c9e | 2014-10-11 03:24:42 | [diff] [blame] | 97 | void PictureLayer::SetNeedsDisplayRect(const gfx::Rect& layer_rect) { |
| 98 | if (!layer_rect.IsEmpty()) { |
[email protected] | 7a9fc13 | 2013-01-10 00:54:58 | [diff] [blame] | 99 | // Clamp invalidation to the layer bounds. |
danakj | 19f0c9e | 2014-10-11 03:24:42 | [diff] [blame] | 100 | pending_invalidation_.Union( |
| 101 | gfx::IntersectRects(layer_rect, gfx::Rect(bounds()))); |
[email protected] | 7a9fc13 | 2013-01-10 00:54:58 | [diff] [blame] | 102 | } |
[email protected] | 7aba666 | 2013-03-12 10:17:34 | [diff] [blame] | 103 | Layer::SetNeedsDisplayRect(layer_rect); |
[email protected] | 3621e18 | 2012-11-09 22:37:09 | [diff] [blame] | 104 | } |
| 105 | |
[email protected] | c50b99729 | 2013-08-03 18:44:30 | [diff] [blame] | 106 | bool PictureLayer::Update(ResourceUpdateQueue* queue, |
[email protected] | 34ba1ffb | 2014-03-05 06:55:03 | [diff] [blame] | 107 | const OcclusionTracker<Layer>* occlusion) { |
[email protected] | abe5134 | 2013-10-28 22:04:38 | [diff] [blame] | 108 | update_source_frame_number_ = layer_tree_host()->source_frame_number(); |
[email protected] | c50b99729 | 2013-08-03 18:44:30 | [diff] [blame] | 109 | bool updated = Layer::Update(queue, occlusion); |
| 110 | |
[email protected] | 4c159895 | 2014-04-16 02:54:48 | [diff] [blame] | 111 | gfx::Rect visible_layer_rect = gfx::ScaleToEnclosingRect( |
| 112 | visible_content_rect(), 1.f / contents_scale_x()); |
[email protected] | a108f5db | 2014-07-16 06:10:27 | [diff] [blame] | 113 | gfx::Size layer_size = paint_properties().bounds; |
[email protected] | 4c159895 | 2014-04-16 02:54:48 | [diff] [blame] | 114 | |
[email protected] | 048ff15 | 2013-11-25 21:02:11 | [diff] [blame] | 115 | if (last_updated_visible_content_rect_ == visible_content_rect() && |
enne | af5bda3 | 2015-02-19 01:27:36 | [diff] [blame^] | 116 | recording_source_->GetSize() == layer_size && |
hendrikw | 9d909aa7 | 2014-11-11 20:19:52 | [diff] [blame] | 117 | pending_invalidation_.IsEmpty()) { |
[email protected] | 048ff15 | 2013-11-25 21:02:11 | [diff] [blame] | 118 | // Only early out if the visible content rect of this layer hasn't changed. |
| 119 | return updated; |
| 120 | } |
| 121 | |
enne | ffe5781 | 2015-02-14 02:37:20 | [diff] [blame] | 122 | recording_source_->SetBackgroundColor(SafeOpaqueBackgroundColor()); |
| 123 | recording_source_->SetRequiresClear(!contents_opaque() && |
| 124 | !client_->FillsBoundsCompletely()); |
| 125 | |
[email protected] | 08ac6f9e | 2013-12-04 05:44:09 | [diff] [blame] | 126 | TRACE_EVENT1("cc", "PictureLayer::Update", |
| 127 | "source_frame_number", |
| 128 | layer_tree_host()->source_frame_number()); |
[email protected] | 8af1ce1 | 2014-06-15 12:18:36 | [diff] [blame] | 129 | devtools_instrumentation::ScopedLayerTreeTask update_layer( |
| 130 | devtools_instrumentation::kUpdateLayer, id(), layer_tree_host()->id()); |
[email protected] | 08ac6f9e | 2013-12-04 05:44:09 | [diff] [blame] | 131 | |
[email protected] | 0e630ea3 | 2012-11-28 03:29:17 | [diff] [blame] | 132 | // Calling paint in WebKit can sometimes cause invalidations, so save |
| 133 | // off the invalidation prior to calling update. |
hendrikw | 9d909aa7 | 2014-11-11 20:19:52 | [diff] [blame] | 134 | pending_invalidation_.Swap(&recording_invalidation_); |
[email protected] | 0e630ea3 | 2012-11-28 03:29:17 | [diff] [blame] | 135 | pending_invalidation_.Clear(); |
| 136 | |
[email protected] | 8a59280 | 2014-07-02 07:31:33 | [diff] [blame] | 137 | if (layer_tree_host()->settings().record_full_layer) { |
[email protected] | c1079b53 | 2013-07-18 01:22:31 | [diff] [blame] | 138 | // Workaround for http://crbug.com/235910 - to retain backwards compat |
| 139 | // the full page content must always be provided in the picture layer. |
[email protected] | a108f5db | 2014-07-16 06:10:27 | [diff] [blame] | 140 | visible_layer_rect = gfx::Rect(layer_size); |
[email protected] | c1079b53 | 2013-07-18 01:22:31 | [diff] [blame] | 141 | } |
[email protected] | 8f322ba | 2014-06-18 23:48:18 | [diff] [blame] | 142 | |
| 143 | // UpdateAndExpandInvalidation will give us an invalidation that covers |
| 144 | // anything not explicitly recorded in this frame. We give this region |
| 145 | // to the impl side so that it drops tiles that may not have a recording |
| 146 | // for them. |
[email protected] | 1d96e03 | 2014-03-25 05:59:08 | [diff] [blame] | 147 | DCHECK(client_); |
hendrikw | 9d909aa7 | 2014-11-11 20:19:52 | [diff] [blame] | 148 | updated |= recording_source_->UpdateAndExpandInvalidation( |
enne | af5bda3 | 2015-02-19 01:27:36 | [diff] [blame^] | 149 | client_, &recording_invalidation_, layer_size, visible_layer_rect, |
| 150 | update_source_frame_number_, RecordingSource::RECORD_NORMALLY); |
[email protected] | 048ff15 | 2013-11-25 21:02:11 | [diff] [blame] | 151 | last_updated_visible_content_rect_ = visible_content_rect(); |
| 152 | |
[email protected] | 214c8697 | 2013-08-20 23:43:06 | [diff] [blame] | 153 | if (updated) { |
| 154 | SetNeedsPushProperties(); |
| 155 | } else { |
hendrikw | 312ee8ac | 2014-11-12 23:24:34 | [diff] [blame] | 156 | // If this invalidation did not affect the recording source, then it can be |
| 157 | // cleared as an optimization. |
hendrikw | 9d909aa7 | 2014-11-11 20:19:52 | [diff] [blame] | 158 | recording_invalidation_.Clear(); |
[email protected] | 49304bde | 2013-07-08 21:31:22 | [diff] [blame] | 159 | } |
[email protected] | 214c8697 | 2013-08-20 23:43:06 | [diff] [blame] | 160 | |
[email protected] | 49304bde | 2013-07-08 21:31:22 | [diff] [blame] | 161 | return updated; |
[email protected] | d98c024 | 2012-11-08 06:22:35 | [diff] [blame] | 162 | } |
| 163 | |
[email protected] | 7aba666 | 2013-03-12 10:17:34 | [diff] [blame] | 164 | void PictureLayer::SetIsMask(bool is_mask) { |
hendrikw | 4e8c632 | 2014-11-18 05:46:32 | [diff] [blame] | 165 | is_mask_ = is_mask; |
[email protected] | f677653 | 2012-12-21 20:24:33 | [diff] [blame] | 166 | } |
| 167 | |
[email protected] | f7837a9 | 2013-08-21 03:00:05 | [diff] [blame] | 168 | skia::RefPtr<SkPicture> PictureLayer::GetPicture() const { |
hendrikw | 312ee8ac | 2014-11-12 23:24:34 | [diff] [blame] | 169 | // We could either flatten the RecordingSource into a single SkPicture, |
[email protected] | f7837a9 | 2013-08-21 03:00:05 | [diff] [blame] | 170 | // or paint a fresh one depending on what we intend to do with the |
| 171 | // picture. For now we just paint a fresh one to get consistent results. |
| 172 | if (!DrawsContent()) |
| 173 | return skia::RefPtr<SkPicture>(); |
| 174 | |
| 175 | int width = bounds().width(); |
| 176 | int height = bounds().height(); |
[email protected] | f7837a9 | 2013-08-21 03:00:05 | [diff] [blame] | 177 | |
[email protected] | e93fbdd | 2014-04-16 17:34:21 | [diff] [blame] | 178 | SkPictureRecorder recorder; |
kulkarni.a | 4015690f1 | 2014-10-10 13:50:06 | [diff] [blame] | 179 | SkCanvas* canvas = recorder.beginRecording(width, height, nullptr, 0); |
schenney | 0154bfa | 2015-02-05 19:46:49 | [diff] [blame] | 180 | client_->PaintContents(canvas, gfx::Rect(width, height), |
| 181 | ContentLayerClient::PAINTING_BEHAVIOR_NORMAL); |
[email protected] | e93fbdd | 2014-04-16 17:34:21 | [diff] [blame] | 182 | skia::RefPtr<SkPicture> picture = skia::AdoptRef(recorder.endRecording()); |
[email protected] | f7837a9 | 2013-08-21 03:00:05 | [diff] [blame] | 183 | return picture; |
| 184 | } |
| 185 | |
[email protected] | a6c1b23 | 2014-05-05 23:53:08 | [diff] [blame] | 186 | bool PictureLayer::IsSuitableForGpuRasterization() const { |
hendrikw | 9d909aa7 | 2014-11-11 20:19:52 | [diff] [blame] | 187 | return recording_source_->IsSuitableForGpuRasterization(); |
[email protected] | a6c1b23 | 2014-05-05 23:53:08 | [diff] [blame] | 188 | } |
| 189 | |
[email protected] | ad63b2f | 2014-08-11 17:39:54 | [diff] [blame] | 190 | void PictureLayer::ClearClient() { |
kulkarni.a | 4015690f1 | 2014-10-10 13:50:06 | [diff] [blame] | 191 | client_ = nullptr; |
[email protected] | ad63b2f | 2014-08-11 17:39:54 | [diff] [blame] | 192 | UpdateDrawsContent(HasDrawableContent()); |
| 193 | } |
| 194 | |
jackhou | 2422961 | 2014-12-13 23:41:00 | [diff] [blame] | 195 | void PictureLayer::SetNearestNeighbor(bool nearest_neighbor) { |
| 196 | if (nearest_neighbor_ == nearest_neighbor) |
| 197 | return; |
| 198 | |
| 199 | nearest_neighbor_ = nearest_neighbor; |
| 200 | SetNeedsCommit(); |
| 201 | } |
| 202 | |
[email protected] | ad63b2f | 2014-08-11 17:39:54 | [diff] [blame] | 203 | bool PictureLayer::HasDrawableContent() const { |
| 204 | return client_ && Layer::HasDrawableContent(); |
| 205 | } |
| 206 | |
[email protected] | 1a55d7dc | 2013-10-12 07:52:20 | [diff] [blame] | 207 | void PictureLayer::RunMicroBenchmark(MicroBenchmark* benchmark) { |
| 208 | benchmark->RunOnLayer(this); |
| 209 | } |
| 210 | |
[email protected] | d98c024 | 2012-11-08 06:22:35 | [diff] [blame] | 211 | } // namespace cc |