Pass the affected tree in AXTreeDelegate calls.
[chromium-blink-merge.git] / cc / playback / display_item_list.cc
blob5ee754ba7605a8b96bd9dd72f266a7b05940ea72
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/display_item_list_settings.h"
16 #include "cc/playback/largest_display_item.h"
17 #include "third_party/skia/include/core/SkCanvas.h"
18 #include "third_party/skia/include/core/SkPictureRecorder.h"
19 #include "third_party/skia/include/utils/SkPictureUtils.h"
20 #include "ui/gfx/skia_util.h"
22 namespace cc {
24 namespace {
26 bool PictureTracingEnabled() {
27 bool tracing_enabled;
28 TRACE_EVENT_CATEGORY_GROUP_ENABLED(
29 TRACE_DISABLED_BY_DEFAULT("cc.debug.picture") ","
30 TRACE_DISABLED_BY_DEFAULT("devtools.timeline.picture"),
31 &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 is_suitable_for_gpu_rasterization_(true),
50 approximate_op_count_(0),
51 picture_memory_usage_(0) {
52 #if DCHECK_IS_ON()
53 needs_process_ = false;
54 #endif
55 if (use_cached_picture_) {
56 SkRTreeFactory factory;
57 recorder_.reset(new SkPictureRecorder());
58 canvas_ = skia::SharePtr(recorder_->beginRecording(
59 layer_rect_.width(), layer_rect_.height(), &factory));
60 canvas_->translate(-layer_rect_.x(), -layer_rect_.y());
61 canvas_->clipRect(gfx::RectToSkRect(layer_rect_));
65 DisplayItemList::DisplayItemList(gfx::Rect layer_rect,
66 const DisplayItemListSettings& settings)
67 : DisplayItemList(layer_rect,
68 settings,
69 !settings.use_cached_picture || PictureTracingEnabled()) {
72 scoped_refptr<DisplayItemList> DisplayItemList::CreateWithoutCachedPicture(
73 const DisplayItemListSettings& settings) {
74 DCHECK(!settings.use_cached_picture);
75 return Create(gfx::Rect(), settings);
78 scoped_refptr<DisplayItemList> DisplayItemList::Create(
79 gfx::Rect layer_rect,
80 bool use_cached_picture) {
81 DisplayItemListSettings settings;
82 settings.use_cached_picture = use_cached_picture;
83 return Create(layer_rect, settings);
86 scoped_refptr<DisplayItemList> DisplayItemList::Create(
87 gfx::Rect layer_rect,
88 const DisplayItemListSettings& settings) {
89 return make_scoped_refptr(new DisplayItemList(layer_rect, settings));
92 DisplayItemList::~DisplayItemList() {
95 void DisplayItemList::Raster(SkCanvas* canvas,
96 SkPicture::AbortCallback* callback,
97 float contents_scale) const {
98 DCHECK(ProcessAppendedItemsCalled());
99 if (!use_cached_picture_) {
100 canvas->save();
101 canvas->scale(contents_scale, contents_scale);
102 for (auto* item : items_)
103 item->Raster(canvas, callback);
104 canvas->restore();
105 } else {
106 DCHECK(picture_);
108 canvas->save();
109 canvas->scale(contents_scale, contents_scale);
110 canvas->translate(layer_rect_.x(), layer_rect_.y());
111 if (callback) {
112 // If we have a callback, we need to call |draw()|, |drawPicture()|
113 // doesn't take a callback. This is used by |AnalysisCanvas| to early
114 // out.
115 picture_->playback(canvas, callback);
116 } else {
117 // Prefer to call |drawPicture()| on the canvas since it could place the
118 // entire picture on the canvas instead of parsing the skia operations.
119 canvas->drawPicture(picture_.get());
121 canvas->restore();
125 void DisplayItemList::ProcessAppendedItemsOnTheFly() {
126 if (retain_individual_display_items_)
127 return;
128 if (items_.size() >= kDefaultNumDisplayItemsToReserve) {
129 ProcessAppendedItems();
130 // This function exists to keep the |items_| from growing indefinitely if
131 // we're not going to store them anyway. So the items better be deleted
132 // after |items_| grows too large and we process it.
133 DCHECK(items_.empty());
137 void DisplayItemList::ProcessAppendedItems() {
138 #if DCHECK_IS_ON()
139 needs_process_ = false;
140 #endif
141 for (const DisplayItem* item : items_) {
142 is_suitable_for_gpu_rasterization_ &=
143 item->is_suitable_for_gpu_rasterization();
144 approximate_op_count_ += item->approximate_op_count();
146 if (use_cached_picture_) {
147 DCHECK(canvas_);
148 item->Raster(canvas_.get(), NULL);
151 if (retain_individual_display_items_) {
152 // Warning: this double-counts SkPicture data if use_cached_picture_ is
153 // also true.
154 picture_memory_usage_ += item->picture_memory_usage();
158 if (!retain_individual_display_items_)
159 items_.clear();
162 void DisplayItemList::Finalize() {
163 ProcessAppendedItems();
165 if (use_cached_picture_) {
166 // Convert to an SkPicture for faster rasterization.
167 DCHECK(use_cached_picture_);
168 DCHECK(!picture_);
169 picture_ = skia::AdoptRef(recorder_->endRecordingAsPicture());
170 DCHECK(picture_);
171 picture_memory_usage_ +=
172 SkPictureUtils::ApproximateBytesUsed(picture_.get());
173 recorder_.reset();
174 canvas_.clear();
178 bool DisplayItemList::IsSuitableForGpuRasterization() const {
179 DCHECK(ProcessAppendedItemsCalled());
180 // This is more permissive than Picture's implementation, since none of the
181 // items might individually trigger a veto even though they collectively have
182 // enough "bad" operations that a corresponding Picture would get vetoed.
183 return is_suitable_for_gpu_rasterization_;
186 int DisplayItemList::ApproximateOpCount() const {
187 DCHECK(ProcessAppendedItemsCalled());
188 return approximate_op_count_;
191 size_t DisplayItemList::PictureMemoryUsage() const {
192 DCHECK(ProcessAppendedItemsCalled());
193 // We double-count in this case. Produce zero to avoid being misleading.
194 if (use_cached_picture_ && retain_individual_display_items_)
195 return 0;
197 DCHECK_IMPLIES(use_cached_picture_, picture_);
198 return picture_memory_usage_;
201 scoped_refptr<base::trace_event::ConvertableToTraceFormat>
202 DisplayItemList::AsValue() const {
203 DCHECK(ProcessAppendedItemsCalled());
204 scoped_refptr<base::trace_event::TracedValue> state =
205 new base::trace_event::TracedValue();
207 state->SetInteger("length", items_.size());
208 state->BeginArray("params.items");
209 for (const DisplayItem* item : items_) {
210 item->AsValueInto(state.get());
212 state->EndArray();
213 state->SetValue("params.layer_rect", MathUtil::AsValue(layer_rect_));
215 if (!layer_rect_.IsEmpty()) {
216 SkPictureRecorder recorder;
217 SkCanvas* canvas =
218 recorder.beginRecording(layer_rect_.width(), layer_rect_.height());
219 canvas->translate(-layer_rect_.x(), -layer_rect_.y());
220 canvas->clipRect(gfx::RectToSkRect(layer_rect_));
221 Raster(canvas, NULL, 1.f);
222 skia::RefPtr<SkPicture> picture =
223 skia::AdoptRef(recorder.endRecordingAsPicture());
225 std::string b64_picture;
226 PictureDebugUtil::SerializeAsBase64(picture.get(), &b64_picture);
227 state->SetString("skp64", b64_picture);
230 return state;
233 void DisplayItemList::EmitTraceSnapshot() const {
234 DCHECK(ProcessAppendedItemsCalled());
235 TRACE_EVENT_OBJECT_SNAPSHOT_WITH_ID(
236 TRACE_DISABLED_BY_DEFAULT("cc.debug.picture") ","
237 TRACE_DISABLED_BY_DEFAULT("devtools.timeline.picture"),
238 "cc::DisplayItemList", this, AsValue());
241 void DisplayItemList::GatherPixelRefs(const gfx::Size& grid_cell_size) {
242 DCHECK(ProcessAppendedItemsCalled());
243 // This should be only called once, and only after CreateAndCacheSkPicture.
244 DCHECK(picture_);
245 DCHECK(!pixel_refs_);
246 pixel_refs_ = make_scoped_ptr(new PixelRefMap(grid_cell_size));
247 if (!picture_->willPlayBackBitmaps())
248 return;
250 pixel_refs_->GatherPixelRefsFromPicture(picture_.get());
253 void* DisplayItemList::GetSidecar(DisplayItem* display_item) {
254 return items_.GetSidecar(display_item);
257 } // namespace cc