[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" |
danakj | 92015685 | 2015-05-18 20:22:29 | [diff] [blame] | 10 | #include "cc/playback/display_list_recording_source.h" |
jamesr | 9b8fda3 | 2015-03-16 19:11:05 | [diff] [blame] | 11 | #include "cc/trees/layer_tree_host.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 | |
loyso | a6edaaff | 2015-05-25 03:26:44 | [diff] [blame] | 18 | scoped_refptr<PictureLayer> PictureLayer::Create(const LayerSettings& settings, |
| 19 | ContentLayerClient* client) { |
| 20 | return make_scoped_refptr(new PictureLayer(settings, client)); |
[email protected] | d98c024 | 2012-11-08 06:22:35 | [diff] [blame] | 21 | } |
| 22 | |
loyso | a6edaaff | 2015-05-25 03:26:44 | [diff] [blame] | 23 | PictureLayer::PictureLayer(const LayerSettings& settings, |
| 24 | ContentLayerClient* client) |
| 25 | : Layer(settings), |
| 26 | client_(client), |
[email protected] | f6d55aa | 2014-03-11 20:42:53 | [diff] [blame] | 27 | instrumentation_object_tracker_(id()), |
[email protected] | 36df063 | 2014-06-12 20:26:15 | [diff] [blame] | 28 | update_source_frame_number_(-1), |
jackhou | 2422961 | 2014-12-13 23:41:00 | [diff] [blame] | 29 | is_mask_(false), |
| 30 | nearest_neighbor_(false) { |
[email protected] | 36df063 | 2014-06-12 20:26:15 | [diff] [blame] | 31 | } |
[email protected] | d98c024 | 2012-11-08 06:22:35 | [diff] [blame] | 32 | |
loyso | a6edaaff | 2015-05-25 03:26:44 | [diff] [blame] | 33 | PictureLayer::PictureLayer(const LayerSettings& settings, |
| 34 | ContentLayerClient* client, |
hendrikw | ddb87659a | 2015-10-06 20:23:37 | [diff] [blame] | 35 | scoped_ptr<DisplayListRecordingSource> source) |
loyso | a6edaaff | 2015-05-25 03:26:44 | [diff] [blame] | 36 | : PictureLayer(settings, client) { |
danakj | a04855a | 2015-11-18 20:39:10 | [diff] [blame] | 37 | recording_source_ = std::move(source); |
danakj | a819c255 | 2014-11-14 02:05:04 | [diff] [blame] | 38 | } |
| 39 | |
[email protected] | d98c024 | 2012-11-08 06:22:35 | [diff] [blame] | 40 | PictureLayer::~PictureLayer() { |
| 41 | } |
| 42 | |
[email protected] | 8c406cda3 | 2013-03-14 16:20:32 | [diff] [blame] | 43 | scoped_ptr<LayerImpl> PictureLayer::CreateLayerImpl(LayerTreeImpl* tree_impl) { |
aelias | d0070ba | 2015-01-31 13:44:49 | [diff] [blame] | 44 | return PictureLayerImpl::Create(tree_impl, id(), is_mask_, |
| 45 | new LayerImpl::SyncedScrollOffset); |
[email protected] | d98c024 | 2012-11-08 06:22:35 | [diff] [blame] | 46 | } |
| 47 | |
[email protected] | 7aba666 | 2013-03-12 10:17:34 | [diff] [blame] | 48 | void PictureLayer::PushPropertiesTo(LayerImpl* base_layer) { |
| 49 | Layer::PushPropertiesTo(base_layer); |
[email protected] | 3621e18 | 2012-11-09 22:37:09 | [diff] [blame] | 50 | PictureLayerImpl* layer_impl = static_cast<PictureLayerImpl*>(base_layer); |
danakj | a4ed6a2 | 2014-12-11 01:09:38 | [diff] [blame] | 51 | // TODO(danakj): Make is_mask_ a constructor parameter for PictureLayer. |
danakj | d193f6b | 2014-12-16 00:06:23 | [diff] [blame] | 52 | DCHECK_EQ(layer_impl->is_mask(), is_mask_); |
[email protected] | 2325768 | 2013-05-17 22:53:03 | [diff] [blame] | 53 | |
danakj | 4537797 | 2014-11-06 22:05:49 | [diff] [blame] | 54 | int source_frame_number = layer_tree_host()->source_frame_number(); |
| 55 | gfx::Size impl_bounds = layer_impl->bounds(); |
hendrikw | 9d909aa7 | 2014-11-11 20:19:52 | [diff] [blame] | 56 | gfx::Size recording_source_bounds = recording_source_->GetSize(); |
danakj | 4537797 | 2014-11-06 22:05:49 | [diff] [blame] | 57 | |
hendrikw | 312ee8ac | 2014-11-12 23:24:34 | [diff] [blame] | 58 | // If update called, then recording source size must match bounds pushed to |
| 59 | // impl layer. |
vmpstr | acfa922 | 2015-10-28 22:42:23 | [diff] [blame] | 60 | DCHECK(update_source_frame_number_ != source_frame_number || |
| 61 | impl_bounds == recording_source_bounds) |
hendrikw | 312ee8ac | 2014-11-12 23:24:34 | [diff] [blame] | 62 | << " bounds " << impl_bounds.ToString() << " recording source " |
hendrikw | 9d909aa7 | 2014-11-11 20:19:52 | [diff] [blame] | 63 | << recording_source_bounds.ToString(); |
danakj | 4537797 | 2014-11-06 22:05:49 | [diff] [blame] | 64 | |
| 65 | if (update_source_frame_number_ != source_frame_number && |
hendrikw | 9d909aa7 | 2014-11-11 20:19:52 | [diff] [blame] | 66 | recording_source_bounds != impl_bounds) { |
danakj | 4537797 | 2014-11-06 22:05:49 | [diff] [blame] | 67 | // 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] | 68 | // for example, even though it has resized making the recording source no |
| 69 | // longer valid. In this case just destroy the recording source. |
hendrikw | 9d909aa7 | 2014-11-11 20:19:52 | [diff] [blame] | 70 | recording_source_->SetEmptyBounds(); |
[email protected] | eda36f0 | 2013-10-23 08:15:31 | [diff] [blame] | 71 | } |
| 72 | |
jackhou | 2422961 | 2014-12-13 23:41:00 | [diff] [blame] | 73 | layer_impl->SetNearestNeighbor(nearest_neighbor_); |
| 74 | |
enne | af5bda3 | 2015-02-19 01:27:36 | [diff] [blame] | 75 | // Preserve lcd text settings from the current raster source. |
| 76 | bool can_use_lcd_text = layer_impl->RasterSourceUsesLCDText(); |
hendrikw | c7014b2 | 2015-10-19 23:45:09 | [diff] [blame] | 77 | scoped_refptr<DisplayListRasterSource> raster_source = |
enne | af5bda3 | 2015-02-19 01:27:36 | [diff] [blame] | 78 | recording_source_->CreateRasterSource(can_use_lcd_text); |
hendrikw | 757ba9e | 2015-03-23 21:13:09 | [diff] [blame] | 79 | layer_impl->set_gpu_raster_max_texture_size( |
| 80 | layer_tree_host()->device_viewport_size()); |
chrishtr | bb9af00 | 2015-12-09 02:00:11 | [diff] [blame^] | 81 | layer_impl->UpdateRasterSource(raster_source, invalidation_.region(), |
danakj | a4ed6a2 | 2014-12-11 01:09:38 | [diff] [blame] | 82 | nullptr); |
chrishtr | bb9af00 | 2015-12-09 02:00:11 | [diff] [blame^] | 83 | DCHECK(invalidation_.IsEmpty()); |
[email protected] | 3621e18 | 2012-11-09 22:37:09 | [diff] [blame] | 84 | } |
| 85 | |
[email protected] | 7aba666 | 2013-03-12 10:17:34 | [diff] [blame] | 86 | void PictureLayer::SetLayerTreeHost(LayerTreeHost* host) { |
| 87 | Layer::SetLayerTreeHost(host); |
enne | bdb6fd20 | 2015-03-10 06:01:57 | [diff] [blame] | 88 | if (!host) |
| 89 | return; |
| 90 | |
vmpstr | 5c4b681 | 2015-09-29 19:57:13 | [diff] [blame] | 91 | if (!recording_source_) |
| 92 | recording_source_.reset(new DisplayListRecordingSource); |
enne | bdb6fd20 | 2015-03-10 06:01:57 | [diff] [blame] | 93 | recording_source_->SetSlowdownRasterScaleFactor( |
| 94 | host->debug_state().slow_down_raster_scale_factor); |
vmpstr | dfd2286 | 2015-09-25 17:42:41 | [diff] [blame] | 95 | // If we need to enable image decode tasks, then we have to generate the |
| 96 | // discardable images metadata. |
vmpstr | 5c4b681 | 2015-09-29 19:57:13 | [diff] [blame] | 97 | const LayerTreeSettings& settings = layer_tree_host()->settings(); |
vmpstr | dfd2286 | 2015-09-25 17:42:41 | [diff] [blame] | 98 | recording_source_->SetGenerateDiscardableImagesMetadata( |
| 99 | settings.image_decode_tasks_enabled); |
[email protected] | ce37a15 | 2013-01-08 17:12:33 | [diff] [blame] | 100 | } |
| 101 | |
danakj | 19f0c9e | 2014-10-11 03:24:42 | [diff] [blame] | 102 | void PictureLayer::SetNeedsDisplayRect(const gfx::Rect& layer_rect) { |
chrishtr | bb9af00 | 2015-12-09 02:00:11 | [diff] [blame^] | 103 | DCHECK(!layer_tree_host() || !layer_tree_host()->in_paint_layer_contents()); |
danakj | 19f0c9e | 2014-10-11 03:24:42 | [diff] [blame] | 104 | if (!layer_rect.IsEmpty()) { |
[email protected] | 7a9fc13 | 2013-01-10 00:54:58 | [diff] [blame] | 105 | // Clamp invalidation to the layer bounds. |
chrishtr | bb9af00 | 2015-12-09 02:00:11 | [diff] [blame^] | 106 | invalidation_.Union(gfx::IntersectRects(layer_rect, gfx::Rect(bounds()))); |
[email protected] | 7a9fc13 | 2013-01-10 00:54:58 | [diff] [blame] | 107 | } |
[email protected] | 7aba666 | 2013-03-12 10:17:34 | [diff] [blame] | 108 | Layer::SetNeedsDisplayRect(layer_rect); |
[email protected] | 3621e18 | 2012-11-09 22:37:09 | [diff] [blame] | 109 | } |
| 110 | |
danakj | 5f46636a | 2015-06-19 00:01:40 | [diff] [blame] | 111 | bool PictureLayer::Update() { |
[email protected] | abe5134 | 2013-10-28 22:04:38 | [diff] [blame] | 112 | update_source_frame_number_ = layer_tree_host()->source_frame_number(); |
danakj | 5f46636a | 2015-06-19 00:01:40 | [diff] [blame] | 113 | bool updated = Layer::Update(); |
[email protected] | c50b99729 | 2013-08-03 18:44:30 | [diff] [blame] | 114 | |
danakj | 64767d90 | 2015-06-19 00:10:43 | [diff] [blame] | 115 | gfx::Rect update_rect = visible_layer_rect(); |
[email protected] | a108f5db | 2014-07-16 06:10:27 | [diff] [blame] | 116 | gfx::Size layer_size = paint_properties().bounds; |
[email protected] | 4c159895 | 2014-04-16 02:54:48 | [diff] [blame] | 117 | |
danakj | 64767d90 | 2015-06-19 00:10:43 | [diff] [blame] | 118 | if (last_updated_visible_layer_rect_ == update_rect && |
chrishtr | bb9af00 | 2015-12-09 02:00:11 | [diff] [blame^] | 119 | recording_source_->GetSize() == layer_size && invalidation_.IsEmpty()) { |
[email protected] | 048ff15 | 2013-11-25 21:02:11 | [diff] [blame] | 120 | // Only early out if the visible content rect of this layer hasn't changed. |
| 121 | return updated; |
| 122 | } |
| 123 | |
enne | ffe5781 | 2015-02-14 02:37:20 | [diff] [blame] | 124 | recording_source_->SetBackgroundColor(SafeOpaqueBackgroundColor()); |
| 125 | recording_source_->SetRequiresClear(!contents_opaque() && |
| 126 | !client_->FillsBoundsCompletely()); |
| 127 | |
[email protected] | 08ac6f9e | 2013-12-04 05:44:09 | [diff] [blame] | 128 | TRACE_EVENT1("cc", "PictureLayer::Update", |
| 129 | "source_frame_number", |
| 130 | layer_tree_host()->source_frame_number()); |
[email protected] | 8af1ce1 | 2014-06-15 12:18:36 | [diff] [blame] | 131 | devtools_instrumentation::ScopedLayerTreeTask update_layer( |
| 132 | devtools_instrumentation::kUpdateLayer, id(), layer_tree_host()->id()); |
[email protected] | 08ac6f9e | 2013-12-04 05:44:09 | [diff] [blame] | 133 | |
[email protected] | 8f322ba | 2014-06-18 23:48:18 | [diff] [blame] | 134 | // UpdateAndExpandInvalidation will give us an invalidation that covers |
| 135 | // anything not explicitly recorded in this frame. We give this region |
| 136 | // to the impl side so that it drops tiles that may not have a recording |
| 137 | // for them. |
[email protected] | 1d96e03 | 2014-03-25 05:59:08 | [diff] [blame] | 138 | DCHECK(client_); |
hendrikw | 9d909aa7 | 2014-11-11 20:19:52 | [diff] [blame] | 139 | updated |= recording_source_->UpdateAndExpandInvalidation( |
chrishtr | bb9af00 | 2015-12-09 02:00:11 | [diff] [blame^] | 140 | client_, invalidation_.region(), layer_size, update_rect, |
hendrikw | ddb87659a | 2015-10-06 20:23:37 | [diff] [blame] | 141 | update_source_frame_number_, DisplayListRecordingSource::RECORD_NORMALLY); |
danakj | 64767d90 | 2015-06-19 00:10:43 | [diff] [blame] | 142 | last_updated_visible_layer_rect_ = visible_layer_rect(); |
[email protected] | 048ff15 | 2013-11-25 21:02:11 | [diff] [blame] | 143 | |
[email protected] | 214c8697 | 2013-08-20 23:43:06 | [diff] [blame] | 144 | if (updated) { |
| 145 | SetNeedsPushProperties(); |
| 146 | } else { |
hendrikw | 312ee8ac | 2014-11-12 23:24:34 | [diff] [blame] | 147 | // If this invalidation did not affect the recording source, then it can be |
| 148 | // cleared as an optimization. |
chrishtr | bb9af00 | 2015-12-09 02:00:11 | [diff] [blame^] | 149 | invalidation_.Clear(); |
[email protected] | 49304bde | 2013-07-08 21:31:22 | [diff] [blame] | 150 | } |
[email protected] | 214c8697 | 2013-08-20 23:43:06 | [diff] [blame] | 151 | |
[email protected] | 49304bde | 2013-07-08 21:31:22 | [diff] [blame] | 152 | return updated; |
[email protected] | d98c024 | 2012-11-08 06:22:35 | [diff] [blame] | 153 | } |
| 154 | |
[email protected] | 7aba666 | 2013-03-12 10:17:34 | [diff] [blame] | 155 | void PictureLayer::SetIsMask(bool is_mask) { |
hendrikw | 4e8c632 | 2014-11-18 05:46:32 | [diff] [blame] | 156 | is_mask_ = is_mask; |
[email protected] | f677653 | 2012-12-21 20:24:33 | [diff] [blame] | 157 | } |
| 158 | |
[email protected] | f7837a9 | 2013-08-21 03:00:05 | [diff] [blame] | 159 | skia::RefPtr<SkPicture> PictureLayer::GetPicture() const { |
hendrikw | ddb87659a | 2015-10-06 20:23:37 | [diff] [blame] | 160 | // We could either flatten the DisplayListRecordingSource into a single |
| 161 | // SkPicture, or paint a fresh one depending on what we intend to do with the |
[email protected] | f7837a9 | 2013-08-21 03:00:05 | [diff] [blame] | 162 | // picture. For now we just paint a fresh one to get consistent results. |
| 163 | if (!DrawsContent()) |
| 164 | return skia::RefPtr<SkPicture>(); |
| 165 | |
pan.deng | 0bd2a73 | 2015-04-01 03:22:05 | [diff] [blame] | 166 | gfx::Size layer_size = bounds(); |
hendrikw | ddb87659a | 2015-10-06 20:23:37 | [diff] [blame] | 167 | scoped_ptr<DisplayListRecordingSource> recording_source( |
| 168 | new DisplayListRecordingSource); |
pdr | b42dfda3 | 2015-09-16 18:34:19 | [diff] [blame] | 169 | Region recording_invalidation; |
| 170 | recording_source->UpdateAndExpandInvalidation( |
| 171 | client_, &recording_invalidation, layer_size, gfx::Rect(layer_size), |
hendrikw | ddb87659a | 2015-10-06 20:23:37 | [diff] [blame] | 172 | update_source_frame_number_, DisplayListRecordingSource::RECORD_NORMALLY); |
pan.deng | 0bd2a73 | 2015-04-01 03:22:05 | [diff] [blame] | 173 | |
hendrikw | c7014b2 | 2015-10-19 23:45:09 | [diff] [blame] | 174 | scoped_refptr<DisplayListRasterSource> raster_source = |
pdr | b42dfda3 | 2015-09-16 18:34:19 | [diff] [blame] | 175 | recording_source->CreateRasterSource(false); |
pan.deng | 0bd2a73 | 2015-04-01 03:22:05 | [diff] [blame] | 176 | |
pdr | b42dfda3 | 2015-09-16 18:34:19 | [diff] [blame] | 177 | return raster_source->GetFlattenedPicture(); |
[email protected] | f7837a9 | 2013-08-21 03:00:05 | [diff] [blame] | 178 | } |
| 179 | |
[email protected] | a6c1b23 | 2014-05-05 23:53:08 | [diff] [blame] | 180 | bool PictureLayer::IsSuitableForGpuRasterization() const { |
hendrikw | 9d909aa7 | 2014-11-11 20:19:52 | [diff] [blame] | 181 | return recording_source_->IsSuitableForGpuRasterization(); |
[email protected] | a6c1b23 | 2014-05-05 23:53:08 | [diff] [blame] | 182 | } |
| 183 | |
[email protected] | ad63b2f | 2014-08-11 17:39:54 | [diff] [blame] | 184 | void PictureLayer::ClearClient() { |
kulkarni.a | 4015690f1 | 2014-10-10 13:50:06 | [diff] [blame] | 185 | client_ = nullptr; |
[email protected] | ad63b2f | 2014-08-11 17:39:54 | [diff] [blame] | 186 | UpdateDrawsContent(HasDrawableContent()); |
| 187 | } |
| 188 | |
jackhou | 2422961 | 2014-12-13 23:41:00 | [diff] [blame] | 189 | void PictureLayer::SetNearestNeighbor(bool nearest_neighbor) { |
| 190 | if (nearest_neighbor_ == nearest_neighbor) |
| 191 | return; |
| 192 | |
| 193 | nearest_neighbor_ = nearest_neighbor; |
| 194 | SetNeedsCommit(); |
| 195 | } |
| 196 | |
[email protected] | ad63b2f | 2014-08-11 17:39:54 | [diff] [blame] | 197 | bool PictureLayer::HasDrawableContent() const { |
| 198 | return client_ && Layer::HasDrawableContent(); |
| 199 | } |
| 200 | |
[email protected] | 1a55d7dc | 2013-10-12 07:52:20 | [diff] [blame] | 201 | void PictureLayer::RunMicroBenchmark(MicroBenchmark* benchmark) { |
| 202 | benchmark->RunOnLayer(this); |
| 203 | } |
| 204 | |
[email protected] | d98c024 | 2012-11-08 06:22:35 | [diff] [blame] | 205 | } // namespace cc |