Remove the JS key log summary, since it's of limited value without the "code" field...
[chromium-blink-merge.git] / cc / resources / picture_pile_impl.cc
blob01c069b14d97d81214f75191d9e2ff133e5d53fb
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/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"
19 namespace {
21 #ifdef NDEBUG
22 const bool kDefaultClearCanvasSetting = false;
23 #else
24 const bool kDefaultClearCanvasSetting = true;
25 #endif
27 } // namespace
29 namespace cc {
31 scoped_refptr<PicturePileImpl> PicturePileImpl::CreateFromPicturePile(
32 const PicturePile* other) {
33 return make_scoped_refptr(new PicturePileImpl(other));
36 PicturePileImpl::PicturePileImpl()
37 : background_color_(SK_ColorTRANSPARENT),
38 requires_clear_(true),
39 can_use_lcd_text_(false),
40 is_solid_color_(false),
41 solid_color_(SK_ColorTRANSPARENT),
42 has_any_recordings_(false),
43 clear_canvas_with_debug_color_(kDefaultClearCanvasSetting),
44 min_contents_scale_(0.f),
45 slow_down_raster_scale_factor_for_debug_(0),
46 should_attempt_to_use_distance_field_text_(false) {
49 PicturePileImpl::PicturePileImpl(const PicturePile* other)
50 : picture_map_(other->picture_map_),
51 tiling_(other->tiling_),
52 background_color_(SK_ColorTRANSPARENT),
53 requires_clear_(true),
54 can_use_lcd_text_(other->can_use_lcd_text_),
55 is_solid_color_(other->is_solid_color_),
56 solid_color_(other->solid_color_),
57 recorded_viewport_(other->recorded_viewport_),
58 has_any_recordings_(other->has_any_recordings_),
59 clear_canvas_with_debug_color_(kDefaultClearCanvasSetting),
60 min_contents_scale_(other->min_contents_scale_),
61 slow_down_raster_scale_factor_for_debug_(
62 other->slow_down_raster_scale_factor_for_debug_),
63 should_attempt_to_use_distance_field_text_(false) {
66 PicturePileImpl::~PicturePileImpl() {
69 void PicturePileImpl::PlaybackToSharedCanvas(SkCanvas* canvas,
70 const gfx::Rect& canvas_rect,
71 float contents_scale) const {
72 RasterCommon(canvas,
73 NULL,
74 canvas_rect,
75 contents_scale,
76 false);
79 void PicturePileImpl::RasterForAnalysis(skia::AnalysisCanvas* canvas,
80 const gfx::Rect& canvas_rect,
81 float contents_scale) const {
82 RasterCommon(canvas, canvas, canvas_rect, contents_scale, true);
85 void PicturePileImpl::PlaybackToCanvas(SkCanvas* canvas,
86 const gfx::Rect& canvas_rect,
87 float contents_scale) const {
88 RasterSourceHelper::PrepareForPlaybackToCanvas(
89 canvas, canvas_rect, gfx::Rect(tiling_.tiling_size()), contents_scale,
90 background_color_, clear_canvas_with_debug_color_, requires_clear_);
92 RasterCommon(canvas,
93 NULL,
94 canvas_rect,
95 contents_scale,
96 false);
99 void PicturePileImpl::CoalesceRasters(const gfx::Rect& canvas_rect,
100 const gfx::Rect& content_rect,
101 float contents_scale,
102 PictureRegionMap* results) const {
103 DCHECK(results);
104 // Rasterize the collection of relevant picture piles.
105 gfx::Rect layer_rect = gfx::ScaleToEnclosingRect(
106 content_rect, 1.f / contents_scale);
108 // Make sure pictures don't overlap by keeping track of previous right/bottom.
109 int min_content_left = -1;
110 int min_content_top = -1;
111 int last_row_index = -1;
112 int last_col_index = -1;
113 gfx::Rect last_content_rect;
115 // Coalesce rasters of the same picture into different rects:
116 // - Compute the clip of each of the pile chunks,
117 // - Subtract it from the canvas rect to get difference region
118 // - Later, use the difference region to subtract each of the comprising
119 // rects from the canvas.
120 // Note that in essence, we're trying to mimic clipRegion with intersect op
121 // that also respects the current canvas transform and clip. In order to use
122 // the canvas transform, we must stick to clipRect operations (clipRegion
123 // ignores the transform). Intersect then can be written as subtracting the
124 // negation of the region we're trying to intersect. Luckily, we know that all
125 // of the rects will have to fit into |content_rect|, so we can start with
126 // that and subtract chunk rects to get the region that we need to subtract
127 // from the canvas. Then, we can use clipRect with difference op to subtract
128 // each rect in the region.
129 bool include_borders = true;
130 for (TilingData::Iterator tile_iter(&tiling_, layer_rect, include_borders);
131 tile_iter;
132 ++tile_iter) {
133 PictureMap::const_iterator map_iter = picture_map_.find(tile_iter.index());
134 if (map_iter == picture_map_.end())
135 continue;
136 const PictureInfo& info = map_iter->second;
137 const Picture* picture = info.GetPicture();
138 if (!picture)
139 continue;
141 // This is intentionally *enclosed* rect, so that the clip is aligned on
142 // integral post-scale content pixels and does not extend past the edges
143 // of the picture chunk's layer rect. The min_contents_scale enforces that
144 // enough buffer pixels have been added such that the enclosed rect
145 // encompasses all invalidated pixels at any larger scale level.
146 gfx::Rect chunk_rect = PaddedRect(tile_iter.index());
147 gfx::Rect content_clip =
148 gfx::ScaleToEnclosedRect(chunk_rect, contents_scale);
149 DCHECK(!content_clip.IsEmpty()) << "Layer rect: "
150 << picture->LayerRect().ToString()
151 << "Contents scale: " << contents_scale;
152 content_clip.Intersect(canvas_rect);
154 // Make sure iterator goes top->bottom.
155 DCHECK_GE(tile_iter.index_y(), last_row_index);
156 if (tile_iter.index_y() > last_row_index) {
157 // First tile in a new row.
158 min_content_left = content_clip.x();
159 min_content_top = last_content_rect.bottom();
160 } else {
161 // Make sure iterator goes left->right.
162 DCHECK_GT(tile_iter.index_x(), last_col_index);
163 min_content_left = last_content_rect.right();
164 min_content_top = last_content_rect.y();
167 last_col_index = tile_iter.index_x();
168 last_row_index = tile_iter.index_y();
170 // Only inset if the content_clip is less than then previous min.
171 int inset_left = std::max(0, min_content_left - content_clip.x());
172 int inset_top = std::max(0, min_content_top - content_clip.y());
173 content_clip.Inset(inset_left, inset_top, 0, 0);
175 PictureRegionMap::iterator it = results->find(picture);
176 Region* clip_region;
177 if (it == results->end()) {
178 // The clip for a set of coalesced pictures starts out clipping the entire
179 // canvas. Each picture added to the set must subtract its own bounds
180 // from the clip region, poking a hole so that the picture is unclipped.
181 clip_region = &(*results)[picture];
182 *clip_region = canvas_rect;
183 } else {
184 clip_region = &it->second;
187 DCHECK(clip_region->Contains(content_clip))
188 << "Content clips should not overlap.";
189 clip_region->Subtract(content_clip);
190 last_content_rect = content_clip;
194 void PicturePileImpl::RasterCommon(
195 SkCanvas* canvas,
196 SkDrawPictureCallback* callback,
197 const gfx::Rect& canvas_rect,
198 float contents_scale,
199 bool is_analysis) const {
200 DCHECK(contents_scale >= min_contents_scale_);
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);
207 canvas->clipRect(gfx::RectToSkRect(content_tiling_rect),
208 SkRegion::kIntersect_Op);
210 PictureRegionMap picture_region_map;
211 CoalesceRasters(
212 canvas_rect, content_tiling_rect, contents_scale, &picture_region_map);
214 #ifndef NDEBUG
215 Region total_clip;
216 #endif // NDEBUG
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;
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
234 int repeat_count = std::max(1, slow_down_raster_scale_factor_for_debug_);
236 for (int j = 0; j < repeat_count; ++j)
237 picture->Raster(canvas, callback, negated_clip_region, contents_scale);
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
253 skia::RefPtr<SkPicture> PicturePileImpl::GetFlattenedPicture() {
254 TRACE_EVENT0("cc", "PicturePileImpl::GetFlattenedPicture");
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());
264 return picture;
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();
277 return total_size;
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;
360 if (!map_iter->second.GetPicture())
361 return false;
363 return true;
366 void PicturePileImpl::SetShouldAttemptToUseDistanceFieldText() {
367 should_attempt_to_use_distance_field_text_ = true;
370 void PicturePileImpl::SetBackgoundColor(SkColor background_color) {
371 background_color_ = background_color;
374 void PicturePileImpl::SetRequiresClear(bool requires_clear) {
375 requires_clear_ = requires_clear;
378 bool PicturePileImpl::ShouldAttemptToUseDistanceFieldText() const {
379 return should_attempt_to_use_distance_field_text_;
382 void PicturePileImpl::AsValueInto(
383 base::trace_event::TracedValue* pictures) const {
384 gfx::Rect tiling_rect(tiling_.tiling_size());
385 std::set<const void*> appended_pictures;
386 bool include_borders = true;
387 for (TilingData::Iterator tile_iter(&tiling_, tiling_rect, include_borders);
388 tile_iter; ++tile_iter) {
389 PictureMap::const_iterator map_iter = picture_map_.find(tile_iter.index());
390 if (map_iter == picture_map_.end())
391 continue;
393 const Picture* picture = map_iter->second.GetPicture();
394 if (picture && (appended_pictures.count(picture) == 0)) {
395 appended_pictures.insert(picture);
396 TracedValue::AppendIDRef(picture, pictures);
401 bool PicturePileImpl::CanUseLCDText() const {
402 return can_use_lcd_text_;
405 PicturePileImpl::PixelRefIterator::PixelRefIterator(
406 const gfx::Rect& content_rect,
407 float contents_scale,
408 const PicturePileImpl* picture_pile)
409 : picture_pile_(picture_pile),
410 layer_rect_(
411 gfx::ScaleToEnclosingRect(content_rect, 1.f / contents_scale)),
412 tile_iterator_(&picture_pile_->tiling_,
413 layer_rect_,
414 false /* include_borders */) {
415 // Early out if there isn't a single tile.
416 if (!tile_iterator_)
417 return;
419 AdvanceToTilePictureWithPixelRefs();
422 PicturePileImpl::PixelRefIterator::~PixelRefIterator() {
425 PicturePileImpl::PixelRefIterator&
426 PicturePileImpl::PixelRefIterator::operator++() {
427 ++pixel_ref_iterator_;
428 if (pixel_ref_iterator_)
429 return *this;
431 ++tile_iterator_;
432 AdvanceToTilePictureWithPixelRefs();
433 return *this;
436 void PicturePileImpl::PixelRefIterator::AdvanceToTilePictureWithPixelRefs() {
437 for (; tile_iterator_; ++tile_iterator_) {
438 PictureMap::const_iterator it =
439 picture_pile_->picture_map_.find(tile_iterator_.index());
440 if (it == picture_pile_->picture_map_.end())
441 continue;
443 const Picture* picture = it->second.GetPicture();
444 if (!picture || (processed_pictures_.count(picture) != 0) ||
445 !picture->WillPlayBackBitmaps())
446 continue;
448 processed_pictures_.insert(picture);
449 pixel_ref_iterator_ = Picture::PixelRefIterator(layer_rect_, picture);
450 if (pixel_ref_iterator_)
451 break;
455 void PicturePileImpl::DidBeginTracing() {
456 std::set<const void*> processed_pictures;
457 for (PictureMap::iterator it = picture_map_.begin();
458 it != picture_map_.end();
459 ++it) {
460 const Picture* picture = it->second.GetPicture();
461 if (picture && (processed_pictures.count(picture) == 0)) {
462 picture->EmitTraceSnapshot();
463 processed_pictures.insert(picture);
468 } // namespace cc