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 "third_party/skia/include/core/SkCanvas.h"
14 #include "third_party/skia/include/core/SkPictureRecorder.h"
15 #include "ui/gfx/skia_util.h"
19 DisplayItemList::DisplayItemList()
20 : is_suitable_for_gpu_rasterization_(true), approximate_op_count_(0) {
23 scoped_refptr
<DisplayItemList
> DisplayItemList::Create() {
24 return make_scoped_refptr(new DisplayItemList());
27 DisplayItemList::~DisplayItemList() {
30 void DisplayItemList::Raster(SkCanvas
* canvas
,
31 SkDrawPictureCallback
* callback
,
32 float contents_scale
) const {
34 canvas
->scale(contents_scale
, contents_scale
);
35 for (size_t i
= 0; i
< items_
.size(); ++i
) {
36 items_
[i
]->Raster(canvas
, callback
);
41 void DisplayItemList::AppendItem(scoped_ptr
<DisplayItem
> item
) {
42 is_suitable_for_gpu_rasterization_
&= item
->IsSuitableForGpuRasterization();
43 approximate_op_count_
+= item
->ApproximateOpCount();
44 items_
.push_back(item
.Pass());
47 bool DisplayItemList::IsSuitableForGpuRasterization() const {
48 // This is more permissive than Picture's implementation, since none of the
49 // items might individually trigger a veto even though they collectively have
50 // enough "bad" operations that a corresponding Picture would get vetoed.
51 return is_suitable_for_gpu_rasterization_
;
54 int DisplayItemList::ApproximateOpCount() const {
55 return approximate_op_count_
;
58 size_t DisplayItemList::PictureMemoryUsage() const {
59 size_t total_size
= 0;
61 for (const auto& item
: items_
) {
62 total_size
+= item
->PictureMemoryUsage();
68 scoped_refptr
<base::trace_event::ConvertableToTraceFormat
>
69 DisplayItemList::AsValue() const {
70 scoped_refptr
<base::trace_event::TracedValue
> state
=
71 new base::trace_event::TracedValue();
73 state
->SetInteger("length", items_
.size());
74 state
->BeginArray("params.items");
75 for (const DisplayItem
* item
: items_
) {
76 item
->AsValueInto(state
.get());
79 state
->SetValue("params.layer_rect",
80 MathUtil::AsValue(layer_rect_
).release());
82 SkPictureRecorder recorder
;
84 recorder
.beginRecording(layer_rect_
.width(), layer_rect_
.height());
85 canvas
->translate(-layer_rect_
.x(), -layer_rect_
.y());
86 canvas
->clipRect(gfx::RectToSkRect(layer_rect_
));
87 for (size_t i
= 0; i
< items_
.size(); ++i
)
88 items_
[i
]->RasterForTracing(canvas
);
89 skia::RefPtr
<SkPicture
> picture
= skia::AdoptRef(recorder
.endRecording());
91 std::string b64_picture
;
92 PictureDebugUtil::SerializeAsBase64(picture
.get(), &b64_picture
);
93 state
->SetString("skp64", b64_picture
);
98 void DisplayItemList::EmitTraceSnapshot() const {
99 TRACE_EVENT_OBJECT_SNAPSHOT_WITH_ID(
100 TRACE_DISABLED_BY_DEFAULT("cc.debug") "," TRACE_DISABLED_BY_DEFAULT(
101 "devtools.timeline.picture"),
102 "cc::DisplayItemList", this, AsValue());