Don't show supervised user as "already on this device" while they're being imported.
[chromium-blink-merge.git] / cc / playback / picture_pile_impl.cc
blobf7d35be8532f0d95d47872417bb76f7f7404ca6c
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_rect,
104 float contents_scale) const {
105 RasterSourceHelper::PrepareForPlaybackToCanvas(
106 canvas, canvas_rect, gfx::Rect(tiling_.tiling_size()), contents_scale,
107 background_color_, clear_canvas_with_debug_color_, requires_clear_);
108 RasterCommon(canvas, NULL, canvas_rect, contents_scale);
111 void PicturePileImpl::CoalesceRasters(const gfx::Rect& canvas_rect,
112 const gfx::Rect& content_rect,
113 float contents_scale,
114 PictureRegionMap* results) const {
115 DCHECK(results);
116 // Rasterize the collection of relevant picture piles.
117 gfx::Rect layer_rect = gfx::ScaleToEnclosingRect(
118 content_rect, 1.f / contents_scale);
120 // Make sure pictures don't overlap by keeping track of previous right/bottom.
121 int min_content_left = -1;
122 int min_content_top = -1;
123 int last_row_index = -1;
124 int last_col_index = -1;
125 gfx::Rect last_content_rect;
127 // Coalesce rasters of the same picture into different rects:
128 // - Compute the clip of each of the pile chunks,
129 // - Subtract it from the canvas rect to get difference region
130 // - Later, use the difference region to subtract each of the comprising
131 // rects from the canvas.
132 // Note that in essence, we're trying to mimic clipRegion with intersect op
133 // that also respects the current canvas transform and clip. In order to use
134 // the canvas transform, we must stick to clipRect operations (clipRegion
135 // ignores the transform). Intersect then can be written as subtracting the
136 // negation of the region we're trying to intersect. Luckily, we know that all
137 // of the rects will have to fit into |content_rect|, so we can start with
138 // that and subtract chunk rects to get the region that we need to subtract
139 // from the canvas. Then, we can use clipRect with difference op to subtract
140 // each rect in the region.
141 bool include_borders = true;
142 for (TilingData::Iterator tile_iter(&tiling_, layer_rect, include_borders);
143 tile_iter;
144 ++tile_iter) {
145 PictureMap::const_iterator map_iter = picture_map_.find(tile_iter.index());
146 if (map_iter == picture_map_.end())
147 continue;
148 const Picture* picture = map_iter->second.get();
149 DCHECK(picture);
151 // This is intentionally *enclosed* rect, so that the clip is aligned on
152 // integral post-scale content pixels and does not extend past the edges
153 // of the picture chunk's layer rect. The min_contents_scale enforces that
154 // enough buffer pixels have been added such that the enclosed rect
155 // encompasses all invalidated pixels at any larger scale level.
156 gfx::Rect chunk_rect = PaddedRect(tile_iter.index());
157 gfx::Rect content_clip =
158 gfx::ScaleToEnclosedRect(chunk_rect, contents_scale);
159 DCHECK(!content_clip.IsEmpty()) << "Layer rect: "
160 << picture->LayerRect().ToString()
161 << "Contents scale: " << contents_scale;
162 content_clip.Intersect(canvas_rect);
164 // Make sure iterator goes top->bottom.
165 DCHECK_GE(tile_iter.index_y(), last_row_index);
166 if (tile_iter.index_y() > last_row_index) {
167 // First tile in a new row.
168 min_content_left = content_clip.x();
169 min_content_top = last_content_rect.bottom();
170 } else {
171 // Make sure iterator goes left->right.
172 DCHECK_GT(tile_iter.index_x(), last_col_index);
173 min_content_left = last_content_rect.right();
174 min_content_top = last_content_rect.y();
177 last_col_index = tile_iter.index_x();
178 last_row_index = tile_iter.index_y();
180 // Only inset if the content_clip is less than then previous min.
181 int inset_left = std::max(0, min_content_left - content_clip.x());
182 int inset_top = std::max(0, min_content_top - content_clip.y());
183 content_clip.Inset(inset_left, inset_top, 0, 0);
185 PictureRegionMap::iterator it = results->find(picture);
186 Region* clip_region;
187 if (it == results->end()) {
188 // The clip for a set of coalesced pictures starts out clipping the entire
189 // canvas. Each picture added to the set must subtract its own bounds
190 // from the clip region, poking a hole so that the picture is unclipped.
191 clip_region = &(*results)[picture];
192 *clip_region = canvas_rect;
193 } else {
194 clip_region = &it->second;
197 DCHECK(clip_region->Contains(content_clip))
198 << "Content clips should not overlap.";
199 clip_region->Subtract(content_clip);
200 last_content_rect = content_clip;
204 void PicturePileImpl::RasterCommon(SkCanvas* canvas,
205 SkPicture::AbortCallback* callback,
206 const gfx::Rect& canvas_rect,
207 float contents_scale) const {
208 DCHECK(contents_scale >= min_contents_scale_);
210 canvas->translate(-canvas_rect.x(), -canvas_rect.y());
211 gfx::Rect content_tiling_rect = gfx::ToEnclosingRect(
212 gfx::ScaleRect(gfx::Rect(tiling_.tiling_size()), contents_scale));
213 content_tiling_rect.Intersect(canvas_rect);
215 canvas->clipRect(gfx::RectToSkRect(content_tiling_rect),
216 SkRegion::kIntersect_Op);
218 PictureRegionMap picture_region_map;
219 CoalesceRasters(
220 canvas_rect, content_tiling_rect, contents_scale, &picture_region_map);
222 #ifndef NDEBUG
223 Region total_clip;
224 #endif // NDEBUG
226 // Iterate the coalesced map and use each picture's region
227 // to clip the canvas.
228 for (PictureRegionMap::iterator it = picture_region_map.begin();
229 it != picture_region_map.end();
230 ++it) {
231 const Picture* picture = it->first;
232 Region negated_clip_region = it->second;
234 #ifndef NDEBUG
235 Region positive_clip = content_tiling_rect;
236 positive_clip.Subtract(negated_clip_region);
237 // Make sure we never rasterize the same region twice.
238 DCHECK(!total_clip.Intersects(positive_clip));
239 total_clip.Union(positive_clip);
240 #endif // NDEBUG
242 int repeat_count = std::max(1, slow_down_raster_scale_factor_for_debug_);
244 for (int j = 0; j < repeat_count; ++j)
245 picture->Raster(canvas, callback, negated_clip_region, contents_scale);
248 #ifndef NDEBUG
249 // Fill the clip with debug color. This allows us to
250 // distinguish between non painted areas and problems with missing
251 // pictures.
252 SkPaint paint;
253 for (Region::Iterator it(total_clip); it.has_rect(); it.next())
254 canvas->clipRect(gfx::RectToSkRect(it.rect()), SkRegion::kDifference_Op);
255 paint.setColor(DebugColors::MissingPictureFillColor());
256 paint.setXfermodeMode(SkXfermode::kSrc_Mode);
257 canvas->drawPaint(paint);
258 #endif // NDEBUG
261 skia::RefPtr<SkPicture> PicturePileImpl::GetFlattenedPicture() {
262 TRACE_EVENT0("cc", "PicturePileImpl::GetFlattenedPicture");
264 gfx::Rect tiling_rect(tiling_.tiling_size());
265 SkPictureRecorder recorder;
266 SkCanvas* canvas =
267 recorder.beginRecording(tiling_rect.width(), tiling_rect.height());
268 if (!tiling_rect.IsEmpty())
269 PlaybackToCanvas(canvas, tiling_rect, 1.0);
270 skia::RefPtr<SkPicture> picture =
271 skia::AdoptRef(recorder.endRecordingAsPicture());
273 return picture;
276 size_t PicturePileImpl::GetPictureMemoryUsage() const {
277 return picture_memory_usage_;
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");
287 gfx::Rect layer_rect = gfx::ScaleToEnclosingRect(
288 content_rect, 1.0f / contents_scale);
290 layer_rect.Intersect(gfx::Rect(tiling_.tiling_size()));
292 skia::AnalysisCanvas canvas(layer_rect.width(), layer_rect.height());
294 RasterForAnalysis(&canvas, layer_rect, 1.0f);
296 analysis->is_solid_color = canvas.GetColorIfSolid(&analysis->solid_color);
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);
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()));
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;
325 return CanRasterSlowTileCheck(layer_rect);
328 gfx::Size PicturePileImpl::GetSize() const {
329 return tiling_.tiling_size();
332 bool PicturePileImpl::IsSolidColor() const {
333 return is_solid_color_;
336 SkColor PicturePileImpl::GetSolidColor() const {
337 DCHECK(IsSolidColor());
338 return solid_color_;
341 bool PicturePileImpl::HasRecordings() const {
342 return has_any_recordings_;
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;
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;
361 return true;
364 void PicturePileImpl::SetShouldAttemptToUseDistanceFieldText() {
365 should_attempt_to_use_distance_field_text_ = true;
368 bool PicturePileImpl::ShouldAttemptToUseDistanceFieldText() const {
369 return should_attempt_to_use_distance_field_text_;
372 void PicturePileImpl::AsValueInto(
373 base::trace_event::TracedValue* pictures) const {
374 gfx::Rect tiling_rect(tiling_.tiling_size());
375 std::set<const void*> appended_pictures;
376 bool include_borders = true;
377 for (TilingData::Iterator tile_iter(&tiling_, tiling_rect, include_borders);
378 tile_iter; ++tile_iter) {
379 PictureMap::const_iterator map_iter = picture_map_.find(tile_iter.index());
380 if (map_iter == picture_map_.end())
381 continue;
383 const Picture* picture = map_iter->second.get();
384 if (appended_pictures.count(picture) == 0) {
385 appended_pictures.insert(picture);
386 TracedValue::AppendIDRef(picture, pictures);
391 bool PicturePileImpl::CanUseLCDText() const {
392 return can_use_lcd_text_;
395 scoped_refptr<RasterSource> PicturePileImpl::CreateCloneWithoutLCDText() const {
396 DCHECK(CanUseLCDText());
397 bool can_use_lcd_text = false;
398 return scoped_refptr<RasterSource>(
399 new PicturePileImpl(this, can_use_lcd_text));
402 PicturePileImpl::PixelRefIterator::PixelRefIterator(
403 const gfx::Rect& content_rect,
404 float contents_scale,
405 const PicturePileImpl* picture_pile)
406 : picture_pile_(picture_pile),
407 layer_rect_(
408 gfx::ScaleToEnclosingRect(content_rect, 1.f / contents_scale)),
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 AdvanceToTilePictureWithPixelRefs();
419 PicturePileImpl::PixelRefIterator::~PixelRefIterator() {
422 PicturePileImpl::PixelRefIterator&
423 PicturePileImpl::PixelRefIterator::operator++() {
424 ++pixel_ref_iterator_;
425 if (pixel_ref_iterator_)
426 return *this;
428 ++tile_iterator_;
429 AdvanceToTilePictureWithPixelRefs();
430 return *this;
433 void PicturePileImpl::PixelRefIterator::AdvanceToTilePictureWithPixelRefs() {
434 for (; tile_iterator_; ++tile_iterator_) {
435 PictureMap::const_iterator it =
436 picture_pile_->picture_map_.find(tile_iterator_.index());
437 if (it == picture_pile_->picture_map_.end())
438 continue;
440 const Picture* picture = it->second.get();
441 if ((processed_pictures_.count(picture) != 0) ||
442 !picture->WillPlayBackBitmaps())
443 continue;
445 processed_pictures_.insert(picture);
446 pixel_ref_iterator_ = picture->GetPixelRefMapIterator(layer_rect_);
447 if (pixel_ref_iterator_)
448 break;
452 void PicturePileImpl::DidBeginTracing() {
453 std::set<const void*> processed_pictures;
454 for (const auto& map_pair : picture_map_) {
455 const Picture* picture = map_pair.second.get();
456 if (processed_pictures.count(picture) == 0) {
457 picture->EmitTraceSnapshot();
458 processed_pictures.insert(picture);
463 } // namespace cc