OLD | NEW |
| (Empty) |
1 // Copyright 2012 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include <algorithm> | |
6 #include <limits> | |
7 #include <set> | |
8 | |
9 #include "base/trace_event/trace_event.h" | |
10 #include "cc/base/region.h" | |
11 #include "cc/debug/debug_colors.h" | |
12 #include "cc/resources/picture_pile_impl.h" | |
13 #include "cc/resources/raster_source_helper.h" | |
14 #include "skia/ext/analysis_canvas.h" | |
15 #include "third_party/skia/include/core/SkCanvas.h" | |
16 #include "third_party/skia/include/core/SkPictureRecorder.h" | |
17 #include "ui/gfx/geometry/rect_conversions.h" | |
18 | |
19 namespace cc { | |
20 | |
21 scoped_refptr<PicturePileImpl> PicturePileImpl::CreateFromPicturePile( | |
22 const PicturePile* other, | |
23 bool can_use_lcd_text) { | |
24 return make_scoped_refptr(new PicturePileImpl(other, can_use_lcd_text)); | |
25 } | |
26 | |
27 PicturePileImpl::PicturePileImpl() | |
28 : background_color_(SK_ColorTRANSPARENT), | |
29 requires_clear_(true), | |
30 can_use_lcd_text_(true), | |
31 is_solid_color_(false), | |
32 solid_color_(SK_ColorTRANSPARENT), | |
33 has_any_recordings_(false), | |
34 clear_canvas_with_debug_color_(false), | |
35 min_contents_scale_(0.f), | |
36 slow_down_raster_scale_factor_for_debug_(0), | |
37 should_attempt_to_use_distance_field_text_(false) { | |
38 } | |
39 | |
40 PicturePileImpl::PicturePileImpl(const PicturePile* other, | |
41 bool can_use_lcd_text) | |
42 : picture_map_(other->picture_map_), | |
43 tiling_(other->tiling_), | |
44 background_color_(other->background_color_), | |
45 requires_clear_(other->requires_clear_), | |
46 can_use_lcd_text_(can_use_lcd_text), | |
47 is_solid_color_(other->is_solid_color_), | |
48 solid_color_(other->solid_color_), | |
49 recorded_viewport_(other->recorded_viewport_), | |
50 has_any_recordings_(other->has_any_recordings_), | |
51 clear_canvas_with_debug_color_(other->clear_canvas_with_debug_color_), | |
52 min_contents_scale_(other->min_contents_scale_), | |
53 slow_down_raster_scale_factor_for_debug_( | |
54 other->slow_down_raster_scale_factor_for_debug_), | |
55 should_attempt_to_use_distance_field_text_(false) { | |
56 } | |
57 | |
58 PicturePileImpl::PicturePileImpl(const PicturePileImpl* other, | |
59 bool can_use_lcd_text) | |
60 : picture_map_(other->picture_map_), | |
61 tiling_(other->tiling_), | |
62 background_color_(other->background_color_), | |
63 requires_clear_(other->requires_clear_), | |
64 can_use_lcd_text_(can_use_lcd_text), | |
65 is_solid_color_(other->is_solid_color_), | |
66 solid_color_(other->solid_color_), | |
67 recorded_viewport_(other->recorded_viewport_), | |
68 has_any_recordings_(other->has_any_recordings_), | |
69 clear_canvas_with_debug_color_(other->clear_canvas_with_debug_color_), | |
70 min_contents_scale_(other->min_contents_scale_), | |
71 slow_down_raster_scale_factor_for_debug_( | |
72 other->slow_down_raster_scale_factor_for_debug_), | |
73 should_attempt_to_use_distance_field_text_( | |
74 other->should_attempt_to_use_distance_field_text_) { | |
75 } | |
76 | |
77 PicturePileImpl::~PicturePileImpl() { | |
78 } | |
79 | |
80 void PicturePileImpl::PlaybackToSharedCanvas(SkCanvas* canvas, | |
81 const gfx::Rect& canvas_rect, | |
82 float contents_scale) const { | |
83 RasterCommon(canvas, NULL, canvas_rect, contents_scale); | |
84 } | |
85 | |
86 void PicturePileImpl::RasterForAnalysis(skia::AnalysisCanvas* canvas, | |
87 const gfx::Rect& canvas_rect, | |
88 float contents_scale) const { | |
89 RasterCommon(canvas, canvas, canvas_rect, contents_scale); | |
90 } | |
91 | |
92 void PicturePileImpl::PlaybackToCanvas(SkCanvas* canvas, | |
93 const gfx::Rect& canvas_rect, | |
94 float contents_scale) const { | |
95 RasterSourceHelper::PrepareForPlaybackToCanvas( | |
96 canvas, canvas_rect, gfx::Rect(tiling_.tiling_size()), contents_scale, | |
97 background_color_, clear_canvas_with_debug_color_, requires_clear_); | |
98 RasterCommon(canvas, NULL, canvas_rect, contents_scale); | |
99 } | |
100 | |
101 void PicturePileImpl::CoalesceRasters(const gfx::Rect& canvas_rect, | |
102 const gfx::Rect& content_rect, | |
103 float contents_scale, | |
104 PictureRegionMap* results) const { | |
105 DCHECK(results); | |
106 // Rasterize the collection of relevant picture piles. | |
107 gfx::Rect layer_rect = gfx::ScaleToEnclosingRect( | |
108 content_rect, 1.f / contents_scale); | |
109 | |
110 // Make sure pictures don't overlap by keeping track of previous right/bottom. | |
111 int min_content_left = -1; | |
112 int min_content_top = -1; | |
113 int last_row_index = -1; | |
114 int last_col_index = -1; | |
115 gfx::Rect last_content_rect; | |
116 | |
117 // Coalesce rasters of the same picture into different rects: | |
118 // - Compute the clip of each of the pile chunks, | |
119 // - Subtract it from the canvas rect to get difference region | |
120 // - Later, use the difference region to subtract each of the comprising | |
121 // rects from the canvas. | |
122 // Note that in essence, we're trying to mimic clipRegion with intersect op | |
123 // that also respects the current canvas transform and clip. In order to use | |
124 // the canvas transform, we must stick to clipRect operations (clipRegion | |
125 // ignores the transform). Intersect then can be written as subtracting the | |
126 // negation of the region we're trying to intersect. Luckily, we know that all | |
127 // of the rects will have to fit into |content_rect|, so we can start with | |
128 // that and subtract chunk rects to get the region that we need to subtract | |
129 // from the canvas. Then, we can use clipRect with difference op to subtract | |
130 // each rect in the region. | |
131 bool include_borders = true; | |
132 for (TilingData::Iterator tile_iter(&tiling_, layer_rect, include_borders); | |
133 tile_iter; | |
134 ++tile_iter) { | |
135 PictureMap::const_iterator map_iter = picture_map_.find(tile_iter.index()); | |
136 if (map_iter == picture_map_.end()) | |
137 continue; | |
138 const PictureInfo& info = map_iter->second; | |
139 const Picture* picture = info.GetPicture(); | |
140 if (!picture) | |
141 continue; | |
142 | |
143 // This is intentionally *enclosed* rect, so that the clip is aligned on | |
144 // integral post-scale content pixels and does not extend past the edges | |
145 // of the picture chunk's layer rect. The min_contents_scale enforces that | |
146 // enough buffer pixels have been added such that the enclosed rect | |
147 // encompasses all invalidated pixels at any larger scale level. | |
148 gfx::Rect chunk_rect = PaddedRect(tile_iter.index()); | |
149 gfx::Rect content_clip = | |
150 gfx::ScaleToEnclosedRect(chunk_rect, contents_scale); | |
151 DCHECK(!content_clip.IsEmpty()) << "Layer rect: " | |
152 << picture->LayerRect().ToString() | |
153 << "Contents scale: " << contents_scale; | |
154 content_clip.Intersect(canvas_rect); | |
155 | |
156 // Make sure iterator goes top->bottom. | |
157 DCHECK_GE(tile_iter.index_y(), last_row_index); | |
158 if (tile_iter.index_y() > last_row_index) { | |
159 // First tile in a new row. | |
160 min_content_left = content_clip.x(); | |
161 min_content_top = last_content_rect.bottom(); | |
162 } else { | |
163 // Make sure iterator goes left->right. | |
164 DCHECK_GT(tile_iter.index_x(), last_col_index); | |
165 min_content_left = last_content_rect.right(); | |
166 min_content_top = last_content_rect.y(); | |
167 } | |
168 | |
169 last_col_index = tile_iter.index_x(); | |
170 last_row_index = tile_iter.index_y(); | |
171 | |
172 // Only inset if the content_clip is less than then previous min. | |
173 int inset_left = std::max(0, min_content_left - content_clip.x()); | |
174 int inset_top = std::max(0, min_content_top - content_clip.y()); | |
175 content_clip.Inset(inset_left, inset_top, 0, 0); | |
176 | |
177 PictureRegionMap::iterator it = results->find(picture); | |
178 Region* clip_region; | |
179 if (it == results->end()) { | |
180 // The clip for a set of coalesced pictures starts out clipping the entire | |
181 // canvas. Each picture added to the set must subtract its own bounds | |
182 // from the clip region, poking a hole so that the picture is unclipped. | |
183 clip_region = &(*results)[picture]; | |
184 *clip_region = canvas_rect; | |
185 } else { | |
186 clip_region = &it->second; | |
187 } | |
188 | |
189 DCHECK(clip_region->Contains(content_clip)) | |
190 << "Content clips should not overlap."; | |
191 clip_region->Subtract(content_clip); | |
192 last_content_rect = content_clip; | |
193 } | |
194 } | |
195 | |
196 void PicturePileImpl::RasterCommon(SkCanvas* canvas, | |
197 SkDrawPictureCallback* callback, | |
198 const gfx::Rect& canvas_rect, | |
199 float contents_scale) const { | |
200 DCHECK(contents_scale >= min_contents_scale_); | |
201 | |
202 canvas->translate(-canvas_rect.x(), -canvas_rect.y()); | |
203 gfx::Rect content_tiling_rect = gfx::ToEnclosingRect( | |
204 gfx::ScaleRect(gfx::Rect(tiling_.tiling_size()), contents_scale)); | |
205 content_tiling_rect.Intersect(canvas_rect); | |
206 | |
207 canvas->clipRect(gfx::RectToSkRect(content_tiling_rect), | |
208 SkRegion::kIntersect_Op); | |
209 | |
210 PictureRegionMap picture_region_map; | |
211 CoalesceRasters( | |
212 canvas_rect, content_tiling_rect, contents_scale, &picture_region_map); | |
213 | |
214 #ifndef NDEBUG | |
215 Region total_clip; | |
216 #endif // NDEBUG | |
217 | |
218 // Iterate the coalesced map and use each picture's region | |
219 // to clip the canvas. | |
220 for (PictureRegionMap::iterator it = picture_region_map.begin(); | |
221 it != picture_region_map.end(); | |
222 ++it) { | |
223 const Picture* picture = it->first; | |
224 Region negated_clip_region = it->second; | |
225 | |
226 #ifndef NDEBUG | |
227 Region positive_clip = content_tiling_rect; | |
228 positive_clip.Subtract(negated_clip_region); | |
229 // Make sure we never rasterize the same region twice. | |
230 DCHECK(!total_clip.Intersects(positive_clip)); | |
231 total_clip.Union(positive_clip); | |
232 #endif // NDEBUG | |
233 | |
234 int repeat_count = std::max(1, slow_down_raster_scale_factor_for_debug_); | |
235 | |
236 for (int j = 0; j < repeat_count; ++j) | |
237 picture->Raster(canvas, callback, negated_clip_region, contents_scale); | |
238 } | |
239 | |
240 #ifndef NDEBUG | |
241 // Fill the clip with debug color. This allows us to | |
242 // distinguish between non painted areas and problems with missing | |
243 // pictures. | |
244 SkPaint paint; | |
245 for (Region::Iterator it(total_clip); it.has_rect(); it.next()) | |
246 canvas->clipRect(gfx::RectToSkRect(it.rect()), SkRegion::kDifference_Op); | |
247 paint.setColor(DebugColors::MissingPictureFillColor()); | |
248 paint.setXfermodeMode(SkXfermode::kSrc_Mode); | |
249 canvas->drawPaint(paint); | |
250 #endif // NDEBUG | |
251 } | |
252 | |
253 skia::RefPtr<SkPicture> PicturePileImpl::GetFlattenedPicture() { | |
254 TRACE_EVENT0("cc", "PicturePileImpl::GetFlattenedPicture"); | |
255 | |
256 gfx::Rect tiling_rect(tiling_.tiling_size()); | |
257 SkPictureRecorder recorder; | |
258 SkCanvas* canvas = | |
259 recorder.beginRecording(tiling_rect.width(), tiling_rect.height()); | |
260 if (!tiling_rect.IsEmpty()) | |
261 PlaybackToCanvas(canvas, tiling_rect, 1.0); | |
262 skia::RefPtr<SkPicture> picture = skia::AdoptRef(recorder.endRecording()); | |
263 | |
264 return picture; | |
265 } | |
266 | |
267 size_t PicturePileImpl::GetPictureMemoryUsage() const { | |
268 // Place all pictures in a set to de-dupe. | |
269 size_t total_size = 0; | |
270 std::set<const Picture*> pictures_seen; | |
271 for (const auto& map_value : picture_map_) { | |
272 const Picture* picture = map_value.second.GetPicture(); | |
273 if (picture && pictures_seen.insert(picture).second) | |
274 total_size += picture->ApproximateMemoryUsage(); | |
275 } | |
276 | |
277 return total_size; | |
278 } | |
279 | |
280 void PicturePileImpl::PerformSolidColorAnalysis( | |
281 const gfx::Rect& content_rect, | |
282 float contents_scale, | |
283 RasterSource::SolidColorAnalysis* analysis) const { | |
284 DCHECK(analysis); | |
285 TRACE_EVENT0("cc", "PicturePileImpl::PerformSolidColorAnalysis"); | |
286 | |
287 gfx::Rect layer_rect = gfx::ScaleToEnclosingRect( | |
288 content_rect, 1.0f / contents_scale); | |
289 | |
290 layer_rect.Intersect(gfx::Rect(tiling_.tiling_size())); | |
291 | |
292 skia::AnalysisCanvas canvas(layer_rect.width(), layer_rect.height()); | |
293 | |
294 RasterForAnalysis(&canvas, layer_rect, 1.0f); | |
295 | |
296 analysis->is_solid_color = canvas.GetColorIfSolid(&analysis->solid_color); | |
297 } | |
298 | |
299 void PicturePileImpl::GatherPixelRefs( | |
300 const gfx::Rect& content_rect, | |
301 float contents_scale, | |
302 std::vector<SkPixelRef*>* pixel_refs) const { | |
303 DCHECK_EQ(0u, pixel_refs->size()); | |
304 for (PixelRefIterator iter(content_rect, contents_scale, this); iter; | |
305 ++iter) { | |
306 pixel_refs->push_back(*iter); | |
307 } | |
308 } | |
309 | |
310 bool PicturePileImpl::CoversRect(const gfx::Rect& content_rect, | |
311 float contents_scale) const { | |
312 if (tiling_.tiling_size().IsEmpty()) | |
313 return false; | |
314 gfx::Rect layer_rect = | |
315 gfx::ScaleToEnclosingRect(content_rect, 1.f / contents_scale); | |
316 layer_rect.Intersect(gfx::Rect(tiling_.tiling_size())); | |
317 | |
318 // Common case inside of viewport to avoid the slower map lookups. | |
319 if (recorded_viewport_.Contains(layer_rect)) { | |
320 // Sanity check that there are no false positives in recorded_viewport_. | |
321 DCHECK(CanRasterSlowTileCheck(layer_rect)); | |
322 return true; | |
323 } | |
324 | |
325 return CanRasterSlowTileCheck(layer_rect); | |
326 } | |
327 | |
328 gfx::Size PicturePileImpl::GetSize() const { | |
329 return tiling_.tiling_size(); | |
330 } | |
331 | |
332 bool PicturePileImpl::IsSolidColor() const { | |
333 return is_solid_color_; | |
334 } | |
335 | |
336 SkColor PicturePileImpl::GetSolidColor() const { | |
337 DCHECK(IsSolidColor()); | |
338 return solid_color_; | |
339 } | |
340 | |
341 bool PicturePileImpl::HasRecordings() const { | |
342 return has_any_recordings_; | |
343 } | |
344 | |
345 gfx::Rect PicturePileImpl::PaddedRect(const PictureMapKey& key) const { | |
346 gfx::Rect padded_rect = tiling_.TileBounds(key.first, key.second); | |
347 padded_rect.Inset(-buffer_pixels(), -buffer_pixels(), -buffer_pixels(), | |
348 -buffer_pixels()); | |
349 return padded_rect; | |
350 } | |
351 | |
352 bool PicturePileImpl::CanRasterSlowTileCheck( | |
353 const gfx::Rect& layer_rect) const { | |
354 bool include_borders = false; | |
355 for (TilingData::Iterator tile_iter(&tiling_, layer_rect, include_borders); | |
356 tile_iter; ++tile_iter) { | |
357 PictureMap::const_iterator map_iter = picture_map_.find(tile_iter.index()); | |
358 if (map_iter == picture_map_.end()) | |
359 return false; | |
360 if (!map_iter->second.GetPicture()) | |
361 return false; | |
362 } | |
363 return true; | |
364 } | |
365 | |
366 void PicturePileImpl::SetShouldAttemptToUseDistanceFieldText() { | |
367 should_attempt_to_use_distance_field_text_ = true; | |
368 } | |
369 | |
370 bool PicturePileImpl::ShouldAttemptToUseDistanceFieldText() const { | |
371 return should_attempt_to_use_distance_field_text_; | |
372 } | |
373 | |
374 void PicturePileImpl::AsValueInto( | |
375 base::trace_event::TracedValue* pictures) const { | |
376 gfx::Rect tiling_rect(tiling_.tiling_size()); | |
377 std::set<const void*> appended_pictures; | |
378 bool include_borders = true; | |
379 for (TilingData::Iterator tile_iter(&tiling_, tiling_rect, include_borders); | |
380 tile_iter; ++tile_iter) { | |
381 PictureMap::const_iterator map_iter = picture_map_.find(tile_iter.index()); | |
382 if (map_iter == picture_map_.end()) | |
383 continue; | |
384 | |
385 const Picture* picture = map_iter->second.GetPicture(); | |
386 if (picture && (appended_pictures.count(picture) == 0)) { | |
387 appended_pictures.insert(picture); | |
388 TracedValue::AppendIDRef(picture, pictures); | |
389 } | |
390 } | |
391 } | |
392 | |
393 bool PicturePileImpl::CanUseLCDText() const { | |
394 return can_use_lcd_text_; | |
395 } | |
396 | |
397 scoped_refptr<RasterSource> PicturePileImpl::CreateCloneWithoutLCDText() const { | |
398 DCHECK(CanUseLCDText()); | |
399 bool can_use_lcd_text = false; | |
400 return scoped_refptr<RasterSource>( | |
401 new PicturePileImpl(this, can_use_lcd_text)); | |
402 } | |
403 | |
404 PicturePileImpl::PixelRefIterator::PixelRefIterator( | |
405 const gfx::Rect& content_rect, | |
406 float contents_scale, | |
407 const PicturePileImpl* picture_pile) | |
408 : picture_pile_(picture_pile), | |
409 layer_rect_( | |
410 gfx::ScaleToEnclosingRect(content_rect, 1.f / contents_scale)), | |
411 tile_iterator_(&picture_pile_->tiling_, | |
412 layer_rect_, | |
413 false /* include_borders */) { | |
414 // Early out if there isn't a single tile. | |
415 if (!tile_iterator_) | |
416 return; | |
417 | |
418 AdvanceToTilePictureWithPixelRefs(); | |
419 } | |
420 | |
421 PicturePileImpl::PixelRefIterator::~PixelRefIterator() { | |
422 } | |
423 | |
424 PicturePileImpl::PixelRefIterator& | |
425 PicturePileImpl::PixelRefIterator::operator++() { | |
426 ++pixel_ref_iterator_; | |
427 if (pixel_ref_iterator_) | |
428 return *this; | |
429 | |
430 ++tile_iterator_; | |
431 AdvanceToTilePictureWithPixelRefs(); | |
432 return *this; | |
433 } | |
434 | |
435 void PicturePileImpl::PixelRefIterator::AdvanceToTilePictureWithPixelRefs() { | |
436 for (; tile_iterator_; ++tile_iterator_) { | |
437 PictureMap::const_iterator it = | |
438 picture_pile_->picture_map_.find(tile_iterator_.index()); | |
439 if (it == picture_pile_->picture_map_.end()) | |
440 continue; | |
441 | |
442 const Picture* picture = it->second.GetPicture(); | |
443 if (!picture || (processed_pictures_.count(picture) != 0) || | |
444 !picture->WillPlayBackBitmaps()) | |
445 continue; | |
446 | |
447 processed_pictures_.insert(picture); | |
448 pixel_ref_iterator_ = picture->GetPixelRefMapIterator(layer_rect_); | |
449 if (pixel_ref_iterator_) | |
450 break; | |
451 } | |
452 } | |
453 | |
454 void PicturePileImpl::DidBeginTracing() { | |
455 std::set<const void*> processed_pictures; | |
456 for (const auto& map_pair : picture_map_) { | |
457 const Picture* picture = map_pair.second.GetPicture(); | |
458 if (picture && (processed_pictures.count(picture) == 0)) { | |
459 picture->EmitTraceSnapshot(); | |
460 processed_pictures.insert(picture); | |
461 } | |
462 } | |
463 } | |
464 | |
465 } // namespace cc | |
OLD | NEW |