ozone: evdev: Sync caps lock LED state to evdev
[chromium-blink-merge.git] / cc / resources / picture_pile_impl.cc
bloba9bad90a9240c6f15f712a1d078837fab8563ba7
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 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) {
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) {
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_) {
77 PicturePileImpl::~PicturePileImpl() {
80 void PicturePileImpl::PlaybackToSharedCanvas(SkCanvas* canvas,
81 const gfx::Rect& canvas_rect,
82 float contents_scale) const {
83 RasterCommon(canvas,
84 NULL,
85 canvas_rect,
86 contents_scale,
87 false);
90 void PicturePileImpl::RasterForAnalysis(skia::AnalysisCanvas* canvas,
91 const gfx::Rect& canvas_rect,
92 float contents_scale) const {
93 RasterCommon(canvas, canvas, canvas_rect, contents_scale, true);
96 void PicturePileImpl::PlaybackToCanvas(SkCanvas* canvas,
97 const gfx::Rect& canvas_rect,
98 float contents_scale) const {
99 RasterSourceHelper::PrepareForPlaybackToCanvas(
100 canvas, canvas_rect, gfx::Rect(tiling_.tiling_size()), contents_scale,
101 background_color_, clear_canvas_with_debug_color_, requires_clear_);
102 RasterCommon(canvas,
103 NULL,
104 canvas_rect,
105 contents_scale,
106 false);
109 void PicturePileImpl::CoalesceRasters(const gfx::Rect& canvas_rect,
110 const gfx::Rect& content_rect,
111 float contents_scale,
112 PictureRegionMap* results) const {
113 DCHECK(results);
114 // Rasterize the collection of relevant picture piles.
115 gfx::Rect layer_rect = gfx::ScaleToEnclosingRect(
116 content_rect, 1.f / contents_scale);
118 // Make sure pictures don't overlap by keeping track of previous right/bottom.
119 int min_content_left = -1;
120 int min_content_top = -1;
121 int last_row_index = -1;
122 int last_col_index = -1;
123 gfx::Rect last_content_rect;
125 // Coalesce rasters of the same picture into different rects:
126 // - Compute the clip of each of the pile chunks,
127 // - Subtract it from the canvas rect to get difference region
128 // - Later, use the difference region to subtract each of the comprising
129 // rects from the canvas.
130 // Note that in essence, we're trying to mimic clipRegion with intersect op
131 // that also respects the current canvas transform and clip. In order to use
132 // the canvas transform, we must stick to clipRect operations (clipRegion
133 // ignores the transform). Intersect then can be written as subtracting the
134 // negation of the region we're trying to intersect. Luckily, we know that all
135 // of the rects will have to fit into |content_rect|, so we can start with
136 // that and subtract chunk rects to get the region that we need to subtract
137 // from the canvas. Then, we can use clipRect with difference op to subtract
138 // each rect in the region.
139 bool include_borders = true;
140 for (TilingData::Iterator tile_iter(&tiling_, layer_rect, include_borders);
141 tile_iter;
142 ++tile_iter) {
143 PictureMap::const_iterator map_iter = picture_map_.find(tile_iter.index());
144 if (map_iter == picture_map_.end())
145 continue;
146 const PictureInfo& info = map_iter->second;
147 const Picture* picture = info.GetPicture();
148 if (!picture)
149 continue;
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(
205 SkCanvas* canvas,
206 SkDrawPictureCallback* callback,
207 const gfx::Rect& canvas_rect,
208 float contents_scale,
209 bool is_analysis) const {
210 DCHECK(contents_scale >= min_contents_scale_);
212 canvas->translate(-canvas_rect.x(), -canvas_rect.y());
213 gfx::Rect content_tiling_rect = gfx::ToEnclosingRect(
214 gfx::ScaleRect(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 j = 0; j < repeat_count; ++j)
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, 1.0);
272 skia::RefPtr<SkPicture> picture = skia::AdoptRef(recorder.endRecording());
274 return picture;
277 size_t PicturePileImpl::GetPictureMemoryUsage() const {
278 // Place all pictures in a set to de-dupe.
279 size_t total_size = 0;
280 std::set<const Picture*> pictures_seen;
281 for (const auto& map_value : picture_map_) {
282 const Picture* picture = map_value.second.GetPicture();
283 if (picture && pictures_seen.insert(picture).second)
284 total_size += picture->ApproximateMemoryUsage();
287 return total_size;
290 void PicturePileImpl::PerformSolidColorAnalysis(
291 const gfx::Rect& content_rect,
292 float contents_scale,
293 RasterSource::SolidColorAnalysis* analysis) const {
294 DCHECK(analysis);
295 TRACE_EVENT0("cc", "PicturePileImpl::PerformSolidColorAnalysis");
297 gfx::Rect layer_rect = gfx::ScaleToEnclosingRect(
298 content_rect, 1.0f / contents_scale);
300 layer_rect.Intersect(gfx::Rect(tiling_.tiling_size()));
302 skia::AnalysisCanvas canvas(layer_rect.width(), layer_rect.height());
304 RasterForAnalysis(&canvas, layer_rect, 1.0f);
306 analysis->is_solid_color = canvas.GetColorIfSolid(&analysis->solid_color);
309 void PicturePileImpl::GatherPixelRefs(
310 const gfx::Rect& content_rect,
311 float contents_scale,
312 std::vector<SkPixelRef*>* pixel_refs) const {
313 DCHECK_EQ(0u, pixel_refs->size());
314 for (PixelRefIterator iter(content_rect, contents_scale, this); iter;
315 ++iter) {
316 pixel_refs->push_back(*iter);
320 bool PicturePileImpl::CoversRect(const gfx::Rect& content_rect,
321 float contents_scale) const {
322 if (tiling_.tiling_size().IsEmpty())
323 return false;
324 gfx::Rect layer_rect =
325 gfx::ScaleToEnclosingRect(content_rect, 1.f / contents_scale);
326 layer_rect.Intersect(gfx::Rect(tiling_.tiling_size()));
328 // Common case inside of viewport to avoid the slower map lookups.
329 if (recorded_viewport_.Contains(layer_rect)) {
330 // Sanity check that there are no false positives in recorded_viewport_.
331 DCHECK(CanRasterSlowTileCheck(layer_rect));
332 return true;
335 return CanRasterSlowTileCheck(layer_rect);
338 gfx::Size PicturePileImpl::GetSize() const {
339 return tiling_.tiling_size();
342 bool PicturePileImpl::IsSolidColor() const {
343 return is_solid_color_;
346 SkColor PicturePileImpl::GetSolidColor() const {
347 DCHECK(IsSolidColor());
348 return solid_color_;
351 bool PicturePileImpl::HasRecordings() const {
352 return has_any_recordings_;
355 gfx::Rect PicturePileImpl::PaddedRect(const PictureMapKey& key) const {
356 gfx::Rect padded_rect = tiling_.TileBounds(key.first, key.second);
357 padded_rect.Inset(-buffer_pixels(), -buffer_pixels(), -buffer_pixels(),
358 -buffer_pixels());
359 return padded_rect;
362 bool PicturePileImpl::CanRasterSlowTileCheck(
363 const gfx::Rect& layer_rect) const {
364 bool include_borders = false;
365 for (TilingData::Iterator tile_iter(&tiling_, layer_rect, include_borders);
366 tile_iter; ++tile_iter) {
367 PictureMap::const_iterator map_iter = picture_map_.find(tile_iter.index());
368 if (map_iter == picture_map_.end())
369 return false;
370 if (!map_iter->second.GetPicture())
371 return false;
373 return true;
376 void PicturePileImpl::SetShouldAttemptToUseDistanceFieldText() {
377 should_attempt_to_use_distance_field_text_ = true;
380 bool PicturePileImpl::ShouldAttemptToUseDistanceFieldText() const {
381 return should_attempt_to_use_distance_field_text_;
384 void PicturePileImpl::AsValueInto(
385 base::trace_event::TracedValue* pictures) const {
386 gfx::Rect tiling_rect(tiling_.tiling_size());
387 std::set<const void*> appended_pictures;
388 bool include_borders = true;
389 for (TilingData::Iterator tile_iter(&tiling_, tiling_rect, include_borders);
390 tile_iter; ++tile_iter) {
391 PictureMap::const_iterator map_iter = picture_map_.find(tile_iter.index());
392 if (map_iter == picture_map_.end())
393 continue;
395 const Picture* picture = map_iter->second.GetPicture();
396 if (picture && (appended_pictures.count(picture) == 0)) {
397 appended_pictures.insert(picture);
398 TracedValue::AppendIDRef(picture, pictures);
403 bool PicturePileImpl::CanUseLCDText() const {
404 return can_use_lcd_text_;
407 scoped_refptr<RasterSource> PicturePileImpl::CreateCloneWithoutLCDText() const {
408 DCHECK(CanUseLCDText());
409 bool can_use_lcd_text = false;
410 return scoped_refptr<RasterSource>(
411 new PicturePileImpl(this, can_use_lcd_text));
414 PicturePileImpl::PixelRefIterator::PixelRefIterator(
415 const gfx::Rect& content_rect,
416 float contents_scale,
417 const PicturePileImpl* picture_pile)
418 : picture_pile_(picture_pile),
419 layer_rect_(
420 gfx::ScaleToEnclosingRect(content_rect, 1.f / contents_scale)),
421 tile_iterator_(&picture_pile_->tiling_,
422 layer_rect_,
423 false /* include_borders */) {
424 // Early out if there isn't a single tile.
425 if (!tile_iterator_)
426 return;
428 AdvanceToTilePictureWithPixelRefs();
431 PicturePileImpl::PixelRefIterator::~PixelRefIterator() {
434 PicturePileImpl::PixelRefIterator&
435 PicturePileImpl::PixelRefIterator::operator++() {
436 ++pixel_ref_iterator_;
437 if (pixel_ref_iterator_)
438 return *this;
440 ++tile_iterator_;
441 AdvanceToTilePictureWithPixelRefs();
442 return *this;
445 void PicturePileImpl::PixelRefIterator::AdvanceToTilePictureWithPixelRefs() {
446 for (; tile_iterator_; ++tile_iterator_) {
447 PictureMap::const_iterator it =
448 picture_pile_->picture_map_.find(tile_iterator_.index());
449 if (it == picture_pile_->picture_map_.end())
450 continue;
452 const Picture* picture = it->second.GetPicture();
453 if (!picture || (processed_pictures_.count(picture) != 0) ||
454 !picture->WillPlayBackBitmaps())
455 continue;
457 processed_pictures_.insert(picture);
458 pixel_ref_iterator_ = Picture::PixelRefIterator(layer_rect_, picture);
459 if (pixel_ref_iterator_)
460 break;
464 void PicturePileImpl::DidBeginTracing() {
465 std::set<const void*> processed_pictures;
466 for (const auto& map_pair : picture_map_) {
467 const Picture* picture = map_pair.second.GetPicture();
468 if (picture && (processed_pictures.count(picture) == 0)) {
469 picture->EmitTraceSnapshot();
470 processed_pictures.insert(picture);
475 } // namespace cc