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

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: Sync to head and flip for LayoutInline. Created 4 years, 6 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 class ImageSerializationProcessor; 34 class ImageSerializationProcessor;
33 35
34 namespace { 36 namespace {
35 37
36 // We don't perform per-layer solid color analysis when there are too many skia 38 // We don't perform per-layer solid color analysis when there are too many skia
37 // operations. 39 // operations.
38 const int kOpCountThatIsOkToAnalyze = 10; 40 const int kOpCountThatIsOkToAnalyze = 10;
39 41
40 bool DisplayItemsTracingEnabled() { 42 bool DisplayItemsTracingEnabled() {
41 bool tracing_enabled; 43 bool tracing_enabled;
42 TRACE_EVENT_CATEGORY_GROUP_ENABLED( 44 TRACE_EVENT_CATEGORY_GROUP_ENABLED(
43 TRACE_DISABLED_BY_DEFAULT("cc.debug.display_items"), &tracing_enabled); 45 TRACE_DISABLED_BY_DEFAULT("cc.debug.display_items"), &tracing_enabled);
44 return tracing_enabled; 46 return tracing_enabled;
45 } 47 }
46 48
47 const int kDefaultNumDisplayItemsToReserve = 100; 49 const int kDefaultNumDisplayItemsToReserve = 100;
48 50
49 } // namespace 51 } // namespace
50 52
51 scoped_refptr<DisplayItemList> DisplayItemList::Create( 53 scoped_refptr<DisplayItemList> DisplayItemList::Create(
vmpstr 2016/06/22 20:14:07 Just as a very optional nit, I've been slowly remo
wkorman 2016/06/24 19:57:14 It looks like we'd need to replace a bunch of call
vmpstr 2016/06/24 20:15:33 Makes sense, and yeah in this case we also have Cr
52 const gfx::Rect& layer_rect,
53 const DisplayItemListSettings& settings) { 54 const DisplayItemListSettings& settings) {
54 return make_scoped_refptr(new DisplayItemList( 55 return make_scoped_refptr(new DisplayItemList(settings));
55 layer_rect, settings,
56 !settings.use_cached_picture || DisplayItemsTracingEnabled()));
57 } 56 }
58 57
59 scoped_refptr<DisplayItemList> DisplayItemList::CreateFromProto( 58 scoped_refptr<DisplayItemList> DisplayItemList::CreateFromProto(
60 const proto::DisplayItemList& proto, 59 const proto::DisplayItemList& proto,
61 ImageSerializationProcessor* image_serialization_processor) { 60 ImageSerializationProcessor* image_serialization_processor) {
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, image_serialization_processor); 68 visual_rect, list.get(), item_proto, image_serialization_processor);
71 } 69 }
72 70
73 list->Finalize(); 71 list->Finalize();
74 72
75 return list; 73 return list;
76 } 74 }
77 75
78 DisplayItemList::DisplayItemList(gfx::Rect layer_rect, 76 DisplayItemList::DisplayItemList(const DisplayItemListSettings& settings)
79 const DisplayItemListSettings& settings,
80 bool retain_individual_display_items)
81 : items_(LargestDisplayItemSize(), 77 : items_(LargestDisplayItemSize(),
82 LargestDisplayItemSize() * kDefaultNumDisplayItemsToReserve), 78 LargestDisplayItemSize() * kDefaultNumDisplayItemsToReserve),
79 retain_visual_rects_(false),
vmpstr 2016/06/22 20:14:07 nit: you can = false in the header (same for the r
wkorman 2016/06/24 19:57:14 Done.
83 settings_(settings), 80 settings_(settings),
84 retain_individual_display_items_(retain_individual_display_items), 81 all_items_are_suitable_for_gpu_rasterization_(true),
85 layer_rect_(layer_rect), 82 approximate_op_count_(0) {}
86 is_suitable_for_gpu_rasterization_(true),
87 approximate_op_count_(0),
88 picture_memory_usage_(0) {
89 if (settings_.use_cached_picture) {
90 SkRTreeFactory factory;
91 recorder_.reset(new SkPictureRecorder());
92
93 SkCanvas* canvas = recorder_->beginRecording(
94 layer_rect_.width(), layer_rect_.height(), &factory);
95 canvas->translate(-layer_rect_.x(), -layer_rect_.y());
96 canvas->clipRect(gfx::RectToSkRect(layer_rect_));
97 }
98 }
99 83
100 DisplayItemList::~DisplayItemList() { 84 DisplayItemList::~DisplayItemList() {
101 } 85 }
102 86
103 void DisplayItemList::ToProtobuf( 87 void DisplayItemList::ToProtobuf(
104 proto::DisplayItemList* proto, 88 proto::DisplayItemList* proto,
105 ImageSerializationProcessor* image_serialization_processor) { 89 ImageSerializationProcessor* image_serialization_processor) {
106 // The flattened SkPicture approach is going away, and the proto 90 // The flattened SkPicture approach is going away, and the proto
107 // doesn't currently support serializing that flattened picture. 91 // doesn't currently support serializing that flattened picture.
108 DCHECK(retain_individual_display_items_);
109
110 RectToProto(layer_rect_, proto->mutable_layer_rect());
111 settings_.ToProtobuf(proto->mutable_settings()); 92 settings_.ToProtobuf(proto->mutable_settings());
112 93
113 DCHECK_EQ(0, proto->items_size()); 94 DCHECK_EQ(0, proto->items_size());
114 for (const auto& item : items_) 95 DCHECK_EQ(0, proto->visual_rects_size());
96 DCHECK(items_.size() == visual_rects_.size())
97 << "items.size() " << items_.size() << " visual_rects.size() "
98 << visual_rects_.size();
99 int i = 0;
100 for (const auto& item : items_) {
vmpstr 2016/06/22 20:14:07 I wonder if we should add operator[] to Contiguous
wkorman 2016/06/24 19:57:14 Added TODO.
115 item.ToProtobuf(proto->add_items(), image_serialization_processor); 101 item.ToProtobuf(proto->add_items(), image_serialization_processor);
102 RectToProto(visual_rects_[i++], proto->add_visual_rects());
103 }
116 } 104 }
117 105
118 void DisplayItemList::Raster(SkCanvas* canvas, 106 void DisplayItemList::Raster(SkCanvas* canvas,
119 SkPicture::AbortCallback* callback, 107 SkPicture::AbortCallback* callback,
120 const gfx::Rect& canvas_target_playback_rect, 108 const gfx::Rect& canvas_playback_rect,
121 float contents_scale) const { 109 float contents_scale) const {
122 if (!settings_.use_cached_picture) { 110 canvas->save();
123 canvas->save(); 111 canvas->scale(contents_scale, contents_scale);
124 canvas->scale(contents_scale, contents_scale);
125 for (const auto& item : items_)
126 item.Raster(canvas, canvas_target_playback_rect, callback);
127 canvas->restore();
128 } else {
129 DCHECK(picture_);
130 112
131 canvas->save(); 113 gfx::Rect query_rect = GetQueryRect(canvas_playback_rect, contents_scale);
132 canvas->scale(contents_scale, contents_scale); 114 std::vector<size_t> indices;
133 canvas->translate(layer_rect_.x(), layer_rect_.y()); 115 rtree_.Search(query_rect, &indices);
134 if (callback) { 116 for (size_t index : indices) {
135 // If we have a callback, we need to call |draw()|, |drawPicture()| 117 items_[index].Raster(canvas, callback);
136 // doesn't take a callback. This is used by |AnalysisCanvas| to early 118 // We use a callback during solid color analysis on the compositor thread to
137 // out. 119 // break out early. Since we're handling a sequence of pictures via rtree
138 picture_->playback(canvas, callback); 120 // query results ourselves, we have to respect the callback and early out.
139 } else { 121 if (callback && callback->abort())
140 // Prefer to call |drawPicture()| on the canvas since it could place the 122 break;
141 // entire picture on the canvas instead of parsing the skia operations.
142 canvas->drawPicture(picture_.get());
143 }
144 canvas->restore();
145 } 123 }
124 canvas->restore();
146 } 125 }
147 126
148 void DisplayItemList::ProcessAppendedItem(const DisplayItem* item) { 127 gfx::Rect DisplayItemList::GetQueryRect(const gfx::Rect& rect,
149 if (settings_.use_cached_picture) { 128 float scale) const {
150 DCHECK(recorder_); 129 // If the playback rect is empty, assume that we need to play back everything.
151 item->Raster(recorder_->getRecordingCanvas(), gfx::Rect(), nullptr); 130 return rect.IsEmpty() ? rtree_.GetBounds()
152 } 131 : ScaleToEnclosingRect(rect, 1.f / scale);
153 if (!retain_individual_display_items_) {
154 items_.Clear();
155 }
156 }
157
158 void DisplayItemList::RasterIntoCanvas(const DisplayItem& item) {
159 DCHECK(recorder_);
160 DCHECK(!retain_individual_display_items_);
161
162 item.Raster(recorder_->getRecordingCanvas(), gfx::Rect(), nullptr);
163 }
164
165 bool DisplayItemList::RetainsIndividualDisplayItems() const {
166 return retain_individual_display_items_;
167 } 132 }
168 133
169 void DisplayItemList::Finalize() { 134 void DisplayItemList::Finalize() {
170 TRACE_EVENT0("cc", "DisplayItemList::Finalize"); 135 TRACE_EVENT0("cc", "DisplayItemList::Finalize");
171 // TODO(dtrainor): Need to deal with serializing visual_rects_. 136 // TODO(dtrainor): Need to deal with serializing visual_rects_.
172 // http://crbug.com/568757. 137 // http://crbug.com/568757.
173 DCHECK(!retain_individual_display_items_ || 138 DCHECK(items_.size() == visual_rects_.size())
174 items_.size() == visual_rects_.size())
175 << "items.size() " << items_.size() << " visual_rects.size() " 139 << "items.size() " << items_.size() << " visual_rects.size() "
176 << visual_rects_.size(); 140 << visual_rects_.size();
141 rtree_.Build(visual_rects_);
177 142
178 // TODO(vmpstr): Build and make use of an RTree from the visual 143 if (!retain_visual_rects_)
179 // rects. For now we just clear them out since we won't ever need 144 // This clears both the vector and the vector's capacity, since
180 // them to stick around post-Finalize. http://crbug.com/527245 145 // visual_rects_
181 // This clears both the vector and the vector's capacity, since visual_rects_ 146 // won't be used anymore.
182 // won't be used anymore. 147 std::vector<gfx::Rect>().swap(visual_rects_);
183 std::vector<gfx::Rect>().swap(visual_rects_);
184
185 if (settings_.use_cached_picture) {
186 // Convert to an SkPicture for faster rasterization.
187 DCHECK(settings_.use_cached_picture);
188 DCHECK(!picture_);
189 picture_ = recorder_->finishRecordingAsPicture();
190 DCHECK(picture_);
191 picture_memory_usage_ =
192 SkPictureUtils::ApproximateBytesUsed(picture_.get());
193 recorder_.reset();
194 }
195 } 148 }
196 149
197 bool DisplayItemList::IsSuitableForGpuRasterization() const { 150 bool DisplayItemList::IsSuitableForGpuRasterization() const {
198 return is_suitable_for_gpu_rasterization_; 151 // This is more permissive than Picture's implementation, since none of the
vmpstr 2016/06/22 20:14:07 nit: Add a TODO(someone) to make it clear that thi
wkorman 2016/06/24 19:57:14 Done.
152 // items might individually trigger a veto even though they collectively have
153 // enough "bad" operations that a corresponding Picture would get vetoed. See
154 // crbug.com/513016.
155 return all_items_are_suitable_for_gpu_rasterization_;
199 } 156 }
200 157
201 int DisplayItemList::ApproximateOpCount() const { 158 int DisplayItemList::ApproximateOpCount() const {
202 if (retain_individual_display_items_) 159 return approximate_op_count_;
203 return approximate_op_count_;
204 return picture_ ? picture_->approximateOpCount() : 0;
205 } 160 }
206 161
207 size_t DisplayItemList::ApproximateMemoryUsage() const { 162 size_t DisplayItemList::ApproximateMemoryUsage() const {
208 // We double-count in this case. Produce zero to avoid being misleading.
209 if (settings_.use_cached_picture && retain_individual_display_items_)
210 return 0;
211
212 DCHECK(!settings_.use_cached_picture || picture_);
213
214 size_t memory_usage = sizeof(*this); 163 size_t memory_usage = sizeof(*this);
215 164
216 size_t external_memory_usage = 0; 165 size_t external_memory_usage = 0;
217 if (retain_individual_display_items_) { 166 // Warning: this double-counts SkPicture data if use_cached_picture is
218 // Warning: this double-counts SkPicture data if use_cached_picture is 167 // also true.
219 // also true. 168 for (const auto& item : items_) {
220 for (const auto& item : items_) { 169 external_memory_usage += item.ExternalMemoryUsage();
221 external_memory_usage += item.ExternalMemoryUsage();
222 }
223 } 170 }
224 171
225 // Memory outside this class due to |items_|. 172 // Memory outside this class due to |items_|.
226 memory_usage += items_.GetCapacityInBytes() + external_memory_usage; 173 memory_usage += items_.GetCapacityInBytes() + external_memory_usage;
227 174
228 // Memory outside this class due to |picture|.
229 memory_usage += picture_memory_usage_;
230
231 // TODO(jbroman): Does anything else owned by this class substantially 175 // TODO(jbroman): Does anything else owned by this class substantially
232 // contribute to memory usage? 176 // contribute to memory usage?
177 // TODO(vmpstr): Probably DiscardableImageMap is worth counting here.
233 178
234 return memory_usage; 179 return memory_usage;
235 } 180 }
236 181
237 bool DisplayItemList::ShouldBeAnalyzedForSolidColor() const { 182 bool DisplayItemList::ShouldBeAnalyzedForSolidColor() const {
238 return ApproximateOpCount() <= kOpCountThatIsOkToAnalyze; 183 return ApproximateOpCount() <= kOpCountThatIsOkToAnalyze;
239 } 184 }
240 185
241 std::unique_ptr<base::trace_event::ConvertableToTraceFormat> 186 std::unique_ptr<base::trace_event::ConvertableToTraceFormat>
242 DisplayItemList::AsValue(bool include_items) const { 187 DisplayItemList::AsValue(bool include_items) const {
243 std::unique_ptr<base::trace_event::TracedValue> state( 188 std::unique_ptr<base::trace_event::TracedValue> state(
244 new base::trace_event::TracedValue()); 189 new base::trace_event::TracedValue());
245 190
246 state->BeginDictionary("params"); 191 state->BeginDictionary("params");
247 if (include_items) { 192 if (include_items) {
248 state->BeginArray("items"); 193 state->BeginArray("items");
249 size_t item_index = 0; 194 size_t item_index = 0;
250 for (const DisplayItem& item : items_) { 195 for (const DisplayItem& item : items_) {
251 item.AsValueInto(item_index < visual_rects_.size() 196 item.AsValueInto(item_index < visual_rects_.size()
252 ? visual_rects_[item_index] 197 ? visual_rects_[item_index]
253 : gfx::Rect(), 198 : gfx::Rect(),
254 state.get()); 199 state.get());
255 item_index++; 200 item_index++;
256 } 201 }
257 state->EndArray(); // "items". 202 state->EndArray(); // "items".
258 } 203 }
259 state->SetValue("layer_rect", MathUtil::AsValue(layer_rect_));
260 state->EndDictionary(); // "params". 204 state->EndDictionary(); // "params".
261 205
262 if (!layer_rect_.IsEmpty()) { 206 SkPictureRecorder recorder;
263 SkPictureRecorder recorder; 207 gfx::Rect bounds = rtree_.GetBounds();
264 SkCanvas* canvas = 208 SkCanvas* canvas = recorder.beginRecording(bounds.width(), bounds.height());
265 recorder.beginRecording(layer_rect_.width(), layer_rect_.height()); 209 canvas->translate(-bounds.x(), -bounds.y());
266 canvas->translate(-layer_rect_.x(), -layer_rect_.y()); 210 canvas->clipRect(gfx::RectToSkRect(bounds));
267 canvas->clipRect(gfx::RectToSkRect(layer_rect_)); 211 Raster(canvas, nullptr, gfx::Rect(), 1.f);
268 Raster(canvas, NULL, gfx::Rect(), 1.f); 212 sk_sp<SkPicture> picture = recorder.finishRecordingAsPicture();
269 sk_sp<SkPicture> picture = recorder.finishRecordingAsPicture();
270 213
271 std::string b64_picture; 214 std::string b64_picture;
272 PictureDebugUtil::SerializeAsBase64(picture.get(), &b64_picture); 215 PictureDebugUtil::SerializeAsBase64(picture.get(), &b64_picture);
273 state->SetString("skp64", b64_picture); 216 state->SetString("skp64", b64_picture);
274 }
275 217
276 return std::move(state); 218 return std::move(state);
277 } 219 }
278 220
279 void DisplayItemList::EmitTraceSnapshot() const { 221 void DisplayItemList::EmitTraceSnapshot() const {
280 TRACE_EVENT_OBJECT_SNAPSHOT_WITH_ID( 222 TRACE_EVENT_OBJECT_SNAPSHOT_WITH_ID(
281 TRACE_DISABLED_BY_DEFAULT("cc.debug.display_items") "," 223 TRACE_DISABLED_BY_DEFAULT("cc.debug.display_items") ","
282 TRACE_DISABLED_BY_DEFAULT("cc.debug.picture") "," 224 TRACE_DISABLED_BY_DEFAULT("cc.debug.picture") ","
283 TRACE_DISABLED_BY_DEFAULT("devtools.timeline.picture"), 225 TRACE_DISABLED_BY_DEFAULT("devtools.timeline.picture"),
284 "cc::DisplayItemList", this, 226 "cc::DisplayItemList", this,
285 TracedDisplayItemList::AsTraceableDisplayItemList(this, 227 TracedDisplayItemList::AsTraceableDisplayItemList(this,
286 DisplayItemsTracingEnabled())); 228 DisplayItemsTracingEnabled()));
287 } 229 }
288 230
289 void DisplayItemList::GenerateDiscardableImagesMetadata() { 231 void DisplayItemList::GenerateDiscardableImagesMetadata() {
290 // This should be only called once, and only after CreateAndCacheSkPicture. 232 // This should be only called once, and only after CreateAndCacheSkPicture.
291 DCHECK(image_map_.empty()); 233 DCHECK(image_map_.empty());
292 DCHECK(!settings_.use_cached_picture || picture_);
293 if (settings_.use_cached_picture && !picture_->willPlayBackBitmaps())
294 return;
295 234
296 // The cached picture is translated by -layer_rect_.origin during record, 235 gfx::Rect bounds = rtree_.GetBounds();
297 // so we need to offset that back in order to get right positioning for
298 // images.
299 DiscardableImageMap::ScopedMetadataGenerator generator( 236 DiscardableImageMap::ScopedMetadataGenerator generator(
300 &image_map_, gfx::Size(layer_rect_.right(), layer_rect_.bottom())); 237 &image_map_, gfx::Size(bounds.right(), bounds.bottom()));
301 Raster(generator.canvas(), nullptr, 238 Raster(generator.canvas(), nullptr, gfx::Rect(), 1.f);
302 gfx::Rect(layer_rect_.right(), layer_rect_.bottom()), 1.f);
303 } 239 }
304 240
305 void DisplayItemList::GetDiscardableImagesInRect( 241 void DisplayItemList::GetDiscardableImagesInRect(
306 const gfx::Rect& rect, 242 const gfx::Rect& rect,
307 float raster_scale, 243 float raster_scale,
308 std::vector<DrawImage>* images) { 244 std::vector<DrawImage>* images) {
309 image_map_.GetDiscardableImagesInRect(rect, raster_scale, images); 245 image_map_.GetDiscardableImagesInRect(rect, raster_scale, images);
310 } 246 }
311 247
312 } // namespace cc 248 } // namespace cc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698