Don't show supervised user as "already on this device" while they're being imported.
[chromium-blink-merge.git] / cc / playback / display_item_list.cc
blobe530e199200df6fd013e09b716743e336d09418a
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::CreateWithoutCachedPicture() {
68 bool use_cached_picture = false;
69 return Create(gfx::Rect(), use_cached_picture);
72 scoped_refptr<DisplayItemList> DisplayItemList::Create(
73 gfx::Rect layer_rect,
74 bool use_cached_picture) {
75 return make_scoped_refptr(
76 new DisplayItemList(layer_rect, use_cached_picture));
79 DisplayItemList::~DisplayItemList() {
82 void DisplayItemList::Raster(SkCanvas* canvas,
83 SkPicture::AbortCallback* callback,
84 float contents_scale) const {
85 DCHECK(ProcessAppendedItemsCalled());
86 if (!use_cached_picture_) {
87 canvas->save();
88 canvas->scale(contents_scale, contents_scale);
89 for (auto* item : items_)
90 item->Raster(canvas, callback);
91 canvas->restore();
92 } else {
93 DCHECK(picture_);
95 canvas->save();
96 canvas->scale(contents_scale, contents_scale);
97 canvas->translate(layer_rect_.x(), layer_rect_.y());
98 if (callback) {
99 // If we have a callback, we need to call |draw()|, |drawPicture()|
100 // doesn't take a callback. This is used by |AnalysisCanvas| to early
101 // out.
102 picture_->playback(canvas, callback);
103 } else {
104 // Prefer to call |drawPicture()| on the canvas since it could place the
105 // entire picture on the canvas instead of parsing the skia operations.
106 canvas->drawPicture(picture_.get());
108 canvas->restore();
112 void DisplayItemList::ProcessAppendedItemsOnTheFly() {
113 if (retain_individual_display_items_)
114 return;
115 if (items_.size() >= kDefaultNumDisplayItemsToReserve) {
116 ProcessAppendedItems();
117 // This function exists to keep the |items_| from growing indefinitely if
118 // we're not going to store them anyway. So the items better be deleted
119 // after |items_| grows too large and we process it.
120 DCHECK(items_.empty());
124 void DisplayItemList::ProcessAppendedItems() {
125 #if DCHECK_IS_ON()
126 needs_process_ = false;
127 #endif
128 for (DisplayItem* item : items_) {
129 is_suitable_for_gpu_rasterization_ &=
130 item->is_suitable_for_gpu_rasterization();
131 approximate_op_count_ += item->approximate_op_count();
133 if (use_cached_picture_) {
134 DCHECK(canvas_);
135 item->Raster(canvas_.get(), NULL);
138 if (retain_individual_display_items_) {
139 // Warning: this double-counts SkPicture data if use_cached_picture_ is
140 // also true.
141 picture_memory_usage_ += item->picture_memory_usage();
145 if (!retain_individual_display_items_)
146 items_.clear();
149 void DisplayItemList::CreateAndCacheSkPicture() {
150 DCHECK(ProcessAppendedItemsCalled());
151 // Convert to an SkPicture for faster rasterization.
152 DCHECK(use_cached_picture_);
153 DCHECK(!picture_);
154 picture_ = skia::AdoptRef(recorder_->endRecordingAsPicture());
155 DCHECK(picture_);
156 picture_memory_usage_ += SkPictureUtils::ApproximateBytesUsed(picture_.get());
157 recorder_.reset();
158 canvas_.clear();
161 bool DisplayItemList::IsSuitableForGpuRasterization() const {
162 DCHECK(ProcessAppendedItemsCalled());
163 // This is more permissive than Picture's implementation, since none of the
164 // items might individually trigger a veto even though they collectively have
165 // enough "bad" operations that a corresponding Picture would get vetoed.
166 return is_suitable_for_gpu_rasterization_;
169 int DisplayItemList::ApproximateOpCount() const {
170 DCHECK(ProcessAppendedItemsCalled());
171 return approximate_op_count_;
174 size_t DisplayItemList::PictureMemoryUsage() const {
175 DCHECK(ProcessAppendedItemsCalled());
176 // We double-count in this case. Produce zero to avoid being misleading.
177 if (use_cached_picture_ && retain_individual_display_items_)
178 return 0;
180 DCHECK_IMPLIES(use_cached_picture_, picture_);
181 return picture_memory_usage_;
184 scoped_refptr<base::trace_event::ConvertableToTraceFormat>
185 DisplayItemList::AsValue() const {
186 DCHECK(ProcessAppendedItemsCalled());
187 scoped_refptr<base::trace_event::TracedValue> state =
188 new base::trace_event::TracedValue();
190 state->SetInteger("length", items_.size());
191 state->BeginArray("params.items");
192 for (const DisplayItem* item : items_) {
193 item->AsValueInto(state.get());
195 state->EndArray();
196 state->SetValue("params.layer_rect", MathUtil::AsValue(layer_rect_));
198 if (!layer_rect_.IsEmpty()) {
199 SkPictureRecorder recorder;
200 SkCanvas* canvas =
201 recorder.beginRecording(layer_rect_.width(), layer_rect_.height());
202 canvas->translate(-layer_rect_.x(), -layer_rect_.y());
203 canvas->clipRect(gfx::RectToSkRect(layer_rect_));
204 Raster(canvas, NULL, 1.f);
205 skia::RefPtr<SkPicture> picture =
206 skia::AdoptRef(recorder.endRecordingAsPicture());
208 std::string b64_picture;
209 PictureDebugUtil::SerializeAsBase64(picture.get(), &b64_picture);
210 state->SetString("skp64", b64_picture);
213 return state;
216 void DisplayItemList::EmitTraceSnapshot() const {
217 DCHECK(ProcessAppendedItemsCalled());
218 TRACE_EVENT_OBJECT_SNAPSHOT_WITH_ID(
219 TRACE_DISABLED_BY_DEFAULT("cc.debug.picture") ","
220 TRACE_DISABLED_BY_DEFAULT("devtools.timeline.picture"),
221 "cc::DisplayItemList", this, AsValue());
224 void DisplayItemList::GatherPixelRefs(const gfx::Size& grid_cell_size) {
225 DCHECK(ProcessAppendedItemsCalled());
226 // This should be only called once, and only after CreateAndCacheSkPicture.
227 DCHECK(picture_);
228 DCHECK(!pixel_refs_);
229 pixel_refs_ = make_scoped_ptr(new PixelRefMap(grid_cell_size));
230 if (!picture_->willPlayBackBitmaps())
231 return;
233 pixel_refs_->GatherPixelRefsFromPicture(picture_.get());
235 } // namespace cc