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