Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / cc / playback / display_list_recording_source.cc
blobb7bdd788adfa9f39aeef360b39005394a7d44d7a
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 = 4000;
23 // This is the distance, in layer space, by which the recorded viewport has to
24 // change before causing a paint of the new content. For example, it means
25 // that one has to scroll a very large page by 512 pixels before we will
26 // re-record a new DisplayItemList for an updated recorded viewport.
27 const int kMinimumDistanceBeforeUpdatingRecordedViewport = 512;
29 #ifdef NDEBUG
30 const bool kDefaultClearCanvasSetting = false;
31 #else
32 const bool kDefaultClearCanvasSetting = true;
33 #endif
35 DEFINE_SCOPED_UMA_HISTOGRAM_AREA_TIMER(
36 ScopedDisplayListRecordingSourceUpdateTimer,
37 "Compositing.%s.DisplayListRecordingSource.UpdateUs",
38 "Compositing.%s.DisplayListRecordingSource.UpdateInvalidatedAreaPerMs");
40 } // namespace
42 namespace cc {
44 DisplayListRecordingSource::DisplayListRecordingSource(
45 const gfx::Size& grid_cell_size)
46 : slow_down_raster_scale_factor_for_debug_(0),
47 gather_images_(false),
48 requires_clear_(false),
49 is_solid_color_(false),
50 clear_canvas_with_debug_color_(kDefaultClearCanvasSetting),
51 solid_color_(SK_ColorTRANSPARENT),
52 background_color_(SK_ColorTRANSPARENT),
53 pixel_record_distance_(kPixelDistanceToRecord),
54 grid_cell_size_(grid_cell_size),
55 painter_reported_memory_usage_(0) {}
57 DisplayListRecordingSource::~DisplayListRecordingSource() {
60 // This method only really makes sense to call if the size of the layer didn't
61 // change.
62 bool DisplayListRecordingSource::ExposesEnoughNewArea(
63 const gfx::Rect& current_recorded_viewport,
64 const gfx::Rect& potential_new_recorded_viewport,
65 const gfx::Size& layer_size) {
66 // If both are empty, nothing to do.
67 if (current_recorded_viewport.IsEmpty() &&
68 potential_new_recorded_viewport.IsEmpty())
69 return false;
71 // Re-record when going from empty to not-empty, to cover cases where
72 // the layer is recorded for the first time, or otherwise becomes visible.
73 if (current_recorded_viewport.IsEmpty())
74 return true;
76 // Re-record if the new viewport includes area outside of a skirt around the
77 // existing viewport.
78 gfx::Rect expanded_viewport(current_recorded_viewport);
79 expanded_viewport.Inset(-kMinimumDistanceBeforeUpdatingRecordedViewport,
80 -kMinimumDistanceBeforeUpdatingRecordedViewport);
81 if (!expanded_viewport.Contains(potential_new_recorded_viewport))
82 return true;
84 // Even if the new viewport doesn't include enough new area to satisfy the
85 // condition above, re-record anyway if touches a layer edge not touched by
86 // the existing viewport. Viewports are clipped to layer boundaries, so if the
87 // new viewport touches a layer edge not touched by the existing viewport,
88 // the new viewport must expose new area that touches this layer edge. Since
89 // this new area touches a layer edge, it's impossible to expose more area in
90 // that direction, so recording cannot be deferred until the exposed new area
91 // satisfies the condition above.
92 if (potential_new_recorded_viewport.x() == 0 &&
93 current_recorded_viewport.x() != 0)
94 return true;
95 if (potential_new_recorded_viewport.y() == 0 &&
96 current_recorded_viewport.y() != 0)
97 return true;
98 if (potential_new_recorded_viewport.right() == layer_size.width() &&
99 current_recorded_viewport.right() != layer_size.width())
100 return true;
101 if (potential_new_recorded_viewport.bottom() == layer_size.height() &&
102 current_recorded_viewport.bottom() != layer_size.height())
103 return true;
105 return false;
108 bool DisplayListRecordingSource::UpdateAndExpandInvalidation(
109 ContentLayerClient* painter,
110 Region* invalidation,
111 const gfx::Size& layer_size,
112 const gfx::Rect& visible_layer_rect,
113 int frame_number,
114 RecordingMode recording_mode) {
115 ScopedDisplayListRecordingSourceUpdateTimer timer;
116 bool updated = false;
118 if (size_ != layer_size) {
119 size_ = layer_size;
120 updated = true;
123 // The recorded viewport is the visible layer rect, expanded
124 // by the pixel record distance, up to a maximum of the total
125 // layer size.
126 gfx::Rect potential_new_recorded_viewport = visible_layer_rect;
127 potential_new_recorded_viewport.Inset(-pixel_record_distance_,
128 -pixel_record_distance_);
129 potential_new_recorded_viewport.Intersect(gfx::Rect(GetSize()));
131 if (updated ||
132 ExposesEnoughNewArea(recorded_viewport_, potential_new_recorded_viewport,
133 GetSize())) {
134 gfx::Rect old_recorded_viewport = recorded_viewport_;
135 recorded_viewport_ = potential_new_recorded_viewport;
137 // Invalidate newly-exposed and no-longer-exposed areas.
138 Region newly_exposed_region(recorded_viewport_);
139 newly_exposed_region.Subtract(old_recorded_viewport);
140 invalidation->Union(newly_exposed_region);
142 Region no_longer_exposed_region(old_recorded_viewport);
143 no_longer_exposed_region.Subtract(recorded_viewport_);
144 invalidation->Union(no_longer_exposed_region);
146 updated = true;
149 // Count the area that is being invalidated.
150 Region recorded_invalidation(*invalidation);
151 recorded_invalidation.Intersect(recorded_viewport_);
152 for (Region::Iterator it(recorded_invalidation); it.has_rect(); it.next())
153 timer.AddArea(it.rect().size().GetArea());
155 if (!updated && !invalidation->Intersects(recorded_viewport_))
156 return false;
158 ContentLayerClient::PaintingControlSetting painting_control =
159 ContentLayerClient::PAINTING_BEHAVIOR_NORMAL;
161 switch (recording_mode) {
162 case RECORD_NORMALLY:
163 // Already setup for normal recording.
164 break;
165 case RECORD_WITH_PAINTING_DISABLED:
166 painting_control = ContentLayerClient::DISPLAY_LIST_PAINTING_DISABLED;
167 break;
168 case RECORD_WITH_CACHING_DISABLED:
169 painting_control = ContentLayerClient::DISPLAY_LIST_CACHING_DISABLED;
170 break;
171 case RECORD_WITH_CONSTRUCTION_DISABLED:
172 painting_control = ContentLayerClient::DISPLAY_LIST_CONSTRUCTION_DISABLED;
173 break;
174 default:
175 // case RecordingSource::RECORD_WITH_SK_NULL_CANVAS should not be reached
176 NOTREACHED();
179 // TODO(vmpstr): Add a slow_down_recording_scale_factor_for_debug_ to be able
180 // to slow down recording.
181 display_list_ =
182 painter->PaintContentsToDisplayList(recorded_viewport_, painting_control);
183 painter_reported_memory_usage_ = painter->GetApproximateUnsharedMemoryUsage();
185 DetermineIfSolidColor();
186 display_list_->EmitTraceSnapshot();
187 if (gather_images_)
188 display_list_->GatherDiscardableImages(grid_cell_size_);
190 return true;
193 gfx::Size DisplayListRecordingSource::GetSize() const {
194 return size_;
197 void DisplayListRecordingSource::SetEmptyBounds() {
198 size_ = gfx::Size();
199 Clear();
202 void DisplayListRecordingSource::SetSlowdownRasterScaleFactor(int factor) {
203 slow_down_raster_scale_factor_for_debug_ = factor;
206 void DisplayListRecordingSource::SetGatherDiscardableImages(
207 bool gather_images) {
208 gather_images_ = gather_images;
211 void DisplayListRecordingSource::SetBackgroundColor(SkColor background_color) {
212 background_color_ = background_color;
215 void DisplayListRecordingSource::SetRequiresClear(bool requires_clear) {
216 requires_clear_ = requires_clear;
219 bool DisplayListRecordingSource::IsSuitableForGpuRasterization() const {
220 // The display list needs to be created (see: UpdateAndExpandInvalidation)
221 // before checking for suitability.
222 DCHECK(display_list_);
223 return display_list_->IsSuitableForGpuRasterization();
226 scoped_refptr<RasterSource> DisplayListRecordingSource::CreateRasterSource(
227 bool can_use_lcd_text) const {
228 return scoped_refptr<RasterSource>(
229 DisplayListRasterSource::CreateFromDisplayListRecordingSource(
230 this, can_use_lcd_text));
233 void DisplayListRecordingSource::DetermineIfSolidColor() {
234 DCHECK(display_list_);
235 is_solid_color_ = false;
236 solid_color_ = SK_ColorTRANSPARENT;
238 if (!display_list_->ShouldBeAnalyzedForSolidColor())
239 return;
241 gfx::Size layer_size = GetSize();
242 skia::AnalysisCanvas canvas(layer_size.width(), layer_size.height());
243 display_list_->Raster(&canvas, nullptr, gfx::Rect(), 1.f);
244 is_solid_color_ = canvas.GetColorIfSolid(&solid_color_);
247 void DisplayListRecordingSource::Clear() {
248 recorded_viewport_ = gfx::Rect();
249 display_list_ = nullptr;
250 painter_reported_memory_usage_ = 0;
251 is_solid_color_ = false;
254 } // namespace cc