Roll src/third_party/WebKit d9c6159:8139f33 (svn 201974:201975)
[chromium-blink-merge.git] / cc / playback / display_item_list.cc
blob050e0292aba0c2cc9f707d199abac8671c5c6b4f
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.
5 #include "cc/playback/display_item_list.h"
7 #include <string>
9 #include "base/numerics/safe_conversions.h"
10 #include "base/trace_event/trace_event.h"
11 #include "base/trace_event/trace_event_argument.h"
12 #include "cc/base/math_util.h"
13 #include "cc/debug/picture_debug_util.h"
14 #include "cc/debug/traced_display_item_list.h"
15 #include "cc/debug/traced_picture.h"
16 #include "cc/debug/traced_value.h"
17 #include "cc/playback/display_item_list_settings.h"
18 #include "cc/playback/largest_display_item.h"
19 #include "third_party/skia/include/core/SkCanvas.h"
20 #include "third_party/skia/include/core/SkPictureRecorder.h"
21 #include "third_party/skia/include/utils/SkPictureUtils.h"
22 #include "ui/gfx/skia_util.h"
24 namespace cc {
26 namespace {
28 // We don't perform per-layer solid color analysis when there are too many skia
29 // operations.
30 const int kOpCountThatIsOkToAnalyze = 10;
32 bool DisplayItemsTracingEnabled() {
33 bool tracing_enabled;
34 TRACE_EVENT_CATEGORY_GROUP_ENABLED(
35 TRACE_DISABLED_BY_DEFAULT("cc.debug.display_items"), &tracing_enabled);
36 return tracing_enabled;
39 const int kDefaultNumDisplayItemsToReserve = 100;
41 } // namespace
43 DisplayItemList::DisplayItemList(gfx::Rect layer_rect,
44 const DisplayItemListSettings& settings,
45 bool retain_individual_display_items)
46 : items_(LargestDisplayItemSize(),
47 settings.max_sidecar_size,
48 kDefaultNumDisplayItemsToReserve,
49 settings.sidecar_destroyer),
50 use_cached_picture_(settings.use_cached_picture),
51 retain_individual_display_items_(retain_individual_display_items),
52 layer_rect_(layer_rect),
53 all_items_are_suitable_for_gpu_rasterization_(true),
54 approximate_op_count_(0),
55 picture_memory_usage_(0),
56 external_memory_usage_(0) {
57 #if DCHECK_IS_ON()
58 needs_process_ = false;
59 #endif
60 if (use_cached_picture_) {
61 SkRTreeFactory factory;
62 recorder_.reset(new SkPictureRecorder());
63 canvas_ = skia::SharePtr(recorder_->beginRecording(
64 layer_rect_.width(), layer_rect_.height(), &factory));
65 canvas_->translate(-layer_rect_.x(), -layer_rect_.y());
66 canvas_->clipRect(gfx::RectToSkRect(layer_rect_));
70 DisplayItemList::DisplayItemList(gfx::Rect layer_rect,
71 const DisplayItemListSettings& settings)
72 : DisplayItemList(
73 layer_rect,
74 settings,
75 !settings.use_cached_picture || DisplayItemsTracingEnabled()) {
78 scoped_refptr<DisplayItemList> DisplayItemList::CreateWithoutCachedPicture(
79 const DisplayItemListSettings& settings) {
80 DCHECK(!settings.use_cached_picture);
81 return Create(gfx::Rect(), settings);
84 scoped_refptr<DisplayItemList> DisplayItemList::Create(
85 gfx::Rect layer_rect,
86 bool use_cached_picture) {
87 DisplayItemListSettings settings;
88 settings.use_cached_picture = use_cached_picture;
89 return Create(layer_rect, settings);
92 scoped_refptr<DisplayItemList> DisplayItemList::Create(
93 gfx::Rect layer_rect,
94 const DisplayItemListSettings& settings) {
95 return make_scoped_refptr(new DisplayItemList(layer_rect, settings));
98 DisplayItemList::~DisplayItemList() {
101 void DisplayItemList::Raster(SkCanvas* canvas,
102 SkPicture::AbortCallback* callback,
103 const gfx::Rect& canvas_target_playback_rect,
104 float contents_scale) const {
105 DCHECK(ProcessAppendedItemsCalled());
106 if (!use_cached_picture_) {
107 canvas->save();
108 canvas->scale(contents_scale, contents_scale);
109 for (auto* item : items_)
110 item->Raster(canvas, canvas_target_playback_rect, callback);
111 canvas->restore();
112 } else {
113 DCHECK(picture_);
115 canvas->save();
116 canvas->scale(contents_scale, contents_scale);
117 canvas->translate(layer_rect_.x(), layer_rect_.y());
118 if (callback) {
119 // If we have a callback, we need to call |draw()|, |drawPicture()|
120 // doesn't take a callback. This is used by |AnalysisCanvas| to early
121 // out.
122 picture_->playback(canvas, callback);
123 } else {
124 // Prefer to call |drawPicture()| on the canvas since it could place the
125 // entire picture on the canvas instead of parsing the skia operations.
126 canvas->drawPicture(picture_.get());
128 canvas->restore();
132 void DisplayItemList::ProcessAppendedItemsOnTheFly() {
133 if (retain_individual_display_items_)
134 return;
135 if (items_.size() >= kDefaultNumDisplayItemsToReserve) {
136 ProcessAppendedItems();
137 // This function exists to keep the |items_| from growing indefinitely if
138 // we're not going to store them anyway. So the items better be deleted
139 // after |items_| grows too large and we process it.
140 DCHECK(items_.empty());
144 void DisplayItemList::ProcessAppendedItems() {
145 #if DCHECK_IS_ON()
146 needs_process_ = false;
147 #endif
148 for (const DisplayItem* item : items_) {
149 all_items_are_suitable_for_gpu_rasterization_ &=
150 item->is_suitable_for_gpu_rasterization();
151 approximate_op_count_ += item->approximate_op_count();
153 if (use_cached_picture_) {
154 DCHECK(canvas_);
155 item->Raster(canvas_.get(), gfx::Rect(), NULL);
158 if (retain_individual_display_items_) {
159 // Warning: this double-counts SkPicture data if use_cached_picture_ is
160 // also true.
161 external_memory_usage_ += item->external_memory_usage();
165 if (!retain_individual_display_items_)
166 items_.clear();
169 void DisplayItemList::RasterIntoCanvas(const DisplayItem& item) {
170 DCHECK(canvas_);
171 DCHECK(!retain_individual_display_items_);
172 all_items_are_suitable_for_gpu_rasterization_ &=
173 item.is_suitable_for_gpu_rasterization();
174 approximate_op_count_ += item.approximate_op_count();
176 item.Raster(canvas_.get(), gfx::Rect(), NULL);
179 bool DisplayItemList::RetainsIndividualDisplayItems() const {
180 return retain_individual_display_items_;
183 void DisplayItemList::RemoveLast() {
184 // We cannot remove the last item if it has been squashed into a picture.
185 // The last item should not have been handled by ProcessAppendedItems, so we
186 // don't need to remove it from approximate_op_count_, etc.
187 DCHECK(retain_individual_display_items_);
188 DCHECK(!use_cached_picture_);
189 items_.RemoveLast();
192 void DisplayItemList::Finalize() {
193 ProcessAppendedItems();
195 if (use_cached_picture_) {
196 // Convert to an SkPicture for faster rasterization.
197 DCHECK(use_cached_picture_);
198 DCHECK(!picture_);
199 picture_ = skia::AdoptRef(recorder_->endRecordingAsPicture());
200 DCHECK(picture_);
201 picture_memory_usage_ =
202 SkPictureUtils::ApproximateBytesUsed(picture_.get());
203 recorder_.reset();
204 canvas_.clear();
208 bool DisplayItemList::IsSuitableForGpuRasterization() const {
209 DCHECK(ProcessAppendedItemsCalled());
210 if (use_cached_picture_)
211 return picture_->suitableForGpuRasterization(NULL);
213 // This is more permissive than Picture's implementation, since none of the
214 // items might individually trigger a veto even though they collectively have
215 // enough "bad" operations that a corresponding Picture would get vetoed. See
216 // crbug.com/513016.
217 return all_items_are_suitable_for_gpu_rasterization_;
220 int DisplayItemList::ApproximateOpCount() const {
221 DCHECK(ProcessAppendedItemsCalled());
222 return approximate_op_count_;
225 size_t DisplayItemList::ApproximateMemoryUsage() const {
226 DCHECK(ProcessAppendedItemsCalled());
227 // We double-count in this case. Produce zero to avoid being misleading.
228 if (use_cached_picture_ && retain_individual_display_items_)
229 return 0;
231 DCHECK_IMPLIES(use_cached_picture_, picture_);
233 size_t memory_usage = sizeof(*this);
235 // Memory outside this class due to |items_|.
236 memory_usage += items_.GetCapacityInBytes() + external_memory_usage_;
238 // Memory outside this class due to |picture|.
239 memory_usage += picture_memory_usage_;
241 // TODO(jbroman): Does anything else owned by this class substantially
242 // contribute to memory usage?
244 return memory_usage;
247 bool DisplayItemList::ShouldBeAnalyzedForSolidColor() const {
248 return ApproximateOpCount() <= kOpCountThatIsOkToAnalyze;
251 scoped_refptr<base::trace_event::ConvertableToTraceFormat>
252 DisplayItemList::AsValue(bool include_items) const {
253 DCHECK(ProcessAppendedItemsCalled());
254 scoped_refptr<base::trace_event::TracedValue> state =
255 new base::trace_event::TracedValue();
257 if (include_items) {
258 state->BeginArray("params.items");
259 for (const DisplayItem* item : items_) {
260 item->AsValueInto(state.get());
262 state->EndArray();
265 state->SetValue("params.layer_rect", MathUtil::AsValue(layer_rect_));
267 if (!layer_rect_.IsEmpty()) {
268 SkPictureRecorder recorder;
269 SkCanvas* canvas =
270 recorder.beginRecording(layer_rect_.width(), layer_rect_.height());
271 canvas->translate(-layer_rect_.x(), -layer_rect_.y());
272 canvas->clipRect(gfx::RectToSkRect(layer_rect_));
273 Raster(canvas, NULL, gfx::Rect(), 1.f);
274 skia::RefPtr<SkPicture> picture =
275 skia::AdoptRef(recorder.endRecordingAsPicture());
277 std::string b64_picture;
278 PictureDebugUtil::SerializeAsBase64(picture.get(), &b64_picture);
279 state->SetString("skp64", b64_picture);
282 return state;
285 void DisplayItemList::EmitTraceSnapshot() const {
286 DCHECK(ProcessAppendedItemsCalled());
287 TRACE_EVENT_OBJECT_SNAPSHOT_WITH_ID(
288 TRACE_DISABLED_BY_DEFAULT("cc.debug.display_items") ","
289 TRACE_DISABLED_BY_DEFAULT("cc.debug.picture") ","
290 TRACE_DISABLED_BY_DEFAULT("devtools.timeline.picture"),
291 "cc::DisplayItemList", this,
292 TracedDisplayItemList::AsTraceableDisplayItemList(this,
293 DisplayItemsTracingEnabled()));
296 void DisplayItemList::GatherPixelRefs(const gfx::Size& grid_cell_size) {
297 DCHECK(ProcessAppendedItemsCalled());
298 // This should be only called once, and only after CreateAndCacheSkPicture.
299 DCHECK(picture_);
300 DCHECK(!pixel_refs_);
301 pixel_refs_ = make_scoped_ptr(new PixelRefMap(grid_cell_size));
302 if (!picture_->willPlayBackBitmaps())
303 return;
305 pixel_refs_->GatherPixelRefsFromPicture(picture_.get(), layer_rect_);
308 void* DisplayItemList::GetSidecar(DisplayItem* display_item) {
309 return items_.GetSidecar(display_item);
312 } // namespace cc