Update V8 to version 4.7.44.
[chromium-blink-merge.git] / cc / playback / display_item_list.cc
blob73e876239263e0c2e57b685df62747c83783dda0
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(), kDefaultNumDisplayItemsToReserve),
47 use_cached_picture_(settings.use_cached_picture),
48 retain_individual_display_items_(retain_individual_display_items),
49 layer_rect_(layer_rect),
50 all_items_are_suitable_for_gpu_rasterization_(true),
51 approximate_op_count_(0),
52 picture_memory_usage_(0),
53 external_memory_usage_(0) {
54 #if DCHECK_IS_ON()
55 needs_process_ = false;
56 #endif
57 if (use_cached_picture_) {
58 SkRTreeFactory factory;
59 recorder_.reset(new SkPictureRecorder());
60 canvas_ = skia::SharePtr(recorder_->beginRecording(
61 layer_rect_.width(), layer_rect_.height(), &factory));
62 canvas_->translate(-layer_rect_.x(), -layer_rect_.y());
63 canvas_->clipRect(gfx::RectToSkRect(layer_rect_));
67 DisplayItemList::DisplayItemList(gfx::Rect layer_rect,
68 const DisplayItemListSettings& settings)
69 : DisplayItemList(
70 layer_rect,
71 settings,
72 !settings.use_cached_picture || DisplayItemsTracingEnabled()) {
75 scoped_refptr<DisplayItemList> DisplayItemList::CreateWithoutCachedPicture(
76 const DisplayItemListSettings& settings) {
77 DCHECK(!settings.use_cached_picture);
78 return Create(gfx::Rect(), settings);
81 scoped_refptr<DisplayItemList> DisplayItemList::Create(
82 gfx::Rect layer_rect,
83 bool use_cached_picture) {
84 DisplayItemListSettings settings;
85 settings.use_cached_picture = use_cached_picture;
86 return Create(layer_rect, settings);
89 scoped_refptr<DisplayItemList> DisplayItemList::Create(
90 gfx::Rect layer_rect,
91 const DisplayItemListSettings& settings) {
92 return make_scoped_refptr(new DisplayItemList(layer_rect, settings));
95 DisplayItemList::~DisplayItemList() {
98 void DisplayItemList::Raster(SkCanvas* canvas,
99 SkPicture::AbortCallback* callback,
100 const gfx::Rect& canvas_target_playback_rect,
101 float contents_scale) const {
102 DCHECK(ProcessAppendedItemsCalled());
103 if (!use_cached_picture_) {
104 canvas->save();
105 canvas->scale(contents_scale, contents_scale);
106 for (auto* item : items_)
107 item->Raster(canvas, canvas_target_playback_rect, callback);
108 canvas->restore();
109 } else {
110 DCHECK(picture_);
112 canvas->save();
113 canvas->scale(contents_scale, contents_scale);
114 canvas->translate(layer_rect_.x(), layer_rect_.y());
115 if (callback) {
116 // If we have a callback, we need to call |draw()|, |drawPicture()|
117 // doesn't take a callback. This is used by |AnalysisCanvas| to early
118 // out.
119 picture_->playback(canvas, callback);
120 } else {
121 // Prefer to call |drawPicture()| on the canvas since it could place the
122 // entire picture on the canvas instead of parsing the skia operations.
123 canvas->drawPicture(picture_.get());
125 canvas->restore();
129 void DisplayItemList::ProcessAppendedItemsOnTheFly() {
130 if (retain_individual_display_items_)
131 return;
132 if (items_.size() >= kDefaultNumDisplayItemsToReserve) {
133 ProcessAppendedItems();
134 // This function exists to keep the |items_| from growing indefinitely if
135 // we're not going to store them anyway. So the items better be deleted
136 // after |items_| grows too large and we process it.
137 DCHECK(items_.empty());
141 void DisplayItemList::ProcessAppendedItems() {
142 #if DCHECK_IS_ON()
143 needs_process_ = false;
144 #endif
145 for (const DisplayItem* item : items_) {
146 all_items_are_suitable_for_gpu_rasterization_ &=
147 item->is_suitable_for_gpu_rasterization();
148 approximate_op_count_ += item->approximate_op_count();
150 if (use_cached_picture_) {
151 DCHECK(canvas_);
152 item->Raster(canvas_.get(), gfx::Rect(), NULL);
155 if (retain_individual_display_items_) {
156 // Warning: this double-counts SkPicture data if use_cached_picture_ is
157 // also true.
158 external_memory_usage_ += item->external_memory_usage();
162 if (!retain_individual_display_items_)
163 items_.clear();
166 void DisplayItemList::RasterIntoCanvas(const DisplayItem& item) {
167 DCHECK(canvas_);
168 DCHECK(!retain_individual_display_items_);
169 all_items_are_suitable_for_gpu_rasterization_ &=
170 item.is_suitable_for_gpu_rasterization();
171 approximate_op_count_ += item.approximate_op_count();
173 item.Raster(canvas_.get(), gfx::Rect(), NULL);
176 bool DisplayItemList::RetainsIndividualDisplayItems() const {
177 return retain_individual_display_items_;
180 void DisplayItemList::RemoveLast() {
181 // We cannot remove the last item if it has been squashed into a picture.
182 // The last item should not have been handled by ProcessAppendedItems, so we
183 // don't need to remove it from approximate_op_count_, etc.
184 DCHECK(retain_individual_display_items_);
185 DCHECK(!use_cached_picture_);
186 items_.RemoveLast();
189 void DisplayItemList::Finalize() {
190 ProcessAppendedItems();
192 if (use_cached_picture_) {
193 // Convert to an SkPicture for faster rasterization.
194 DCHECK(use_cached_picture_);
195 DCHECK(!picture_);
196 picture_ = skia::AdoptRef(recorder_->endRecordingAsPicture());
197 DCHECK(picture_);
198 picture_memory_usage_ =
199 SkPictureUtils::ApproximateBytesUsed(picture_.get());
200 recorder_.reset();
201 canvas_.clear();
205 bool DisplayItemList::IsSuitableForGpuRasterization() const {
206 DCHECK(ProcessAppendedItemsCalled());
207 if (use_cached_picture_)
208 return picture_->suitableForGpuRasterization(NULL);
210 // This is more permissive than Picture's implementation, since none of the
211 // items might individually trigger a veto even though they collectively have
212 // enough "bad" operations that a corresponding Picture would get vetoed. See
213 // crbug.com/513016.
214 return all_items_are_suitable_for_gpu_rasterization_;
217 int DisplayItemList::ApproximateOpCount() const {
218 DCHECK(ProcessAppendedItemsCalled());
219 return approximate_op_count_;
222 size_t DisplayItemList::ApproximateMemoryUsage() const {
223 DCHECK(ProcessAppendedItemsCalled());
224 // We double-count in this case. Produce zero to avoid being misleading.
225 if (use_cached_picture_ && retain_individual_display_items_)
226 return 0;
228 DCHECK_IMPLIES(use_cached_picture_, picture_);
230 size_t memory_usage = sizeof(*this);
232 // Memory outside this class due to |items_|.
233 memory_usage += items_.GetCapacityInBytes() + external_memory_usage_;
235 // Memory outside this class due to |picture|.
236 memory_usage += picture_memory_usage_;
238 // TODO(jbroman): Does anything else owned by this class substantially
239 // contribute to memory usage?
241 return memory_usage;
244 bool DisplayItemList::ShouldBeAnalyzedForSolidColor() const {
245 return ApproximateOpCount() <= kOpCountThatIsOkToAnalyze;
248 scoped_refptr<base::trace_event::ConvertableToTraceFormat>
249 DisplayItemList::AsValue(bool include_items) const {
250 DCHECK(ProcessAppendedItemsCalled());
251 scoped_refptr<base::trace_event::TracedValue> state =
252 new base::trace_event::TracedValue();
254 if (include_items) {
255 state->BeginArray("params.items");
256 for (const DisplayItem* item : items_) {
257 item->AsValueInto(state.get());
259 state->EndArray();
262 state->SetValue("params.layer_rect", MathUtil::AsValue(layer_rect_));
264 if (!layer_rect_.IsEmpty()) {
265 SkPictureRecorder recorder;
266 SkCanvas* canvas =
267 recorder.beginRecording(layer_rect_.width(), layer_rect_.height());
268 canvas->translate(-layer_rect_.x(), -layer_rect_.y());
269 canvas->clipRect(gfx::RectToSkRect(layer_rect_));
270 Raster(canvas, NULL, gfx::Rect(), 1.f);
271 skia::RefPtr<SkPicture> picture =
272 skia::AdoptRef(recorder.endRecordingAsPicture());
274 std::string b64_picture;
275 PictureDebugUtil::SerializeAsBase64(picture.get(), &b64_picture);
276 state->SetString("skp64", b64_picture);
279 return state;
282 void DisplayItemList::EmitTraceSnapshot() const {
283 DCHECK(ProcessAppendedItemsCalled());
284 TRACE_EVENT_OBJECT_SNAPSHOT_WITH_ID(
285 TRACE_DISABLED_BY_DEFAULT("cc.debug.display_items") ","
286 TRACE_DISABLED_BY_DEFAULT("cc.debug.picture") ","
287 TRACE_DISABLED_BY_DEFAULT("devtools.timeline.picture"),
288 "cc::DisplayItemList", this,
289 TracedDisplayItemList::AsTraceableDisplayItemList(this,
290 DisplayItemsTracingEnabled()));
293 void DisplayItemList::GatherDiscardableImages(const gfx::Size& grid_cell_size) {
294 DCHECK(ProcessAppendedItemsCalled());
295 // This should be only called once, and only after CreateAndCacheSkPicture.
296 DCHECK(picture_);
297 DCHECK(!images_);
298 images_ = make_scoped_ptr(new DiscardableImageMap(grid_cell_size));
299 if (!picture_->willPlayBackBitmaps())
300 return;
302 images_->GatherImagesFromPicture(picture_.get(), layer_rect_);
305 } // namespace cc