Update V8 to version 4.6.55.
[chromium-blink-merge.git] / cc / playback / display_item_list.cc
blob08fe71d26e6d2c4afb0bc765d7f372762798c371
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 bool DisplayItemsTracingEnabled() {
29 bool tracing_enabled;
30 TRACE_EVENT_CATEGORY_GROUP_ENABLED(
31 TRACE_DISABLED_BY_DEFAULT("cc.debug.display_items"), &tracing_enabled);
32 return tracing_enabled;
35 const int kDefaultNumDisplayItemsToReserve = 100;
37 } // namespace
39 DisplayItemList::DisplayItemList(gfx::Rect layer_rect,
40 const DisplayItemListSettings& settings,
41 bool retain_individual_display_items)
42 : items_(LargestDisplayItemSize(),
43 settings.max_sidecar_size,
44 kDefaultNumDisplayItemsToReserve,
45 settings.sidecar_destroyer),
46 use_cached_picture_(settings.use_cached_picture),
47 retain_individual_display_items_(retain_individual_display_items),
48 layer_rect_(layer_rect),
49 all_items_are_suitable_for_gpu_rasterization_(true),
50 approximate_op_count_(0),
51 picture_memory_usage_(0),
52 external_memory_usage_(0) {
53 #if DCHECK_IS_ON()
54 needs_process_ = false;
55 #endif
56 if (use_cached_picture_) {
57 SkRTreeFactory factory;
58 recorder_.reset(new SkPictureRecorder());
59 canvas_ = skia::SharePtr(recorder_->beginRecording(
60 layer_rect_.width(), layer_rect_.height(), &factory));
61 canvas_->translate(-layer_rect_.x(), -layer_rect_.y());
62 canvas_->clipRect(gfx::RectToSkRect(layer_rect_));
66 DisplayItemList::DisplayItemList(gfx::Rect layer_rect,
67 const DisplayItemListSettings& settings)
68 : DisplayItemList(
69 layer_rect,
70 settings,
71 !settings.use_cached_picture || DisplayItemsTracingEnabled()) {
74 scoped_refptr<DisplayItemList> DisplayItemList::CreateWithoutCachedPicture(
75 const DisplayItemListSettings& settings) {
76 DCHECK(!settings.use_cached_picture);
77 return Create(gfx::Rect(), settings);
80 scoped_refptr<DisplayItemList> DisplayItemList::Create(
81 gfx::Rect layer_rect,
82 bool use_cached_picture) {
83 DisplayItemListSettings settings;
84 settings.use_cached_picture = use_cached_picture;
85 return Create(layer_rect, settings);
88 scoped_refptr<DisplayItemList> DisplayItemList::Create(
89 gfx::Rect layer_rect,
90 const DisplayItemListSettings& settings) {
91 return make_scoped_refptr(new DisplayItemList(layer_rect, settings));
94 DisplayItemList::~DisplayItemList() {
97 void DisplayItemList::Raster(SkCanvas* canvas,
98 SkPicture::AbortCallback* callback,
99 const gfx::Rect& canvas_target_playback_rect,
100 float contents_scale) const {
101 DCHECK(ProcessAppendedItemsCalled());
102 if (!use_cached_picture_) {
103 canvas->save();
104 canvas->scale(contents_scale, contents_scale);
105 for (auto* item : items_)
106 item->Raster(canvas, canvas_target_playback_rect, callback);
107 canvas->restore();
108 } else {
109 DCHECK(picture_);
111 canvas->save();
112 canvas->scale(contents_scale, contents_scale);
113 canvas->translate(layer_rect_.x(), layer_rect_.y());
114 if (callback) {
115 // If we have a callback, we need to call |draw()|, |drawPicture()|
116 // doesn't take a callback. This is used by |AnalysisCanvas| to early
117 // out.
118 picture_->playback(canvas, callback);
119 } else {
120 // Prefer to call |drawPicture()| on the canvas since it could place the
121 // entire picture on the canvas instead of parsing the skia operations.
122 canvas->drawPicture(picture_.get());
124 canvas->restore();
128 void DisplayItemList::ProcessAppendedItemsOnTheFly() {
129 if (retain_individual_display_items_)
130 return;
131 if (items_.size() >= kDefaultNumDisplayItemsToReserve) {
132 ProcessAppendedItems();
133 // This function exists to keep the |items_| from growing indefinitely if
134 // we're not going to store them anyway. So the items better be deleted
135 // after |items_| grows too large and we process it.
136 DCHECK(items_.empty());
140 void DisplayItemList::ProcessAppendedItems() {
141 #if DCHECK_IS_ON()
142 needs_process_ = false;
143 #endif
144 for (const DisplayItem* item : items_) {
145 all_items_are_suitable_for_gpu_rasterization_ &=
146 item->is_suitable_for_gpu_rasterization();
147 approximate_op_count_ += item->approximate_op_count();
149 if (use_cached_picture_) {
150 DCHECK(canvas_);
151 item->Raster(canvas_.get(), gfx::Rect(), NULL);
154 if (retain_individual_display_items_) {
155 // Warning: this double-counts SkPicture data if use_cached_picture_ is
156 // also true.
157 external_memory_usage_ += item->external_memory_usage();
161 if (!retain_individual_display_items_)
162 items_.clear();
165 void DisplayItemList::RasterIntoCanvas(const DisplayItem& item) {
166 DCHECK(canvas_);
167 DCHECK(!retain_individual_display_items_);
168 all_items_are_suitable_for_gpu_rasterization_ &=
169 item.is_suitable_for_gpu_rasterization();
170 approximate_op_count_ += item.approximate_op_count();
172 item.Raster(canvas_.get(), gfx::Rect(), NULL);
175 bool DisplayItemList::RetainsIndividualDisplayItems() const {
176 return retain_individual_display_items_;
179 void DisplayItemList::RemoveLast() {
180 // We cannot remove the last item if it has been squashed into a picture.
181 // The last item should not have been handled by ProcessAppendedItems, so we
182 // don't need to remove it from approximate_op_count_, etc.
183 DCHECK(retain_individual_display_items_);
184 DCHECK(!use_cached_picture_);
185 items_.RemoveLast();
188 void DisplayItemList::Finalize() {
189 ProcessAppendedItems();
191 if (use_cached_picture_) {
192 // Convert to an SkPicture for faster rasterization.
193 DCHECK(use_cached_picture_);
194 DCHECK(!picture_);
195 picture_ = skia::AdoptRef(recorder_->endRecordingAsPicture());
196 DCHECK(picture_);
197 picture_memory_usage_ =
198 SkPictureUtils::ApproximateBytesUsed(picture_.get());
199 recorder_.reset();
200 canvas_.clear();
204 bool DisplayItemList::IsSuitableForGpuRasterization() const {
205 DCHECK(ProcessAppendedItemsCalled());
206 if (use_cached_picture_)
207 return picture_->suitableForGpuRasterization(NULL);
209 // This is more permissive than Picture's implementation, since none of the
210 // items might individually trigger a veto even though they collectively have
211 // enough "bad" operations that a corresponding Picture would get vetoed. See
212 // crbug.com/513016.
213 return all_items_are_suitable_for_gpu_rasterization_;
216 int DisplayItemList::ApproximateOpCount() const {
217 DCHECK(ProcessAppendedItemsCalled());
218 return approximate_op_count_;
221 size_t DisplayItemList::ApproximateMemoryUsage() const {
222 DCHECK(ProcessAppendedItemsCalled());
223 // We double-count in this case. Produce zero to avoid being misleading.
224 if (use_cached_picture_ && retain_individual_display_items_)
225 return 0;
227 DCHECK_IMPLIES(use_cached_picture_, picture_);
229 size_t memory_usage = sizeof(*this);
231 // Memory outside this class due to |items_|.
232 memory_usage += items_.GetCapacityInBytes() + external_memory_usage_;
234 // Memory outside this class due to |picture|.
235 memory_usage += picture_memory_usage_;
237 // TODO(jbroman): Does anything else owned by this class substantially
238 // contribute to memory usage?
240 return memory_usage;
243 scoped_refptr<base::trace_event::ConvertableToTraceFormat>
244 DisplayItemList::AsValue(bool include_items) const {
245 DCHECK(ProcessAppendedItemsCalled());
246 scoped_refptr<base::trace_event::TracedValue> state =
247 new base::trace_event::TracedValue();
249 if (include_items) {
250 state->BeginArray("params.items");
251 for (const DisplayItem* item : items_) {
252 item->AsValueInto(state.get());
254 state->EndArray();
257 state->SetValue("params.layer_rect", MathUtil::AsValue(layer_rect_));
259 if (!layer_rect_.IsEmpty()) {
260 SkPictureRecorder recorder;
261 SkCanvas* canvas =
262 recorder.beginRecording(layer_rect_.width(), layer_rect_.height());
263 canvas->translate(-layer_rect_.x(), -layer_rect_.y());
264 canvas->clipRect(gfx::RectToSkRect(layer_rect_));
265 Raster(canvas, NULL, gfx::Rect(), 1.f);
266 skia::RefPtr<SkPicture> picture =
267 skia::AdoptRef(recorder.endRecordingAsPicture());
269 std::string b64_picture;
270 PictureDebugUtil::SerializeAsBase64(picture.get(), &b64_picture);
271 state->SetString("skp64", b64_picture);
274 return state;
277 void DisplayItemList::EmitTraceSnapshot() const {
278 DCHECK(ProcessAppendedItemsCalled());
279 TRACE_EVENT_OBJECT_SNAPSHOT_WITH_ID(
280 TRACE_DISABLED_BY_DEFAULT("cc.debug.display_items") ","
281 TRACE_DISABLED_BY_DEFAULT("cc.debug.picture") ","
282 TRACE_DISABLED_BY_DEFAULT("devtools.timeline.picture"),
283 "cc::DisplayItemList", this,
284 TracedDisplayItemList::AsTraceableDisplayItemList(this,
285 DisplayItemsTracingEnabled()));
288 void DisplayItemList::GatherPixelRefs(const gfx::Size& grid_cell_size) {
289 DCHECK(ProcessAppendedItemsCalled());
290 // This should be only called once, and only after CreateAndCacheSkPicture.
291 DCHECK(picture_);
292 DCHECK(!pixel_refs_);
293 pixel_refs_ = make_scoped_ptr(new PixelRefMap(grid_cell_size));
294 if (!picture_->willPlayBackBitmaps())
295 return;
297 pixel_refs_->GatherPixelRefsFromPicture(picture_.get());
300 void* DisplayItemList::GetSidecar(DisplayItem* display_item) {
301 return items_.GetSidecar(display_item);
304 } // namespace cc