Roll src/third_party/WebKit d9c6159:8139f33 (svn 201974:201975)
[chromium-blink-merge.git] / cc / playback / display_list_raster_source.cc
blobee48065d6193ee2459a954b1972af2d58b947618
1 // Copyright 2014 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 "cc/playback/display_list_raster_source.h"
7 #include "base/trace_event/trace_event.h"
8 #include "cc/base/region.h"
9 #include "cc/debug/debug_colors.h"
10 #include "cc/playback/display_item_list.h"
11 #include "cc/playback/raster_source_helper.h"
12 #include "skia/ext/analysis_canvas.h"
13 #include "third_party/skia/include/core/SkCanvas.h"
14 #include "third_party/skia/include/core/SkPictureRecorder.h"
15 #include "ui/gfx/geometry/rect_conversions.h"
17 namespace cc {
19 scoped_refptr<DisplayListRasterSource>
20 DisplayListRasterSource::CreateFromDisplayListRecordingSource(
21 const DisplayListRecordingSource* other,
22 bool can_use_lcd_text) {
23 return make_scoped_refptr(
24 new DisplayListRasterSource(other, can_use_lcd_text));
27 DisplayListRasterSource::DisplayListRasterSource()
28 : painter_reported_memory_usage_(0),
29 background_color_(SK_ColorTRANSPARENT),
30 requires_clear_(true),
31 can_use_lcd_text_(true),
32 is_solid_color_(false),
33 solid_color_(SK_ColorTRANSPARENT),
34 clear_canvas_with_debug_color_(false),
35 slow_down_raster_scale_factor_for_debug_(0),
36 should_attempt_to_use_distance_field_text_(false) {}
38 DisplayListRasterSource::DisplayListRasterSource(
39 const DisplayListRecordingSource* other,
40 bool can_use_lcd_text)
41 : display_list_(other->display_list_),
42 painter_reported_memory_usage_(other->painter_reported_memory_usage_),
43 background_color_(other->background_color_),
44 requires_clear_(other->requires_clear_),
45 can_use_lcd_text_(can_use_lcd_text),
46 is_solid_color_(other->is_solid_color_),
47 solid_color_(other->solid_color_),
48 recorded_viewport_(other->recorded_viewport_),
49 size_(other->size_),
50 clear_canvas_with_debug_color_(other->clear_canvas_with_debug_color_),
51 slow_down_raster_scale_factor_for_debug_(
52 other->slow_down_raster_scale_factor_for_debug_),
53 should_attempt_to_use_distance_field_text_(false) {}
55 DisplayListRasterSource::DisplayListRasterSource(
56 const DisplayListRasterSource* other,
57 bool can_use_lcd_text)
58 : display_list_(other->display_list_),
59 painter_reported_memory_usage_(other->painter_reported_memory_usage_),
60 background_color_(other->background_color_),
61 requires_clear_(other->requires_clear_),
62 can_use_lcd_text_(can_use_lcd_text),
63 is_solid_color_(other->is_solid_color_),
64 solid_color_(other->solid_color_),
65 recorded_viewport_(other->recorded_viewport_),
66 size_(other->size_),
67 clear_canvas_with_debug_color_(other->clear_canvas_with_debug_color_),
68 slow_down_raster_scale_factor_for_debug_(
69 other->slow_down_raster_scale_factor_for_debug_),
70 should_attempt_to_use_distance_field_text_(
71 other->should_attempt_to_use_distance_field_text_) {}
73 DisplayListRasterSource::~DisplayListRasterSource() {
76 void DisplayListRasterSource::PlaybackToSharedCanvas(
77 SkCanvas* canvas,
78 const gfx::Rect& canvas_rect,
79 float contents_scale) const {
80 RasterCommon(canvas, NULL, canvas_rect, canvas_rect, contents_scale);
83 void DisplayListRasterSource::RasterForAnalysis(skia::AnalysisCanvas* canvas,
84 const gfx::Rect& canvas_rect,
85 float contents_scale) const {
86 RasterCommon(canvas, canvas, canvas_rect, canvas_rect, contents_scale);
89 void DisplayListRasterSource::PlaybackToCanvas(
90 SkCanvas* canvas,
91 const gfx::Rect& canvas_bitmap_rect,
92 const gfx::Rect& canvas_playback_rect,
93 float contents_scale) const {
94 RasterSourceHelper::PrepareForPlaybackToCanvas(
95 canvas, canvas_bitmap_rect, canvas_playback_rect, gfx::Rect(size_),
96 contents_scale, background_color_, clear_canvas_with_debug_color_,
97 requires_clear_);
99 RasterCommon(canvas, NULL, canvas_bitmap_rect, canvas_playback_rect,
100 contents_scale);
103 void DisplayListRasterSource::RasterCommon(
104 SkCanvas* canvas,
105 SkPicture::AbortCallback* callback,
106 const gfx::Rect& canvas_bitmap_rect,
107 const gfx::Rect& canvas_playback_rect,
108 float contents_scale) const {
109 canvas->translate(-canvas_bitmap_rect.x(), -canvas_bitmap_rect.y());
110 gfx::Rect content_rect =
111 gfx::ScaleToEnclosingRect(gfx::Rect(size_), contents_scale);
112 content_rect.Intersect(canvas_playback_rect);
114 canvas->clipRect(gfx::RectToSkRect(content_rect), SkRegion::kIntersect_Op);
116 DCHECK(display_list_.get());
117 gfx::Rect canvas_target_playback_rect =
118 canvas_playback_rect - canvas_bitmap_rect.OffsetFromOrigin();
119 int repeat_count = std::max(1, slow_down_raster_scale_factor_for_debug_);
120 for (int i = 0; i < repeat_count; ++i) {
121 display_list_->Raster(canvas, callback, canvas_target_playback_rect,
122 contents_scale);
126 skia::RefPtr<SkPicture> DisplayListRasterSource::GetFlattenedPicture() {
127 TRACE_EVENT0("cc", "DisplayListRasterSource::GetFlattenedPicture");
129 gfx::Rect display_list_rect(size_);
130 SkPictureRecorder recorder;
131 SkCanvas* canvas = recorder.beginRecording(display_list_rect.width(),
132 display_list_rect.height());
133 if (!display_list_rect.IsEmpty())
134 PlaybackToCanvas(canvas, display_list_rect, display_list_rect, 1.0);
135 skia::RefPtr<SkPicture> picture =
136 skia::AdoptRef(recorder.endRecordingAsPicture());
138 return picture;
141 size_t DisplayListRasterSource::GetPictureMemoryUsage() const {
142 if (!display_list_)
143 return 0;
144 return display_list_->ApproximateMemoryUsage() +
145 painter_reported_memory_usage_;
148 void DisplayListRasterSource::PerformSolidColorAnalysis(
149 const gfx::Rect& content_rect,
150 float contents_scale,
151 RasterSource::SolidColorAnalysis* analysis) const {
152 DCHECK(analysis);
153 TRACE_EVENT0("cc", "DisplayListRasterSource::PerformSolidColorAnalysis");
155 gfx::Rect layer_rect =
156 gfx::ScaleToEnclosingRect(content_rect, 1.0f / contents_scale);
158 layer_rect.Intersect(gfx::Rect(size_));
159 skia::AnalysisCanvas canvas(layer_rect.width(), layer_rect.height());
160 RasterForAnalysis(&canvas, layer_rect, 1.0f);
161 analysis->is_solid_color = canvas.GetColorIfSolid(&analysis->solid_color);
164 void DisplayListRasterSource::GatherPixelRefs(
165 const gfx::Rect& layer_rect,
166 std::vector<skia::PositionPixelRef>* pixel_refs) const {
167 DCHECK_EQ(0u, pixel_refs->size());
169 PixelRefMap::Iterator iterator(layer_rect, display_list_.get());
170 while (iterator) {
171 pixel_refs->push_back(*iterator);
172 ++iterator;
176 bool DisplayListRasterSource::CoversRect(const gfx::Rect& layer_rect) const {
177 if (size_.IsEmpty())
178 return false;
179 gfx::Rect bounded_rect = layer_rect;
180 bounded_rect.Intersect(gfx::Rect(size_));
181 return recorded_viewport_.Contains(bounded_rect);
184 gfx::Size DisplayListRasterSource::GetSize() const {
185 return size_;
188 bool DisplayListRasterSource::IsSolidColor() const {
189 return is_solid_color_;
192 SkColor DisplayListRasterSource::GetSolidColor() const {
193 DCHECK(IsSolidColor());
194 return solid_color_;
197 bool DisplayListRasterSource::HasRecordings() const {
198 return !!display_list_.get();
201 gfx::Rect DisplayListRasterSource::RecordedViewport() const {
202 return recorded_viewport_;
205 void DisplayListRasterSource::SetShouldAttemptToUseDistanceFieldText() {
206 should_attempt_to_use_distance_field_text_ = true;
209 bool DisplayListRasterSource::ShouldAttemptToUseDistanceFieldText() const {
210 return should_attempt_to_use_distance_field_text_;
213 void DisplayListRasterSource::AsValueInto(
214 base::trace_event::TracedValue* array) const {
215 if (display_list_.get())
216 TracedValue::AppendIDRef(display_list_.get(), array);
219 void DisplayListRasterSource::DidBeginTracing() {
220 if (display_list_.get())
221 display_list_->EmitTraceSnapshot();
224 bool DisplayListRasterSource::CanUseLCDText() const {
225 return can_use_lcd_text_;
228 scoped_refptr<RasterSource> DisplayListRasterSource::CreateCloneWithoutLCDText()
229 const {
230 bool can_use_lcd_text = false;
231 return scoped_refptr<RasterSource>(
232 new DisplayListRasterSource(this, can_use_lcd_text));
235 } // namespace cc