Update comments of TabObserver#onLoadStarted and rename onContentChanged
[chromium-blink-merge.git] / cc / playback / display_item_list.cc
blob6d82bd94e03f891374e63b12f21c8420244de628
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/trace_event/trace_event.h"
10 #include "base/trace_event/trace_event_argument.h"
11 #include "cc/base/math_util.h"
12 #include "cc/debug/picture_debug_util.h"
13 #include "cc/debug/traced_picture.h"
14 #include "cc/debug/traced_value.h"
15 #include "cc/playback/largest_display_item.h"
16 #include "third_party/skia/include/core/SkCanvas.h"
17 #include "third_party/skia/include/core/SkPictureRecorder.h"
18 #include "third_party/skia/include/utils/SkPictureUtils.h"
19 #include "ui/gfx/skia_util.h"
21 namespace cc {
23 namespace {
25 bool PictureTracingEnabled() {
26 bool tracing_enabled;
27 TRACE_EVENT_CATEGORY_GROUP_ENABLED(
28 TRACE_DISABLED_BY_DEFAULT("cc.debug.picture") ","
29 TRACE_DISABLED_BY_DEFAULT("devtools.timeline.picture"),
30 &tracing_enabled);
31 return tracing_enabled;
34 const int kDefaultNumDisplayItemsToReserve = 100;
36 } // namespace
38 DisplayItemList::DisplayItemList(gfx::Rect layer_rect,
39 bool use_cached_picture,
40 bool retain_individual_display_items)
41 : items_(LargestDisplayItemSize(), kDefaultNumDisplayItemsToReserve),
42 use_cached_picture_(use_cached_picture),
43 retain_individual_display_items_(retain_individual_display_items),
44 layer_rect_(layer_rect),
45 is_suitable_for_gpu_rasterization_(true),
46 approximate_op_count_(0),
47 picture_memory_usage_(0) {
48 #if DCHECK_IS_ON()
49 needs_process_ = false;
50 #endif
51 if (use_cached_picture_) {
52 SkRTreeFactory factory;
53 recorder_.reset(new SkPictureRecorder());
54 canvas_ = skia::SharePtr(recorder_->beginRecording(
55 layer_rect_.width(), layer_rect_.height(), &factory));
56 canvas_->translate(-layer_rect_.x(), -layer_rect_.y());
57 canvas_->clipRect(gfx::RectToSkRect(layer_rect_));
61 DisplayItemList::DisplayItemList(gfx::Rect layer_rect, bool use_cached_picture)
62 : DisplayItemList(layer_rect,
63 use_cached_picture,
64 !use_cached_picture || PictureTracingEnabled()) {
67 scoped_refptr<DisplayItemList> DisplayItemList::Create(
68 gfx::Rect layer_rect,
69 bool use_cached_picture) {
70 return make_scoped_refptr(
71 new DisplayItemList(layer_rect, use_cached_picture));
74 DisplayItemList::~DisplayItemList() {
77 void DisplayItemList::Raster(SkCanvas* canvas,
78 SkPicture::AbortCallback* callback,
79 float contents_scale) const {
80 DCHECK(ProcessAppendedItemsCalled());
81 if (!use_cached_picture_) {
82 canvas->save();
83 canvas->scale(contents_scale, contents_scale);
84 for (auto* item : items_)
85 item->Raster(canvas, callback);
86 canvas->restore();
87 } else {
88 DCHECK(picture_);
90 canvas->save();
91 canvas->scale(contents_scale, contents_scale);
92 canvas->translate(layer_rect_.x(), layer_rect_.y());
93 if (callback) {
94 // If we have a callback, we need to call |draw()|, |drawPicture()|
95 // doesn't take a callback. This is used by |AnalysisCanvas| to early
96 // out.
97 picture_->playback(canvas, callback);
98 } else {
99 // Prefer to call |drawPicture()| on the canvas since it could place the
100 // entire picture on the canvas instead of parsing the skia operations.
101 canvas->drawPicture(picture_.get());
103 canvas->restore();
107 void DisplayItemList::ProcessAppendedItemsOnTheFly() {
108 if (retain_individual_display_items_)
109 return;
110 if (items_.size() >= kDefaultNumDisplayItemsToReserve) {
111 ProcessAppendedItems();
112 // This function exists to keep the |items_| from growing indefinitely if
113 // we're not going to store them anyway. So the items better be deleted
114 // after |items_| grows too large and we process it.
115 DCHECK(items_.empty());
119 void DisplayItemList::ProcessAppendedItems() {
120 #if DCHECK_IS_ON()
121 needs_process_ = false;
122 #endif
123 for (DisplayItem* item : items_) {
124 is_suitable_for_gpu_rasterization_ &=
125 item->is_suitable_for_gpu_rasterization();
126 approximate_op_count_ += item->approximate_op_count();
128 if (use_cached_picture_) {
129 DCHECK(canvas_);
130 item->Raster(canvas_.get(), NULL);
133 if (retain_individual_display_items_) {
134 // Warning: this double-counts SkPicture data if use_cached_picture_ is
135 // also true.
136 picture_memory_usage_ += item->picture_memory_usage();
140 if (!retain_individual_display_items_)
141 items_.clear();
144 void DisplayItemList::CreateAndCacheSkPicture() {
145 DCHECK(ProcessAppendedItemsCalled());
146 // Convert to an SkPicture for faster rasterization.
147 DCHECK(use_cached_picture_);
148 DCHECK(!picture_);
149 picture_ = skia::AdoptRef(recorder_->endRecordingAsPicture());
150 DCHECK(picture_);
151 picture_memory_usage_ += SkPictureUtils::ApproximateBytesUsed(picture_.get());
152 recorder_.reset();
153 canvas_.clear();
156 bool DisplayItemList::IsSuitableForGpuRasterization() const {
157 DCHECK(ProcessAppendedItemsCalled());
158 // This is more permissive than Picture's implementation, since none of the
159 // items might individually trigger a veto even though they collectively have
160 // enough "bad" operations that a corresponding Picture would get vetoed.
161 return is_suitable_for_gpu_rasterization_;
164 int DisplayItemList::ApproximateOpCount() const {
165 DCHECK(ProcessAppendedItemsCalled());
166 return approximate_op_count_;
169 size_t DisplayItemList::PictureMemoryUsage() const {
170 DCHECK(ProcessAppendedItemsCalled());
171 // We double-count in this case. Produce zero to avoid being misleading.
172 if (use_cached_picture_ && retain_individual_display_items_)
173 return 0;
175 DCHECK_IMPLIES(use_cached_picture_, picture_);
176 return picture_memory_usage_;
179 scoped_refptr<base::trace_event::ConvertableToTraceFormat>
180 DisplayItemList::AsValue() const {
181 DCHECK(ProcessAppendedItemsCalled());
182 scoped_refptr<base::trace_event::TracedValue> state =
183 new base::trace_event::TracedValue();
185 state->SetInteger("length", items_.size());
186 state->BeginArray("params.items");
187 for (const DisplayItem* item : items_) {
188 item->AsValueInto(state.get());
190 state->EndArray();
191 state->SetValue("params.layer_rect", MathUtil::AsValue(layer_rect_));
193 SkPictureRecorder recorder;
194 SkCanvas* canvas =
195 recorder.beginRecording(layer_rect_.width(), layer_rect_.height());
196 canvas->translate(-layer_rect_.x(), -layer_rect_.y());
197 canvas->clipRect(gfx::RectToSkRect(layer_rect_));
198 Raster(canvas, NULL, 1.f);
199 skia::RefPtr<SkPicture> picture =
200 skia::AdoptRef(recorder.endRecordingAsPicture());
202 std::string b64_picture;
203 PictureDebugUtil::SerializeAsBase64(picture.get(), &b64_picture);
204 state->SetString("skp64", b64_picture);
206 return state;
209 void DisplayItemList::EmitTraceSnapshot() const {
210 DCHECK(ProcessAppendedItemsCalled());
211 TRACE_EVENT_OBJECT_SNAPSHOT_WITH_ID(
212 TRACE_DISABLED_BY_DEFAULT("cc.debug.picture") ","
213 TRACE_DISABLED_BY_DEFAULT("devtools.timeline.picture"),
214 "cc::DisplayItemList", this, AsValue());
217 void DisplayItemList::GatherPixelRefs(const gfx::Size& grid_cell_size) {
218 DCHECK(ProcessAppendedItemsCalled());
219 // This should be only called once, and only after CreateAndCacheSkPicture.
220 DCHECK(picture_);
221 DCHECK(!pixel_refs_);
222 pixel_refs_ = make_scoped_ptr(new PixelRefMap(grid_cell_size));
223 if (!picture_->willPlayBackBitmaps())
224 return;
226 pixel_refs_->GatherPixelRefsFromPicture(picture_.get());
228 } // namespace cc