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 scoped_refptr
<DisplayItemList
> DisplayItemList::Create(
44 const gfx::Rect
& layer_rect
,
45 const DisplayItemListSettings
& settings
) {
46 return make_scoped_refptr(new DisplayItemList(
48 !settings
.use_cached_picture
|| DisplayItemsTracingEnabled()));
51 DisplayItemList::DisplayItemList(gfx::Rect layer_rect
,
52 const DisplayItemListSettings
& settings
,
53 bool retain_individual_display_items
)
54 : items_(LargestDisplayItemSize(), kDefaultNumDisplayItemsToReserve
),
55 use_cached_picture_(settings
.use_cached_picture
),
56 retain_individual_display_items_(retain_individual_display_items
),
57 layer_rect_(layer_rect
),
58 is_suitable_for_gpu_rasterization_(true),
59 approximate_op_count_(0),
60 picture_memory_usage_(0),
61 external_memory_usage_(0) {
63 needs_process_
= false;
65 if (use_cached_picture_
) {
66 SkRTreeFactory factory
;
67 recorder_
.reset(new SkPictureRecorder());
68 canvas_
= skia::SharePtr(recorder_
->beginRecording(
69 layer_rect_
.width(), layer_rect_
.height(), &factory
));
70 canvas_
->translate(-layer_rect_
.x(), -layer_rect_
.y());
71 canvas_
->clipRect(gfx::RectToSkRect(layer_rect_
));
75 DisplayItemList::~DisplayItemList() {
78 void DisplayItemList::Raster(SkCanvas
* canvas
,
79 SkPicture::AbortCallback
* callback
,
80 const gfx::Rect
& canvas_target_playback_rect
,
81 float contents_scale
) const {
82 DCHECK(ProcessAppendedItemsCalled());
83 if (!use_cached_picture_
) {
85 canvas
->scale(contents_scale
, contents_scale
);
86 for (auto* item
: items_
)
87 item
->Raster(canvas
, canvas_target_playback_rect
, callback
);
93 canvas
->scale(contents_scale
, contents_scale
);
94 canvas
->translate(layer_rect_
.x(), layer_rect_
.y());
96 // If we have a callback, we need to call |draw()|, |drawPicture()|
97 // doesn't take a callback. This is used by |AnalysisCanvas| to early
99 picture_
->playback(canvas
, callback
);
101 // Prefer to call |drawPicture()| on the canvas since it could place the
102 // entire picture on the canvas instead of parsing the skia operations.
103 canvas
->drawPicture(picture_
.get());
109 void DisplayItemList::ProcessAppendedItemsOnTheFly() {
110 if (retain_individual_display_items_
)
112 if (items_
.size() >= kDefaultNumDisplayItemsToReserve
) {
113 ProcessAppendedItems();
114 // This function exists to keep the |items_| from growing indefinitely if
115 // we're not going to store them anyway. So the items better be deleted
116 // after |items_| grows too large and we process it.
117 DCHECK(items_
.empty());
121 void DisplayItemList::ProcessAppendedItems() {
123 needs_process_
= false;
125 for (const DisplayItem
* item
: items_
) {
126 if (use_cached_picture_
) {
127 // When using a cached picture we will calculate gpu suitability on the
128 // entire cached picture instead of the items. This is more permissive
129 // since none of the items might individually trigger a veto even though
130 // they collectively have enough "bad" operations that a corresponding
131 // Picture would get vetoed. See crbug.com/513016.
133 approximate_op_count_
+= item
->approximate_op_count();
134 item
->Raster(canvas_
.get(), gfx::Rect(), nullptr);
136 is_suitable_for_gpu_rasterization_
&=
137 item
->is_suitable_for_gpu_rasterization();
138 approximate_op_count_
+= item
->approximate_op_count();
141 if (retain_individual_display_items_
) {
142 // Warning: this double-counts SkPicture data if use_cached_picture_ is
144 external_memory_usage_
+= item
->external_memory_usage();
148 if (!retain_individual_display_items_
)
152 void DisplayItemList::RasterIntoCanvas(const DisplayItem
& item
) {
154 DCHECK(!retain_individual_display_items_
);
155 approximate_op_count_
+= item
.approximate_op_count();
157 item
.Raster(canvas_
.get(), gfx::Rect(), nullptr);
160 bool DisplayItemList::RetainsIndividualDisplayItems() const {
161 return retain_individual_display_items_
;
164 void DisplayItemList::RemoveLast() {
165 // We cannot remove the last item if it has been squashed into a picture.
166 // The last item should not have been handled by ProcessAppendedItems, so we
167 // don't need to remove it from approximate_op_count_, etc.
168 DCHECK(retain_individual_display_items_
);
169 DCHECK(!use_cached_picture_
);
173 void DisplayItemList::Finalize() {
174 ProcessAppendedItems();
176 if (use_cached_picture_
) {
177 // Convert to an SkPicture for faster rasterization.
178 DCHECK(use_cached_picture_
);
180 picture_
= skia::AdoptRef(recorder_
->endRecordingAsPicture());
182 picture_memory_usage_
=
183 SkPictureUtils::ApproximateBytesUsed(picture_
.get());
186 is_suitable_for_gpu_rasterization_
=
187 picture_
->suitableForGpuRasterization(nullptr);
191 bool DisplayItemList::IsSuitableForGpuRasterization() const {
192 DCHECK(ProcessAppendedItemsCalled());
193 return is_suitable_for_gpu_rasterization_
;
196 int DisplayItemList::ApproximateOpCount() const {
197 DCHECK(ProcessAppendedItemsCalled());
198 return approximate_op_count_
;
201 size_t DisplayItemList::ApproximateMemoryUsage() const {
202 DCHECK(ProcessAppendedItemsCalled());
203 // We double-count in this case. Produce zero to avoid being misleading.
204 if (use_cached_picture_
&& retain_individual_display_items_
)
207 DCHECK_IMPLIES(use_cached_picture_
, picture_
);
209 size_t memory_usage
= sizeof(*this);
211 // Memory outside this class due to |items_|.
212 memory_usage
+= items_
.GetCapacityInBytes() + external_memory_usage_
;
214 // Memory outside this class due to |picture|.
215 memory_usage
+= picture_memory_usage_
;
217 // TODO(jbroman): Does anything else owned by this class substantially
218 // contribute to memory usage?
223 bool DisplayItemList::ShouldBeAnalyzedForSolidColor() const {
224 return ApproximateOpCount() <= kOpCountThatIsOkToAnalyze
;
227 scoped_refptr
<base::trace_event::ConvertableToTraceFormat
>
228 DisplayItemList::AsValue(bool include_items
) const {
229 DCHECK(ProcessAppendedItemsCalled());
230 scoped_refptr
<base::trace_event::TracedValue
> state
=
231 new base::trace_event::TracedValue();
233 state
->BeginDictionary("params");
235 state
->BeginArray("items");
236 for (const DisplayItem
* item
: items_
) {
237 item
->AsValueInto(state
.get());
239 state
->EndArray(); // "items".
241 state
->SetValue("layer_rect", MathUtil::AsValue(layer_rect_
));
242 state
->EndDictionary(); // "params".
244 if (!layer_rect_
.IsEmpty()) {
245 SkPictureRecorder recorder
;
247 recorder
.beginRecording(layer_rect_
.width(), layer_rect_
.height());
248 canvas
->translate(-layer_rect_
.x(), -layer_rect_
.y());
249 canvas
->clipRect(gfx::RectToSkRect(layer_rect_
));
250 Raster(canvas
, NULL
, gfx::Rect(), 1.f
);
251 skia::RefPtr
<SkPicture
> picture
=
252 skia::AdoptRef(recorder
.endRecordingAsPicture());
254 std::string b64_picture
;
255 PictureDebugUtil::SerializeAsBase64(picture
.get(), &b64_picture
);
256 state
->SetString("skp64", b64_picture
);
262 void DisplayItemList::EmitTraceSnapshot() const {
263 DCHECK(ProcessAppendedItemsCalled());
264 TRACE_EVENT_OBJECT_SNAPSHOT_WITH_ID(
265 TRACE_DISABLED_BY_DEFAULT("cc.debug.display_items") ","
266 TRACE_DISABLED_BY_DEFAULT("cc.debug.picture") ","
267 TRACE_DISABLED_BY_DEFAULT("devtools.timeline.picture"),
268 "cc::DisplayItemList", this,
269 TracedDisplayItemList::AsTraceableDisplayItemList(this,
270 DisplayItemsTracingEnabled()));
273 void DisplayItemList::GatherDiscardableImages(const gfx::Size
& grid_cell_size
) {
274 DCHECK(ProcessAppendedItemsCalled());
275 // This should be only called once, and only after CreateAndCacheSkPicture.
278 images_
= make_scoped_ptr(new DiscardableImageMap(grid_cell_size
));
279 if (!picture_
->willPlayBackBitmaps())
282 images_
->GatherImagesFromPicture(picture_
.get(), layer_rect_
);