Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(181)

Side by Side Diff: cc/playback/display_item_list.cc

Issue 1484163002: Raster display item lists via a visual rect RTree. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Update test expectations. Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/playback/display_item_list.h" 5 #include "cc/playback/display_item_list.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 8
9 #include <string> 9 #include <string>
10 10
11 #include "base/numerics/safe_conversions.h" 11 #include "base/numerics/safe_conversions.h"
12 #include "base/process/process.h"
12 #include "base/strings/stringprintf.h" 13 #include "base/strings/stringprintf.h"
13 #include "base/trace_event/trace_event.h" 14 #include "base/trace_event/trace_event.h"
14 #include "base/trace_event/trace_event_argument.h" 15 #include "base/trace_event/trace_event_argument.h"
15 #include "cc/base/math_util.h" 16 #include "cc/base/math_util.h"
16 #include "cc/debug/picture_debug_util.h" 17 #include "cc/debug/picture_debug_util.h"
17 #include "cc/debug/traced_display_item_list.h" 18 #include "cc/debug/traced_display_item_list.h"
18 #include "cc/debug/traced_value.h" 19 #include "cc/debug/traced_value.h"
19 #include "cc/playback/display_item_list_settings.h" 20 #include "cc/playback/display_item_list_settings.h"
20 #include "cc/playback/display_item_proto_factory.h" 21 #include "cc/playback/display_item_proto_factory.h"
21 #include "cc/playback/drawing_display_item.h" 22 #include "cc/playback/drawing_display_item.h"
22 #include "cc/playback/largest_display_item.h" 23 #include "cc/playback/largest_display_item.h"
23 #include "cc/proto/display_item.pb.h" 24 #include "cc/proto/display_item.pb.h"
24 #include "cc/proto/gfx_conversions.h" 25 #include "cc/proto/gfx_conversions.h"
25 #include "third_party/skia/include/core/SkCanvas.h" 26 #include "third_party/skia/include/core/SkCanvas.h"
26 #include "third_party/skia/include/core/SkPictureRecorder.h" 27 #include "third_party/skia/include/core/SkPictureRecorder.h"
27 #include "third_party/skia/include/utils/SkPictureUtils.h" 28 #include "third_party/skia/include/utils/SkPictureUtils.h"
28 #include "ui/gfx/geometry/rect.h" 29 #include "ui/gfx/geometry/rect.h"
30 #include "ui/gfx/geometry/rect_conversions.h"
29 #include "ui/gfx/skia_util.h" 31 #include "ui/gfx/skia_util.h"
30 32
31 namespace cc { 33 namespace cc {
32 34
33 namespace { 35 namespace {
34 36
35 // We don't perform per-layer solid color analysis when there are too many skia 37 // We don't perform per-layer solid color analysis when there are too many skia
36 // operations. 38 // operations.
37 const int kOpCountThatIsOkToAnalyze = 10; 39 const int kOpCountThatIsOkToAnalyze = 10;
38 40
39 bool DisplayItemsTracingEnabled() { 41 bool DisplayItemsTracingEnabled() {
40 bool tracing_enabled; 42 bool tracing_enabled;
41 TRACE_EVENT_CATEGORY_GROUP_ENABLED( 43 TRACE_EVENT_CATEGORY_GROUP_ENABLED(
42 TRACE_DISABLED_BY_DEFAULT("cc.debug.display_items"), &tracing_enabled); 44 TRACE_DISABLED_BY_DEFAULT("cc.debug.display_items"), &tracing_enabled);
43 return tracing_enabled; 45 return tracing_enabled;
44 } 46 }
45 47
46 const int kDefaultNumDisplayItemsToReserve = 100; 48 const int kDefaultNumDisplayItemsToReserve = 100;
47 49
48 } // namespace 50 } // namespace
49 51
50 scoped_refptr<DisplayItemList> DisplayItemList::Create( 52 scoped_refptr<DisplayItemList> DisplayItemList::Create(
51 const gfx::Rect& layer_rect,
52 const DisplayItemListSettings& settings) { 53 const DisplayItemListSettings& settings) {
53 return make_scoped_refptr(new DisplayItemList( 54 return make_scoped_refptr(new DisplayItemList(settings));
54 layer_rect, settings,
55 !settings.use_cached_picture || DisplayItemsTracingEnabled()));
56 } 55 }
57 56
58 scoped_refptr<DisplayItemList> DisplayItemList::CreateFromProto( 57 scoped_refptr<DisplayItemList> DisplayItemList::CreateFromProto(
59 const proto::DisplayItemList& proto, 58 const proto::DisplayItemList& proto,
60 ClientPictureCache* client_picture_cache, 59 ClientPictureCache* client_picture_cache,
61 std::vector<uint32_t>* used_engine_picture_ids) { 60 std::vector<uint32_t>* used_engine_picture_ids) {
62 gfx::Rect layer_rect = ProtoToRect(proto.layer_rect());
63 scoped_refptr<DisplayItemList> list = 61 scoped_refptr<DisplayItemList> list =
64 DisplayItemList::Create(ProtoToRect(proto.layer_rect()), 62 DisplayItemList::Create(DisplayItemListSettings(proto.settings()));
65 DisplayItemListSettings(proto.settings()));
66 63
67 for (int i = 0; i < proto.items_size(); i++) { 64 for (int i = 0; i < proto.items_size(); i++) {
68 const proto::DisplayItem& item_proto = proto.items(i); 65 const proto::DisplayItem& item_proto = proto.items(i);
66 const gfx::Rect visual_rect = ProtoToRect(proto.visual_rects(i));
69 DisplayItemProtoFactory::AllocateAndConstruct( 67 DisplayItemProtoFactory::AllocateAndConstruct(
70 layer_rect, list.get(), item_proto, client_picture_cache, 68 visual_rect, list.get(), item_proto, client_picture_cache,
71 used_engine_picture_ids); 69 used_engine_picture_ids);
72 } 70 }
73 71
74 list->Finalize(); 72 list->Finalize();
75 73
76 return list; 74 return list;
77 } 75 }
78 76
79 DisplayItemList::DisplayItemList(gfx::Rect layer_rect, 77 DisplayItemList::DisplayItemList(const DisplayItemListSettings& settings)
80 const DisplayItemListSettings& settings,
81 bool retain_individual_display_items)
82 : items_(LargestDisplayItemSize(), 78 : items_(LargestDisplayItemSize(),
83 LargestDisplayItemSize() * kDefaultNumDisplayItemsToReserve), 79 LargestDisplayItemSize() * kDefaultNumDisplayItemsToReserve),
84 settings_(settings), 80 settings_(settings) {}
85 retain_individual_display_items_(retain_individual_display_items),
86 layer_rect_(layer_rect),
87 is_suitable_for_gpu_rasterization_(true),
88 approximate_op_count_(0),
89 picture_memory_usage_(0) {
90 if (settings_.use_cached_picture) {
91 SkRTreeFactory factory;
92 recorder_.reset(new SkPictureRecorder());
93
94 SkCanvas* canvas = recorder_->beginRecording(
95 layer_rect_.width(), layer_rect_.height(), &factory);
96 canvas->translate(-layer_rect_.x(), -layer_rect_.y());
97 canvas->clipRect(gfx::RectToSkRect(layer_rect_));
98 }
99 }
100 81
101 DisplayItemList::~DisplayItemList() { 82 DisplayItemList::~DisplayItemList() {
102 } 83 }
103 84
104 void DisplayItemList::ToProtobuf(proto::DisplayItemList* proto) { 85 void DisplayItemList::ToProtobuf(proto::DisplayItemList* proto) {
105 // The flattened SkPicture approach is going away, and the proto 86 // The flattened SkPicture approach is going away, and the proto
106 // doesn't currently support serializing that flattened picture. 87 // doesn't currently support serializing that flattened picture.
107 DCHECK(retain_individual_display_items_);
108
109 RectToProto(layer_rect_, proto->mutable_layer_rect());
110 settings_.ToProtobuf(proto->mutable_settings()); 88 settings_.ToProtobuf(proto->mutable_settings());
111 89
112 DCHECK_EQ(0, proto->items_size()); 90 DCHECK_EQ(0, proto->items_size());
113 for (const auto& item : items_) 91 DCHECK_EQ(0, proto->visual_rects_size());
92 DCHECK(items_.size() == visual_rects_.size())
93 << "items.size() " << items_.size() << " visual_rects.size() "
94 << visual_rects_.size();
95 int i = 0;
96 for (const auto& item : items_) {
97 RectToProto(visual_rects_[i++], proto->add_visual_rects());
114 item.ToProtobuf(proto->add_items()); 98 item.ToProtobuf(proto->add_items());
99 }
115 } 100 }
116 101
117 void DisplayItemList::Raster(SkCanvas* canvas, 102 void DisplayItemList::Raster(SkCanvas* canvas,
118 SkPicture::AbortCallback* callback, 103 SkPicture::AbortCallback* callback,
119 const gfx::Rect& canvas_target_playback_rect, 104 const gfx::Rect& canvas_target_playback_rect,
120 float contents_scale) const { 105 float contents_scale) const {
121 canvas->save(); 106 canvas->save();
122
123 if (!canvas_target_playback_rect.IsEmpty()) { 107 if (!canvas_target_playback_rect.IsEmpty()) {
124 // canvas_target_playback_rect is specified in device space. We can't 108 // canvas_target_playback_rect is specified in device space. We can't
125 // use clipRect because canvas CTM will be applied on it. Use clipRegion 109 // use clipRect because canvas CTM will be applied on it. Use clipRegion
126 // instead because it ignores canvas CTM. 110 // instead because it ignores canvas CTM.
127 SkRegion device_clip; 111 SkRegion device_clip;
128 device_clip.setRect(gfx::RectToSkIRect(canvas_target_playback_rect)); 112 device_clip.setRect(gfx::RectToSkIRect(canvas_target_playback_rect));
129 canvas->clipRegion(device_clip); 113 canvas->clipRegion(device_clip);
130 } 114 }
131
132 canvas->scale(contents_scale, contents_scale); 115 canvas->scale(contents_scale, contents_scale);
133 Raster(canvas, callback); 116 Raster(canvas, callback);
134 canvas->restore(); 117 canvas->restore();
135 } 118 }
136 119
120 static bool GetCanvasClipBounds(SkCanvas* canvas, gfx::Rect* clip_bounds) {
121 SkRect canvas_clip_bounds;
122 if (!canvas->getClipBounds(&canvas_clip_bounds))
123 return false;
124 *clip_bounds = ToEnclosingRect(gfx::SkRectToRectF(canvas_clip_bounds));
125 return true;
126 }
127
137 void DisplayItemList::Raster(SkCanvas* canvas, 128 void DisplayItemList::Raster(SkCanvas* canvas,
138 SkPicture::AbortCallback* callback) const { 129 SkPicture::AbortCallback* callback) const {
139 if (!settings_.use_cached_picture) { 130 gfx::Rect canvas_playback_rect;
140 for (const auto& item : items_) 131 if (!GetCanvasClipBounds(canvas, &canvas_playback_rect))
141 item.Raster(canvas, callback); 132 return;
142 } else {
143 DCHECK(picture_);
144 133
145 canvas->save(); 134 std::vector<size_t> indices;
146 canvas->translate(layer_rect_.x(), layer_rect_.y()); 135 rtree_.Search(canvas_playback_rect, &indices);
147 if (callback) { 136 for (size_t index : indices) {
148 // If we have a callback, we need to call |draw()|, |drawPicture()| 137 items_[index].Raster(canvas, callback);
149 // doesn't take a callback. This is used by |AnalysisCanvas| to early 138 // We use a callback during solid color analysis on the compositor thread to
150 // out. 139 // break out early. Since we're handling a sequence of pictures via rtree
151 picture_->playback(canvas, callback); 140 // query results ourselves, we have to respect the callback and early out.
152 } else { 141 if (callback && callback->abort())
153 // Prefer to call |drawPicture()| on the canvas since it could place the 142 break;
154 // entire picture on the canvas instead of parsing the skia operations.
155 canvas->drawPicture(picture_.get());
156 }
157 canvas->restore();
158 } 143 }
159 } 144 }
160 145
161 void DisplayItemList::ProcessAppendedItem(const DisplayItem* item) {
162 if (settings_.use_cached_picture) {
163 DCHECK(recorder_);
164 item->Raster(recorder_->getRecordingCanvas(), nullptr);
165 }
166 if (!retain_individual_display_items_) {
167 items_.Clear();
168 }
169 }
170
171 void DisplayItemList::RasterIntoCanvas(const DisplayItem& item) {
172 DCHECK(recorder_);
173 DCHECK(!retain_individual_display_items_);
174
175 item.Raster(recorder_->getRecordingCanvas(), nullptr);
176 }
177
178 bool DisplayItemList::RetainsIndividualDisplayItems() const {
179 return retain_individual_display_items_;
180 }
181
182 void DisplayItemList::Finalize() { 146 void DisplayItemList::Finalize() {
183 TRACE_EVENT0("cc", "DisplayItemList::Finalize"); 147 TRACE_EVENT0("cc", "DisplayItemList::Finalize");
184 // TODO(dtrainor): Need to deal with serializing visual_rects_. 148 // TODO(dtrainor): Need to deal with serializing visual_rects_.
185 // http://crbug.com/568757. 149 // http://crbug.com/568757.
186 DCHECK(!retain_individual_display_items_ || 150 DCHECK(items_.size() == visual_rects_.size())
187 items_.size() == visual_rects_.size())
188 << "items.size() " << items_.size() << " visual_rects.size() " 151 << "items.size() " << items_.size() << " visual_rects.size() "
189 << visual_rects_.size(); 152 << visual_rects_.size();
153 rtree_.Build(visual_rects_);
190 154
191 // TODO(vmpstr): Build and make use of an RTree from the visual 155 if (!retain_visual_rects_)
192 // rects. For now we just clear them out since we won't ever need 156 // This clears both the vector and the vector's capacity, since
193 // them to stick around post-Finalize. http://crbug.com/527245 157 // visual_rects_
194 // This clears both the vector and the vector's capacity, since visual_rects_ 158 // won't be used anymore.
195 // won't be used anymore. 159 std::vector<gfx::Rect>().swap(visual_rects_);
196 std::vector<gfx::Rect>().swap(visual_rects_);
197
198 if (settings_.use_cached_picture) {
199 // Convert to an SkPicture for faster rasterization.
200 DCHECK(settings_.use_cached_picture);
201 DCHECK(!picture_);
202 picture_ = recorder_->finishRecordingAsPicture();
203 DCHECK(picture_);
204 picture_memory_usage_ =
205 SkPictureUtils::ApproximateBytesUsed(picture_.get());
206 recorder_.reset();
207 }
208 } 160 }
209 161
210 bool DisplayItemList::IsSuitableForGpuRasterization() const { 162 bool DisplayItemList::IsSuitableForGpuRasterization() const {
211 return is_suitable_for_gpu_rasterization_; 163 // TODO(wkorman): This is more permissive than Picture's implementation, since
164 // none of the items might individually trigger a veto even though they
165 // collectively have enough "bad" operations that a corresponding Picture
166 // would get vetoed. See crbug.com/513016.
167 return all_items_are_suitable_for_gpu_rasterization_;
212 } 168 }
213 169
214 int DisplayItemList::ApproximateOpCount() const { 170 int DisplayItemList::ApproximateOpCount() const {
215 if (retain_individual_display_items_) 171 return approximate_op_count_;
216 return approximate_op_count_;
217 return picture_ ? picture_->approximateOpCount() : 0;
218 } 172 }
219 173
220 size_t DisplayItemList::ApproximateMemoryUsage() const { 174 size_t DisplayItemList::ApproximateMemoryUsage() const {
221 // We double-count in this case. Produce zero to avoid being misleading.
222 if (settings_.use_cached_picture && retain_individual_display_items_)
223 return 0;
224
225 DCHECK(!settings_.use_cached_picture || picture_);
226
227 size_t memory_usage = sizeof(*this); 175 size_t memory_usage = sizeof(*this);
228 176
229 size_t external_memory_usage = 0; 177 size_t external_memory_usage = 0;
230 if (retain_individual_display_items_) { 178 // Warning: this double-counts SkPicture data if use_cached_picture is
231 // Warning: this double-counts SkPicture data if use_cached_picture is 179 // also true.
232 // also true. 180 for (const auto& item : items_) {
233 for (const auto& item : items_) { 181 external_memory_usage += item.ExternalMemoryUsage();
234 external_memory_usage += item.ExternalMemoryUsage();
235 }
236 } 182 }
237 183
238 // Memory outside this class due to |items_|. 184 // Memory outside this class due to |items_|.
239 memory_usage += items_.GetCapacityInBytes() + external_memory_usage; 185 memory_usage += items_.GetCapacityInBytes() + external_memory_usage;
240 186
241 // Memory outside this class due to |picture|.
242 memory_usage += picture_memory_usage_;
243
244 // TODO(jbroman): Does anything else owned by this class substantially 187 // TODO(jbroman): Does anything else owned by this class substantially
245 // contribute to memory usage? 188 // contribute to memory usage?
189 // TODO(vmpstr): Probably DiscardableImageMap is worth counting here.
246 190
247 return memory_usage; 191 return memory_usage;
248 } 192 }
249 193
250 bool DisplayItemList::ShouldBeAnalyzedForSolidColor() const { 194 bool DisplayItemList::ShouldBeAnalyzedForSolidColor() const {
251 return ApproximateOpCount() <= kOpCountThatIsOkToAnalyze; 195 return ApproximateOpCount() <= kOpCountThatIsOkToAnalyze;
252 } 196 }
253 197
254 std::unique_ptr<base::trace_event::ConvertableToTraceFormat> 198 std::unique_ptr<base::trace_event::ConvertableToTraceFormat>
255 DisplayItemList::AsValue(bool include_items) const { 199 DisplayItemList::AsValue(bool include_items) const {
256 std::unique_ptr<base::trace_event::TracedValue> state( 200 std::unique_ptr<base::trace_event::TracedValue> state(
257 new base::trace_event::TracedValue()); 201 new base::trace_event::TracedValue());
258 202
259 state->BeginDictionary("params"); 203 state->BeginDictionary("params");
260 if (include_items) { 204 if (include_items) {
261 state->BeginArray("items"); 205 state->BeginArray("items");
262 size_t item_index = 0; 206 size_t item_index = 0;
263 for (const DisplayItem& item : items_) { 207 for (const DisplayItem& item : items_) {
264 item.AsValueInto(item_index < visual_rects_.size() 208 item.AsValueInto(item_index < visual_rects_.size()
265 ? visual_rects_[item_index] 209 ? visual_rects_[item_index]
266 : gfx::Rect(), 210 : gfx::Rect(),
267 state.get()); 211 state.get());
268 item_index++; 212 item_index++;
269 } 213 }
270 state->EndArray(); // "items". 214 state->EndArray(); // "items".
271 } 215 }
272 state->SetValue("layer_rect", MathUtil::AsValue(layer_rect_));
273 state->EndDictionary(); // "params". 216 state->EndDictionary(); // "params".
274 217
275 if (!layer_rect_.IsEmpty()) { 218 SkPictureRecorder recorder;
276 SkPictureRecorder recorder; 219 gfx::Rect bounds = rtree_.GetBounds();
277 SkCanvas* canvas = 220 SkCanvas* canvas = recorder.beginRecording(bounds.width(), bounds.height());
278 recorder.beginRecording(layer_rect_.width(), layer_rect_.height()); 221 canvas->translate(-bounds.x(), -bounds.y());
279 canvas->translate(-layer_rect_.x(), -layer_rect_.y()); 222 canvas->clipRect(gfx::RectToSkRect(bounds));
280 canvas->clipRect(gfx::RectToSkRect(layer_rect_)); 223 Raster(canvas, nullptr, gfx::Rect(), 1.f);
281 Raster(canvas, NULL, gfx::Rect(), 1.f); 224 sk_sp<SkPicture> picture = recorder.finishRecordingAsPicture();
282 sk_sp<SkPicture> picture = recorder.finishRecordingAsPicture();
283 225
284 std::string b64_picture; 226 std::string b64_picture;
285 PictureDebugUtil::SerializeAsBase64(picture.get(), &b64_picture); 227 PictureDebugUtil::SerializeAsBase64(picture.get(), &b64_picture);
286 state->SetString("skp64", b64_picture); 228 state->SetString("skp64", b64_picture);
287 }
288 229
289 return std::move(state); 230 return std::move(state);
290 } 231 }
291 232
292 void DisplayItemList::EmitTraceSnapshot() const { 233 void DisplayItemList::EmitTraceSnapshot() const {
293 TRACE_EVENT_OBJECT_SNAPSHOT_WITH_ID( 234 TRACE_EVENT_OBJECT_SNAPSHOT_WITH_ID(
294 TRACE_DISABLED_BY_DEFAULT("cc.debug.display_items") "," 235 TRACE_DISABLED_BY_DEFAULT("cc.debug.display_items") ","
295 TRACE_DISABLED_BY_DEFAULT("cc.debug.picture") "," 236 TRACE_DISABLED_BY_DEFAULT("cc.debug.picture") ","
296 TRACE_DISABLED_BY_DEFAULT("devtools.timeline.picture"), 237 TRACE_DISABLED_BY_DEFAULT("devtools.timeline.picture"),
297 "cc::DisplayItemList", this, 238 "cc::DisplayItemList", this,
298 TracedDisplayItemList::AsTraceableDisplayItemList(this, 239 TracedDisplayItemList::AsTraceableDisplayItemList(this,
299 DisplayItemsTracingEnabled())); 240 DisplayItemsTracingEnabled()));
300 } 241 }
301 242
302 void DisplayItemList::GenerateDiscardableImagesMetadata() { 243 void DisplayItemList::GenerateDiscardableImagesMetadata() {
303 // This should be only called once, and only after CreateAndCacheSkPicture. 244 // This should be only called once, and only after CreateAndCacheSkPicture.
304 DCHECK(image_map_.empty()); 245 DCHECK(image_map_.empty());
305 DCHECK(!settings_.use_cached_picture || picture_);
306 if (settings_.use_cached_picture && !picture_->willPlayBackBitmaps())
307 return;
308 246
309 // The cached picture is translated by -layer_rect_.origin during record, 247 gfx::Rect bounds = rtree_.GetBounds();
310 // so we need to offset that back in order to get right positioning for
311 // images.
312 DiscardableImageMap::ScopedMetadataGenerator generator( 248 DiscardableImageMap::ScopedMetadataGenerator generator(
313 &image_map_, gfx::Size(layer_rect_.right(), layer_rect_.bottom())); 249 &image_map_, gfx::Size(bounds.right(), bounds.bottom()));
314 Raster(generator.canvas(), nullptr, 250 Raster(generator.canvas(), nullptr, gfx::Rect(), 1.f);
315 gfx::Rect(layer_rect_.right(), layer_rect_.bottom()), 1.f);
316 } 251 }
317 252
318 void DisplayItemList::GetDiscardableImagesInRect( 253 void DisplayItemList::GetDiscardableImagesInRect(
319 const gfx::Rect& rect, 254 const gfx::Rect& rect,
320 float raster_scale, 255 float raster_scale,
321 std::vector<DrawImage>* images) { 256 std::vector<DrawImage>* images) {
322 image_map_.GetDiscardableImagesInRect(rect, raster_scale, images); 257 image_map_.GetDiscardableImagesInRect(rect, raster_scale, images);
323 } 258 }
324 259
325 } // namespace cc 260 } // namespace cc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698