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/resources/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 "third_party/skia/include/core/SkCanvas.h"
16 #include "third_party/skia/include/core/SkDrawPictureCallback.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"
23 DisplayItemList::DisplayItemList()
24 : is_suitable_for_gpu_rasterization_(true), approximate_op_count_(0) {
27 scoped_refptr
<DisplayItemList
> DisplayItemList::Create() {
28 return make_scoped_refptr(new DisplayItemList());
31 DisplayItemList::~DisplayItemList() {
34 void DisplayItemList::Raster(SkCanvas
* canvas
,
35 SkDrawPictureCallback
* callback
,
36 float contents_scale
) const {
39 canvas
->scale(contents_scale
, contents_scale
);
40 for (size_t i
= 0; i
< items_
.size(); ++i
) {
41 items_
[i
]->Raster(canvas
, callback
);
48 canvas
->scale(contents_scale
, contents_scale
);
50 // If we have a callback, we need to call |draw()|, |drawPicture()|
51 // doesn't take a callback. This is used by |AnalysisCanvas| to early
53 picture_
->playback(canvas
, callback
);
55 // Prefer to call |drawPicture()| on the canvas since it could place the
56 // entire picture on the canvas instead of parsing the skia operations.
57 canvas
->drawPicture(picture_
.get());
63 void DisplayItemList::CreateAndCacheSkPicture() {
64 // Convert to an SkPicture for faster rasterization. Code is identical to
65 // that in Picture::Record.
66 SkRTreeFactory factory
;
67 SkPictureRecorder recorder
;
68 skia::RefPtr
<SkCanvas
> canvas
;
69 canvas
= skia::SharePtr(recorder
.beginRecording(
70 layer_rect_
.width(), layer_rect_
.height(), &factory
));
71 canvas
->translate(-layer_rect_
.x(), -layer_rect_
.y());
72 canvas
->clipRect(gfx::RectToSkRect(layer_rect_
));
73 for (size_t i
= 0; i
< items_
.size(); ++i
)
74 items_
[i
]->Raster(canvas
.get(), NULL
);
75 picture_
= skia::AdoptRef(recorder
.endRecording());
79 void DisplayItemList::AppendItem(scoped_ptr
<DisplayItem
> item
) {
80 is_suitable_for_gpu_rasterization_
&= item
->IsSuitableForGpuRasterization();
81 approximate_op_count_
+= item
->ApproximateOpCount();
82 items_
.push_back(item
.Pass());
85 bool DisplayItemList::IsSuitableForGpuRasterization() const {
86 // This is more permissive than Picture's implementation, since none of the
87 // items might individually trigger a veto even though they collectively have
88 // enough "bad" operations that a corresponding Picture would get vetoed.
89 return is_suitable_for_gpu_rasterization_
;
92 int DisplayItemList::ApproximateOpCount() const {
93 return approximate_op_count_
;
96 size_t DisplayItemList::PictureMemoryUsage() const {
97 size_t total_size
= 0;
99 for (const auto& item
: items_
) {
100 total_size
+= item
->PictureMemoryUsage();
104 total_size
+= SkPictureUtils::ApproximateBytesUsed(picture_
.get());
109 scoped_refptr
<base::trace_event::ConvertableToTraceFormat
>
110 DisplayItemList::AsValue() const {
111 scoped_refptr
<base::trace_event::TracedValue
> state
=
112 new base::trace_event::TracedValue();
114 state
->SetInteger("length", items_
.size());
115 state
->BeginArray("params.items");
116 for (const DisplayItem
* item
: items_
) {
117 item
->AsValueInto(state
.get());
120 state
->SetValue("params.layer_rect",
121 MathUtil::AsValue(layer_rect_
).release());
123 SkPictureRecorder recorder
;
125 recorder
.beginRecording(layer_rect_
.width(), layer_rect_
.height());
126 canvas
->translate(-layer_rect_
.x(), -layer_rect_
.y());
127 canvas
->clipRect(gfx::RectToSkRect(layer_rect_
));
128 for (size_t i
= 0; i
< items_
.size(); ++i
)
129 items_
[i
]->RasterForTracing(canvas
);
130 skia::RefPtr
<SkPicture
> picture
= skia::AdoptRef(recorder
.endRecording());
132 std::string b64_picture
;
133 PictureDebugUtil::SerializeAsBase64(picture
.get(), &b64_picture
);
134 state
->SetString("skp64", b64_picture
);
139 void DisplayItemList::EmitTraceSnapshot() const {
140 TRACE_EVENT_OBJECT_SNAPSHOT_WITH_ID(
141 TRACE_DISABLED_BY_DEFAULT("cc.debug.picture") ","
142 TRACE_DISABLED_BY_DEFAULT("devtools.timeline.picture"),
143 "cc::DisplayItemList", this, AsValue());