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
= 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;
30 const bool kDefaultClearCanvasSetting
= false;
32 const bool kDefaultClearCanvasSetting
= true;
35 DEFINE_SCOPED_UMA_HISTOGRAM_AREA_TIMER(
36 ScopedDisplayListRecordingSourceUpdateTimer
,
37 "Compositing.%s.DisplayListRecordingSource.UpdateUs",
38 "Compositing.%s.DisplayListRecordingSource.UpdateInvalidatedAreaPerMs");
44 DisplayListRecordingSource::DisplayListRecordingSource(
45 const gfx::Size
& grid_cell_size
)
46 : slow_down_raster_scale_factor_for_debug_(0),
47 gather_pixel_refs_(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),
56 is_suitable_for_gpu_rasterization_(true) {}
58 DisplayListRecordingSource::~DisplayListRecordingSource() {
61 // This method only really makes sense to call if the size of the layer didn't
63 bool DisplayListRecordingSource::ExposesEnoughNewArea(
64 const gfx::Rect
& current_recorded_viewport
,
65 const gfx::Rect
& potential_new_recorded_viewport
,
66 const gfx::Size
& layer_size
) {
67 // If both are empty, nothing to do.
68 if (current_recorded_viewport
.IsEmpty() &&
69 potential_new_recorded_viewport
.IsEmpty())
72 // Re-record when going from empty to not-empty, to cover cases where
73 // the layer is recorded for the first time, or otherwise becomes visible.
74 if (current_recorded_viewport
.IsEmpty())
77 // Re-record if the new viewport includes area outside of a skirt around the
79 gfx::Rect
expanded_viewport(current_recorded_viewport
);
80 expanded_viewport
.Inset(-kMinimumDistanceBeforeUpdatingRecordedViewport
,
81 -kMinimumDistanceBeforeUpdatingRecordedViewport
);
82 if (!expanded_viewport
.Contains(potential_new_recorded_viewport
))
85 // Even if the new viewport doesn't include enough new area to satisfy the
86 // condition above, re-record anyway if touches a layer edge not touched by
87 // the existing viewport. Viewports are clipped to layer boundaries, so if the
88 // new viewport touches a layer edge not touched by the existing viewport,
89 // the new viewport must expose new area that touches this layer edge. Since
90 // this new area touches a layer edge, it's impossible to expose more area in
91 // that direction, so recording cannot be deferred until the exposed new area
92 // satisfies the condition above.
93 if (potential_new_recorded_viewport
.x() == 0 &&
94 current_recorded_viewport
.x() != 0)
96 if (potential_new_recorded_viewport
.y() == 0 &&
97 current_recorded_viewport
.y() != 0)
99 if (potential_new_recorded_viewport
.right() == layer_size
.width() &&
100 current_recorded_viewport
.right() != layer_size
.width())
102 if (potential_new_recorded_viewport
.bottom() == layer_size
.height() &&
103 current_recorded_viewport
.bottom() != layer_size
.height())
109 bool DisplayListRecordingSource::UpdateAndExpandInvalidation(
110 ContentLayerClient
* painter
,
111 Region
* invalidation
,
112 const gfx::Size
& layer_size
,
113 const gfx::Rect
& visible_layer_rect
,
115 RecordingMode recording_mode
) {
116 ScopedDisplayListRecordingSourceUpdateTimer timer
;
117 bool updated
= false;
119 if (size_
!= layer_size
) {
124 // The recorded viewport is the visible layer rect, expanded
125 // by the pixel record distance, up to a maximum of the total
127 gfx::Rect potential_new_recorded_viewport
= visible_layer_rect
;
128 potential_new_recorded_viewport
.Inset(-pixel_record_distance_
,
129 -pixel_record_distance_
);
130 potential_new_recorded_viewport
.Intersect(gfx::Rect(GetSize()));
133 ExposesEnoughNewArea(recorded_viewport_
, potential_new_recorded_viewport
,
135 gfx::Rect old_recorded_viewport
= recorded_viewport_
;
136 recorded_viewport_
= potential_new_recorded_viewport
;
138 // Invalidate newly-exposed and no-longer-exposed areas.
139 Region
newly_exposed_region(recorded_viewport_
);
140 newly_exposed_region
.Subtract(old_recorded_viewport
);
141 invalidation
->Union(newly_exposed_region
);
143 Region
no_longer_exposed_region(old_recorded_viewport
);
144 no_longer_exposed_region
.Subtract(recorded_viewport_
);
145 invalidation
->Union(no_longer_exposed_region
);
150 // Count the area that is being invalidated.
151 Region
recorded_invalidation(*invalidation
);
152 recorded_invalidation
.Intersect(recorded_viewport_
);
153 for (Region::Iterator
it(recorded_invalidation
); it
.has_rect(); it
.next())
154 timer
.AddArea(it
.rect().size().GetArea());
156 if (!updated
&& !invalidation
->Intersects(recorded_viewport_
))
159 ContentLayerClient::PaintingControlSetting painting_control
=
160 ContentLayerClient::PAINTING_BEHAVIOR_NORMAL
;
162 switch (recording_mode
) {
163 case RECORD_NORMALLY
:
164 // Already setup for normal recording.
166 case RECORD_WITH_PAINTING_DISABLED
:
167 painting_control
= ContentLayerClient::DISPLAY_LIST_PAINTING_DISABLED
;
169 case RECORD_WITH_CACHING_DISABLED
:
170 painting_control
= ContentLayerClient::DISPLAY_LIST_CACHING_DISABLED
;
172 case RECORD_WITH_CONSTRUCTION_DISABLED
:
173 painting_control
= ContentLayerClient::DISPLAY_LIST_CONSTRUCTION_DISABLED
;
176 // case RecordingSource::RECORD_WITH_SK_NULL_CANVAS should not be reached
180 // TODO(vmpstr): Add a slow_down_recording_scale_factor_for_debug_ to be able
181 // to slow down recording.
183 painter
->PaintContentsToDisplayList(recorded_viewport_
, painting_control
);
184 painter_reported_memory_usage_
= painter
->GetApproximateUnsharedMemoryUsage();
186 is_suitable_for_gpu_rasterization_
=
187 display_list_
->IsSuitableForGpuRasterization();
188 DetermineIfSolidColor();
189 display_list_
->EmitTraceSnapshot();
190 if (gather_pixel_refs_
)
191 display_list_
->GatherPixelRefs(grid_cell_size_
);
196 gfx::Size
DisplayListRecordingSource::GetSize() const {
200 void DisplayListRecordingSource::SetEmptyBounds() {
205 void DisplayListRecordingSource::SetSlowdownRasterScaleFactor(int factor
) {
206 slow_down_raster_scale_factor_for_debug_
= factor
;
209 void DisplayListRecordingSource::SetGatherPixelRefs(bool gather_pixel_refs
) {
210 gather_pixel_refs_
= gather_pixel_refs
;
213 void DisplayListRecordingSource::SetBackgroundColor(SkColor background_color
) {
214 background_color_
= background_color
;
217 void DisplayListRecordingSource::SetRequiresClear(bool requires_clear
) {
218 requires_clear_
= requires_clear
;
221 void DisplayListRecordingSource::SetUnsuitableForGpuRasterizationForTesting() {
222 is_suitable_for_gpu_rasterization_
= false;
225 bool DisplayListRecordingSource::IsSuitableForGpuRasterization() const {
226 return is_suitable_for_gpu_rasterization_
;
229 scoped_refptr
<RasterSource
> DisplayListRecordingSource::CreateRasterSource(
230 bool can_use_lcd_text
) const {
231 return scoped_refptr
<RasterSource
>(
232 DisplayListRasterSource::CreateFromDisplayListRecordingSource(
233 this, can_use_lcd_text
));
236 gfx::Size
DisplayListRecordingSource::GetTileGridSizeForTesting() const {
240 void DisplayListRecordingSource::DetermineIfSolidColor() {
241 DCHECK(display_list_
.get());
242 is_solid_color_
= false;
243 solid_color_
= SK_ColorTRANSPARENT
;
245 if (!display_list_
->ShouldBeAnalyzedForSolidColor())
248 gfx::Size layer_size
= GetSize();
249 skia::AnalysisCanvas
canvas(layer_size
.width(), layer_size
.height());
250 display_list_
->Raster(&canvas
, nullptr, gfx::Rect(), 1.f
);
251 is_solid_color_
= canvas
.GetColorIfSolid(&solid_color_
);
254 void DisplayListRecordingSource::Clear() {
255 recorded_viewport_
= gfx::Rect();
256 display_list_
= NULL
;
257 painter_reported_memory_usage_
= 0;
258 is_solid_color_
= false;