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/display_item_list.h" | 5 #include "cc/playback/display_item_list.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/numerics/safe_conversions.h" | 9 #include "base/numerics/safe_conversions.h" |
10 #include "base/trace_event/trace_event.h" | 10 #include "base/trace_event/trace_event.h" |
11 #include "base/trace_event/trace_event_argument.h" | 11 #include "base/trace_event/trace_event_argument.h" |
12 #include "cc/base/math_util.h" | 12 #include "cc/base/math_util.h" |
13 #include "cc/debug/picture_debug_util.h" | 13 #include "cc/debug/picture_debug_util.h" |
14 #include "cc/debug/traced_display_item_list.h" | 14 #include "cc/debug/traced_display_item_list.h" |
15 #include "cc/debug/traced_value.h" | 15 #include "cc/debug/traced_value.h" |
16 #include "cc/playback/display_item_list_settings.h" | 16 #include "cc/playback/display_item_list_settings.h" |
| 17 #include "cc/playback/display_item_proto_factory.h" |
17 #include "cc/playback/largest_display_item.h" | 18 #include "cc/playback/largest_display_item.h" |
| 19 #include "cc/proto/display_item.pb.h" |
| 20 #include "cc/proto/gfx_conversions.h" |
18 #include "third_party/skia/include/core/SkCanvas.h" | 21 #include "third_party/skia/include/core/SkCanvas.h" |
19 #include "third_party/skia/include/core/SkPictureRecorder.h" | 22 #include "third_party/skia/include/core/SkPictureRecorder.h" |
20 #include "third_party/skia/include/utils/SkPictureUtils.h" | 23 #include "third_party/skia/include/utils/SkPictureUtils.h" |
21 #include "ui/gfx/skia_util.h" | 24 #include "ui/gfx/skia_util.h" |
22 | 25 |
23 namespace cc { | 26 namespace cc { |
24 | 27 |
25 namespace { | 28 namespace { |
26 | 29 |
27 // We don't perform per-layer solid color analysis when there are too many skia | 30 // We don't perform per-layer solid color analysis when there are too many skia |
(...skipping 12 matching lines...) Expand all Loading... |
40 } // namespace | 43 } // namespace |
41 | 44 |
42 scoped_refptr<DisplayItemList> DisplayItemList::Create( | 45 scoped_refptr<DisplayItemList> DisplayItemList::Create( |
43 const gfx::Rect& layer_rect, | 46 const gfx::Rect& layer_rect, |
44 const DisplayItemListSettings& settings) { | 47 const DisplayItemListSettings& settings) { |
45 return make_scoped_refptr(new DisplayItemList( | 48 return make_scoped_refptr(new DisplayItemList( |
46 layer_rect, settings, | 49 layer_rect, settings, |
47 !settings.use_cached_picture || DisplayItemsTracingEnabled())); | 50 !settings.use_cached_picture || DisplayItemsTracingEnabled())); |
48 } | 51 } |
49 | 52 |
| 53 scoped_refptr<DisplayItemList> DisplayItemList::CreateFromProto( |
| 54 const proto::DisplayItemList& proto) { |
| 55 gfx::Rect layer_rect = ProtoToRect(proto.layer_rect()); |
| 56 scoped_refptr<DisplayItemList> list = |
| 57 DisplayItemList::Create(ProtoToRect(proto.layer_rect()), |
| 58 DisplayItemListSettings(proto.settings())); |
| 59 |
| 60 for (int i = 0; i < proto.items_size(); i++) { |
| 61 const proto::DisplayItem& item_proto = proto.items(i); |
| 62 DisplayItem* item = |
| 63 DisplayItemProtoFactory::AllocateAndConstruct(list, item_proto); |
| 64 if (item) |
| 65 item->FromProtobuf(item_proto); |
| 66 } |
| 67 |
| 68 return list; |
| 69 } |
| 70 |
50 DisplayItemList::DisplayItemList(gfx::Rect layer_rect, | 71 DisplayItemList::DisplayItemList(gfx::Rect layer_rect, |
51 const DisplayItemListSettings& settings, | 72 const DisplayItemListSettings& settings, |
52 bool retain_individual_display_items) | 73 bool retain_individual_display_items) |
53 : items_(LargestDisplayItemSize(), kDefaultNumDisplayItemsToReserve), | 74 : items_(LargestDisplayItemSize(), kDefaultNumDisplayItemsToReserve), |
54 use_cached_picture_(settings.use_cached_picture), | 75 settings_(settings), |
55 retain_individual_display_items_(retain_individual_display_items), | 76 retain_individual_display_items_(retain_individual_display_items), |
56 layer_rect_(layer_rect), | 77 layer_rect_(layer_rect), |
57 is_suitable_for_gpu_rasterization_(true), | 78 is_suitable_for_gpu_rasterization_(true), |
58 approximate_op_count_(0), | 79 approximate_op_count_(0), |
59 picture_memory_usage_(0), | 80 picture_memory_usage_(0), |
60 external_memory_usage_(0) { | 81 external_memory_usage_(0) { |
61 #if DCHECK_IS_ON() | 82 #if DCHECK_IS_ON() |
62 needs_process_ = false; | 83 needs_process_ = false; |
63 #endif | 84 #endif |
64 if (use_cached_picture_) { | 85 if (settings_.use_cached_picture) { |
65 SkRTreeFactory factory; | 86 SkRTreeFactory factory; |
66 recorder_.reset(new SkPictureRecorder()); | 87 recorder_.reset(new SkPictureRecorder()); |
67 canvas_ = skia::SharePtr(recorder_->beginRecording( | 88 canvas_ = skia::SharePtr(recorder_->beginRecording( |
68 layer_rect_.width(), layer_rect_.height(), &factory)); | 89 layer_rect_.width(), layer_rect_.height(), &factory)); |
69 canvas_->translate(-layer_rect_.x(), -layer_rect_.y()); | 90 canvas_->translate(-layer_rect_.x(), -layer_rect_.y()); |
70 canvas_->clipRect(gfx::RectToSkRect(layer_rect_)); | 91 canvas_->clipRect(gfx::RectToSkRect(layer_rect_)); |
71 } | 92 } |
72 } | 93 } |
73 | 94 |
74 DisplayItemList::~DisplayItemList() { | 95 DisplayItemList::~DisplayItemList() { |
75 } | 96 } |
76 | 97 |
| 98 void DisplayItemList::ToProtobuf(proto::DisplayItemList* proto) { |
| 99 RectToProto(layer_rect_, proto->mutable_layer_rect()); |
| 100 settings_.ToProtobuf(proto->mutable_settings()); |
| 101 |
| 102 DCHECK_EQ(0, proto->items_size()); |
| 103 for (auto* item : items_) |
| 104 item->ToProtobuf(proto->add_items()); |
| 105 } |
| 106 |
77 void DisplayItemList::Raster(SkCanvas* canvas, | 107 void DisplayItemList::Raster(SkCanvas* canvas, |
78 SkPicture::AbortCallback* callback, | 108 SkPicture::AbortCallback* callback, |
79 const gfx::Rect& canvas_target_playback_rect, | 109 const gfx::Rect& canvas_target_playback_rect, |
80 float contents_scale) const { | 110 float contents_scale) const { |
81 DCHECK(ProcessAppendedItemsCalled()); | 111 DCHECK(ProcessAppendedItemsCalled()); |
82 if (!use_cached_picture_) { | 112 if (!settings_.use_cached_picture) { |
83 canvas->save(); | 113 canvas->save(); |
84 canvas->scale(contents_scale, contents_scale); | 114 canvas->scale(contents_scale, contents_scale); |
85 for (auto* item : items_) | 115 for (auto* item : items_) |
86 item->Raster(canvas, canvas_target_playback_rect, callback); | 116 item->Raster(canvas, canvas_target_playback_rect, callback); |
87 canvas->restore(); | 117 canvas->restore(); |
88 } else { | 118 } else { |
89 DCHECK(picture_); | 119 DCHECK(picture_); |
90 | 120 |
91 canvas->save(); | 121 canvas->save(); |
92 canvas->scale(contents_scale, contents_scale); | 122 canvas->scale(contents_scale, contents_scale); |
(...skipping 22 matching lines...) Expand all Loading... |
115 // after |items_| grows too large and we process it. | 145 // after |items_| grows too large and we process it. |
116 DCHECK(items_.empty()); | 146 DCHECK(items_.empty()); |
117 } | 147 } |
118 } | 148 } |
119 | 149 |
120 void DisplayItemList::ProcessAppendedItems() { | 150 void DisplayItemList::ProcessAppendedItems() { |
121 #if DCHECK_IS_ON() | 151 #if DCHECK_IS_ON() |
122 needs_process_ = false; | 152 needs_process_ = false; |
123 #endif | 153 #endif |
124 for (const DisplayItem* item : items_) { | 154 for (const DisplayItem* item : items_) { |
125 if (use_cached_picture_) { | 155 if (settings_.use_cached_picture) { |
126 // When using a cached picture we will calculate gpu suitability on the | 156 // When using a cached picture we will calculate gpu suitability on the |
127 // entire cached picture instead of the items. This is more permissive | 157 // entire cached picture instead of the items. This is more permissive |
128 // since none of the items might individually trigger a veto even though | 158 // since none of the items might individually trigger a veto even though |
129 // they collectively have enough "bad" operations that a corresponding | 159 // they collectively have enough "bad" operations that a corresponding |
130 // Picture would get vetoed. See crbug.com/513016. | 160 // Picture would get vetoed. See crbug.com/513016. |
131 DCHECK(canvas_); | 161 DCHECK(canvas_); |
132 approximate_op_count_ += item->approximate_op_count(); | 162 approximate_op_count_ += item->approximate_op_count(); |
133 item->Raster(canvas_.get(), gfx::Rect(), nullptr); | 163 item->Raster(canvas_.get(), gfx::Rect(), nullptr); |
134 } else { | 164 } else { |
135 is_suitable_for_gpu_rasterization_ &= | 165 is_suitable_for_gpu_rasterization_ &= |
(...skipping 22 matching lines...) Expand all Loading... |
158 | 188 |
159 bool DisplayItemList::RetainsIndividualDisplayItems() const { | 189 bool DisplayItemList::RetainsIndividualDisplayItems() const { |
160 return retain_individual_display_items_; | 190 return retain_individual_display_items_; |
161 } | 191 } |
162 | 192 |
163 void DisplayItemList::RemoveLast() { | 193 void DisplayItemList::RemoveLast() { |
164 // We cannot remove the last item if it has been squashed into a picture. | 194 // We cannot remove the last item if it has been squashed into a picture. |
165 // The last item should not have been handled by ProcessAppendedItems, so we | 195 // The last item should not have been handled by ProcessAppendedItems, so we |
166 // don't need to remove it from approximate_op_count_, etc. | 196 // don't need to remove it from approximate_op_count_, etc. |
167 DCHECK(retain_individual_display_items_); | 197 DCHECK(retain_individual_display_items_); |
168 DCHECK(!use_cached_picture_); | 198 DCHECK(!settings_.use_cached_picture); |
169 items_.RemoveLast(); | 199 items_.RemoveLast(); |
170 } | 200 } |
171 | 201 |
172 void DisplayItemList::Finalize() { | 202 void DisplayItemList::Finalize() { |
173 ProcessAppendedItems(); | 203 ProcessAppendedItems(); |
174 | 204 |
175 if (use_cached_picture_) { | 205 if (settings_.use_cached_picture) { |
176 // Convert to an SkPicture for faster rasterization. | 206 // Convert to an SkPicture for faster rasterization. |
177 DCHECK(use_cached_picture_); | 207 DCHECK(settings_.use_cached_picture); |
178 DCHECK(!picture_); | 208 DCHECK(!picture_); |
179 picture_ = skia::AdoptRef(recorder_->endRecordingAsPicture()); | 209 picture_ = skia::AdoptRef(recorder_->endRecordingAsPicture()); |
180 DCHECK(picture_); | 210 DCHECK(picture_); |
181 picture_memory_usage_ = | 211 picture_memory_usage_ = |
182 SkPictureUtils::ApproximateBytesUsed(picture_.get()); | 212 SkPictureUtils::ApproximateBytesUsed(picture_.get()); |
183 recorder_.reset(); | 213 recorder_.reset(); |
184 canvas_.clear(); | 214 canvas_.clear(); |
185 is_suitable_for_gpu_rasterization_ = | 215 is_suitable_for_gpu_rasterization_ = |
186 picture_->suitableForGpuRasterization(nullptr); | 216 picture_->suitableForGpuRasterization(nullptr); |
187 } | 217 } |
188 } | 218 } |
189 | 219 |
190 bool DisplayItemList::IsSuitableForGpuRasterization() const { | 220 bool DisplayItemList::IsSuitableForGpuRasterization() const { |
191 DCHECK(ProcessAppendedItemsCalled()); | 221 DCHECK(ProcessAppendedItemsCalled()); |
192 return is_suitable_for_gpu_rasterization_; | 222 return is_suitable_for_gpu_rasterization_; |
193 } | 223 } |
194 | 224 |
195 int DisplayItemList::ApproximateOpCount() const { | 225 int DisplayItemList::ApproximateOpCount() const { |
196 DCHECK(ProcessAppendedItemsCalled()); | 226 DCHECK(ProcessAppendedItemsCalled()); |
197 return approximate_op_count_; | 227 return approximate_op_count_; |
198 } | 228 } |
199 | 229 |
200 size_t DisplayItemList::ApproximateMemoryUsage() const { | 230 size_t DisplayItemList::ApproximateMemoryUsage() const { |
201 DCHECK(ProcessAppendedItemsCalled()); | 231 DCHECK(ProcessAppendedItemsCalled()); |
202 // We double-count in this case. Produce zero to avoid being misleading. | 232 // We double-count in this case. Produce zero to avoid being misleading. |
203 if (use_cached_picture_ && retain_individual_display_items_) | 233 if (settings_.use_cached_picture && retain_individual_display_items_) |
204 return 0; | 234 return 0; |
205 | 235 |
206 DCHECK_IMPLIES(use_cached_picture_, picture_); | 236 DCHECK_IMPLIES(settings_.use_cached_picture, picture_); |
207 | 237 |
208 size_t memory_usage = sizeof(*this); | 238 size_t memory_usage = sizeof(*this); |
209 | 239 |
210 // Memory outside this class due to |items_|. | 240 // Memory outside this class due to |items_|. |
211 memory_usage += items_.GetCapacityInBytes() + external_memory_usage_; | 241 memory_usage += items_.GetCapacityInBytes() + external_memory_usage_; |
212 | 242 |
213 // Memory outside this class due to |picture|. | 243 // Memory outside this class due to |picture|. |
214 memory_usage += picture_memory_usage_; | 244 memory_usage += picture_memory_usage_; |
215 | 245 |
216 // TODO(jbroman): Does anything else owned by this class substantially | 246 // TODO(jbroman): Does anything else owned by this class substantially |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
266 TRACE_DISABLED_BY_DEFAULT("devtools.timeline.picture"), | 296 TRACE_DISABLED_BY_DEFAULT("devtools.timeline.picture"), |
267 "cc::DisplayItemList", this, | 297 "cc::DisplayItemList", this, |
268 TracedDisplayItemList::AsTraceableDisplayItemList(this, | 298 TracedDisplayItemList::AsTraceableDisplayItemList(this, |
269 DisplayItemsTracingEnabled())); | 299 DisplayItemsTracingEnabled())); |
270 } | 300 } |
271 | 301 |
272 void DisplayItemList::GenerateDiscardableImagesMetadata() { | 302 void DisplayItemList::GenerateDiscardableImagesMetadata() { |
273 DCHECK(ProcessAppendedItemsCalled()); | 303 DCHECK(ProcessAppendedItemsCalled()); |
274 // This should be only called once, and only after CreateAndCacheSkPicture. | 304 // This should be only called once, and only after CreateAndCacheSkPicture. |
275 DCHECK(image_map_.empty()); | 305 DCHECK(image_map_.empty()); |
276 DCHECK_IMPLIES(use_cached_picture_, picture_); | 306 DCHECK_IMPLIES(settings_.use_cached_picture, picture_); |
277 if (use_cached_picture_ && !picture_->willPlayBackBitmaps()) | 307 if (settings_.use_cached_picture && !picture_->willPlayBackBitmaps()) |
278 return; | 308 return; |
279 | 309 |
280 // The cached picture is translated by -layer_rect_.origin during record, | 310 // The cached picture is translated by -layer_rect_.origin during record, |
281 // so we need to offset that back in order to get right positioning for | 311 // so we need to offset that back in order to get right positioning for |
282 // images. | 312 // images. |
283 DiscardableImageMap::ScopedMetadataGenerator generator( | 313 DiscardableImageMap::ScopedMetadataGenerator generator( |
284 &image_map_, gfx::Size(layer_rect_.right(), layer_rect_.bottom())); | 314 &image_map_, gfx::Size(layer_rect_.right(), layer_rect_.bottom())); |
285 Raster(generator.canvas(), nullptr, | 315 Raster(generator.canvas(), nullptr, |
286 gfx::Rect(layer_rect_.right(), layer_rect_.bottom()), 1.f); | 316 gfx::Rect(layer_rect_.right(), layer_rect_.bottom()), 1.f); |
287 } | 317 } |
288 | 318 |
289 void DisplayItemList::GetDiscardableImagesInRect( | 319 void DisplayItemList::GetDiscardableImagesInRect( |
290 const gfx::Rect& rect, | 320 const gfx::Rect& rect, |
291 float raster_scale, | 321 float raster_scale, |
292 std::vector<DrawImage>* images) { | 322 std::vector<DrawImage>* images) { |
293 image_map_.GetDiscardableImagesInRect(rect, raster_scale, images); | 323 image_map_.GetDiscardableImagesInRect(rect, raster_scale, images); |
294 } | 324 } |
295 | 325 |
296 } // namespace cc | 326 } // namespace cc |
OLD | NEW |