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

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

Issue 2149743003: cc: Clean up DisplayItemList API (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: 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
« no previous file with comments | « cc/playback/display_item_list.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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
(...skipping 29 matching lines...) Expand all
40 bool tracing_enabled; 40 bool tracing_enabled;
41 TRACE_EVENT_CATEGORY_GROUP_ENABLED( 41 TRACE_EVENT_CATEGORY_GROUP_ENABLED(
42 TRACE_DISABLED_BY_DEFAULT("cc.debug.display_items"), &tracing_enabled); 42 TRACE_DISABLED_BY_DEFAULT("cc.debug.display_items"), &tracing_enabled);
43 return tracing_enabled; 43 return tracing_enabled;
44 } 44 }
45 45
46 const int kDefaultNumDisplayItemsToReserve = 100; 46 const int kDefaultNumDisplayItemsToReserve = 100;
47 47
48 } // namespace 48 } // namespace
49 49
50 DisplayItemList::Inputs::Inputs(gfx::Rect layer_rect,
51 const DisplayItemListSettings& settings)
52 : items(LargestDisplayItemSize(),
53 LargestDisplayItemSize() * kDefaultNumDisplayItemsToReserve),
54 settings(settings),
55 layer_rect(layer_rect),
56 is_suitable_for_gpu_rasterization(true) {}
57 DisplayItemList::Inputs::~Inputs() {}
Khushal 2016/07/14 00:03:03 nit: new line above.
Menglin 2016/07/18 18:15:21 Done.
58
50 scoped_refptr<DisplayItemList> DisplayItemList::Create( 59 scoped_refptr<DisplayItemList> DisplayItemList::Create(
51 const gfx::Rect& layer_rect, 60 const gfx::Rect& layer_rect,
52 const DisplayItemListSettings& settings) { 61 const DisplayItemListSettings& settings) {
53 return make_scoped_refptr(new DisplayItemList( 62 return make_scoped_refptr(new DisplayItemList(
54 layer_rect, settings, 63 layer_rect, settings,
55 !settings.use_cached_picture || DisplayItemsTracingEnabled())); 64 !settings.use_cached_picture || DisplayItemsTracingEnabled()));
56 } 65 }
57 66
58 scoped_refptr<DisplayItemList> DisplayItemList::CreateFromProto( 67 scoped_refptr<DisplayItemList> DisplayItemList::CreateFromProto(
59 const proto::DisplayItemList& proto, 68 const proto::DisplayItemList& proto,
(...skipping 12 matching lines...) Expand all
72 } 81 }
73 82
74 list->Finalize(); 83 list->Finalize();
75 84
76 return list; 85 return list;
77 } 86 }
78 87
79 DisplayItemList::DisplayItemList(gfx::Rect layer_rect, 88 DisplayItemList::DisplayItemList(gfx::Rect layer_rect,
80 const DisplayItemListSettings& settings, 89 const DisplayItemListSettings& settings,
81 bool retain_individual_display_items) 90 bool retain_individual_display_items)
82 : items_(LargestDisplayItemSize(), 91 : retain_individual_display_items_(retain_individual_display_items),
83 LargestDisplayItemSize() * kDefaultNumDisplayItemsToReserve),
84 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), 92 approximate_op_count_(0),
89 picture_memory_usage_(0) { 93 picture_memory_usage_(0),
90 if (settings_.use_cached_picture) { 94 inputs_(layer_rect, settings) {
95 if (inputs_.settings.use_cached_picture) {
91 SkRTreeFactory factory; 96 SkRTreeFactory factory;
92 recorder_.reset(new SkPictureRecorder()); 97 recorder_.reset(new SkPictureRecorder());
93 98
94 SkCanvas* canvas = recorder_->beginRecording( 99 SkCanvas* canvas = recorder_->beginRecording(
95 layer_rect_.width(), layer_rect_.height(), &factory); 100 inputs_.layer_rect.width(), inputs_.layer_rect.height(), &factory);
96 canvas->translate(-layer_rect_.x(), -layer_rect_.y()); 101 canvas->translate(-inputs_.layer_rect.x(), -inputs_.layer_rect.y());
97 canvas->clipRect(gfx::RectToSkRect(layer_rect_)); 102 canvas->clipRect(gfx::RectToSkRect(inputs_.layer_rect));
98 } 103 }
99 } 104 }
100 105
101 DisplayItemList::~DisplayItemList() { 106 DisplayItemList::~DisplayItemList() {
102 } 107 }
103 108
104 void DisplayItemList::ToProtobuf(proto::DisplayItemList* proto) { 109 void DisplayItemList::ToProtobuf(proto::DisplayItemList* proto) {
105 // The flattened SkPicture approach is going away, and the proto 110 // The flattened SkPicture approach is going away, and the proto
106 // doesn't currently support serializing that flattened picture. 111 // doesn't currently support serializing that flattened picture.
107 DCHECK(retain_individual_display_items_); 112 DCHECK(retain_individual_display_items_);
108 113
109 RectToProto(layer_rect_, proto->mutable_layer_rect()); 114 RectToProto(inputs_.layer_rect, proto->mutable_layer_rect());
110 settings_.ToProtobuf(proto->mutable_settings()); 115 inputs_.settings.ToProtobuf(proto->mutable_settings());
111 116
112 DCHECK_EQ(0, proto->items_size()); 117 DCHECK_EQ(0, proto->items_size());
113 for (const auto& item : items_) 118 for (const auto& item : inputs_.items)
114 item.ToProtobuf(proto->add_items()); 119 item.ToProtobuf(proto->add_items());
115 } 120 }
116 121
117 void DisplayItemList::Raster(SkCanvas* canvas, 122 void DisplayItemList::Raster(SkCanvas* canvas,
118 SkPicture::AbortCallback* callback, 123 SkPicture::AbortCallback* callback,
119 const gfx::Rect& canvas_target_playback_rect, 124 const gfx::Rect& canvas_target_playback_rect,
120 float contents_scale) const { 125 float contents_scale) const {
121 canvas->save(); 126 canvas->save();
122 127
123 if (!canvas_target_playback_rect.IsEmpty()) { 128 if (!canvas_target_playback_rect.IsEmpty()) {
124 // canvas_target_playback_rect is specified in device space. We can't 129 // 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 130 // use clipRect because canvas CTM will be applied on it. Use clipRegion
126 // instead because it ignores canvas CTM. 131 // instead because it ignores canvas CTM.
127 SkRegion device_clip; 132 SkRegion device_clip;
128 device_clip.setRect(gfx::RectToSkIRect(canvas_target_playback_rect)); 133 device_clip.setRect(gfx::RectToSkIRect(canvas_target_playback_rect));
129 canvas->clipRegion(device_clip); 134 canvas->clipRegion(device_clip);
130 } 135 }
131 136
132 canvas->scale(contents_scale, contents_scale); 137 canvas->scale(contents_scale, contents_scale);
133 Raster(canvas, callback); 138 Raster(canvas, callback);
134 canvas->restore(); 139 canvas->restore();
135 } 140 }
136 141
137 void DisplayItemList::Raster(SkCanvas* canvas, 142 void DisplayItemList::Raster(SkCanvas* canvas,
138 SkPicture::AbortCallback* callback) const { 143 SkPicture::AbortCallback* callback) const {
139 if (!settings_.use_cached_picture) { 144 if (!inputs_.settings.use_cached_picture) {
140 for (const auto& item : items_) 145 for (const auto& item : inputs_.items)
141 item.Raster(canvas, callback); 146 item.Raster(canvas, callback);
142 } else { 147 } else {
143 DCHECK(picture_); 148 DCHECK(picture_);
144 149
145 canvas->save(); 150 canvas->save();
146 canvas->translate(layer_rect_.x(), layer_rect_.y()); 151 canvas->translate(inputs_.layer_rect.x(), inputs_.layer_rect.y());
147 if (callback) { 152 if (callback) {
148 // If we have a callback, we need to call |draw()|, |drawPicture()| 153 // If we have a callback, we need to call |draw()|, |drawPicture()|
149 // doesn't take a callback. This is used by |AnalysisCanvas| to early 154 // doesn't take a callback. This is used by |AnalysisCanvas| to early
150 // out. 155 // out.
151 picture_->playback(canvas, callback); 156 picture_->playback(canvas, callback);
152 } else { 157 } else {
153 // Prefer to call |drawPicture()| on the canvas since it could place the 158 // Prefer to call |drawPicture()| on the canvas since it could place the
154 // entire picture on the canvas instead of parsing the skia operations. 159 // entire picture on the canvas instead of parsing the skia operations.
155 canvas->drawPicture(picture_.get()); 160 canvas->drawPicture(picture_.get());
156 } 161 }
157 canvas->restore(); 162 canvas->restore();
158 } 163 }
159 } 164 }
160 165
161 void DisplayItemList::ProcessAppendedItem(const DisplayItem* item) { 166 void DisplayItemList::ProcessAppendedItem(const DisplayItem* item) {
162 if (settings_.use_cached_picture) { 167 if (inputs_.settings.use_cached_picture) {
163 DCHECK(recorder_); 168 DCHECK(recorder_);
164 item->Raster(recorder_->getRecordingCanvas(), nullptr); 169 item->Raster(recorder_->getRecordingCanvas(), nullptr);
165 } 170 }
166 if (!retain_individual_display_items_) { 171 if (!retain_individual_display_items_) {
167 items_.Clear(); 172 inputs_.items.Clear();
168 } 173 }
169 } 174 }
170 175
171 void DisplayItemList::RasterIntoCanvas(const DisplayItem& item) { 176 void DisplayItemList::RasterIntoCanvas(const DisplayItem& item) {
172 DCHECK(recorder_); 177 DCHECK(recorder_);
173 DCHECK(!retain_individual_display_items_); 178 DCHECK(!retain_individual_display_items_);
174 179
175 item.Raster(recorder_->getRecordingCanvas(), nullptr); 180 item.Raster(recorder_->getRecordingCanvas(), nullptr);
176 } 181 }
177 182
178 bool DisplayItemList::RetainsIndividualDisplayItems() const { 183 bool DisplayItemList::RetainsIndividualDisplayItems() const {
179 return retain_individual_display_items_; 184 return retain_individual_display_items_;
180 } 185 }
181 186
182 void DisplayItemList::Finalize() { 187 void DisplayItemList::Finalize() {
183 TRACE_EVENT0("cc", "DisplayItemList::Finalize"); 188 TRACE_EVENT0("cc", "DisplayItemList::Finalize");
184 // TODO(dtrainor): Need to deal with serializing visual_rects_. 189 // TODO(dtrainor): Need to deal with serializing visual_rects_.
185 // http://crbug.com/568757. 190 // http://crbug.com/568757.
186 DCHECK(!retain_individual_display_items_ || 191 DCHECK(!retain_individual_display_items_ ||
187 items_.size() == visual_rects_.size()) 192 inputs_.items.size() == inputs_.visual_rects.size())
188 << "items.size() " << items_.size() << " visual_rects.size() " 193 << "items.size() " << inputs_.items.size() << " visual_rects.size() "
189 << visual_rects_.size(); 194 << inputs_.visual_rects.size();
190 195
191 // TODO(vmpstr): Build and make use of an RTree from the visual 196 // TODO(vmpstr): Build and make use of an RTree from the visual
192 // rects. For now we just clear them out since we won't ever need 197 // rects. For now we just clear them out since we won't ever need
193 // them to stick around post-Finalize. http://crbug.com/527245 198 // them to stick around post-Finalize. http://crbug.com/527245
194 // This clears both the vector and the vector's capacity, since visual_rects_ 199 // This clears both the vector and the vector's capacity, since visual_rects_
195 // won't be used anymore. 200 // won't be used anymore.
196 std::vector<gfx::Rect>().swap(visual_rects_); 201 std::vector<gfx::Rect>().swap(inputs_.visual_rects);
197 202
198 if (settings_.use_cached_picture) { 203 if (inputs_.settings.use_cached_picture) {
199 // Convert to an SkPicture for faster rasterization. 204 // Convert to an SkPicture for faster rasterization.
200 DCHECK(settings_.use_cached_picture); 205 DCHECK(inputs_.settings.use_cached_picture);
201 DCHECK(!picture_); 206 DCHECK(!picture_);
202 picture_ = recorder_->finishRecordingAsPicture(); 207 picture_ = recorder_->finishRecordingAsPicture();
203 DCHECK(picture_); 208 DCHECK(picture_);
204 picture_memory_usage_ = 209 picture_memory_usage_ =
205 SkPictureUtils::ApproximateBytesUsed(picture_.get()); 210 SkPictureUtils::ApproximateBytesUsed(picture_.get());
206 recorder_.reset(); 211 recorder_.reset();
207 } 212 }
208 } 213 }
209 214
210 bool DisplayItemList::IsSuitableForGpuRasterization() const { 215 bool DisplayItemList::IsSuitableForGpuRasterization() const {
211 return is_suitable_for_gpu_rasterization_; 216 return inputs_.is_suitable_for_gpu_rasterization;
212 } 217 }
213 218
214 int DisplayItemList::ApproximateOpCount() const { 219 int DisplayItemList::ApproximateOpCount() const {
215 if (retain_individual_display_items_) 220 if (retain_individual_display_items_)
216 return approximate_op_count_; 221 return approximate_op_count_;
217 return picture_ ? picture_->approximateOpCount() : 0; 222 return picture_ ? picture_->approximateOpCount() : 0;
218 } 223 }
219 224
220 size_t DisplayItemList::ApproximateMemoryUsage() const { 225 size_t DisplayItemList::ApproximateMemoryUsage() const {
221 // We double-count in this case. Produce zero to avoid being misleading. 226 // We double-count in this case. Produce zero to avoid being misleading.
222 if (settings_.use_cached_picture && retain_individual_display_items_) 227 if (inputs_.settings.use_cached_picture && retain_individual_display_items_)
223 return 0; 228 return 0;
224 229
225 DCHECK(!settings_.use_cached_picture || picture_); 230 DCHECK(!inputs_.settings.use_cached_picture || picture_);
226 231
227 size_t memory_usage = sizeof(*this); 232 size_t memory_usage = sizeof(*this);
228 233
229 size_t external_memory_usage = 0; 234 size_t external_memory_usage = 0;
230 if (retain_individual_display_items_) { 235 if (retain_individual_display_items_) {
231 // Warning: this double-counts SkPicture data if use_cached_picture is 236 // Warning: this double-counts SkPicture data if use_cached_picture is
232 // also true. 237 // also true.
233 for (const auto& item : items_) { 238 for (const auto& item : inputs_.items) {
234 external_memory_usage += item.ExternalMemoryUsage(); 239 external_memory_usage += item.ExternalMemoryUsage();
235 } 240 }
236 } 241 }
237 242
238 // Memory outside this class due to |items_|. 243 // Memory outside this class due to |items_|.
239 memory_usage += items_.GetCapacityInBytes() + external_memory_usage; 244 memory_usage += inputs_.items.GetCapacityInBytes() + external_memory_usage;
240 245
241 // Memory outside this class due to |picture|. 246 // Memory outside this class due to |picture|.
242 memory_usage += picture_memory_usage_; 247 memory_usage += picture_memory_usage_;
243 248
244 // TODO(jbroman): Does anything else owned by this class substantially 249 // TODO(jbroman): Does anything else owned by this class substantially
245 // contribute to memory usage? 250 // contribute to memory usage?
246 251
247 return memory_usage; 252 return memory_usage;
248 } 253 }
249 254
250 bool DisplayItemList::ShouldBeAnalyzedForSolidColor() const { 255 bool DisplayItemList::ShouldBeAnalyzedForSolidColor() const {
251 return ApproximateOpCount() <= kOpCountThatIsOkToAnalyze; 256 return ApproximateOpCount() <= kOpCountThatIsOkToAnalyze;
252 } 257 }
253 258
254 std::unique_ptr<base::trace_event::ConvertableToTraceFormat> 259 std::unique_ptr<base::trace_event::ConvertableToTraceFormat>
255 DisplayItemList::AsValue(bool include_items) const { 260 DisplayItemList::AsValue(bool include_items) const {
256 std::unique_ptr<base::trace_event::TracedValue> state( 261 std::unique_ptr<base::trace_event::TracedValue> state(
257 new base::trace_event::TracedValue()); 262 new base::trace_event::TracedValue());
258 263
259 state->BeginDictionary("params"); 264 state->BeginDictionary("params");
260 if (include_items) { 265 if (include_items) {
261 state->BeginArray("items"); 266 state->BeginArray("items");
262 size_t item_index = 0; 267 size_t item_index = 0;
263 for (const DisplayItem& item : items_) { 268 for (const DisplayItem& item : inputs_.items) {
264 item.AsValueInto(item_index < visual_rects_.size() 269 item.AsValueInto(item_index < inputs_.visual_rects.size()
265 ? visual_rects_[item_index] 270 ? inputs_.visual_rects[item_index]
266 : gfx::Rect(), 271 : gfx::Rect(),
267 state.get()); 272 state.get());
268 item_index++; 273 item_index++;
269 } 274 }
270 state->EndArray(); // "items". 275 state->EndArray(); // "items".
271 } 276 }
272 state->SetValue("layer_rect", MathUtil::AsValue(layer_rect_)); 277 state->SetValue("layer_rect", MathUtil::AsValue(inputs_.layer_rect));
273 state->EndDictionary(); // "params". 278 state->EndDictionary(); // "params".
274 279
275 if (!layer_rect_.IsEmpty()) { 280 if (!inputs_.layer_rect.IsEmpty()) {
276 SkPictureRecorder recorder; 281 SkPictureRecorder recorder;
277 SkCanvas* canvas = 282 SkCanvas* canvas = recorder.beginRecording(inputs_.layer_rect.width(),
278 recorder.beginRecording(layer_rect_.width(), layer_rect_.height()); 283 inputs_.layer_rect.height());
279 canvas->translate(-layer_rect_.x(), -layer_rect_.y()); 284 canvas->translate(-inputs_.layer_rect.x(), -inputs_.layer_rect.y());
280 canvas->clipRect(gfx::RectToSkRect(layer_rect_)); 285 canvas->clipRect(gfx::RectToSkRect(inputs_.layer_rect));
281 Raster(canvas, NULL, gfx::Rect(), 1.f); 286 Raster(canvas, NULL, gfx::Rect(), 1.f);
282 sk_sp<SkPicture> picture = recorder.finishRecordingAsPicture(); 287 sk_sp<SkPicture> picture = recorder.finishRecordingAsPicture();
283 288
284 std::string b64_picture; 289 std::string b64_picture;
285 PictureDebugUtil::SerializeAsBase64(picture.get(), &b64_picture); 290 PictureDebugUtil::SerializeAsBase64(picture.get(), &b64_picture);
286 state->SetString("skp64", b64_picture); 291 state->SetString("skp64", b64_picture);
287 } 292 }
288 293
289 return std::move(state); 294 return std::move(state);
290 } 295 }
291 296
292 void DisplayItemList::EmitTraceSnapshot() const { 297 void DisplayItemList::EmitTraceSnapshot() const {
293 TRACE_EVENT_OBJECT_SNAPSHOT_WITH_ID( 298 TRACE_EVENT_OBJECT_SNAPSHOT_WITH_ID(
294 TRACE_DISABLED_BY_DEFAULT("cc.debug.display_items") "," 299 TRACE_DISABLED_BY_DEFAULT("cc.debug.display_items") ","
295 TRACE_DISABLED_BY_DEFAULT("cc.debug.picture") "," 300 TRACE_DISABLED_BY_DEFAULT("cc.debug.picture") ","
296 TRACE_DISABLED_BY_DEFAULT("devtools.timeline.picture"), 301 TRACE_DISABLED_BY_DEFAULT("devtools.timeline.picture"),
297 "cc::DisplayItemList", this, 302 "cc::DisplayItemList", this,
298 TracedDisplayItemList::AsTraceableDisplayItemList(this, 303 TracedDisplayItemList::AsTraceableDisplayItemList(this,
299 DisplayItemsTracingEnabled())); 304 DisplayItemsTracingEnabled()));
300 } 305 }
301 306
302 void DisplayItemList::GenerateDiscardableImagesMetadata() { 307 void DisplayItemList::GenerateDiscardableImagesMetadata() {
303 // This should be only called once, and only after CreateAndCacheSkPicture. 308 // This should be only called once, and only after CreateAndCacheSkPicture.
304 DCHECK(image_map_.empty()); 309 DCHECK(image_map_.empty());
305 DCHECK(!settings_.use_cached_picture || picture_); 310 DCHECK(!inputs_.settings.use_cached_picture || picture_);
306 if (settings_.use_cached_picture && !picture_->willPlayBackBitmaps()) 311 if (inputs_.settings.use_cached_picture && !picture_->willPlayBackBitmaps())
307 return; 312 return;
308 313
309 // The cached picture is translated by -layer_rect_.origin during record, 314 // The cached picture is translated by -layer_rect_.origin during record,
310 // so we need to offset that back in order to get right positioning for 315 // so we need to offset that back in order to get right positioning for
311 // images. 316 // images.
312 DiscardableImageMap::ScopedMetadataGenerator generator( 317 DiscardableImageMap::ScopedMetadataGenerator generator(
313 &image_map_, gfx::Size(layer_rect_.right(), layer_rect_.bottom())); 318 &image_map_,
319 gfx::Size(inputs_.layer_rect.right(), inputs_.layer_rect.bottom()));
314 Raster(generator.canvas(), nullptr, 320 Raster(generator.canvas(), nullptr,
315 gfx::Rect(layer_rect_.right(), layer_rect_.bottom()), 1.f); 321 gfx::Rect(inputs_.layer_rect.right(), inputs_.layer_rect.bottom()),
322 1.f);
316 } 323 }
317 324
318 void DisplayItemList::GetDiscardableImagesInRect( 325 void DisplayItemList::GetDiscardableImagesInRect(
319 const gfx::Rect& rect, 326 const gfx::Rect& rect,
320 float raster_scale, 327 float raster_scale,
321 std::vector<DrawImage>* images) { 328 std::vector<DrawImage>* images) {
322 image_map_.GetDiscardableImagesInRect(rect, raster_scale, images); 329 image_map_.GetDiscardableImagesInRect(rect, raster_scale, images);
323 } 330 }
324 331
325 } // namespace cc 332 } // namespace cc
OLDNEW
« no previous file with comments | « cc/playback/display_item_list.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698