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/strings/stringprintf.h" | 10 #include "base/strings/stringprintf.h" |
11 #include "base/trace_event/trace_event.h" | 11 #include "base/trace_event/trace_event.h" |
12 #include "base/trace_event/trace_event_argument.h" | 12 #include "base/trace_event/trace_event_argument.h" |
13 #include "cc/base/math_util.h" | 13 #include "cc/base/math_util.h" |
14 #include "cc/debug/picture_debug_util.h" | 14 #include "cc/debug/picture_debug_util.h" |
15 #include "cc/debug/traced_display_item_list.h" | 15 #include "cc/debug/traced_display_item_list.h" |
16 #include "cc/debug/traced_value.h" | 16 #include "cc/debug/traced_value.h" |
17 #include "cc/playback/display_item_list_settings.h" | 17 #include "cc/playback/display_item_list_settings.h" |
18 #include "cc/playback/display_item_proto_factory.h" | 18 #include "cc/playback/display_item_proto_factory.h" |
19 #include "cc/playback/drawing_display_item.h" | |
20 #include "cc/playback/largest_display_item.h" | 19 #include "cc/playback/largest_display_item.h" |
21 #include "cc/proto/display_item.pb.h" | 20 #include "cc/proto/display_item.pb.h" |
22 #include "cc/proto/gfx_conversions.h" | 21 #include "cc/proto/gfx_conversions.h" |
23 #include "third_party/skia/include/core/SkCanvas.h" | 22 #include "third_party/skia/include/core/SkCanvas.h" |
24 #include "third_party/skia/include/core/SkPictureRecorder.h" | 23 #include "third_party/skia/include/core/SkPictureRecorder.h" |
25 #include "third_party/skia/include/utils/SkPictureUtils.h" | 24 #include "third_party/skia/include/utils/SkPictureUtils.h" |
26 #include "ui/gfx/geometry/rect.h" | 25 #include "ui/gfx/geometry/rect.h" |
27 #include "ui/gfx/skia_util.h" | 26 #include "ui/gfx/skia_util.h" |
28 | 27 |
29 namespace cc { | 28 namespace cc { |
(...skipping 25 matching lines...) Expand all Loading... |
55 | 54 |
56 scoped_refptr<DisplayItemList> DisplayItemList::CreateFromProto( | 55 scoped_refptr<DisplayItemList> DisplayItemList::CreateFromProto( |
57 const proto::DisplayItemList& proto) { | 56 const proto::DisplayItemList& proto) { |
58 gfx::Rect layer_rect = ProtoToRect(proto.layer_rect()); | 57 gfx::Rect layer_rect = ProtoToRect(proto.layer_rect()); |
59 scoped_refptr<DisplayItemList> list = | 58 scoped_refptr<DisplayItemList> list = |
60 DisplayItemList::Create(ProtoToRect(proto.layer_rect()), | 59 DisplayItemList::Create(ProtoToRect(proto.layer_rect()), |
61 DisplayItemListSettings(proto.settings())); | 60 DisplayItemListSettings(proto.settings())); |
62 | 61 |
63 for (int i = 0; i < proto.items_size(); i++) { | 62 for (int i = 0; i < proto.items_size(); i++) { |
64 const proto::DisplayItem& item_proto = proto.items(i); | 63 const proto::DisplayItem& item_proto = proto.items(i); |
65 DisplayItemProtoFactory::AllocateAndConstruct(layer_rect, list.get(), | 64 DisplayItem* item = DisplayItemProtoFactory::AllocateAndConstruct( |
66 item_proto); | 65 layer_rect, list.get(), item_proto); |
| 66 if (item) |
| 67 item->FromProtobuf(item_proto); |
67 } | 68 } |
68 | 69 |
69 return list; | 70 return list; |
70 } | 71 } |
71 | 72 |
72 DisplayItemList::DisplayItemList(gfx::Rect layer_rect, | 73 DisplayItemList::DisplayItemList(gfx::Rect layer_rect, |
73 const DisplayItemListSettings& settings, | 74 const DisplayItemListSettings& settings, |
74 bool retain_individual_display_items) | 75 bool retain_individual_display_items) |
75 : items_(LargestDisplayItemSize(), | 76 : items_(LargestDisplayItemSize(), |
76 LargestDisplayItemSize() * kDefaultNumDisplayItemsToReserve), | 77 LargestDisplayItemSize() * kDefaultNumDisplayItemsToReserve), |
77 settings_(settings), | 78 settings_(settings), |
78 retain_individual_display_items_(retain_individual_display_items), | 79 retain_individual_display_items_(retain_individual_display_items), |
79 layer_rect_(layer_rect), | 80 layer_rect_(layer_rect), |
80 is_suitable_for_gpu_rasterization_(true), | 81 is_suitable_for_gpu_rasterization_(true), |
81 approximate_op_count_(0), | 82 approximate_op_count_(0), |
82 picture_memory_usage_(0) { | 83 picture_memory_usage_(0), |
| 84 external_memory_usage_(0) { |
| 85 #if DCHECK_IS_ON() |
| 86 needs_process_ = false; |
| 87 #endif |
83 if (settings_.use_cached_picture) { | 88 if (settings_.use_cached_picture) { |
84 SkRTreeFactory factory; | 89 SkRTreeFactory factory; |
85 recorder_.reset(new SkPictureRecorder()); | 90 recorder_.reset(new SkPictureRecorder()); |
86 canvas_ = skia::SharePtr(recorder_->beginRecording( | 91 canvas_ = skia::SharePtr(recorder_->beginRecording( |
87 layer_rect_.width(), layer_rect_.height(), &factory)); | 92 layer_rect_.width(), layer_rect_.height(), &factory)); |
88 canvas_->translate(-layer_rect_.x(), -layer_rect_.y()); | 93 canvas_->translate(-layer_rect_.x(), -layer_rect_.y()); |
89 canvas_->clipRect(gfx::RectToSkRect(layer_rect_)); | 94 canvas_->clipRect(gfx::RectToSkRect(layer_rect_)); |
90 } | 95 } |
91 } | 96 } |
92 | 97 |
(...skipping 10 matching lines...) Expand all Loading... |
103 | 108 |
104 DCHECK_EQ(0, proto->items_size()); | 109 DCHECK_EQ(0, proto->items_size()); |
105 for (const auto& item : items_) | 110 for (const auto& item : items_) |
106 item.ToProtobuf(proto->add_items()); | 111 item.ToProtobuf(proto->add_items()); |
107 } | 112 } |
108 | 113 |
109 void DisplayItemList::Raster(SkCanvas* canvas, | 114 void DisplayItemList::Raster(SkCanvas* canvas, |
110 SkPicture::AbortCallback* callback, | 115 SkPicture::AbortCallback* callback, |
111 const gfx::Rect& canvas_target_playback_rect, | 116 const gfx::Rect& canvas_target_playback_rect, |
112 float contents_scale) const { | 117 float contents_scale) const { |
| 118 DCHECK(ProcessAppendedItemsCalled()); |
113 if (!settings_.use_cached_picture) { | 119 if (!settings_.use_cached_picture) { |
114 canvas->save(); | 120 canvas->save(); |
115 canvas->scale(contents_scale, contents_scale); | 121 canvas->scale(contents_scale, contents_scale); |
116 for (const auto& item : items_) | 122 for (const auto& item : items_) |
117 item.Raster(canvas, canvas_target_playback_rect, callback); | 123 item.Raster(canvas, canvas_target_playback_rect, callback); |
118 canvas->restore(); | 124 canvas->restore(); |
119 } else { | 125 } else { |
120 DCHECK(picture_); | 126 DCHECK(picture_); |
121 | 127 |
122 canvas->save(); | 128 canvas->save(); |
123 canvas->scale(contents_scale, contents_scale); | 129 canvas->scale(contents_scale, contents_scale); |
124 canvas->translate(layer_rect_.x(), layer_rect_.y()); | 130 canvas->translate(layer_rect_.x(), layer_rect_.y()); |
125 if (callback) { | 131 if (callback) { |
126 // If we have a callback, we need to call |draw()|, |drawPicture()| | 132 // If we have a callback, we need to call |draw()|, |drawPicture()| |
127 // doesn't take a callback. This is used by |AnalysisCanvas| to early | 133 // doesn't take a callback. This is used by |AnalysisCanvas| to early |
128 // out. | 134 // out. |
129 picture_->playback(canvas, callback); | 135 picture_->playback(canvas, callback); |
130 } else { | 136 } else { |
131 // Prefer to call |drawPicture()| on the canvas since it could place the | 137 // Prefer to call |drawPicture()| on the canvas since it could place the |
132 // entire picture on the canvas instead of parsing the skia operations. | 138 // entire picture on the canvas instead of parsing the skia operations. |
133 canvas->drawPicture(picture_.get()); | 139 canvas->drawPicture(picture_.get()); |
134 } | 140 } |
135 canvas->restore(); | 141 canvas->restore(); |
136 } | 142 } |
137 } | 143 } |
138 | 144 |
139 void DisplayItemList::ProcessAppendedItem(const DisplayItem* item) { | 145 void DisplayItemList::ProcessAppendedItemsOnTheFly() { |
140 if (settings_.use_cached_picture) { | 146 if (retain_individual_display_items_) |
141 DCHECK(canvas_); | 147 return; |
142 item->Raster(canvas_.get(), gfx::Rect(), nullptr); | 148 if (items_.size() >= kDefaultNumDisplayItemsToReserve) { |
| 149 ProcessAppendedItems(); |
| 150 // This function exists to keep the |items_| from growing indefinitely if |
| 151 // we're not going to store them anyway. So the items better be deleted |
| 152 // after |items_| grows too large and we process it. |
| 153 DCHECK(items_.empty()); |
143 } | 154 } |
144 if (!retain_individual_display_items_) { | 155 } |
| 156 |
| 157 void DisplayItemList::ProcessAppendedItems() { |
| 158 #if DCHECK_IS_ON() |
| 159 needs_process_ = false; |
| 160 #endif |
| 161 for (const DisplayItem& item : items_) { |
| 162 if (settings_.use_cached_picture) { |
| 163 // When using a cached picture we will calculate gpu suitability on the |
| 164 // entire cached picture instead of the items. This is more permissive |
| 165 // since none of the items might individually trigger a veto even though |
| 166 // they collectively have enough "bad" operations that a corresponding |
| 167 // Picture would get vetoed. See crbug.com/513016. |
| 168 DCHECK(canvas_); |
| 169 approximate_op_count_ += item.approximate_op_count(); |
| 170 item.Raster(canvas_.get(), gfx::Rect(), nullptr); |
| 171 } else { |
| 172 is_suitable_for_gpu_rasterization_ &= |
| 173 item.is_suitable_for_gpu_rasterization(); |
| 174 approximate_op_count_ += item.approximate_op_count(); |
| 175 } |
| 176 |
| 177 if (retain_individual_display_items_) { |
| 178 // Warning: this double-counts SkPicture data if use_cached_picture is |
| 179 // also true. |
| 180 external_memory_usage_ += item.external_memory_usage(); |
| 181 } |
| 182 } |
| 183 |
| 184 if (!retain_individual_display_items_) |
145 items_.Clear(); | 185 items_.Clear(); |
146 } | |
147 } | 186 } |
148 | 187 |
149 void DisplayItemList::RasterIntoCanvas(const DisplayItem& item) { | 188 void DisplayItemList::RasterIntoCanvas(const DisplayItem& item) { |
150 DCHECK(canvas_); | 189 DCHECK(canvas_); |
151 DCHECK(!retain_individual_display_items_); | 190 DCHECK(!retain_individual_display_items_); |
| 191 approximate_op_count_ += item.approximate_op_count(); |
152 | 192 |
153 item.Raster(canvas_.get(), gfx::Rect(), nullptr); | 193 item.Raster(canvas_.get(), gfx::Rect(), nullptr); |
154 } | 194 } |
155 | 195 |
156 bool DisplayItemList::RetainsIndividualDisplayItems() const { | 196 bool DisplayItemList::RetainsIndividualDisplayItems() const { |
157 return retain_individual_display_items_; | 197 return retain_individual_display_items_; |
158 } | 198 } |
159 | 199 |
| 200 void DisplayItemList::RemoveLast() { |
| 201 // We cannot remove the last item if it has been squashed into a picture. |
| 202 // The last item should not have been handled by ProcessAppendedItems, so we |
| 203 // don't need to remove it from approximate_op_count_, etc. |
| 204 DCHECK(retain_individual_display_items_); |
| 205 DCHECK(!settings_.use_cached_picture); |
| 206 items_.RemoveLast(); |
| 207 } |
| 208 |
160 void DisplayItemList::Finalize() { | 209 void DisplayItemList::Finalize() { |
161 // TODO(wkorman): Uncomment the assert below once we've investigated | 210 // TODO(wkorman): Uncomment the assert below once we've investigated |
162 // and resolved issues. http://crbug.com/557905 | 211 // and resolved issues. http://crbug.com/557905 |
163 // DCHECK_EQ(items_.size(), visual_rects_.size()); | 212 // DCHECK_EQ(items_.size(), visual_rects_.size()); |
164 | 213 |
165 // TODO(vmpstr): Build and make use of an RTree from the visual | 214 // TODO(vmpstr): Build and make use of an RTree from the visual |
166 // rects. For now we just clear them out since we won't ever need | 215 // rects. For now we just clear them out since we won't ever need |
167 // them to stick around post-Finalize. http://crbug.com/527245 | 216 // them to stick around post-Finalize. http://crbug.com/527245 |
168 visual_rects_.clear(); | 217 visual_rects_.clear(); |
169 | 218 |
| 219 ProcessAppendedItems(); |
| 220 |
170 if (settings_.use_cached_picture) { | 221 if (settings_.use_cached_picture) { |
171 // Convert to an SkPicture for faster rasterization. | 222 // Convert to an SkPicture for faster rasterization. |
172 DCHECK(settings_.use_cached_picture); | 223 DCHECK(settings_.use_cached_picture); |
173 DCHECK(!picture_); | 224 DCHECK(!picture_); |
174 picture_ = skia::AdoptRef(recorder_->endRecordingAsPicture()); | 225 picture_ = skia::AdoptRef(recorder_->endRecordingAsPicture()); |
175 DCHECK(picture_); | 226 DCHECK(picture_); |
176 picture_memory_usage_ = | 227 picture_memory_usage_ = |
177 SkPictureUtils::ApproximateBytesUsed(picture_.get()); | 228 SkPictureUtils::ApproximateBytesUsed(picture_.get()); |
178 recorder_.reset(); | 229 recorder_.reset(); |
179 canvas_.clear(); | 230 canvas_.clear(); |
| 231 is_suitable_for_gpu_rasterization_ = |
| 232 picture_->suitableForGpuRasterization(nullptr); |
180 } | 233 } |
181 } | 234 } |
182 | 235 |
183 bool DisplayItemList::IsSuitableForGpuRasterization() const { | 236 bool DisplayItemList::IsSuitableForGpuRasterization() const { |
| 237 DCHECK(ProcessAppendedItemsCalled()); |
184 return is_suitable_for_gpu_rasterization_; | 238 return is_suitable_for_gpu_rasterization_; |
185 } | 239 } |
186 | 240 |
187 int DisplayItemList::ApproximateOpCount() const { | 241 int DisplayItemList::ApproximateOpCount() const { |
| 242 DCHECK(ProcessAppendedItemsCalled()); |
188 return approximate_op_count_; | 243 return approximate_op_count_; |
189 } | 244 } |
190 | 245 |
191 size_t DisplayItemList::ApproximateMemoryUsage() const { | 246 size_t DisplayItemList::ApproximateMemoryUsage() const { |
| 247 DCHECK(ProcessAppendedItemsCalled()); |
192 // We double-count in this case. Produce zero to avoid being misleading. | 248 // We double-count in this case. Produce zero to avoid being misleading. |
193 if (settings_.use_cached_picture && retain_individual_display_items_) | 249 if (settings_.use_cached_picture && retain_individual_display_items_) |
194 return 0; | 250 return 0; |
195 | 251 |
196 DCHECK(!settings_.use_cached_picture || picture_); | 252 DCHECK(!settings_.use_cached_picture || picture_); |
197 | 253 |
198 size_t memory_usage = sizeof(*this); | 254 size_t memory_usage = sizeof(*this); |
199 | 255 |
200 size_t external_memory_usage = 0; | |
201 if (retain_individual_display_items_) { | |
202 // Warning: this double-counts SkPicture data if use_cached_picture is | |
203 // also true. | |
204 for (const auto& item : items_) { | |
205 external_memory_usage += item.ExternalMemoryUsage(); | |
206 } | |
207 } | |
208 | |
209 // Memory outside this class due to |items_|. | 256 // Memory outside this class due to |items_|. |
210 memory_usage += items_.GetCapacityInBytes() + external_memory_usage; | 257 memory_usage += items_.GetCapacityInBytes() + external_memory_usage_; |
211 | 258 |
212 // Memory outside this class due to |picture|. | 259 // Memory outside this class due to |picture|. |
213 memory_usage += picture_memory_usage_; | 260 memory_usage += picture_memory_usage_; |
214 | 261 |
215 // TODO(jbroman): Does anything else owned by this class substantially | 262 // TODO(jbroman): Does anything else owned by this class substantially |
216 // contribute to memory usage? | 263 // contribute to memory usage? |
217 | 264 |
218 return memory_usage; | 265 return memory_usage; |
219 } | 266 } |
220 | 267 |
221 bool DisplayItemList::ShouldBeAnalyzedForSolidColor() const { | 268 bool DisplayItemList::ShouldBeAnalyzedForSolidColor() const { |
222 return ApproximateOpCount() <= kOpCountThatIsOkToAnalyze; | 269 return ApproximateOpCount() <= kOpCountThatIsOkToAnalyze; |
223 } | 270 } |
224 | 271 |
225 scoped_refptr<base::trace_event::ConvertableToTraceFormat> | 272 scoped_refptr<base::trace_event::ConvertableToTraceFormat> |
226 DisplayItemList::AsValue(bool include_items) const { | 273 DisplayItemList::AsValue(bool include_items) const { |
| 274 DCHECK(ProcessAppendedItemsCalled()); |
227 scoped_refptr<base::trace_event::TracedValue> state = | 275 scoped_refptr<base::trace_event::TracedValue> state = |
228 new base::trace_event::TracedValue(); | 276 new base::trace_event::TracedValue(); |
229 | 277 |
230 state->BeginDictionary("params"); | 278 state->BeginDictionary("params"); |
231 if (include_items) { | 279 if (include_items) { |
232 state->BeginArray("items"); | 280 state->BeginArray("items"); |
233 size_t item_index = 0; | 281 size_t item_index = 0; |
234 for (const DisplayItem& item : items_) { | 282 for (const DisplayItem& item : items_) { |
235 item.AsValueInto(visual_rects_.size() >= item_index | 283 item.AsValueInto(visual_rects_.size() >= item_index |
236 ? visual_rects_[item_index] | 284 ? visual_rects_[item_index] |
(...skipping 18 matching lines...) Expand all Loading... |
255 | 303 |
256 std::string b64_picture; | 304 std::string b64_picture; |
257 PictureDebugUtil::SerializeAsBase64(picture.get(), &b64_picture); | 305 PictureDebugUtil::SerializeAsBase64(picture.get(), &b64_picture); |
258 state->SetString("skp64", b64_picture); | 306 state->SetString("skp64", b64_picture); |
259 } | 307 } |
260 | 308 |
261 return state; | 309 return state; |
262 } | 310 } |
263 | 311 |
264 void DisplayItemList::EmitTraceSnapshot() const { | 312 void DisplayItemList::EmitTraceSnapshot() const { |
| 313 DCHECK(ProcessAppendedItemsCalled()); |
265 TRACE_EVENT_OBJECT_SNAPSHOT_WITH_ID( | 314 TRACE_EVENT_OBJECT_SNAPSHOT_WITH_ID( |
266 TRACE_DISABLED_BY_DEFAULT("cc.debug.display_items") "," | 315 TRACE_DISABLED_BY_DEFAULT("cc.debug.display_items") "," |
267 TRACE_DISABLED_BY_DEFAULT("cc.debug.picture") "," | 316 TRACE_DISABLED_BY_DEFAULT("cc.debug.picture") "," |
268 TRACE_DISABLED_BY_DEFAULT("devtools.timeline.picture"), | 317 TRACE_DISABLED_BY_DEFAULT("devtools.timeline.picture"), |
269 "cc::DisplayItemList", this, | 318 "cc::DisplayItemList", this, |
270 TracedDisplayItemList::AsTraceableDisplayItemList(this, | 319 TracedDisplayItemList::AsTraceableDisplayItemList(this, |
271 DisplayItemsTracingEnabled())); | 320 DisplayItemsTracingEnabled())); |
272 } | 321 } |
273 | 322 |
274 void DisplayItemList::GenerateDiscardableImagesMetadata() { | 323 void DisplayItemList::GenerateDiscardableImagesMetadata() { |
| 324 DCHECK(ProcessAppendedItemsCalled()); |
275 // This should be only called once, and only after CreateAndCacheSkPicture. | 325 // This should be only called once, and only after CreateAndCacheSkPicture. |
276 DCHECK(image_map_.empty()); | 326 DCHECK(image_map_.empty()); |
277 DCHECK(!settings_.use_cached_picture || picture_); | 327 DCHECK(!settings_.use_cached_picture || picture_); |
278 if (settings_.use_cached_picture && !picture_->willPlayBackBitmaps()) | 328 if (settings_.use_cached_picture && !picture_->willPlayBackBitmaps()) |
279 return; | 329 return; |
280 | 330 |
281 // The cached picture is translated by -layer_rect_.origin during record, | 331 // The cached picture is translated by -layer_rect_.origin during record, |
282 // so we need to offset that back in order to get right positioning for | 332 // so we need to offset that back in order to get right positioning for |
283 // images. | 333 // images. |
284 DiscardableImageMap::ScopedMetadataGenerator generator( | 334 DiscardableImageMap::ScopedMetadataGenerator generator( |
285 &image_map_, gfx::Size(layer_rect_.right(), layer_rect_.bottom())); | 335 &image_map_, gfx::Size(layer_rect_.right(), layer_rect_.bottom())); |
286 Raster(generator.canvas(), nullptr, | 336 Raster(generator.canvas(), nullptr, |
287 gfx::Rect(layer_rect_.right(), layer_rect_.bottom()), 1.f); | 337 gfx::Rect(layer_rect_.right(), layer_rect_.bottom()), 1.f); |
288 } | 338 } |
289 | 339 |
290 void DisplayItemList::GetDiscardableImagesInRect( | 340 void DisplayItemList::GetDiscardableImagesInRect( |
291 const gfx::Rect& rect, | 341 const gfx::Rect& rect, |
292 float raster_scale, | 342 float raster_scale, |
293 std::vector<DrawImage>* images) { | 343 std::vector<DrawImage>* images) { |
294 image_map_.GetDiscardableImagesInRect(rect, raster_scale, images); | 344 image_map_.GetDiscardableImagesInRect(rect, raster_scale, images); |
295 } | 345 } |
296 | 346 |
297 } // namespace cc | 347 } // namespace cc |
OLD | NEW |