Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / cc / playback / picture_pile_impl.cc
blob736bb917e7fd07475276e5d3af05ad54d895e56f
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.
5 #include <algorithm>
6 #include <limits>
7 #include <set>
9 #include "base/trace_event/trace_event.h"
10 #include "cc/base/region.h"
11 #include "cc/debug/debug_colors.h"
12 #include "cc/playback/picture_pile_impl.h"
13 #include "cc/playback/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"
19 namespace cc {
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));
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 picture_memory_usage_(0) {
41 PicturePileImpl::PicturePileImpl(const PicturePile* other,
42 bool can_use_lcd_text)
43 : picture_map_(other->picture_map_),
44 tiling_(other->tiling_),
45 background_color_(other->background_color_),
46 requires_clear_(other->requires_clear_),
47 can_use_lcd_text_(can_use_lcd_text),
48 is_solid_color_(other->is_solid_color_),
49 solid_color_(other->solid_color_),
50 recorded_viewport_(other->recorded_viewport_),
51 has_any_recordings_(other->has_any_recordings_),
52 clear_canvas_with_debug_color_(other->clear_canvas_with_debug_color_),
53 min_contents_scale_(other->min_contents_scale_),
54 slow_down_raster_scale_factor_for_debug_(
55 other->slow_down_raster_scale_factor_for_debug_),
56 should_attempt_to_use_distance_field_text_(false),
57 picture_memory_usage_(0) {
58 // Figure out the picture size upon construction.
59 base::hash_set<const Picture*> pictures_seen;
60 for (const auto& map_value : picture_map_) {
61 const Picture* picture = map_value.second.get();
62 if (pictures_seen.insert(picture).second)
63 picture_memory_usage_ += picture->ApproximateMemoryUsage();
67 PicturePileImpl::PicturePileImpl(const PicturePileImpl* other,
68 bool can_use_lcd_text)
69 : picture_map_(other->picture_map_),
70 tiling_(other->tiling_),
71 background_color_(other->background_color_),
72 requires_clear_(other->requires_clear_),
73 can_use_lcd_text_(can_use_lcd_text),
74 is_solid_color_(other->is_solid_color_),
75 solid_color_(other->solid_color_),
76 recorded_viewport_(other->recorded_viewport_),
77 has_any_recordings_(other->has_any_recordings_),
78 clear_canvas_with_debug_color_(other->clear_canvas_with_debug_color_),
79 min_contents_scale_(other->min_contents_scale_),
80 slow_down_raster_scale_factor_for_debug_(
81 other->slow_down_raster_scale_factor_for_debug_),
82 should_attempt_to_use_distance_field_text_(
83 other->should_attempt_to_use_distance_field_text_),
84 picture_memory_usage_(other->picture_memory_usage_) {
87 PicturePileImpl::~PicturePileImpl() {
90 void PicturePileImpl::PlaybackToSharedCanvas(SkCanvas* canvas,
91 const gfx::Rect& canvas_rect,
92 float contents_scale) const {
93 RasterCommon(canvas, NULL, canvas_rect, contents_scale);
96 void PicturePileImpl::RasterForAnalysis(skia::AnalysisCanvas* canvas,
97 const gfx::Rect& canvas_rect,
98 float contents_scale) const {
99 RasterCommon(canvas, canvas, canvas_rect, contents_scale);
102 void PicturePileImpl::PlaybackToCanvas(SkCanvas* canvas,
103 const gfx::Rect& canvas_bitmap_rect,
104 const gfx::Rect& canvas_playback_rect,
105 float contents_scale) const {
106 RasterSourceHelper::PrepareForPlaybackToCanvas(
107 canvas, canvas_bitmap_rect, canvas_bitmap_rect,
108 gfx::Rect(tiling_.tiling_size()), contents_scale, background_color_,
109 clear_canvas_with_debug_color_, requires_clear_);
110 RasterCommon(canvas, NULL, canvas_bitmap_rect, contents_scale);
113 void PicturePileImpl::CoalesceRasters(const gfx::Rect& canvas_rect,
114 const gfx::Rect& content_rect,
115 float contents_scale,
116 PictureRegionMap* results) const {
117 DCHECK(results);
118 // Rasterize the collection of relevant picture piles.
119 gfx::Rect layer_rect = gfx::ScaleToEnclosingRect(
120 content_rect, 1.f / contents_scale);
122 // Make sure pictures don't overlap by keeping track of previous right/bottom.
123 int min_content_left = -1;
124 int min_content_top = -1;
125 int last_row_index = -1;
126 int last_col_index = -1;
127 gfx::Rect last_content_rect;
129 // Coalesce rasters of the same picture into different rects:
130 // - Compute the clip of each of the pile chunks,
131 // - Subtract it from the canvas rect to get difference region
132 // - Later, use the difference region to subtract each of the comprising
133 // rects from the canvas.
134 // Note that in essence, we're trying to mimic clipRegion with intersect op
135 // that also respects the current canvas transform and clip. In order to use
136 // the canvas transform, we must stick to clipRect operations (clipRegion
137 // ignores the transform). Intersect then can be written as subtracting the
138 // negation of the region we're trying to intersect. Luckily, we know that all
139 // of the rects will have to fit into |content_rect|, so we can start with
140 // that and subtract chunk rects to get the region that we need to subtract
141 // from the canvas. Then, we can use clipRect with difference op to subtract
142 // each rect in the region.
143 bool include_borders = true;
144 for (TilingData::Iterator tile_iter(&tiling_, layer_rect, include_borders);
145 tile_iter;
146 ++tile_iter) {
147 PictureMap::const_iterator map_iter = picture_map_.find(tile_iter.index());
148 if (map_iter == picture_map_.end())
149 continue;
150 const Picture* picture = map_iter->second.get();
151 DCHECK(picture);
153 // This is intentionally *enclosed* rect, so that the clip is aligned on
154 // integral post-scale content pixels and does not extend past the edges
155 // of the picture chunk's layer rect. The min_contents_scale enforces that
156 // enough buffer pixels have been added such that the enclosed rect
157 // encompasses all invalidated pixels at any larger scale level.
158 gfx::Rect chunk_rect = PaddedRect(tile_iter.index());
159 gfx::Rect content_clip =
160 gfx::ScaleToEnclosedRect(chunk_rect, contents_scale);
161 DCHECK(!content_clip.IsEmpty()) << "Layer rect: "
162 << picture->LayerRect().ToString()
163 << "Contents scale: " << contents_scale;
164 content_clip.Intersect(canvas_rect);
166 // Make sure iterator goes top->bottom.
167 DCHECK_GE(tile_iter.index_y(), last_row_index);
168 if (tile_iter.index_y() > last_row_index) {
169 // First tile in a new row.
170 min_content_left = content_clip.x();
171 min_content_top = last_content_rect.bottom();
172 } else {
173 // Make sure iterator goes left->right.
174 DCHECK_GT(tile_iter.index_x(), last_col_index);
175 min_content_left = last_content_rect.right();
176 min_content_top = last_content_rect.y();
179 last_col_index = tile_iter.index_x();
180 last_row_index = tile_iter.index_y();
182 // Only inset if the content_clip is less than then previous min.
183 int inset_left = std::max(0, min_content_left - content_clip.x());
184 int inset_top = std::max(0, min_content_top - content_clip.y());
185 content_clip.Inset(inset_left, inset_top, 0, 0);
187 PictureRegionMap::iterator it = results->find(picture);
188 Region* clip_region;
189 if (it == results->end()) {
190 // The clip for a set of coalesced pictures starts out clipping the entire
191 // canvas. Each picture added to the set must subtract its own bounds
192 // from the clip region, poking a hole so that the picture is unclipped.
193 clip_region = &(*results)[picture];
194 *clip_region = canvas_rect;
195 } else {
196 clip_region = &it->second;
199 DCHECK(clip_region->Contains(content_clip))
200 << "Content clips should not overlap.";
201 clip_region->Subtract(content_clip);
202 last_content_rect = content_clip;
206 void PicturePileImpl::RasterCommon(SkCanvas* canvas,
207 SkPicture::AbortCallback* callback,
208 const gfx::Rect& canvas_rect,
209 float contents_scale) const {
210 DCHECK(contents_scale >= min_contents_scale_);
212 canvas->translate(-canvas_rect.x(), -canvas_rect.y());
213 gfx::Rect content_tiling_rect = gfx::ScaleToEnclosingRect(
214 gfx::Rect(tiling_.tiling_size()), contents_scale);
215 content_tiling_rect.Intersect(canvas_rect);
217 canvas->clipRect(gfx::RectToSkRect(content_tiling_rect),
218 SkRegion::kIntersect_Op);
220 PictureRegionMap picture_region_map;
221 CoalesceRasters(
222 canvas_rect, content_tiling_rect, contents_scale, &picture_region_map);
224 #ifndef NDEBUG
225 Region total_clip;
226 #endif // NDEBUG
228 // Iterate the coalesced map and use each picture's region
229 // to clip the canvas.
230 for (PictureRegionMap::iterator it = picture_region_map.begin();
231 it != picture_region_map.end();
232 ++it) {
233 const Picture* picture = it->first;
234 Region negated_clip_region = it->second;
236 #ifndef NDEBUG
237 Region positive_clip = content_tiling_rect;
238 positive_clip.Subtract(negated_clip_region);
239 // Make sure we never rasterize the same region twice.
240 DCHECK(!total_clip.Intersects(positive_clip));
241 total_clip.Union(positive_clip);
242 #endif // NDEBUG
244 int repeat_count = std::max(1, slow_down_raster_scale_factor_for_debug_);
246 for (int i = 0; i < repeat_count; ++i)
247 picture->Raster(canvas, callback, negated_clip_region, contents_scale);
250 #ifndef NDEBUG
251 // Fill the clip with debug color. This allows us to
252 // distinguish between non painted areas and problems with missing
253 // pictures.
254 SkPaint paint;
255 for (Region::Iterator it(total_clip); it.has_rect(); it.next())
256 canvas->clipRect(gfx::RectToSkRect(it.rect()), SkRegion::kDifference_Op);
257 paint.setColor(DebugColors::MissingPictureFillColor());
258 paint.setXfermodeMode(SkXfermode::kSrc_Mode);
259 canvas->drawPaint(paint);
260 #endif // NDEBUG
263 skia::RefPtr<SkPicture> PicturePileImpl::GetFlattenedPicture() {
264 TRACE_EVENT0("cc", "PicturePileImpl::GetFlattenedPicture");
266 gfx::Rect tiling_rect(tiling_.tiling_size());
267 SkPictureRecorder recorder;
268 SkCanvas* canvas =
269 recorder.beginRecording(tiling_rect.width(), tiling_rect.height());
270 if (!tiling_rect.IsEmpty())
271 PlaybackToCanvas(canvas, tiling_rect, tiling_rect, 1.0);
272 skia::RefPtr<SkPicture> picture =
273 skia::AdoptRef(recorder.endRecordingAsPicture());
275 return picture;
278 size_t PicturePileImpl::GetPictureMemoryUsage() const {
279 return picture_memory_usage_;
282 void PicturePileImpl::PerformSolidColorAnalysis(
283 const gfx::Rect& content_rect,
284 float contents_scale,
285 RasterSource::SolidColorAnalysis* analysis) const {
286 DCHECK(analysis);
287 TRACE_EVENT0("cc", "PicturePileImpl::PerformSolidColorAnalysis");
289 gfx::Rect layer_rect = gfx::ScaleToEnclosingRect(
290 content_rect, 1.0f / contents_scale);
292 layer_rect.Intersect(gfx::Rect(tiling_.tiling_size()));
294 skia::AnalysisCanvas canvas(layer_rect.width(), layer_rect.height());
296 RasterForAnalysis(&canvas, layer_rect, 1.0f);
298 analysis->is_solid_color = canvas.GetColorIfSolid(&analysis->solid_color);
301 void PicturePileImpl::GatherDiscardableImages(
302 const gfx::Rect& layer_rect,
303 std::vector<skia::PositionImage>* images) const {
304 DCHECK_EQ(0u, images->size());
305 for (ImageIterator iter(layer_rect, this); iter; ++iter) {
306 images->push_back(*iter);
310 bool PicturePileImpl::CoversRect(const gfx::Rect& layer_rect) const {
311 if (tiling_.tiling_size().IsEmpty())
312 return false;
313 gfx::Rect bounded_rect = layer_rect;
314 bounded_rect.Intersect(gfx::Rect(tiling_.tiling_size()));
316 // Common case inside of viewport to avoid the slower map lookups.
317 if (recorded_viewport_.Contains(bounded_rect)) {
318 // Sanity check that there are no false positives in recorded_viewport_.
319 DCHECK(CanRasterSlowTileCheck(bounded_rect));
320 return true;
323 return CanRasterSlowTileCheck(bounded_rect);
326 gfx::Size PicturePileImpl::GetSize() const {
327 return tiling_.tiling_size();
330 bool PicturePileImpl::IsSolidColor() const {
331 return is_solid_color_;
334 SkColor PicturePileImpl::GetSolidColor() const {
335 DCHECK(IsSolidColor());
336 return solid_color_;
339 bool PicturePileImpl::HasRecordings() const {
340 return has_any_recordings_;
343 gfx::Rect PicturePileImpl::RecordedViewport() const {
344 return recorded_viewport_;
347 gfx::Rect PicturePileImpl::PaddedRect(const PictureMapKey& key) const {
348 gfx::Rect padded_rect = tiling_.TileBounds(key.first, key.second);
349 padded_rect.Inset(-buffer_pixels(), -buffer_pixels(), -buffer_pixels(),
350 -buffer_pixels());
351 return padded_rect;
354 bool PicturePileImpl::CanRasterSlowTileCheck(
355 const gfx::Rect& layer_rect) const {
356 bool include_borders = false;
357 for (TilingData::Iterator tile_iter(&tiling_, layer_rect, include_borders);
358 tile_iter; ++tile_iter) {
359 PictureMap::const_iterator map_iter = picture_map_.find(tile_iter.index());
360 if (map_iter == picture_map_.end())
361 return false;
363 return true;
366 void PicturePileImpl::SetShouldAttemptToUseDistanceFieldText() {
367 should_attempt_to_use_distance_field_text_ = true;
370 bool PicturePileImpl::ShouldAttemptToUseDistanceFieldText() const {
371 return should_attempt_to_use_distance_field_text_;
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;
385 const Picture* picture = map_iter->second.get();
386 if (appended_pictures.count(picture) == 0) {
387 appended_pictures.insert(picture);
388 TracedValue::AppendIDRef(picture, pictures);
393 bool PicturePileImpl::CanUseLCDText() const {
394 return can_use_lcd_text_;
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));
404 PicturePileImpl::ImageIterator::ImageIterator(
405 const gfx::Rect& layer_rect,
406 const PicturePileImpl* picture_pile)
407 : picture_pile_(picture_pile),
408 layer_rect_(layer_rect),
409 tile_iterator_(&picture_pile_->tiling_,
410 layer_rect_,
411 false /* include_borders */) {
412 // Early out if there isn't a single tile.
413 if (!tile_iterator_)
414 return;
416 AdvanceToTilePictureWithImages();
419 PicturePileImpl::ImageIterator::~ImageIterator() {}
421 PicturePileImpl::ImageIterator& PicturePileImpl::ImageIterator::operator++() {
422 ++image_iterator_;
423 if (image_iterator_)
424 return *this;
426 ++tile_iterator_;
427 AdvanceToTilePictureWithImages();
428 return *this;
431 void PicturePileImpl::ImageIterator::AdvanceToTilePictureWithImages() {
432 for (; tile_iterator_; ++tile_iterator_) {
433 PictureMap::const_iterator it =
434 picture_pile_->picture_map_.find(tile_iterator_.index());
435 if (it == picture_pile_->picture_map_.end())
436 continue;
438 const Picture* picture = it->second.get();
439 if ((processed_pictures_.count(picture) != 0) ||
440 !picture->WillPlayBackBitmaps())
441 continue;
443 processed_pictures_.insert(picture);
444 image_iterator_ = picture->GetDiscardableImageMapIterator(layer_rect_);
445 if (image_iterator_)
446 break;
450 void PicturePileImpl::DidBeginTracing() {
451 std::set<const void*> processed_pictures;
452 for (const auto& map_pair : picture_map_) {
453 const Picture* picture = map_pair.second.get();
454 if (processed_pictures.count(picture) == 0) {
455 picture->EmitTraceSnapshot();
456 processed_pictures.insert(picture);
461 } // namespace cc