Revert of [Webapp Refactor] Remove remoting.SessionConnector. (patchset #3 id:60001...
[chromium-blink-merge.git] / cc / resources / display_list_recording_source.cc
blob701db9bfd9dbd23fb5b238590bdb0e24b5260136
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/resources/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/resources/display_item_list.h"
13 #include "cc/resources/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 DEFINE_SCOPED_UMA_HISTOGRAM_AREA_TIMER(
27 ScopedDisplayListRecordingSourceUpdateTimer,
28 "Compositing.DisplayListRecordingSource.UpdateUs",
29 "Compositing.DisplayListRecordingSource.UpdateInvalidatedAreaPerMs");
31 } // namespace
33 namespace cc {
35 DisplayListRecordingSource::DisplayListRecordingSource(
36 const gfx::Size& grid_cell_size)
37 : slow_down_raster_scale_factor_for_debug_(0),
38 gather_pixel_refs_(false),
39 requires_clear_(false),
40 is_solid_color_(false),
41 solid_color_(SK_ColorTRANSPARENT),
42 background_color_(SK_ColorTRANSPARENT),
43 pixel_record_distance_(kPixelDistanceToRecord),
44 grid_cell_size_(grid_cell_size),
45 is_suitable_for_gpu_rasterization_(true) {
48 DisplayListRecordingSource::~DisplayListRecordingSource() {
51 bool DisplayListRecordingSource::UpdateAndExpandInvalidation(
52 ContentLayerClient* painter,
53 Region* invalidation,
54 const gfx::Size& layer_size,
55 const gfx::Rect& visible_layer_rect,
56 int frame_number,
57 RecordingMode recording_mode) {
58 ScopedDisplayListRecordingSourceUpdateTimer timer;
59 bool updated = false;
61 if (size_ != layer_size) {
62 size_ = layer_size;
63 updated = true;
66 gfx::Rect old_recorded_viewport = recorded_viewport_;
67 recorded_viewport_ = visible_layer_rect;
68 recorded_viewport_.Inset(-pixel_record_distance_, -pixel_record_distance_);
69 recorded_viewport_.Intersect(gfx::Rect(GetSize()));
71 if (recorded_viewport_ != old_recorded_viewport) {
72 // Invalidate newly-exposed and no-longer-exposed areas.
73 Region newly_exposed_region(recorded_viewport_);
74 newly_exposed_region.Subtract(old_recorded_viewport);
75 invalidation->Union(newly_exposed_region);
77 Region no_longer_exposed_region(old_recorded_viewport);
78 no_longer_exposed_region.Subtract(recorded_viewport_);
79 invalidation->Union(no_longer_exposed_region);
81 updated = true;
84 // Count the area that is being invalidated.
85 Region recorded_invalidation(*invalidation);
86 recorded_invalidation.Intersect(recorded_viewport_);
87 for (Region::Iterator it(recorded_invalidation); it.has_rect(); it.next())
88 timer.AddArea(it.rect().size().GetArea());
90 if (!updated && !invalidation->Intersects(recorded_viewport_))
91 return false;
93 ContentLayerClient::PaintingControlSetting painting_control =
94 ContentLayerClient::PAINTING_BEHAVIOR_NORMAL;
96 switch (recording_mode) {
97 case RECORD_NORMALLY:
98 // Already setup for normal recording.
99 break;
100 case RECORD_WITH_SK_NULL_CANVAS:
101 // TODO(schenney): Remove this when DisplayList recording is the only
102 // option. For now, fall through and disable construction.
103 case RECORD_WITH_PAINTING_DISABLED:
104 painting_control = ContentLayerClient::DISPLAY_LIST_CONSTRUCTION_DISABLED;
105 break;
106 case RECORD_WITH_CACHING_DISABLED:
107 painting_control = ContentLayerClient::DISPLAY_LIST_CACHING_DISABLED;
108 break;
109 default:
110 NOTREACHED();
113 int repeat_count = 1;
114 if (slow_down_raster_scale_factor_for_debug_ > 1) {
115 repeat_count = slow_down_raster_scale_factor_for_debug_;
116 if (painting_control !=
117 ContentLayerClient::DISPLAY_LIST_CONSTRUCTION_DISABLED) {
118 painting_control = ContentLayerClient::DISPLAY_LIST_CACHING_DISABLED;
121 for (int i = 0; i < repeat_count; ++i) {
122 const bool use_cached_picture = true;
123 display_list_ =
124 DisplayItemList::Create(recorded_viewport_, use_cached_picture);
125 painter->PaintContentsToDisplayList(display_list_.get(), recorded_viewport_,
126 painting_control);
128 display_list_->CreateAndCacheSkPicture();
130 is_suitable_for_gpu_rasterization_ =
131 display_list_->IsSuitableForGpuRasterization();
132 DetermineIfSolidColor();
133 display_list_->EmitTraceSnapshot();
134 if (gather_pixel_refs_)
135 display_list_->GatherPixelRefs(grid_cell_size_);
137 return true;
140 gfx::Size DisplayListRecordingSource::GetSize() const {
141 return size_;
144 void DisplayListRecordingSource::SetEmptyBounds() {
145 size_ = gfx::Size();
146 Clear();
149 void DisplayListRecordingSource::SetSlowdownRasterScaleFactor(int factor) {
150 slow_down_raster_scale_factor_for_debug_ = factor;
153 void DisplayListRecordingSource::SetGatherPixelRefs(bool gather_pixel_refs) {
154 gather_pixel_refs_ = gather_pixel_refs;
157 void DisplayListRecordingSource::SetBackgroundColor(SkColor background_color) {
158 background_color_ = background_color;
161 void DisplayListRecordingSource::SetRequiresClear(bool requires_clear) {
162 requires_clear_ = requires_clear;
165 void DisplayListRecordingSource::SetUnsuitableForGpuRasterizationForTesting() {
166 is_suitable_for_gpu_rasterization_ = false;
169 bool DisplayListRecordingSource::IsSuitableForGpuRasterization() const {
170 return is_suitable_for_gpu_rasterization_;
173 scoped_refptr<RasterSource> DisplayListRecordingSource::CreateRasterSource(
174 bool can_use_lcd_text) const {
175 return scoped_refptr<RasterSource>(
176 DisplayListRasterSource::CreateFromDisplayListRecordingSource(
177 this, can_use_lcd_text));
180 gfx::Size DisplayListRecordingSource::GetTileGridSizeForTesting() const {
181 return gfx::Size();
184 void DisplayListRecordingSource::DetermineIfSolidColor() {
185 DCHECK(display_list_.get());
186 is_solid_color_ = false;
187 solid_color_ = SK_ColorTRANSPARENT;
189 if (display_list_->ApproximateOpCount() > kOpCountThatIsOkToAnalyze)
190 return;
192 gfx::Size layer_size = GetSize();
193 skia::AnalysisCanvas canvas(layer_size.width(), layer_size.height());
194 display_list_->Raster(&canvas, nullptr, 1.f);
195 is_solid_color_ = canvas.GetColorIfSolid(&solid_color_);
198 void DisplayListRecordingSource::Clear() {
199 recorded_viewport_ = gfx::Rect();
200 display_list_ = NULL;
201 is_solid_color_ = false;
204 } // namespace cc