Supervised user whitelists: Cleanup
[chromium-blink-merge.git] / cc / resources / display_item_list.cc
blob0b2e12e06c1e394e6d934ad70705e5d9c9fcf152
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/resources/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 "third_party/skia/include/core/SkCanvas.h"
16 #include "third_party/skia/include/core/SkDrawPictureCallback.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 DisplayItemList::DisplayItemList(gfx::Rect layer_rect, bool use_cached_picture)
24 : recorder_(new SkPictureRecorder()),
25 use_cached_picture_(use_cached_picture),
26 retain_individual_display_items_(!use_cached_picture),
27 layer_rect_(layer_rect),
28 is_suitable_for_gpu_rasterization_(true),
29 approximate_op_count_(0) {
30 if (use_cached_picture_) {
31 SkRTreeFactory factory;
32 recorder_.reset(new SkPictureRecorder());
33 canvas_ = skia::SharePtr(recorder_->beginRecording(
34 layer_rect_.width(), layer_rect_.height(), &factory));
35 canvas_->translate(-layer_rect_.x(), -layer_rect_.y());
36 canvas_->clipRect(gfx::RectToSkRect(layer_rect_));
38 bool tracing_enabled;
39 TRACE_EVENT_CATEGORY_GROUP_ENABLED(
40 TRACE_DISABLED_BY_DEFAULT("cc.debug.picture") ","
41 TRACE_DISABLED_BY_DEFAULT("devtools.timeline.picture"),
42 &tracing_enabled);
43 if (tracing_enabled)
44 retain_individual_display_items_ = true;
48 scoped_refptr<DisplayItemList> DisplayItemList::Create(
49 gfx::Rect layer_rect,
50 bool use_cached_picture) {
51 return make_scoped_refptr(
52 new DisplayItemList(layer_rect, use_cached_picture));
55 DisplayItemList::~DisplayItemList() {
58 void DisplayItemList::Raster(SkCanvas* canvas,
59 SkDrawPictureCallback* callback,
60 float contents_scale) const {
61 if (!use_cached_picture_) {
62 canvas->save();
63 canvas->scale(contents_scale, contents_scale);
64 for (size_t i = 0; i < items_.size(); ++i) {
65 items_[i]->Raster(canvas, callback);
67 canvas->restore();
68 } else {
69 DCHECK(picture_);
71 canvas->save();
72 canvas->scale(contents_scale, contents_scale);
73 canvas->translate(layer_rect_.x(), layer_rect_.y());
74 if (callback) {
75 // If we have a callback, we need to call |draw()|, |drawPicture()|
76 // doesn't take a callback. This is used by |AnalysisCanvas| to early
77 // out.
78 picture_->playback(canvas, callback);
79 } else {
80 // Prefer to call |drawPicture()| on the canvas since it could place the
81 // entire picture on the canvas instead of parsing the skia operations.
82 canvas->drawPicture(picture_.get());
84 canvas->restore();
88 void DisplayItemList::CreateAndCacheSkPicture() {
89 // Convert to an SkPicture for faster rasterization.
90 DCHECK(use_cached_picture_);
91 picture_ = skia::AdoptRef(recorder_->endRecordingAsPicture());
92 DCHECK(picture_);
93 recorder_.reset();
94 canvas_.clear();
97 void DisplayItemList::AppendItem(scoped_ptr<DisplayItem> item) {
98 is_suitable_for_gpu_rasterization_ &= item->IsSuitableForGpuRasterization();
99 approximate_op_count_ += item->ApproximateOpCount();
101 if (use_cached_picture_) {
102 DCHECK(canvas_);
103 item->Raster(canvas_.get(), NULL);
106 if (retain_individual_display_items_)
107 items_.push_back(item.Pass());
110 bool DisplayItemList::IsSuitableForGpuRasterization() const {
111 // This is more permissive than Picture's implementation, since none of the
112 // items might individually trigger a veto even though they collectively have
113 // enough "bad" operations that a corresponding Picture would get vetoed.
114 return is_suitable_for_gpu_rasterization_;
117 int DisplayItemList::ApproximateOpCount() const {
118 return approximate_op_count_;
121 size_t DisplayItemList::PictureMemoryUsage() const {
122 size_t total_size = 0;
124 for (const auto& item : items_) {
125 total_size += item->PictureMemoryUsage();
128 if (picture_)
129 total_size += SkPictureUtils::ApproximateBytesUsed(picture_.get());
131 return total_size;
134 scoped_refptr<base::trace_event::ConvertableToTraceFormat>
135 DisplayItemList::AsValue() const {
136 scoped_refptr<base::trace_event::TracedValue> state =
137 new base::trace_event::TracedValue();
139 state->SetInteger("length", items_.size());
140 state->BeginArray("params.items");
141 for (const DisplayItem* item : items_) {
142 item->AsValueInto(state.get());
144 state->EndArray();
145 state->SetValue("params.layer_rect",
146 MathUtil::AsValue(layer_rect_).release());
148 SkPictureRecorder recorder;
149 SkCanvas* canvas =
150 recorder.beginRecording(layer_rect_.width(), layer_rect_.height());
151 canvas->translate(-layer_rect_.x(), -layer_rect_.y());
152 canvas->clipRect(gfx::RectToSkRect(layer_rect_));
153 Raster(canvas, NULL, 1.f);
154 skia::RefPtr<SkPicture> picture =
155 skia::AdoptRef(recorder.endRecordingAsPicture());
157 std::string b64_picture;
158 PictureDebugUtil::SerializeAsBase64(picture.get(), &b64_picture);
159 state->SetString("skp64", b64_picture);
161 return state;
164 void DisplayItemList::EmitTraceSnapshot() const {
165 TRACE_EVENT_OBJECT_SNAPSHOT_WITH_ID(
166 TRACE_DISABLED_BY_DEFAULT("cc.debug.picture") ","
167 TRACE_DISABLED_BY_DEFAULT("devtools.timeline.picture"),
168 "cc::DisplayItemList", this, AsValue());
171 void DisplayItemList::GatherPixelRefs(const gfx::Size& grid_cell_size) {
172 // This should be only called once, and only after CreateAndCacheSkPicture.
173 DCHECK(picture_);
174 DCHECK(!pixel_refs_);
175 pixel_refs_ = make_scoped_ptr(new PixelRefMap(grid_cell_size));
176 if (!picture_->willPlayBackBitmaps())
177 return;
179 pixel_refs_->GatherPixelRefsFromPicture(picture_.get());
181 } // namespace cc