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::Create( | |
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); | |
vmpstr
2015/10/26 17:46:17
Maybe FromProtobuf functionality can be a part of
David Trainor- moved to gerrit
2015/10/26 20:42:56
I was trying to hide the subclasses from this file
| |
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 // TODO(dtrainor): Send only changed state. | |
100 RectToProto(layer_rect_, proto->mutable_layer_rect()); | |
101 settings_.ToProtobuf(proto->mutable_settings()); | |
102 | |
103 DCHECK_EQ(0, proto->items_size()); | |
104 for (auto* item : items_) | |
105 item->ToProtobuf(proto->add_items()); | |
106 } | |
107 | |
77 void DisplayItemList::Raster(SkCanvas* canvas, | 108 void DisplayItemList::Raster(SkCanvas* canvas, |
78 SkPicture::AbortCallback* callback, | 109 SkPicture::AbortCallback* callback, |
79 const gfx::Rect& canvas_target_playback_rect, | 110 const gfx::Rect& canvas_target_playback_rect, |
80 float contents_scale) const { | 111 float contents_scale) const { |
81 DCHECK(ProcessAppendedItemsCalled()); | 112 DCHECK(ProcessAppendedItemsCalled()); |
82 if (!use_cached_picture_) { | 113 if (!settings_.use_cached_picture) { |
83 canvas->save(); | 114 canvas->save(); |
84 canvas->scale(contents_scale, contents_scale); | 115 canvas->scale(contents_scale, contents_scale); |
85 for (auto* item : items_) | 116 for (auto* item : items_) |
86 item->Raster(canvas, canvas_target_playback_rect, callback); | 117 item->Raster(canvas, canvas_target_playback_rect, callback); |
87 canvas->restore(); | 118 canvas->restore(); |
88 } else { | 119 } else { |
89 DCHECK(picture_); | 120 DCHECK(picture_); |
90 | 121 |
91 canvas->save(); | 122 canvas->save(); |
92 canvas->scale(contents_scale, contents_scale); | 123 canvas->scale(contents_scale, contents_scale); |
(...skipping 22 matching lines...) Expand all Loading... | |
115 // after |items_| grows too large and we process it. | 146 // after |items_| grows too large and we process it. |
116 DCHECK(items_.empty()); | 147 DCHECK(items_.empty()); |
117 } | 148 } |
118 } | 149 } |
119 | 150 |
120 void DisplayItemList::ProcessAppendedItems() { | 151 void DisplayItemList::ProcessAppendedItems() { |
121 #if DCHECK_IS_ON() | 152 #if DCHECK_IS_ON() |
122 needs_process_ = false; | 153 needs_process_ = false; |
123 #endif | 154 #endif |
124 for (const DisplayItem* item : items_) { | 155 for (const DisplayItem* item : items_) { |
125 if (use_cached_picture_) { | 156 if (settings_.use_cached_picture) { |
126 // When using a cached picture we will calculate gpu suitability on the | 157 // When using a cached picture we will calculate gpu suitability on the |
127 // entire cached picture instead of the items. This is more permissive | 158 // entire cached picture instead of the items. This is more permissive |
128 // since none of the items might individually trigger a veto even though | 159 // since none of the items might individually trigger a veto even though |
129 // they collectively have enough "bad" operations that a corresponding | 160 // they collectively have enough "bad" operations that a corresponding |
130 // Picture would get vetoed. See crbug.com/513016. | 161 // Picture would get vetoed. See crbug.com/513016. |
131 DCHECK(canvas_); | 162 DCHECK(canvas_); |
132 approximate_op_count_ += item->approximate_op_count(); | 163 approximate_op_count_ += item->approximate_op_count(); |
133 item->Raster(canvas_.get(), gfx::Rect(), nullptr); | 164 item->Raster(canvas_.get(), gfx::Rect(), nullptr); |
134 } else { | 165 } else { |
135 is_suitable_for_gpu_rasterization_ &= | 166 is_suitable_for_gpu_rasterization_ &= |
(...skipping 22 matching lines...) Expand all Loading... | |
158 | 189 |
159 bool DisplayItemList::RetainsIndividualDisplayItems() const { | 190 bool DisplayItemList::RetainsIndividualDisplayItems() const { |
160 return retain_individual_display_items_; | 191 return retain_individual_display_items_; |
161 } | 192 } |
162 | 193 |
163 void DisplayItemList::RemoveLast() { | 194 void DisplayItemList::RemoveLast() { |
164 // We cannot remove the last item if it has been squashed into a picture. | 195 // 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 | 196 // The last item should not have been handled by ProcessAppendedItems, so we |
166 // don't need to remove it from approximate_op_count_, etc. | 197 // don't need to remove it from approximate_op_count_, etc. |
167 DCHECK(retain_individual_display_items_); | 198 DCHECK(retain_individual_display_items_); |
168 DCHECK(!use_cached_picture_); | 199 DCHECK(!settings_.use_cached_picture); |
169 items_.RemoveLast(); | 200 items_.RemoveLast(); |
170 } | 201 } |
171 | 202 |
172 void DisplayItemList::Finalize() { | 203 void DisplayItemList::Finalize() { |
173 ProcessAppendedItems(); | 204 ProcessAppendedItems(); |
174 | 205 |
175 if (use_cached_picture_) { | 206 if (settings_.use_cached_picture) { |
176 // Convert to an SkPicture for faster rasterization. | 207 // Convert to an SkPicture for faster rasterization. |
177 DCHECK(use_cached_picture_); | 208 DCHECK(settings_.use_cached_picture); |
178 DCHECK(!picture_); | 209 DCHECK(!picture_); |
179 picture_ = skia::AdoptRef(recorder_->endRecordingAsPicture()); | 210 picture_ = skia::AdoptRef(recorder_->endRecordingAsPicture()); |
180 DCHECK(picture_); | 211 DCHECK(picture_); |
181 picture_memory_usage_ = | 212 picture_memory_usage_ = |
182 SkPictureUtils::ApproximateBytesUsed(picture_.get()); | 213 SkPictureUtils::ApproximateBytesUsed(picture_.get()); |
183 recorder_.reset(); | 214 recorder_.reset(); |
184 canvas_.clear(); | 215 canvas_.clear(); |
185 is_suitable_for_gpu_rasterization_ = | 216 is_suitable_for_gpu_rasterization_ = |
186 picture_->suitableForGpuRasterization(nullptr); | 217 picture_->suitableForGpuRasterization(nullptr); |
187 } | 218 } |
188 } | 219 } |
189 | 220 |
190 bool DisplayItemList::IsSuitableForGpuRasterization() const { | 221 bool DisplayItemList::IsSuitableForGpuRasterization() const { |
191 DCHECK(ProcessAppendedItemsCalled()); | 222 DCHECK(ProcessAppendedItemsCalled()); |
192 return is_suitable_for_gpu_rasterization_; | 223 return is_suitable_for_gpu_rasterization_; |
193 } | 224 } |
194 | 225 |
195 int DisplayItemList::ApproximateOpCount() const { | 226 int DisplayItemList::ApproximateOpCount() const { |
196 DCHECK(ProcessAppendedItemsCalled()); | 227 DCHECK(ProcessAppendedItemsCalled()); |
197 return approximate_op_count_; | 228 return approximate_op_count_; |
198 } | 229 } |
199 | 230 |
200 size_t DisplayItemList::ApproximateMemoryUsage() const { | 231 size_t DisplayItemList::ApproximateMemoryUsage() const { |
201 DCHECK(ProcessAppendedItemsCalled()); | 232 DCHECK(ProcessAppendedItemsCalled()); |
202 // We double-count in this case. Produce zero to avoid being misleading. | 233 // We double-count in this case. Produce zero to avoid being misleading. |
203 if (use_cached_picture_ && retain_individual_display_items_) | 234 if (settings_.use_cached_picture && retain_individual_display_items_) |
204 return 0; | 235 return 0; |
205 | 236 |
206 DCHECK_IMPLIES(use_cached_picture_, picture_); | 237 DCHECK_IMPLIES(settings_.use_cached_picture, picture_); |
207 | 238 |
208 size_t memory_usage = sizeof(*this); | 239 size_t memory_usage = sizeof(*this); |
209 | 240 |
210 // Memory outside this class due to |items_|. | 241 // Memory outside this class due to |items_|. |
211 memory_usage += items_.GetCapacityInBytes() + external_memory_usage_; | 242 memory_usage += items_.GetCapacityInBytes() + external_memory_usage_; |
212 | 243 |
213 // Memory outside this class due to |picture|. | 244 // Memory outside this class due to |picture|. |
214 memory_usage += picture_memory_usage_; | 245 memory_usage += picture_memory_usage_; |
215 | 246 |
216 // TODO(jbroman): Does anything else owned by this class substantially | 247 // 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"), | 297 TRACE_DISABLED_BY_DEFAULT("devtools.timeline.picture"), |
267 "cc::DisplayItemList", this, | 298 "cc::DisplayItemList", this, |
268 TracedDisplayItemList::AsTraceableDisplayItemList(this, | 299 TracedDisplayItemList::AsTraceableDisplayItemList(this, |
269 DisplayItemsTracingEnabled())); | 300 DisplayItemsTracingEnabled())); |
270 } | 301 } |
271 | 302 |
272 void DisplayItemList::GenerateDiscardableImagesMetadata() { | 303 void DisplayItemList::GenerateDiscardableImagesMetadata() { |
273 DCHECK(ProcessAppendedItemsCalled()); | 304 DCHECK(ProcessAppendedItemsCalled()); |
274 // This should be only called once, and only after CreateAndCacheSkPicture. | 305 // This should be only called once, and only after CreateAndCacheSkPicture. |
275 DCHECK(image_map_.empty()); | 306 DCHECK(image_map_.empty()); |
276 DCHECK_IMPLIES(use_cached_picture_, picture_); | 307 DCHECK_IMPLIES(settings_.use_cached_picture, picture_); |
277 if (use_cached_picture_ && !picture_->willPlayBackBitmaps()) | 308 if (settings_.use_cached_picture && !picture_->willPlayBackBitmaps()) |
278 return; | 309 return; |
279 | 310 |
280 // The cached picture is translated by -layer_rect_.origin during record, | 311 // 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 | 312 // so we need to offset that back in order to get right positioning for |
282 // images. | 313 // images. |
283 DiscardableImageMap::ScopedMetadataGenerator generator( | 314 DiscardableImageMap::ScopedMetadataGenerator generator( |
284 &image_map_, gfx::Size(layer_rect_.right(), layer_rect_.bottom())); | 315 &image_map_, gfx::Size(layer_rect_.right(), layer_rect_.bottom())); |
285 Raster(generator.canvas(), nullptr, | 316 Raster(generator.canvas(), nullptr, |
286 gfx::Rect(layer_rect_.right(), layer_rect_.bottom()), 1.f); | 317 gfx::Rect(layer_rect_.right(), layer_rect_.bottom()), 1.f); |
287 } | 318 } |
288 | 319 |
289 void DisplayItemList::GetDiscardableImagesInRect( | 320 void DisplayItemList::GetDiscardableImagesInRect( |
290 const gfx::Rect& rect, | 321 const gfx::Rect& rect, |
291 float raster_scale, | 322 float raster_scale, |
292 std::vector<DrawImage>* images) { | 323 std::vector<DrawImage>* images) { |
293 image_map_.GetDiscardableImagesInRect(rect, raster_scale, images); | 324 image_map_.GetDiscardableImagesInRect(rect, raster_scale, images); |
294 } | 325 } |
295 | 326 |
296 } // namespace cc | 327 } // namespace cc |
OLD | NEW |