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/numerics/safe_conversions.h"
10 #include "base/trace_event/trace_event.h"
11 #include "base/trace_event/trace_event_argument.h"
12 #include "cc/base/math_util.h"
13 #include "cc/debug/picture_debug_util.h"
14 #include "cc/debug/traced_display_item_list.h"
15 #include "cc/debug/traced_picture.h"
16 #include "cc/debug/traced_value.h"
17 #include "cc/playback/display_item_list_settings.h"
18 #include "cc/playback/largest_display_item.h"
19 #include "third_party/skia/include/core/SkCanvas.h"
20 #include "third_party/skia/include/core/SkPictureRecorder.h"
21 #include "third_party/skia/include/utils/SkPictureUtils.h"
22 #include "ui/gfx/skia_util.h"
28 // We don't perform per-layer solid color analysis when there are too many skia
30 const int kOpCountThatIsOkToAnalyze
= 10;
32 bool DisplayItemsTracingEnabled() {
34 TRACE_EVENT_CATEGORY_GROUP_ENABLED(
35 TRACE_DISABLED_BY_DEFAULT("cc.debug.display_items"), &tracing_enabled
);
36 return tracing_enabled
;
39 const int kDefaultNumDisplayItemsToReserve
= 100;
43 DisplayItemList::DisplayItemList(gfx::Rect layer_rect
,
44 const DisplayItemListSettings
& settings
,
45 bool retain_individual_display_items
)
46 : items_(LargestDisplayItemSize(), kDefaultNumDisplayItemsToReserve
),
47 use_cached_picture_(settings
.use_cached_picture
),
48 retain_individual_display_items_(retain_individual_display_items
),
49 layer_rect_(layer_rect
),
50 all_items_are_suitable_for_gpu_rasterization_(true),
51 approximate_op_count_(0),
52 picture_memory_usage_(0),
53 external_memory_usage_(0) {
55 needs_process_
= false;
57 if (use_cached_picture_
) {
58 SkRTreeFactory factory
;
59 recorder_
.reset(new SkPictureRecorder());
60 canvas_
= skia::SharePtr(recorder_
->beginRecording(
61 layer_rect_
.width(), layer_rect_
.height(), &factory
));
62 canvas_
->translate(-layer_rect_
.x(), -layer_rect_
.y());
63 canvas_
->clipRect(gfx::RectToSkRect(layer_rect_
));
67 DisplayItemList::DisplayItemList(gfx::Rect layer_rect
,
68 const DisplayItemListSettings
& settings
)
72 !settings
.use_cached_picture
|| DisplayItemsTracingEnabled()) {
75 scoped_refptr
<DisplayItemList
> DisplayItemList::CreateWithoutCachedPicture(
76 const DisplayItemListSettings
& settings
) {
77 DCHECK(!settings
.use_cached_picture
);
78 return Create(gfx::Rect(), settings
);
81 scoped_refptr
<DisplayItemList
> DisplayItemList::Create(
83 bool use_cached_picture
) {
84 DisplayItemListSettings settings
;
85 settings
.use_cached_picture
= use_cached_picture
;
86 return Create(layer_rect
, settings
);
89 scoped_refptr
<DisplayItemList
> DisplayItemList::Create(
91 const DisplayItemListSettings
& settings
) {
92 return make_scoped_refptr(new DisplayItemList(layer_rect
, settings
));
95 DisplayItemList::~DisplayItemList() {
98 void DisplayItemList::Raster(SkCanvas
* canvas
,
99 SkPicture::AbortCallback
* callback
,
100 const gfx::Rect
& canvas_target_playback_rect
,
101 float contents_scale
) const {
102 DCHECK(ProcessAppendedItemsCalled());
103 if (!use_cached_picture_
) {
105 canvas
->scale(contents_scale
, contents_scale
);
106 for (auto* item
: items_
)
107 item
->Raster(canvas
, canvas_target_playback_rect
, callback
);
113 canvas
->scale(contents_scale
, contents_scale
);
114 canvas
->translate(layer_rect_
.x(), layer_rect_
.y());
116 // If we have a callback, we need to call |draw()|, |drawPicture()|
117 // doesn't take a callback. This is used by |AnalysisCanvas| to early
119 picture_
->playback(canvas
, callback
);
121 // Prefer to call |drawPicture()| on the canvas since it could place the
122 // entire picture on the canvas instead of parsing the skia operations.
123 canvas
->drawPicture(picture_
.get());
129 void DisplayItemList::ProcessAppendedItemsOnTheFly() {
130 if (retain_individual_display_items_
)
132 if (items_
.size() >= kDefaultNumDisplayItemsToReserve
) {
133 ProcessAppendedItems();
134 // This function exists to keep the |items_| from growing indefinitely if
135 // we're not going to store them anyway. So the items better be deleted
136 // after |items_| grows too large and we process it.
137 DCHECK(items_
.empty());
141 void DisplayItemList::ProcessAppendedItems() {
143 needs_process_
= false;
145 for (const DisplayItem
* item
: items_
) {
146 all_items_are_suitable_for_gpu_rasterization_
&=
147 item
->is_suitable_for_gpu_rasterization();
148 approximate_op_count_
+= item
->approximate_op_count();
150 if (use_cached_picture_
) {
152 item
->Raster(canvas_
.get(), gfx::Rect(), NULL
);
155 if (retain_individual_display_items_
) {
156 // Warning: this double-counts SkPicture data if use_cached_picture_ is
158 external_memory_usage_
+= item
->external_memory_usage();
162 if (!retain_individual_display_items_
)
166 void DisplayItemList::RasterIntoCanvas(const DisplayItem
& item
) {
168 DCHECK(!retain_individual_display_items_
);
169 all_items_are_suitable_for_gpu_rasterization_
&=
170 item
.is_suitable_for_gpu_rasterization();
171 approximate_op_count_
+= item
.approximate_op_count();
173 item
.Raster(canvas_
.get(), gfx::Rect(), NULL
);
176 bool DisplayItemList::RetainsIndividualDisplayItems() const {
177 return retain_individual_display_items_
;
180 void DisplayItemList::RemoveLast() {
181 // We cannot remove the last item if it has been squashed into a picture.
182 // The last item should not have been handled by ProcessAppendedItems, so we
183 // don't need to remove it from approximate_op_count_, etc.
184 DCHECK(retain_individual_display_items_
);
185 DCHECK(!use_cached_picture_
);
189 void DisplayItemList::Finalize() {
190 ProcessAppendedItems();
192 if (use_cached_picture_
) {
193 // Convert to an SkPicture for faster rasterization.
194 DCHECK(use_cached_picture_
);
196 picture_
= skia::AdoptRef(recorder_
->endRecordingAsPicture());
198 picture_memory_usage_
=
199 SkPictureUtils::ApproximateBytesUsed(picture_
.get());
205 bool DisplayItemList::IsSuitableForGpuRasterization() const {
206 DCHECK(ProcessAppendedItemsCalled());
207 if (use_cached_picture_
)
208 return picture_
->suitableForGpuRasterization(NULL
);
210 // This is more permissive than Picture's implementation, since none of the
211 // items might individually trigger a veto even though they collectively have
212 // enough "bad" operations that a corresponding Picture would get vetoed. See
214 return all_items_are_suitable_for_gpu_rasterization_
;
217 int DisplayItemList::ApproximateOpCount() const {
218 DCHECK(ProcessAppendedItemsCalled());
219 return approximate_op_count_
;
222 size_t DisplayItemList::ApproximateMemoryUsage() const {
223 DCHECK(ProcessAppendedItemsCalled());
224 // We double-count in this case. Produce zero to avoid being misleading.
225 if (use_cached_picture_
&& retain_individual_display_items_
)
228 DCHECK_IMPLIES(use_cached_picture_
, picture_
);
230 size_t memory_usage
= sizeof(*this);
232 // Memory outside this class due to |items_|.
233 memory_usage
+= items_
.GetCapacityInBytes() + external_memory_usage_
;
235 // Memory outside this class due to |picture|.
236 memory_usage
+= picture_memory_usage_
;
238 // TODO(jbroman): Does anything else owned by this class substantially
239 // contribute to memory usage?
244 bool DisplayItemList::ShouldBeAnalyzedForSolidColor() const {
245 return ApproximateOpCount() <= kOpCountThatIsOkToAnalyze
;
248 scoped_refptr
<base::trace_event::ConvertableToTraceFormat
>
249 DisplayItemList::AsValue(bool include_items
) const {
250 DCHECK(ProcessAppendedItemsCalled());
251 scoped_refptr
<base::trace_event::TracedValue
> state
=
252 new base::trace_event::TracedValue();
255 state
->BeginArray("params.items");
256 for (const DisplayItem
* item
: items_
) {
257 item
->AsValueInto(state
.get());
262 state
->SetValue("params.layer_rect", MathUtil::AsValue(layer_rect_
));
264 if (!layer_rect_
.IsEmpty()) {
265 SkPictureRecorder recorder
;
267 recorder
.beginRecording(layer_rect_
.width(), layer_rect_
.height());
268 canvas
->translate(-layer_rect_
.x(), -layer_rect_
.y());
269 canvas
->clipRect(gfx::RectToSkRect(layer_rect_
));
270 Raster(canvas
, NULL
, gfx::Rect(), 1.f
);
271 skia::RefPtr
<SkPicture
> picture
=
272 skia::AdoptRef(recorder
.endRecordingAsPicture());
274 std::string b64_picture
;
275 PictureDebugUtil::SerializeAsBase64(picture
.get(), &b64_picture
);
276 state
->SetString("skp64", b64_picture
);
282 void DisplayItemList::EmitTraceSnapshot() const {
283 DCHECK(ProcessAppendedItemsCalled());
284 TRACE_EVENT_OBJECT_SNAPSHOT_WITH_ID(
285 TRACE_DISABLED_BY_DEFAULT("cc.debug.display_items") ","
286 TRACE_DISABLED_BY_DEFAULT("cc.debug.picture") ","
287 TRACE_DISABLED_BY_DEFAULT("devtools.timeline.picture"),
288 "cc::DisplayItemList", this,
289 TracedDisplayItemList::AsTraceableDisplayItemList(this,
290 DisplayItemsTracingEnabled()));
293 void DisplayItemList::GatherDiscardableImages(const gfx::Size
& grid_cell_size
) {
294 DCHECK(ProcessAppendedItemsCalled());
295 // This should be only called once, and only after CreateAndCacheSkPicture.
298 images_
= make_scoped_ptr(new DiscardableImageMap(grid_cell_size
));
299 if (!picture_
->willPlayBackBitmaps())
302 images_
->GatherImagesFromPicture(picture_
.get(), layer_rect_
);