ajuma | 5e77f7d4 | 2014-11-27 14:19:14 | [diff] [blame] | 1 | // Copyright 2014 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 | |
chrishtr | ac41ff9 | 2017-03-17 05:07:30 | [diff] [blame] | 5 | #include "cc/layers/recording_source.h" |
ajuma | 5e77f7d4 | 2014-11-27 14:19:14 | [diff] [blame] | 6 | |
avi | 02a4d17 | 2015-12-21 06:14:36 | [diff] [blame] | 7 | #include <stdint.h> |
| 8 | |
ajuma | 5e77f7d4 | 2014-11-27 14:19:14 | [diff] [blame] | 9 | #include <algorithm> |
| 10 | |
vmpstr | e2e1a4f0 | 2015-09-30 19:04:09 | [diff] [blame] | 11 | #include "base/numerics/safe_math.h" |
ajuma | 5e77f7d4 | 2014-11-27 14:19:14 | [diff] [blame] | 12 | #include "cc/base/region.h" |
| 13 | #include "cc/layers/content_layer_client.h" |
chrishtr | ac41ff9 | 2017-03-17 05:07:30 | [diff] [blame] | 14 | #include "cc/paint/display_item_list.h" |
Vladimir Levin | aee53910 | 2017-06-23 16:46:08 | [diff] [blame] | 15 | #include "cc/paint/solid_color_analyzer.h" |
chrishtr | ac41ff9 | 2017-03-17 05:07:30 | [diff] [blame] | 16 | #include "cc/raster/raster_source.h" |
ajuma | 5e77f7d4 | 2014-11-27 14:19:14 | [diff] [blame] | 17 | |
| 18 | namespace { |
| 19 | |
Eric Karl | 8efa3b7 | 2017-07-14 01:00:35 | [diff] [blame] | 20 | // We don't perform per-layer solid color analysis when there are too many skia |
| 21 | // operations. |
| 22 | const int kMaxOpsToAnalyzeForLayer = 10; |
| 23 | |
ajuma | 5e77f7d4 | 2014-11-27 14:19:14 | [diff] [blame] | 24 | } // namespace |
| 25 | |
| 26 | namespace cc { |
| 27 | |
vmpstr | e17fd21 | 2016-03-30 20:03:32 | [diff] [blame] | 28 | RecordingSource::RecordingSource() |
jbroman | 16d628c | 2015-05-29 20:11:59 | [diff] [blame] | 29 | : slow_down_raster_scale_factor_for_debug_(0), |
enne | ffe5781 | 2015-02-14 02:37:20 | [diff] [blame] | 30 | requires_clear_(false), |
ajuma | 5e77f7d4 | 2014-11-27 14:19:14 | [diff] [blame] | 31 | is_solid_color_(false), |
| 32 | solid_color_(SK_ColorTRANSPARENT), |
malaykeshav | 55c9f6d | 2017-08-03 01:05:43 | [diff] [blame] | 33 | background_color_(SK_ColorTRANSPARENT), |
| 34 | recording_scale_factor_(1.f) {} |
ajuma | 5e77f7d4 | 2014-11-27 14:19:14 | [diff] [blame] | 35 | |
Chris Watkins | f635329 | 2017-12-04 02:36:05 | [diff] [blame] | 36 | RecordingSource::~RecordingSource() = default; |
ajuma | 5e77f7d4 | 2014-11-27 14:19:14 | [diff] [blame] | 37 | |
vmpstr | e17fd21 | 2016-03-30 20:03:32 | [diff] [blame] | 38 | void RecordingSource::UpdateInvalidationForNewViewport( |
chrishtr | 01539b80 | 2015-11-24 08:11:32 | [diff] [blame] | 39 | const gfx::Rect& old_recorded_viewport, |
| 40 | const gfx::Rect& new_recorded_viewport, |
| 41 | Region* invalidation) { |
| 42 | // Invalidate newly-exposed and no-longer-exposed areas. |
| 43 | Region newly_exposed_region(new_recorded_viewport); |
| 44 | newly_exposed_region.Subtract(old_recorded_viewport); |
| 45 | invalidation->Union(newly_exposed_region); |
chrishtr | 877f8370 | 2015-07-01 18:22:25 | [diff] [blame] | 46 | |
chrishtr | 01539b80 | 2015-11-24 08:11:32 | [diff] [blame] | 47 | Region no_longer_exposed_region(old_recorded_viewport); |
| 48 | no_longer_exposed_region.Subtract(new_recorded_viewport); |
| 49 | invalidation->Union(no_longer_exposed_region); |
chrishtr | 877f8370 | 2015-07-01 18:22:25 | [diff] [blame] | 50 | } |
| 51 | |
vmpstr | e17fd21 | 2016-03-30 20:03:32 | [diff] [blame] | 52 | void RecordingSource::FinishDisplayItemListUpdate() { |
wangxianzhu | a382d93 | 2016-05-16 18:03:30 | [diff] [blame] | 53 | TRACE_EVENT0("cc", "RecordingSource::FinishDisplayItemListUpdate"); |
dtrainor | fb0a162 | 2015-12-11 16:20:15 | [diff] [blame] | 54 | DetermineIfSolidColor(); |
| 55 | display_list_->EmitTraceSnapshot(); |
vmpstr | 5cff918d | 2017-04-27 17:48:35 | [diff] [blame] | 56 | display_list_->GenerateDiscardableImagesMetadata(); |
dtrainor | fb0a162 | 2015-12-11 16:20:15 | [diff] [blame] | 57 | } |
| 58 | |
vmpstr | e17fd21 | 2016-03-30 20:03:32 | [diff] [blame] | 59 | void RecordingSource::SetNeedsDisplayRect(const gfx::Rect& layer_rect) { |
chrishtr | c41aca7b | 2016-03-18 15:44:29 | [diff] [blame] | 60 | if (!layer_rect.IsEmpty()) { |
| 61 | // Clamp invalidation to the layer bounds. |
| 62 | invalidation_.Union(gfx::IntersectRects(layer_rect, gfx::Rect(size_))); |
| 63 | } |
| 64 | } |
| 65 | |
vmpstr | e17fd21 | 2016-03-30 20:03:32 | [diff] [blame] | 66 | bool RecordingSource::UpdateAndExpandInvalidation( |
ajuma | 5e77f7d4 | 2014-11-27 14:19:14 | [diff] [blame] | 67 | Region* invalidation, |
ajuma | 5e77f7d4 | 2014-11-27 14:19:14 | [diff] [blame] | 68 | const gfx::Size& layer_size, |
mlliu | 51126b7 | 2016-08-02 22:18:24 | [diff] [blame] | 69 | const gfx::Rect& new_recorded_viewport) { |
ajuma | 5e77f7d4 | 2014-11-27 14:19:14 | [diff] [blame] | 70 | bool updated = false; |
| 71 | |
chrishtr | 7271c4e6 | 2016-04-21 22:34:46 | [diff] [blame] | 72 | if (size_ != layer_size) |
ajuma | 5e77f7d4 | 2014-11-27 14:19:14 | [diff] [blame] | 73 | size_ = layer_size; |
ajuma | 5e77f7d4 | 2014-11-27 14:19:14 | [diff] [blame] | 74 | |
chrishtr | c41aca7b | 2016-03-18 15:44:29 | [diff] [blame] | 75 | invalidation_.Swap(invalidation); |
| 76 | invalidation_.Clear(); |
| 77 | |
chrishtr | 01539b80 | 2015-11-24 08:11:32 | [diff] [blame] | 78 | if (new_recorded_viewport != recorded_viewport_) { |
| 79 | UpdateInvalidationForNewViewport(recorded_viewport_, new_recorded_viewport, |
| 80 | invalidation); |
| 81 | recorded_viewport_ = new_recorded_viewport; |
ajuma | 5e77f7d4 | 2014-11-27 14:19:14 | [diff] [blame] | 82 | updated = true; |
| 83 | } |
| 84 | |
| 85 | if (!updated && !invalidation->Intersects(recorded_viewport_)) |
| 86 | return false; |
| 87 | |
chrishtr | c41aca7b | 2016-03-18 15:44:29 | [diff] [blame] | 88 | if (invalidation->IsEmpty()) |
| 89 | return false; |
| 90 | |
mlliu | 51126b7 | 2016-08-02 22:18:24 | [diff] [blame] | 91 | return true; |
| 92 | } |
schenney | 0154bfa | 2015-02-05 19:46:49 | [diff] [blame] | 93 | |
mlliu | 51126b7 | 2016-08-02 22:18:24 | [diff] [blame] | 94 | void RecordingSource::UpdateDisplayItemList( |
| 95 | const scoped_refptr<DisplayItemList>& display_list, |
F#m | 57abc3e | 2017-11-30 01:49:35 | [diff] [blame] | 96 | const size_t& painter_reported_memory_usage, |
| 97 | float recording_scale_factor) { |
| 98 | recording_scale_factor_ = recording_scale_factor; |
| 99 | |
mlliu | 51126b7 | 2016-08-02 22:18:24 | [diff] [blame] | 100 | display_list_ = display_list; |
| 101 | painter_reported_memory_usage_ = painter_reported_memory_usage; |
Bartosz Fabianowski | 85a82381 | 2015-04-16 10:27:51 | [diff] [blame] | 102 | |
dtrainor | fb0a162 | 2015-12-11 16:20:15 | [diff] [blame] | 103 | FinishDisplayItemListUpdate(); |
ajuma | 5e77f7d4 | 2014-11-27 14:19:14 | [diff] [blame] | 104 | } |
| 105 | |
vmpstr | e17fd21 | 2016-03-30 20:03:32 | [diff] [blame] | 106 | gfx::Size RecordingSource::GetSize() const { |
ajuma | 5e77f7d4 | 2014-11-27 14:19:14 | [diff] [blame] | 107 | return size_; |
| 108 | } |
| 109 | |
vmpstr | e17fd21 | 2016-03-30 20:03:32 | [diff] [blame] | 110 | void RecordingSource::SetEmptyBounds() { |
ajuma | 5e77f7d4 | 2014-11-27 14:19:14 | [diff] [blame] | 111 | size_ = gfx::Size(); |
mlliu | 51126b7 | 2016-08-02 22:18:24 | [diff] [blame] | 112 | is_solid_color_ = false; |
| 113 | |
| 114 | recorded_viewport_ = gfx::Rect(); |
| 115 | display_list_ = nullptr; |
| 116 | painter_reported_memory_usage_ = 0; |
ajuma | 5e77f7d4 | 2014-11-27 14:19:14 | [diff] [blame] | 117 | } |
| 118 | |
vmpstr | e17fd21 | 2016-03-30 20:03:32 | [diff] [blame] | 119 | void RecordingSource::SetSlowdownRasterScaleFactor(int factor) { |
ajuma | 5e77f7d4 | 2014-11-27 14:19:14 | [diff] [blame] | 120 | slow_down_raster_scale_factor_for_debug_ = factor; |
| 121 | } |
| 122 | |
vmpstr | e17fd21 | 2016-03-30 20:03:32 | [diff] [blame] | 123 | void RecordingSource::SetBackgroundColor(SkColor background_color) { |
enne | ffe5781 | 2015-02-14 02:37:20 | [diff] [blame] | 124 | background_color_ = background_color; |
| 125 | } |
| 126 | |
vmpstr | e17fd21 | 2016-03-30 20:03:32 | [diff] [blame] | 127 | void RecordingSource::SetRequiresClear(bool requires_clear) { |
enne | ffe5781 | 2015-02-14 02:37:20 | [diff] [blame] | 128 | requires_clear_ = requires_clear; |
| 129 | } |
| 130 | |
Vladimir Levin | 55c6640 | 2017-07-13 02:21:06 | [diff] [blame] | 131 | scoped_refptr<RasterSource> RecordingSource::CreateRasterSource() const { |
| 132 | return scoped_refptr<RasterSource>(new RasterSource(this)); |
ajuma | 5e77f7d4 | 2014-11-27 14:19:14 | [diff] [blame] | 133 | } |
| 134 | |
vmpstr | e17fd21 | 2016-03-30 20:03:32 | [diff] [blame] | 135 | void RecordingSource::DetermineIfSolidColor() { |
pdr | 8a191b47 | 2015-09-22 22:25:37 | [diff] [blame] | 136 | DCHECK(display_list_); |
ajuma | 5e77f7d4 | 2014-11-27 14:19:14 | [diff] [blame] | 137 | is_solid_color_ = false; |
| 138 | solid_color_ = SK_ColorTRANSPARENT; |
| 139 | |
Xianzhu Wang | 02333c5 | 2018-04-06 03:17:22 | [diff] [blame] | 140 | if (display_list_->TotalOpCount() > kMaxOpsToAnalyzeForLayer) |
ajuma | 5e77f7d4 | 2014-11-27 14:19:14 | [diff] [blame] | 141 | return; |
| 142 | |
wangxianzhu | a382d93 | 2016-05-16 18:03:30 | [diff] [blame] | 143 | TRACE_EVENT1("cc", "RecordingSource::DetermineIfSolidColor", "opcount", |
Xianzhu Wang | 02333c5 | 2018-04-06 03:17:22 | [diff] [blame] | 144 | display_list_->TotalOpCount()); |
Eric Karl | 8efa3b7 | 2017-07-14 01:00:35 | [diff] [blame] | 145 | is_solid_color_ = display_list_->GetColorIfSolidInRect( |
F#m | 6e66c748 | 2017-09-20 01:22:35 | [diff] [blame] | 146 | gfx::ScaleToRoundedRect(gfx::Rect(GetSize()), recording_scale_factor_), |
| 147 | &solid_color_, kMaxOpsToAnalyzeForLayer); |
ajuma | 5e77f7d4 | 2014-11-27 14:19:14 | [diff] [blame] | 148 | } |
| 149 | |
ajuma | 5e77f7d4 | 2014-11-27 14:19:14 | [diff] [blame] | 150 | } // namespace cc |