| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "cc/playback/recording_source.h" | 5 #include "cc/playback/recording_source.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 | 8 |
| 9 #include <algorithm> | 9 #include <algorithm> |
| 10 | 10 |
| 11 #include "base/numerics/safe_math.h" | 11 #include "base/numerics/safe_math.h" |
| 12 #include "cc/base/region.h" | 12 #include "cc/base/region.h" |
| 13 #include "cc/layers/content_layer_client.h" | 13 #include "cc/layers/content_layer_client.h" |
| 14 #include "cc/playback/display_item_list.h" | 14 #include "cc/playback/display_item_list.h" |
| 15 #include "cc/playback/raster_source.h" | 15 #include "cc/playback/raster_source.h" |
| 16 #include "cc/proto/gfx_conversions.h" | 16 #include "cc/proto/gfx_conversions.h" |
| 17 #include "cc/proto/recording_source.pb.h" | 17 #include "cc/proto/recording_source.pb.h" |
| 18 #include "skia/ext/analysis_canvas.h" | 18 #include "skia/ext/analysis_canvas.h" |
| 19 | 19 |
| 20 namespace { | |
| 21 | |
| 22 #ifdef NDEBUG | |
| 23 const bool kDefaultClearCanvasSetting = false; | |
| 24 #else | |
| 25 const bool kDefaultClearCanvasSetting = true; | |
| 26 #endif | |
| 27 | |
| 28 } // namespace | |
| 29 | 20 |
| 30 namespace cc { | 21 namespace cc { |
| 31 | 22 |
| 32 RecordingSource::RecordingSource() | 23 PictureLayerData::PictureLayerData() = default; |
| 33 : slow_down_raster_scale_factor_for_debug_(0), | 24 |
| 34 generate_discardable_images_metadata_(false), | 25 PictureLayerData::~PictureLayerData() = default; |
| 35 requires_clear_(false), | 26 |
| 36 is_solid_color_(false), | 27 ContentLayerClientData::ContentLayerClientData() = default; |
| 37 clear_canvas_with_debug_color_(kDefaultClearCanvasSetting), | 28 |
| 38 solid_color_(SK_ColorTRANSPARENT), | 29 ContentLayerClientData::ContentLayerClientData(const ContentLayerClientData&) = |
| 39 background_color_(SK_ColorTRANSPARENT), | 30 default; |
| 40 painter_reported_memory_usage_(0) {} | 31 |
| 32 ContentLayerClientData::~ContentLayerClientData() = default; |
| 41 | 33 |
| 42 RecordingSource::~RecordingSource() {} | 34 RecordingSource::~RecordingSource() {} |
| 43 | 35 |
| 44 void RecordingSource::ToProtobuf(proto::RecordingSource* proto) const { | 36 RecordingSource::RecordingSource(const PictureLayerData& pl_data, |
| 45 RectToProto(recorded_viewport_, proto->mutable_recorded_viewport()); | 37 const ContentLayerClientData& clc_data) |
| 46 SizeToProto(size_, proto->mutable_size()); | 38 : recorded_viewport_(clc_data.recorded_viewport), |
| 47 proto->set_slow_down_raster_scale_factor_for_debug( | 39 size_(pl_data.size), |
| 48 slow_down_raster_scale_factor_for_debug_); | 40 slow_down_raster_scale_factor_for_debug_( |
| 49 proto->set_generate_discardable_images_metadata( | 41 pl_data.slow_down_raster_scale_factor_for_debug), |
| 50 generate_discardable_images_metadata_); | 42 generate_discardable_images_metadata_( |
| 51 proto->set_requires_clear(requires_clear_); | 43 pl_data.generate_discardable_images_metadata), |
| 52 proto->set_is_solid_color(is_solid_color_); | 44 requires_clear_(pl_data.requires_clear), |
| 53 proto->set_clear_canvas_with_debug_color(clear_canvas_with_debug_color_); | 45 is_solid_color_(pl_data.is_solid_color), |
| 54 proto->set_solid_color(static_cast<uint64_t>(solid_color_)); | 46 clear_canvas_with_debug_color_(pl_data.clear_canvas_with_debug_color), |
| 55 proto->set_background_color(static_cast<uint64_t>(background_color_)); | 47 solid_color_(pl_data.solid_color), |
| 56 if (display_list_) | 48 background_color_(pl_data.background_color), |
| 57 display_list_->ToProtobuf(proto->mutable_display_list()); | 49 display_list_(clc_data.display_list), |
| 58 } | 50 painter_reported_memory_usage_(clc_data.painter_reported_memory_usage) {} |
| 59 | |
| 60 void RecordingSource::FromProtobuf( | |
| 61 const proto::RecordingSource& proto, | |
| 62 ClientPictureCache* client_picture_cache, | |
| 63 std::vector<uint32_t>* used_engine_picture_ids) { | |
| 64 DCHECK(client_picture_cache); | |
| 65 recorded_viewport_ = ProtoToRect(proto.recorded_viewport()); | |
| 66 size_ = ProtoToSize(proto.size()); | |
| 67 slow_down_raster_scale_factor_for_debug_ = | |
| 68 proto.slow_down_raster_scale_factor_for_debug(); | |
| 69 generate_discardable_images_metadata_ = | |
| 70 proto.generate_discardable_images_metadata(); | |
| 71 requires_clear_ = proto.requires_clear(); | |
| 72 is_solid_color_ = proto.is_solid_color(); | |
| 73 clear_canvas_with_debug_color_ = proto.clear_canvas_with_debug_color(); | |
| 74 solid_color_ = static_cast<SkColor>(proto.solid_color()); | |
| 75 background_color_ = static_cast<SkColor>(proto.background_color()); | |
| 76 | |
| 77 // This might not exist if the |display_list_| of the serialized | |
| 78 // RecordingSource was null, wich can happen if |Clear()| is | |
| 79 // called. | |
| 80 if (proto.has_display_list()) { | |
| 81 display_list_ = DisplayItemList::CreateFromProto( | |
| 82 proto.display_list(), client_picture_cache, used_engine_picture_ids); | |
| 83 FinishDisplayItemListUpdate(); | |
| 84 } else { | |
| 85 display_list_ = nullptr; | |
| 86 } | |
| 87 } | |
| 88 | |
| 89 void RecordingSource::UpdateInvalidationForNewViewport( | |
| 90 const gfx::Rect& old_recorded_viewport, | |
| 91 const gfx::Rect& new_recorded_viewport, | |
| 92 Region* invalidation) { | |
| 93 // Invalidate newly-exposed and no-longer-exposed areas. | |
| 94 Region newly_exposed_region(new_recorded_viewport); | |
| 95 newly_exposed_region.Subtract(old_recorded_viewport); | |
| 96 invalidation->Union(newly_exposed_region); | |
| 97 | |
| 98 Region no_longer_exposed_region(old_recorded_viewport); | |
| 99 no_longer_exposed_region.Subtract(new_recorded_viewport); | |
| 100 invalidation->Union(no_longer_exposed_region); | |
| 101 } | |
| 102 | |
| 103 void RecordingSource::FinishDisplayItemListUpdate() { | |
| 104 TRACE_EVENT0("cc", "RecordingSource::FinishDisplayItemListUpdate"); | |
| 105 DetermineIfSolidColor(); | |
| 106 display_list_->EmitTraceSnapshot(); | |
| 107 if (generate_discardable_images_metadata_) | |
| 108 display_list_->GenerateDiscardableImagesMetadata(); | |
| 109 } | |
| 110 | |
| 111 void RecordingSource::SetNeedsDisplayRect(const gfx::Rect& layer_rect) { | |
| 112 if (!layer_rect.IsEmpty()) { | |
| 113 // Clamp invalidation to the layer bounds. | |
| 114 invalidation_.Union(gfx::IntersectRects(layer_rect, gfx::Rect(size_))); | |
| 115 } | |
| 116 } | |
| 117 | |
| 118 bool RecordingSource::UpdateAndExpandInvalidation( | |
| 119 ContentLayerClient* painter, | |
| 120 Region* invalidation, | |
| 121 const gfx::Size& layer_size, | |
| 122 int frame_number, | |
| 123 RecordingMode recording_mode) { | |
| 124 bool updated = false; | |
| 125 | |
| 126 if (size_ != layer_size) | |
| 127 size_ = layer_size; | |
| 128 | |
| 129 invalidation_.Swap(invalidation); | |
| 130 invalidation_.Clear(); | |
| 131 | |
| 132 gfx::Rect new_recorded_viewport = painter->PaintableRegion(); | |
| 133 if (new_recorded_viewport != recorded_viewport_) { | |
| 134 UpdateInvalidationForNewViewport(recorded_viewport_, new_recorded_viewport, | |
| 135 invalidation); | |
| 136 recorded_viewport_ = new_recorded_viewport; | |
| 137 updated = true; | |
| 138 } | |
| 139 | |
| 140 if (!updated && !invalidation->Intersects(recorded_viewport_)) | |
| 141 return false; | |
| 142 | |
| 143 if (invalidation->IsEmpty()) | |
| 144 return false; | |
| 145 | |
| 146 ContentLayerClient::PaintingControlSetting painting_control = | |
| 147 ContentLayerClient::PAINTING_BEHAVIOR_NORMAL; | |
| 148 | |
| 149 switch (recording_mode) { | |
| 150 case RECORD_NORMALLY: | |
| 151 // Already setup for normal recording. | |
| 152 break; | |
| 153 case RECORD_WITH_PAINTING_DISABLED: | |
| 154 painting_control = ContentLayerClient::DISPLAY_LIST_PAINTING_DISABLED; | |
| 155 break; | |
| 156 case RECORD_WITH_CACHING_DISABLED: | |
| 157 painting_control = ContentLayerClient::DISPLAY_LIST_CACHING_DISABLED; | |
| 158 break; | |
| 159 case RECORD_WITH_CONSTRUCTION_DISABLED: | |
| 160 painting_control = ContentLayerClient::DISPLAY_LIST_CONSTRUCTION_DISABLED; | |
| 161 break; | |
| 162 case RECORD_WITH_SUBSEQUENCE_CACHING_DISABLED: | |
| 163 painting_control = ContentLayerClient::SUBSEQUENCE_CACHING_DISABLED; | |
| 164 break; | |
| 165 case RECORD_WITH_SK_NULL_CANVAS: | |
| 166 case RECORDING_MODE_COUNT: | |
| 167 NOTREACHED(); | |
| 168 } | |
| 169 | |
| 170 // TODO(vmpstr): Add a slow_down_recording_scale_factor_for_debug_ to be able | |
| 171 // to slow down recording. | |
| 172 display_list_ = painter->PaintContentsToDisplayList(painting_control); | |
| 173 painter_reported_memory_usage_ = painter->GetApproximateUnsharedMemoryUsage(); | |
| 174 | |
| 175 FinishDisplayItemListUpdate(); | |
| 176 | |
| 177 return true; | |
| 178 } | |
| 179 | |
| 180 gfx::Size RecordingSource::GetSize() const { | |
| 181 return size_; | |
| 182 } | |
| 183 | |
| 184 void RecordingSource::SetEmptyBounds() { | |
| 185 size_ = gfx::Size(); | |
| 186 Clear(); | |
| 187 } | |
| 188 | |
| 189 void RecordingSource::SetSlowdownRasterScaleFactor(int factor) { | |
| 190 slow_down_raster_scale_factor_for_debug_ = factor; | |
| 191 } | |
| 192 | |
| 193 void RecordingSource::SetGenerateDiscardableImagesMetadata( | |
| 194 bool generate_metadata) { | |
| 195 generate_discardable_images_metadata_ = generate_metadata; | |
| 196 } | |
| 197 | |
| 198 void RecordingSource::SetBackgroundColor(SkColor background_color) { | |
| 199 background_color_ = background_color; | |
| 200 } | |
| 201 | |
| 202 void RecordingSource::SetRequiresClear(bool requires_clear) { | |
| 203 requires_clear_ = requires_clear; | |
| 204 } | |
| 205 | |
| 206 bool RecordingSource::IsSuitableForGpuRasterization() const { | |
| 207 // The display list needs to be created (see: UpdateAndExpandInvalidation) | |
| 208 // before checking for suitability. There are cases where an update will not | |
| 209 // create a display list (e.g., if the size is empty). We return true in these | |
| 210 // cases because the gpu suitability bit sticks false. | |
| 211 return !display_list_ || display_list_->IsSuitableForGpuRasterization(); | |
| 212 } | |
| 213 | |
| 214 const DisplayItemList* RecordingSource::GetDisplayItemList() { | |
| 215 return display_list_.get(); | |
| 216 } | |
| 217 | 51 |
| 218 scoped_refptr<RasterSource> RecordingSource::CreateRasterSource( | 52 scoped_refptr<RasterSource> RecordingSource::CreateRasterSource( |
| 219 bool can_use_lcd_text) const { | 53 bool can_use_lcd_text) const { |
| 220 return scoped_refptr<RasterSource>( | 54 return scoped_refptr<RasterSource>( |
| 221 RasterSource::CreateFromRecordingSource(this, can_use_lcd_text)); | 55 RasterSource::CreateFromRecordingSource(this, can_use_lcd_text)); |
| 222 } | 56 } |
| 223 | 57 |
| 224 void RecordingSource::DetermineIfSolidColor() { | |
| 225 DCHECK(display_list_); | |
| 226 is_solid_color_ = false; | |
| 227 solid_color_ = SK_ColorTRANSPARENT; | |
| 228 | 58 |
| 229 if (!display_list_->ShouldBeAnalyzedForSolidColor()) | |
| 230 return; | |
| 231 | 59 |
| 232 TRACE_EVENT1("cc", "RecordingSource::DetermineIfSolidColor", "opcount", | |
| 233 display_list_->ApproximateOpCount()); | |
| 234 gfx::Size layer_size = GetSize(); | |
| 235 skia::AnalysisCanvas canvas(layer_size.width(), layer_size.height()); | |
| 236 display_list_->Raster(&canvas, nullptr, gfx::Rect(), 1.f); | |
| 237 is_solid_color_ = canvas.GetColorIfSolid(&solid_color_); | |
| 238 } | |
| 239 | |
| 240 void RecordingSource::Clear() { | |
| 241 recorded_viewport_ = gfx::Rect(); | |
| 242 display_list_ = nullptr; | |
| 243 painter_reported_memory_usage_ = 0; | |
| 244 is_solid_color_ = false; | |
| 245 } | |
| 246 | 60 |
| 247 } // namespace cc | 61 } // namespace cc |
| OLD | NEW |