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/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"
26 bool PictureTracingEnabled() {
28 TRACE_EVENT_CATEGORY_GROUP_ENABLED(
29 TRACE_DISABLED_BY_DEFAULT("cc.debug.picture") ","
30 TRACE_DISABLED_BY_DEFAULT("devtools.timeline.picture"),
32 return tracing_enabled
;
35 const int kDefaultNumDisplayItemsToReserve
= 100;
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) {
53 needs_process_
= false;
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
,
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(
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(
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_
) {
101 canvas
->scale(contents_scale
, contents_scale
);
102 for (auto* item
: items_
)
103 item
->Raster(canvas
, callback
);
109 canvas
->scale(contents_scale
, contents_scale
);
110 canvas
->translate(layer_rect_
.x(), layer_rect_
.y());
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
115 picture_
->playback(canvas
, callback
);
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());
125 void DisplayItemList::ProcessAppendedItemsOnTheFly() {
126 if (retain_individual_display_items_
)
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() {
139 needs_process_
= false;
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_
) {
148 item
->Raster(canvas_
.get(), NULL
);
151 if (retain_individual_display_items_
) {
152 // Warning: this double-counts SkPicture data if use_cached_picture_ is
154 picture_memory_usage_
+= item
->picture_memory_usage();
158 if (!retain_individual_display_items_
)
162 void DisplayItemList::Finalize() {
163 ProcessAppendedItems();
165 if (use_cached_picture_
) {
166 // Convert to an SkPicture for faster rasterization.
167 DCHECK(use_cached_picture_
);
169 picture_
= skia::AdoptRef(recorder_
->endRecordingAsPicture());
171 picture_memory_usage_
+=
172 SkPictureUtils::ApproximateBytesUsed(picture_
.get());
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_
)
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());
213 state
->SetValue("params.layer_rect", MathUtil::AsValue(layer_rect_
));
215 if (!layer_rect_
.IsEmpty()) {
216 SkPictureRecorder recorder
;
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
);
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.
245 DCHECK(!pixel_refs_
);
246 pixel_refs_
= make_scoped_ptr(new PixelRefMap(grid_cell_size
));
247 if (!picture_
->willPlayBackBitmaps())
250 pixel_refs_
->GatherPixelRefsFromPicture(picture_
.get());
253 void* DisplayItemList::GetSidecar(DisplayItem
* display_item
) {
254 return items_
.GetSidecar(display_item
);