OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "cc/resources/display_item_list.h" | 5 #include "cc/resources/display_item_list.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/trace_event/trace_event.h" | 9 #include "base/trace_event/trace_event.h" |
10 #include "base/trace_event/trace_event_argument.h" | 10 #include "base/trace_event/trace_event_argument.h" |
11 #include "cc/base/math_util.h" | 11 #include "cc/base/math_util.h" |
12 #include "cc/debug/picture_debug_util.h" | 12 #include "cc/debug/picture_debug_util.h" |
13 #include "cc/debug/traced_picture.h" | 13 #include "cc/debug/traced_picture.h" |
14 #include "cc/debug/traced_value.h" | 14 #include "cc/debug/traced_value.h" |
15 #include "third_party/skia/include/core/SkCanvas.h" | 15 #include "third_party/skia/include/core/SkCanvas.h" |
16 #include "third_party/skia/include/core/SkDrawPictureCallback.h" | 16 #include "third_party/skia/include/core/SkDrawPictureCallback.h" |
17 #include "third_party/skia/include/core/SkPictureRecorder.h" | 17 #include "third_party/skia/include/core/SkPictureRecorder.h" |
18 #include "third_party/skia/include/utils/SkPictureUtils.h" | 18 #include "third_party/skia/include/utils/SkPictureUtils.h" |
19 #include "ui/gfx/skia_util.h" | 19 #include "ui/gfx/skia_util.h" |
20 | 20 |
21 namespace cc { | 21 namespace cc { |
22 | 22 |
23 DisplayItemList::DisplayItemList(gfx::Rect layer_rect, bool use_cached_picture) | 23 DisplayItemList::DisplayItemList() |
24 : recorder_(new SkPictureRecorder()), | 24 : is_suitable_for_gpu_rasterization_(true), approximate_op_count_(0) { |
25 use_cached_picture_(use_cached_picture), | |
26 retain_individual_display_items_(!use_cached_picture), | |
27 layer_rect_(layer_rect), | |
28 is_suitable_for_gpu_rasterization_(true), | |
29 approximate_op_count_(0) { | |
30 if (use_cached_picture_) { | |
31 SkRTreeFactory factory; | |
32 recorder_.reset(new SkPictureRecorder()); | |
33 canvas_ = skia::SharePtr(recorder_->beginRecording( | |
34 layer_rect_.width(), layer_rect_.height(), &factory)); | |
35 canvas_->translate(-layer_rect_.x(), -layer_rect_.y()); | |
36 canvas_->clipRect(gfx::RectToSkRect(layer_rect_)); | |
37 | |
38 bool tracing_enabled; | |
39 TRACE_EVENT_CATEGORY_GROUP_ENABLED( | |
40 TRACE_DISABLED_BY_DEFAULT("cc.debug.picture") "," | |
41 TRACE_DISABLED_BY_DEFAULT("devtools.timeline.picture"), | |
42 &tracing_enabled); | |
43 if (tracing_enabled) | |
44 retain_individual_display_items_ = true; | |
45 } | |
46 } | 25 } |
47 | 26 |
48 scoped_refptr<DisplayItemList> DisplayItemList::Create( | 27 scoped_refptr<DisplayItemList> DisplayItemList::Create() { |
49 gfx::Rect layer_rect, | 28 return make_scoped_refptr(new DisplayItemList()); |
50 bool use_cached_picture) { | |
51 return make_scoped_refptr( | |
52 new DisplayItemList(layer_rect, use_cached_picture)); | |
53 } | 29 } |
54 | 30 |
55 DisplayItemList::~DisplayItemList() { | 31 DisplayItemList::~DisplayItemList() { |
56 } | 32 } |
57 | 33 |
58 void DisplayItemList::Raster(SkCanvas* canvas, | 34 void DisplayItemList::Raster(SkCanvas* canvas, |
59 SkDrawPictureCallback* callback, | 35 SkDrawPictureCallback* callback, |
60 float contents_scale) const { | 36 float contents_scale) const { |
61 if (!use_cached_picture_) { | 37 if (!picture_) { |
62 canvas->save(); | 38 canvas->save(); |
63 canvas->scale(contents_scale, contents_scale); | 39 canvas->scale(contents_scale, contents_scale); |
64 for (size_t i = 0; i < items_.size(); ++i) { | 40 for (size_t i = 0; i < items_.size(); ++i) { |
65 items_[i]->Raster(canvas, callback); | 41 items_[i]->Raster(canvas, callback); |
66 } | 42 } |
67 canvas->restore(); | 43 canvas->restore(); |
68 } else { | 44 } else { |
69 DCHECK(picture_); | 45 DCHECK(picture_); |
70 | 46 |
71 canvas->save(); | 47 canvas->save(); |
72 canvas->scale(contents_scale, contents_scale); | 48 canvas->scale(contents_scale, contents_scale); |
73 canvas->translate(layer_rect_.x(), layer_rect_.y()); | 49 canvas->translate(layer_rect_.x(), layer_rect_.y()); |
74 if (callback) { | 50 if (callback) { |
75 // If we have a callback, we need to call |draw()|, |drawPicture()| | 51 // If we have a callback, we need to call |draw()|, |drawPicture()| |
76 // doesn't take a callback. This is used by |AnalysisCanvas| to early | 52 // doesn't take a callback. This is used by |AnalysisCanvas| to early |
77 // out. | 53 // out. |
78 picture_->playback(canvas, callback); | 54 picture_->playback(canvas, callback); |
79 } else { | 55 } else { |
80 // Prefer to call |drawPicture()| on the canvas since it could place the | 56 // Prefer to call |drawPicture()| on the canvas since it could place the |
81 // entire picture on the canvas instead of parsing the skia operations. | 57 // entire picture on the canvas instead of parsing the skia operations. |
82 canvas->drawPicture(picture_.get()); | 58 canvas->drawPicture(picture_.get()); |
83 } | 59 } |
84 canvas->restore(); | 60 canvas->restore(); |
85 } | 61 } |
86 } | 62 } |
87 | 63 |
88 void DisplayItemList::CreateAndCacheSkPicture() { | 64 void DisplayItemList::CreateAndCacheSkPicture() { |
89 // Convert to an SkPicture for faster rasterization. | 65 // Convert to an SkPicture for faster rasterization. Code is identical to |
90 DCHECK(use_cached_picture_); | 66 // that in Picture::Record. |
91 picture_ = skia::AdoptRef(recorder_->endRecordingAsPicture()); | 67 SkRTreeFactory factory; |
| 68 SkPictureRecorder recorder; |
| 69 skia::RefPtr<SkCanvas> canvas; |
| 70 canvas = skia::SharePtr(recorder.beginRecording( |
| 71 layer_rect_.width(), layer_rect_.height(), &factory)); |
| 72 canvas->translate(-layer_rect_.x(), -layer_rect_.y()); |
| 73 canvas->clipRect(gfx::RectToSkRect(layer_rect_)); |
| 74 for (size_t i = 0; i < items_.size(); ++i) |
| 75 items_[i]->Raster(canvas.get(), NULL); |
| 76 picture_ = skia::AdoptRef(recorder.endRecordingAsPicture()); |
92 DCHECK(picture_); | 77 DCHECK(picture_); |
93 recorder_.reset(); | |
94 canvas_.clear(); | |
95 } | 78 } |
96 | 79 |
97 void DisplayItemList::AppendItem(scoped_ptr<DisplayItem> item) { | 80 void DisplayItemList::AppendItem(scoped_ptr<DisplayItem> item) { |
98 is_suitable_for_gpu_rasterization_ &= item->IsSuitableForGpuRasterization(); | 81 is_suitable_for_gpu_rasterization_ &= item->IsSuitableForGpuRasterization(); |
99 approximate_op_count_ += item->ApproximateOpCount(); | 82 approximate_op_count_ += item->ApproximateOpCount(); |
100 | 83 items_.push_back(item.Pass()); |
101 if (use_cached_picture_) { | |
102 DCHECK(canvas_); | |
103 item->Raster(canvas_.get(), NULL); | |
104 } | |
105 | |
106 if (retain_individual_display_items_) | |
107 items_.push_back(item.Pass()); | |
108 } | 84 } |
109 | 85 |
110 bool DisplayItemList::IsSuitableForGpuRasterization() const { | 86 bool DisplayItemList::IsSuitableForGpuRasterization() const { |
111 // This is more permissive than Picture's implementation, since none of the | 87 // This is more permissive than Picture's implementation, since none of the |
112 // items might individually trigger a veto even though they collectively have | 88 // items might individually trigger a veto even though they collectively have |
113 // enough "bad" operations that a corresponding Picture would get vetoed. | 89 // enough "bad" operations that a corresponding Picture would get vetoed. |
114 return is_suitable_for_gpu_rasterization_; | 90 return is_suitable_for_gpu_rasterization_; |
115 } | 91 } |
116 | 92 |
117 int DisplayItemList::ApproximateOpCount() const { | 93 int DisplayItemList::ApproximateOpCount() const { |
(...skipping 25 matching lines...) Expand all Loading... |
143 } | 119 } |
144 state->EndArray(); | 120 state->EndArray(); |
145 state->SetValue("params.layer_rect", | 121 state->SetValue("params.layer_rect", |
146 MathUtil::AsValue(layer_rect_).release()); | 122 MathUtil::AsValue(layer_rect_).release()); |
147 | 123 |
148 SkPictureRecorder recorder; | 124 SkPictureRecorder recorder; |
149 SkCanvas* canvas = | 125 SkCanvas* canvas = |
150 recorder.beginRecording(layer_rect_.width(), layer_rect_.height()); | 126 recorder.beginRecording(layer_rect_.width(), layer_rect_.height()); |
151 canvas->translate(-layer_rect_.x(), -layer_rect_.y()); | 127 canvas->translate(-layer_rect_.x(), -layer_rect_.y()); |
152 canvas->clipRect(gfx::RectToSkRect(layer_rect_)); | 128 canvas->clipRect(gfx::RectToSkRect(layer_rect_)); |
153 Raster(canvas, NULL, 1.f); | 129 for (size_t i = 0; i < items_.size(); ++i) |
| 130 items_[i]->RasterForTracing(canvas); |
154 skia::RefPtr<SkPicture> picture = | 131 skia::RefPtr<SkPicture> picture = |
155 skia::AdoptRef(recorder.endRecordingAsPicture()); | 132 skia::AdoptRef(recorder.endRecordingAsPicture()); |
156 | 133 |
157 std::string b64_picture; | 134 std::string b64_picture; |
158 PictureDebugUtil::SerializeAsBase64(picture.get(), &b64_picture); | 135 PictureDebugUtil::SerializeAsBase64(picture.get(), &b64_picture); |
159 state->SetString("skp64", b64_picture); | 136 state->SetString("skp64", b64_picture); |
160 | 137 |
161 return state; | 138 return state; |
162 } | 139 } |
163 | 140 |
164 void DisplayItemList::EmitTraceSnapshot() const { | 141 void DisplayItemList::EmitTraceSnapshot() const { |
165 TRACE_EVENT_OBJECT_SNAPSHOT_WITH_ID( | 142 TRACE_EVENT_OBJECT_SNAPSHOT_WITH_ID( |
166 TRACE_DISABLED_BY_DEFAULT("cc.debug.picture") "," | 143 TRACE_DISABLED_BY_DEFAULT("cc.debug.picture") "," |
167 TRACE_DISABLED_BY_DEFAULT("devtools.timeline.picture"), | 144 TRACE_DISABLED_BY_DEFAULT("devtools.timeline.picture"), |
168 "cc::DisplayItemList", this, AsValue()); | 145 "cc::DisplayItemList", this, AsValue()); |
169 } | 146 } |
170 | 147 |
171 void DisplayItemList::GatherPixelRefs(const gfx::Size& grid_cell_size) { | 148 void DisplayItemList::GatherPixelRefs(const gfx::Size& grid_cell_size) { |
172 // This should be only called once, and only after CreateAndCacheSkPicture. | 149 // This should be only called once, and only after CreateAndCacheSkPicture. |
173 DCHECK(picture_); | 150 DCHECK(picture_); |
174 DCHECK(!pixel_refs_); | 151 DCHECK(!pixel_refs_); |
175 pixel_refs_ = make_scoped_ptr(new PixelRefMap(grid_cell_size)); | 152 pixel_refs_ = make_scoped_ptr(new PixelRefMap(grid_cell_size)); |
176 if (!picture_->willPlayBackBitmaps()) | 153 if (!picture_->willPlayBackBitmaps()) |
177 return; | 154 return; |
178 | 155 |
179 pixel_refs_->GatherPixelRefsFromPicture(picture_.get()); | 156 pixel_refs_->GatherPixelRefsFromPicture(picture_.get()); |
180 } | 157 } |
181 } // namespace cc | 158 } // namespace cc |
OLD | NEW |