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"
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"
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
21 const int kPixelDistanceToRecord
= 8000;
22 // We don't perform solid color analysis on images that have more than 10 skia
24 const int kOpCountThatIsOkToAnalyze
= 10;
26 // This is the distance, in layer space, by which the recorded viewport has to
27 // change before causing a paint of the new content. For example, it means
28 // that one has to scroll a very large page by 512 pixels before we will
29 // re-record a new DisplayItemList for an updated recorded viewport.
30 const int kMinimumDistanceBeforeUpdatingRecordedViewport
= 512;
33 const bool kDefaultClearCanvasSetting
= false;
35 const bool kDefaultClearCanvasSetting
= true;
38 DEFINE_SCOPED_UMA_HISTOGRAM_AREA_TIMER(
39 ScopedDisplayListRecordingSourceUpdateTimer
,
40 "Compositing.DisplayListRecordingSource.UpdateUs",
41 "Compositing.DisplayListRecordingSource.UpdateInvalidatedAreaPerMs");
47 DisplayListRecordingSource::DisplayListRecordingSource(
48 const gfx::Size
& grid_cell_size
)
49 : slow_down_raster_scale_factor_for_debug_(0),
50 gather_pixel_refs_(false),
51 requires_clear_(false),
52 is_solid_color_(false),
53 clear_canvas_with_debug_color_(kDefaultClearCanvasSetting
),
54 solid_color_(SK_ColorTRANSPARENT
),
55 background_color_(SK_ColorTRANSPARENT
),
56 pixel_record_distance_(kPixelDistanceToRecord
),
57 grid_cell_size_(grid_cell_size
),
58 painter_reported_memory_usage_(0),
59 is_suitable_for_gpu_rasterization_(true) {}
61 DisplayListRecordingSource::~DisplayListRecordingSource() {
64 // This method only really makes sense to call if the size of the layer didn't
66 bool DisplayListRecordingSource::ExposesEnoughNewArea(
67 const gfx::Rect
& current_recorded_viewport
,
68 const gfx::Rect
& potential_new_recorded_viewport
,
69 const gfx::Size
& layer_size
) {
70 // If both are empty, nothing to do.
71 if (current_recorded_viewport
.IsEmpty() &&
72 potential_new_recorded_viewport
.IsEmpty())
75 // Re-record when going from empty to not-empty, to cover cases where
76 // the layer is recorded for the first time, or otherwise becomes visible.
77 if (current_recorded_viewport
.IsEmpty())
80 // Re-record if the new viewport includes area outside of a skirt around the
82 gfx::Rect
expanded_viewport(current_recorded_viewport
);
83 expanded_viewport
.Inset(-kMinimumDistanceBeforeUpdatingRecordedViewport
,
84 -kMinimumDistanceBeforeUpdatingRecordedViewport
);
85 if (!expanded_viewport
.Contains(potential_new_recorded_viewport
))
88 // Even if the new viewport doesn't include enough new area to satisfy the
89 // condition above, re-record anyway if touches a layer edge not touched by
90 // the existing viewport. Viewports are clipped to layer boundaries, so if the
91 // new viewport touches a layer edge not touched by the existing viewport,
92 // the new viewport must expose new area that touches this layer edge. Since
93 // this new area touches a layer edge, it's impossible to expose more area in
94 // that direction, so recording cannot be deferred until the exposed new area
95 // satisfies the condition above.
96 if (potential_new_recorded_viewport
.x() == 0 &&
97 current_recorded_viewport
.x() != 0)
99 if (potential_new_recorded_viewport
.y() == 0 &&
100 current_recorded_viewport
.y() != 0)
102 if (potential_new_recorded_viewport
.right() == layer_size
.width() &&
103 current_recorded_viewport
.right() != layer_size
.width())
105 if (potential_new_recorded_viewport
.bottom() == layer_size
.height() &&
106 current_recorded_viewport
.bottom() != layer_size
.height())
112 bool DisplayListRecordingSource::UpdateAndExpandInvalidation(
113 ContentLayerClient
* painter
,
114 Region
* invalidation
,
115 const gfx::Size
& layer_size
,
116 const gfx::Rect
& visible_layer_rect
,
118 RecordingMode recording_mode
) {
119 ScopedDisplayListRecordingSourceUpdateTimer timer
;
120 bool updated
= false;
122 if (size_
!= layer_size
) {
127 // The recorded viewport is the visible layer rect, expanded
128 // by the pixel record distance, up to a maximum of the total
130 gfx::Rect potential_new_recorded_viewport
= visible_layer_rect
;
131 potential_new_recorded_viewport
.Inset(-pixel_record_distance_
,
132 -pixel_record_distance_
);
133 potential_new_recorded_viewport
.Intersect(gfx::Rect(GetSize()));
136 ExposesEnoughNewArea(recorded_viewport_
, potential_new_recorded_viewport
,
138 gfx::Rect old_recorded_viewport
= recorded_viewport_
;
139 recorded_viewport_
= potential_new_recorded_viewport
;
141 // Invalidate newly-exposed and no-longer-exposed areas.
142 Region
newly_exposed_region(recorded_viewport_
);
143 newly_exposed_region
.Subtract(old_recorded_viewport
);
144 invalidation
->Union(newly_exposed_region
);
146 Region
no_longer_exposed_region(old_recorded_viewport
);
147 no_longer_exposed_region
.Subtract(recorded_viewport_
);
148 invalidation
->Union(no_longer_exposed_region
);
153 // Count the area that is being invalidated.
154 Region
recorded_invalidation(*invalidation
);
155 recorded_invalidation
.Intersect(recorded_viewport_
);
156 for (Region::Iterator
it(recorded_invalidation
); it
.has_rect(); it
.next())
157 timer
.AddArea(it
.rect().size().GetArea());
159 if (!updated
&& !invalidation
->Intersects(recorded_viewport_
))
162 ContentLayerClient::PaintingControlSetting painting_control
=
163 ContentLayerClient::PAINTING_BEHAVIOR_NORMAL
;
165 switch (recording_mode
) {
166 case RECORD_NORMALLY
:
167 // Already setup for normal recording.
169 case RECORD_WITH_PAINTING_DISABLED
:
170 painting_control
= ContentLayerClient::DISPLAY_LIST_PAINTING_DISABLED
;
172 case RECORD_WITH_CACHING_DISABLED
:
173 painting_control
= ContentLayerClient::DISPLAY_LIST_CACHING_DISABLED
;
175 case RECORD_WITH_CONSTRUCTION_DISABLED
:
176 painting_control
= ContentLayerClient::DISPLAY_LIST_CONSTRUCTION_DISABLED
;
179 // case RecordingSource::RECORD_WITH_SK_NULL_CANVAS should not be reached
183 // TODO(vmpstr): Add a slow_down_recording_scale_factor_for_debug_ to be able
184 // to slow down recording.
186 painter
->PaintContentsToDisplayList(recorded_viewport_
, painting_control
);
187 painter_reported_memory_usage_
= painter
->GetApproximateUnsharedMemoryUsage();
189 is_suitable_for_gpu_rasterization_
=
190 display_list_
->IsSuitableForGpuRasterization();
191 DetermineIfSolidColor();
192 display_list_
->EmitTraceSnapshot();
193 if (gather_pixel_refs_
)
194 display_list_
->GatherPixelRefs(grid_cell_size_
);
199 gfx::Size
DisplayListRecordingSource::GetSize() const {
203 void DisplayListRecordingSource::SetEmptyBounds() {
208 void DisplayListRecordingSource::SetSlowdownRasterScaleFactor(int factor
) {
209 slow_down_raster_scale_factor_for_debug_
= factor
;
212 void DisplayListRecordingSource::SetGatherPixelRefs(bool gather_pixel_refs
) {
213 gather_pixel_refs_
= gather_pixel_refs
;
216 void DisplayListRecordingSource::SetBackgroundColor(SkColor background_color
) {
217 background_color_
= background_color
;
220 void DisplayListRecordingSource::SetRequiresClear(bool requires_clear
) {
221 requires_clear_
= requires_clear
;
224 void DisplayListRecordingSource::SetUnsuitableForGpuRasterizationForTesting() {
225 is_suitable_for_gpu_rasterization_
= false;
228 bool DisplayListRecordingSource::IsSuitableForGpuRasterization() const {
229 return is_suitable_for_gpu_rasterization_
;
232 scoped_refptr
<RasterSource
> DisplayListRecordingSource::CreateRasterSource(
233 bool can_use_lcd_text
) const {
234 return scoped_refptr
<RasterSource
>(
235 DisplayListRasterSource::CreateFromDisplayListRecordingSource(
236 this, can_use_lcd_text
));
239 gfx::Size
DisplayListRecordingSource::GetTileGridSizeForTesting() const {
243 void DisplayListRecordingSource::DetermineIfSolidColor() {
244 DCHECK(display_list_
.get());
245 is_solid_color_
= false;
246 solid_color_
= SK_ColorTRANSPARENT
;
248 if (display_list_
->ApproximateOpCount() > kOpCountThatIsOkToAnalyze
)
251 gfx::Size layer_size
= GetSize();
252 skia::AnalysisCanvas
canvas(layer_size
.width(), layer_size
.height());
253 display_list_
->Raster(&canvas
, nullptr, gfx::Rect(), 1.f
);
254 is_solid_color_
= canvas
.GetColorIfSolid(&solid_color_
);
257 void DisplayListRecordingSource::Clear() {
258 recorded_viewport_
= gfx::Rect();
259 display_list_
= NULL
;
260 painter_reported_memory_usage_
= 0;
261 is_solid_color_
= false;