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"
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"
25 bool PictureTracingEnabled() {
27 TRACE_EVENT_CATEGORY_GROUP_ENABLED(
28 TRACE_DISABLED_BY_DEFAULT("cc.debug.picture") ","
29 TRACE_DISABLED_BY_DEFAULT("devtools.timeline.picture"),
31 return tracing_enabled
;
34 const int kDefaultNumDisplayItemsToReserve
= 100;
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) {
49 needs_process_
= false;
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
,
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(
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_
) {
88 canvas
->scale(contents_scale
, contents_scale
);
89 for (auto* item
: items_
)
90 item
->Raster(canvas
, callback
);
96 canvas
->scale(contents_scale
, contents_scale
);
97 canvas
->translate(layer_rect_
.x(), layer_rect_
.y());
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
102 picture_
->playback(canvas
, callback
);
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());
112 void DisplayItemList::ProcessAppendedItemsOnTheFly() {
113 if (retain_individual_display_items_
)
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() {
126 needs_process_
= false;
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_
) {
135 item
->Raster(canvas_
.get(), NULL
);
138 if (retain_individual_display_items_
) {
139 // Warning: this double-counts SkPicture data if use_cached_picture_ is
141 picture_memory_usage_
+= item
->picture_memory_usage();
145 if (!retain_individual_display_items_
)
149 void DisplayItemList::CreateAndCacheSkPicture() {
150 DCHECK(ProcessAppendedItemsCalled());
151 // Convert to an SkPicture for faster rasterization.
152 DCHECK(use_cached_picture_
);
154 picture_
= skia::AdoptRef(recorder_
->endRecordingAsPicture());
156 picture_memory_usage_
+= SkPictureUtils::ApproximateBytesUsed(picture_
.get());
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_
)
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());
196 state
->SetValue("params.layer_rect", MathUtil::AsValue(layer_rect_
));
198 if (!layer_rect_
.IsEmpty()) {
199 SkPictureRecorder recorder
;
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
);
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.
228 DCHECK(!pixel_refs_
);
229 pixel_refs_
= make_scoped_ptr(new PixelRefMap(grid_cell_size
));
230 if (!picture_
->willPlayBackBitmaps())
233 pixel_refs_
->GatherPixelRefsFromPicture(picture_
.get());