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(),
47 settings
.max_sidecar_size
,
48 kDefaultNumDisplayItemsToReserve
,
49 settings
.sidecar_destroyer
),
50 use_cached_picture_(settings
.use_cached_picture
),
51 retain_individual_display_items_(retain_individual_display_items
),
52 layer_rect_(layer_rect
),
53 all_items_are_suitable_for_gpu_rasterization_(true),
54 approximate_op_count_(0),
55 picture_memory_usage_(0),
56 external_memory_usage_(0) {
58 needs_process_
= false;
60 if (use_cached_picture_
) {
61 SkRTreeFactory factory
;
62 recorder_
.reset(new SkPictureRecorder());
63 canvas_
= skia::SharePtr(recorder_
->beginRecording(
64 layer_rect_
.width(), layer_rect_
.height(), &factory
));
65 canvas_
->translate(-layer_rect_
.x(), -layer_rect_
.y());
66 canvas_
->clipRect(gfx::RectToSkRect(layer_rect_
));
70 DisplayItemList::DisplayItemList(gfx::Rect layer_rect
,
71 const DisplayItemListSettings
& settings
)
75 !settings
.use_cached_picture
|| DisplayItemsTracingEnabled()) {
78 scoped_refptr
<DisplayItemList
> DisplayItemList::CreateWithoutCachedPicture(
79 const DisplayItemListSettings
& settings
) {
80 DCHECK(!settings
.use_cached_picture
);
81 return Create(gfx::Rect(), settings
);
84 scoped_refptr
<DisplayItemList
> DisplayItemList::Create(
86 bool use_cached_picture
) {
87 DisplayItemListSettings settings
;
88 settings
.use_cached_picture
= use_cached_picture
;
89 return Create(layer_rect
, settings
);
92 scoped_refptr
<DisplayItemList
> DisplayItemList::Create(
94 const DisplayItemListSettings
& settings
) {
95 return make_scoped_refptr(new DisplayItemList(layer_rect
, settings
));
98 DisplayItemList::~DisplayItemList() {
101 void DisplayItemList::Raster(SkCanvas
* canvas
,
102 SkPicture::AbortCallback
* callback
,
103 const gfx::Rect
& canvas_target_playback_rect
,
104 float contents_scale
) const {
105 DCHECK(ProcessAppendedItemsCalled());
106 if (!use_cached_picture_
) {
108 canvas
->scale(contents_scale
, contents_scale
);
109 for (auto* item
: items_
)
110 item
->Raster(canvas
, canvas_target_playback_rect
, callback
);
116 canvas
->scale(contents_scale
, contents_scale
);
117 canvas
->translate(layer_rect_
.x(), layer_rect_
.y());
119 // If we have a callback, we need to call |draw()|, |drawPicture()|
120 // doesn't take a callback. This is used by |AnalysisCanvas| to early
122 picture_
->playback(canvas
, callback
);
124 // Prefer to call |drawPicture()| on the canvas since it could place the
125 // entire picture on the canvas instead of parsing the skia operations.
126 canvas
->drawPicture(picture_
.get());
132 void DisplayItemList::ProcessAppendedItemsOnTheFly() {
133 if (retain_individual_display_items_
)
135 if (items_
.size() >= kDefaultNumDisplayItemsToReserve
) {
136 ProcessAppendedItems();
137 // This function exists to keep the |items_| from growing indefinitely if
138 // we're not going to store them anyway. So the items better be deleted
139 // after |items_| grows too large and we process it.
140 DCHECK(items_
.empty());
144 void DisplayItemList::ProcessAppendedItems() {
146 needs_process_
= false;
148 for (const DisplayItem
* item
: items_
) {
149 all_items_are_suitable_for_gpu_rasterization_
&=
150 item
->is_suitable_for_gpu_rasterization();
151 approximate_op_count_
+= item
->approximate_op_count();
153 if (use_cached_picture_
) {
155 item
->Raster(canvas_
.get(), gfx::Rect(), NULL
);
158 if (retain_individual_display_items_
) {
159 // Warning: this double-counts SkPicture data if use_cached_picture_ is
161 external_memory_usage_
+= item
->external_memory_usage();
165 if (!retain_individual_display_items_
)
169 void DisplayItemList::RasterIntoCanvas(const DisplayItem
& item
) {
171 DCHECK(!retain_individual_display_items_
);
172 all_items_are_suitable_for_gpu_rasterization_
&=
173 item
.is_suitable_for_gpu_rasterization();
174 approximate_op_count_
+= item
.approximate_op_count();
176 item
.Raster(canvas_
.get(), gfx::Rect(), NULL
);
179 bool DisplayItemList::RetainsIndividualDisplayItems() const {
180 return retain_individual_display_items_
;
183 void DisplayItemList::RemoveLast() {
184 // We cannot remove the last item if it has been squashed into a picture.
185 // The last item should not have been handled by ProcessAppendedItems, so we
186 // don't need to remove it from approximate_op_count_, etc.
187 DCHECK(retain_individual_display_items_
);
188 DCHECK(!use_cached_picture_
);
192 void DisplayItemList::Finalize() {
193 ProcessAppendedItems();
195 if (use_cached_picture_
) {
196 // Convert to an SkPicture for faster rasterization.
197 DCHECK(use_cached_picture_
);
199 picture_
= skia::AdoptRef(recorder_
->endRecordingAsPicture());
201 picture_memory_usage_
=
202 SkPictureUtils::ApproximateBytesUsed(picture_
.get());
208 bool DisplayItemList::IsSuitableForGpuRasterization() const {
209 DCHECK(ProcessAppendedItemsCalled());
210 if (use_cached_picture_
)
211 return picture_
->suitableForGpuRasterization(NULL
);
213 // This is more permissive than Picture's implementation, since none of the
214 // items might individually trigger a veto even though they collectively have
215 // enough "bad" operations that a corresponding Picture would get vetoed. See
217 return all_items_are_suitable_for_gpu_rasterization_
;
220 int DisplayItemList::ApproximateOpCount() const {
221 DCHECK(ProcessAppendedItemsCalled());
222 return approximate_op_count_
;
225 size_t DisplayItemList::ApproximateMemoryUsage() const {
226 DCHECK(ProcessAppendedItemsCalled());
227 // We double-count in this case. Produce zero to avoid being misleading.
228 if (use_cached_picture_
&& retain_individual_display_items_
)
231 DCHECK_IMPLIES(use_cached_picture_
, picture_
);
233 size_t memory_usage
= sizeof(*this);
235 // Memory outside this class due to |items_|.
236 memory_usage
+= items_
.GetCapacityInBytes() + external_memory_usage_
;
238 // Memory outside this class due to |picture|.
239 memory_usage
+= picture_memory_usage_
;
241 // TODO(jbroman): Does anything else owned by this class substantially
242 // contribute to memory usage?
247 bool DisplayItemList::ShouldBeAnalyzedForSolidColor() const {
248 return ApproximateOpCount() <= kOpCountThatIsOkToAnalyze
;
251 scoped_refptr
<base::trace_event::ConvertableToTraceFormat
>
252 DisplayItemList::AsValue(bool include_items
) const {
253 DCHECK(ProcessAppendedItemsCalled());
254 scoped_refptr
<base::trace_event::TracedValue
> state
=
255 new base::trace_event::TracedValue();
258 state
->BeginArray("params.items");
259 for (const DisplayItem
* item
: items_
) {
260 item
->AsValueInto(state
.get());
265 state
->SetValue("params.layer_rect", MathUtil::AsValue(layer_rect_
));
267 if (!layer_rect_
.IsEmpty()) {
268 SkPictureRecorder recorder
;
270 recorder
.beginRecording(layer_rect_
.width(), layer_rect_
.height());
271 canvas
->translate(-layer_rect_
.x(), -layer_rect_
.y());
272 canvas
->clipRect(gfx::RectToSkRect(layer_rect_
));
273 Raster(canvas
, NULL
, gfx::Rect(), 1.f
);
274 skia::RefPtr
<SkPicture
> picture
=
275 skia::AdoptRef(recorder
.endRecordingAsPicture());
277 std::string b64_picture
;
278 PictureDebugUtil::SerializeAsBase64(picture
.get(), &b64_picture
);
279 state
->SetString("skp64", b64_picture
);
285 void DisplayItemList::EmitTraceSnapshot() const {
286 DCHECK(ProcessAppendedItemsCalled());
287 TRACE_EVENT_OBJECT_SNAPSHOT_WITH_ID(
288 TRACE_DISABLED_BY_DEFAULT("cc.debug.display_items") ","
289 TRACE_DISABLED_BY_DEFAULT("cc.debug.picture") ","
290 TRACE_DISABLED_BY_DEFAULT("devtools.timeline.picture"),
291 "cc::DisplayItemList", this,
292 TracedDisplayItemList::AsTraceableDisplayItemList(this,
293 DisplayItemsTracingEnabled()));
296 void DisplayItemList::GatherPixelRefs(const gfx::Size
& grid_cell_size
) {
297 DCHECK(ProcessAppendedItemsCalled());
298 // This should be only called once, and only after CreateAndCacheSkPicture.
300 DCHECK(!pixel_refs_
);
301 pixel_refs_
= make_scoped_ptr(new PixelRefMap(grid_cell_size
));
302 if (!picture_
->willPlayBackBitmaps())
305 pixel_refs_
->GatherPixelRefsFromPicture(picture_
.get(), layer_rect_
);
308 void* DisplayItemList::GetSidecar(DisplayItem
* display_item
) {
309 return items_
.GetSidecar(display_item
);