cc: Add tests for DisplayListRasterSource.
[chromium-blink-merge.git] / cc / playback / display_list_recording_source.cc
blob43d11287f74d7a945e7c95a19349e67696f456d6
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_recording_source.h"
7 #include <algorithm>
9 #include "cc/base/histograms.h"
10 #include "cc/base/region.h"
11 #include "cc/layers/content_layer_client.h"
12 #include "cc/playback/display_item_list.h"
13 #include "cc/playback/display_list_raster_source.h"
14 #include "skia/ext/analysis_canvas.h"
16 namespace {
18 // Layout pixel buffer around the visible layer rect to record. Any base
19 // picture that intersects the visible layer rect expanded by this distance
20 // will be recorded.
21 const int kPixelDistanceToRecord = 8000;
22 // We don't perform solid color analysis on images that have more than 10 skia
23 // operations.
24 const int kOpCountThatIsOkToAnalyze = 10;
26 #ifdef NDEBUG
27 const bool kDefaultClearCanvasSetting = false;
28 #else
29 const bool kDefaultClearCanvasSetting = true;
30 #endif
32 DEFINE_SCOPED_UMA_HISTOGRAM_AREA_TIMER(
33 ScopedDisplayListRecordingSourceUpdateTimer,
34 "Compositing.DisplayListRecordingSource.UpdateUs",
35 "Compositing.DisplayListRecordingSource.UpdateInvalidatedAreaPerMs");
37 } // namespace
39 namespace cc {
41 DisplayListRecordingSource::DisplayListRecordingSource(
42 const gfx::Size& grid_cell_size)
43 : slow_down_raster_scale_factor_for_debug_(0),
44 gather_pixel_refs_(false),
45 requires_clear_(false),
46 is_solid_color_(false),
47 clear_canvas_with_debug_color_(kDefaultClearCanvasSetting),
48 solid_color_(SK_ColorTRANSPARENT),
49 background_color_(SK_ColorTRANSPARENT),
50 pixel_record_distance_(kPixelDistanceToRecord),
51 grid_cell_size_(grid_cell_size),
52 is_suitable_for_gpu_rasterization_(true) {
55 DisplayListRecordingSource::~DisplayListRecordingSource() {
58 bool DisplayListRecordingSource::UpdateAndExpandInvalidation(
59 ContentLayerClient* painter,
60 Region* invalidation,
61 const gfx::Size& layer_size,
62 const gfx::Rect& visible_layer_rect,
63 int frame_number,
64 RecordingMode recording_mode) {
65 ScopedDisplayListRecordingSourceUpdateTimer timer;
66 bool updated = false;
68 if (size_ != layer_size) {
69 size_ = layer_size;
70 updated = true;
73 gfx::Rect old_recorded_viewport = recorded_viewport_;
74 recorded_viewport_ = visible_layer_rect;
75 recorded_viewport_.Inset(-pixel_record_distance_, -pixel_record_distance_);
76 recorded_viewport_.Intersect(gfx::Rect(GetSize()));
78 if (recorded_viewport_ != old_recorded_viewport) {
79 // Invalidate newly-exposed and no-longer-exposed areas.
80 Region newly_exposed_region(recorded_viewport_);
81 newly_exposed_region.Subtract(old_recorded_viewport);
82 invalidation->Union(newly_exposed_region);
84 Region no_longer_exposed_region(old_recorded_viewport);
85 no_longer_exposed_region.Subtract(recorded_viewport_);
86 invalidation->Union(no_longer_exposed_region);
88 updated = true;
91 // Count the area that is being invalidated.
92 Region recorded_invalidation(*invalidation);
93 recorded_invalidation.Intersect(recorded_viewport_);
94 for (Region::Iterator it(recorded_invalidation); it.has_rect(); it.next())
95 timer.AddArea(it.rect().size().GetArea());
97 if (!updated && !invalidation->Intersects(recorded_viewport_))
98 return false;
100 ContentLayerClient::PaintingControlSetting painting_control =
101 ContentLayerClient::PAINTING_BEHAVIOR_NORMAL;
103 switch (recording_mode) {
104 case RECORD_NORMALLY:
105 // Already setup for normal recording.
106 break;
107 case RECORD_WITH_PAINTING_DISABLED:
108 painting_control = ContentLayerClient::DISPLAY_LIST_PAINTING_DISABLED;
109 break;
110 case RECORD_WITH_CACHING_DISABLED:
111 painting_control = ContentLayerClient::DISPLAY_LIST_CACHING_DISABLED;
112 break;
113 case RECORD_WITH_CONSTRUCTION_DISABLED:
114 painting_control = ContentLayerClient::DISPLAY_LIST_CONSTRUCTION_DISABLED;
115 break;
116 default:
117 // case RecordingSource::RECORD_WITH_SK_NULL_CANVAS should not be reached
118 NOTREACHED();
121 int repeat_count = 1;
122 if (slow_down_raster_scale_factor_for_debug_ > 1) {
123 repeat_count = slow_down_raster_scale_factor_for_debug_;
124 painting_control = ContentLayerClient::DISPLAY_LIST_CACHING_DISABLED;
127 for (int i = 0; i < repeat_count; ++i) {
128 display_list_ = painter->PaintContentsToDisplayList(recorded_viewport_,
129 painting_control);
132 is_suitable_for_gpu_rasterization_ =
133 display_list_->IsSuitableForGpuRasterization();
134 DetermineIfSolidColor();
135 display_list_->EmitTraceSnapshot();
136 if (gather_pixel_refs_)
137 display_list_->GatherPixelRefs(grid_cell_size_);
139 return true;
142 gfx::Size DisplayListRecordingSource::GetSize() const {
143 return size_;
146 void DisplayListRecordingSource::SetEmptyBounds() {
147 size_ = gfx::Size();
148 Clear();
151 void DisplayListRecordingSource::SetSlowdownRasterScaleFactor(int factor) {
152 slow_down_raster_scale_factor_for_debug_ = factor;
155 void DisplayListRecordingSource::SetGatherPixelRefs(bool gather_pixel_refs) {
156 gather_pixel_refs_ = gather_pixel_refs;
159 void DisplayListRecordingSource::SetBackgroundColor(SkColor background_color) {
160 background_color_ = background_color;
163 void DisplayListRecordingSource::SetRequiresClear(bool requires_clear) {
164 requires_clear_ = requires_clear;
167 void DisplayListRecordingSource::SetUnsuitableForGpuRasterizationForTesting() {
168 is_suitable_for_gpu_rasterization_ = false;
171 bool DisplayListRecordingSource::IsSuitableForGpuRasterization() const {
172 return is_suitable_for_gpu_rasterization_;
175 scoped_refptr<RasterSource> DisplayListRecordingSource::CreateRasterSource(
176 bool can_use_lcd_text) const {
177 return scoped_refptr<RasterSource>(
178 DisplayListRasterSource::CreateFromDisplayListRecordingSource(
179 this, can_use_lcd_text));
182 gfx::Size DisplayListRecordingSource::GetTileGridSizeForTesting() const {
183 return gfx::Size();
186 void DisplayListRecordingSource::DetermineIfSolidColor() {
187 DCHECK(display_list_.get());
188 is_solid_color_ = false;
189 solid_color_ = SK_ColorTRANSPARENT;
191 if (display_list_->ApproximateOpCount() > kOpCountThatIsOkToAnalyze)
192 return;
194 gfx::Size layer_size = GetSize();
195 skia::AnalysisCanvas canvas(layer_size.width(), layer_size.height());
196 display_list_->Raster(&canvas, nullptr, 1.f);
197 is_solid_color_ = canvas.GetColorIfSolid(&solid_color_);
200 void DisplayListRecordingSource::Clear() {
201 recorded_viewport_ = gfx::Rect();
202 display_list_ = NULL;
203 is_solid_color_ = false;
206 } // namespace cc