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"
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"
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 DEFINE_SCOPED_UMA_HISTOGRAM_AREA_TIMER(
27 ScopedDisplayListRecordingSourceUpdateTimer
,
28 "Compositing.DisplayListRecordingSource.UpdateUs",
29 "Compositing.DisplayListRecordingSource.UpdateInvalidatedAreaPerMs");
35 DisplayListRecordingSource::DisplayListRecordingSource(
36 const gfx::Size
& grid_cell_size
,
37 bool use_cached_picture
)
38 : use_cached_picture_(use_cached_picture
),
39 slow_down_raster_scale_factor_for_debug_(0),
40 gather_pixel_refs_(false),
41 requires_clear_(false),
42 is_solid_color_(false),
43 solid_color_(SK_ColorTRANSPARENT
),
44 background_color_(SK_ColorTRANSPARENT
),
45 pixel_record_distance_(kPixelDistanceToRecord
),
46 grid_cell_size_(grid_cell_size
),
47 is_suitable_for_gpu_rasterization_(true) {
50 DisplayListRecordingSource::~DisplayListRecordingSource() {
53 bool DisplayListRecordingSource::UpdateAndExpandInvalidation(
54 ContentLayerClient
* painter
,
56 const gfx::Size
& layer_size
,
57 const gfx::Rect
& visible_layer_rect
,
59 RecordingMode recording_mode
) {
60 ScopedDisplayListRecordingSourceUpdateTimer timer
;
63 if (size_
!= layer_size
) {
68 gfx::Rect old_recorded_viewport
= recorded_viewport_
;
69 recorded_viewport_
= visible_layer_rect
;
70 recorded_viewport_
.Inset(-pixel_record_distance_
, -pixel_record_distance_
);
71 recorded_viewport_
.Intersect(gfx::Rect(GetSize()));
73 if (recorded_viewport_
!= old_recorded_viewport
) {
74 // Invalidate newly-exposed and no-longer-exposed areas.
75 Region
newly_exposed_region(recorded_viewport_
);
76 newly_exposed_region
.Subtract(old_recorded_viewport
);
77 invalidation
->Union(newly_exposed_region
);
79 Region
no_longer_exposed_region(old_recorded_viewport
);
80 no_longer_exposed_region
.Subtract(recorded_viewport_
);
81 invalidation
->Union(no_longer_exposed_region
);
86 // Count the area that is being invalidated.
87 Region
recorded_invalidation(*invalidation
);
88 recorded_invalidation
.Intersect(recorded_viewport_
);
89 for (Region::Iterator
it(recorded_invalidation
); it
.has_rect(); it
.next())
90 timer
.AddArea(it
.rect().size().GetArea());
92 if (!updated
&& !invalidation
->Intersects(recorded_viewport_
))
95 ContentLayerClient::PaintingControlSetting painting_control
=
96 ContentLayerClient::PAINTING_BEHAVIOR_NORMAL
;
98 switch (recording_mode
) {
100 // Already setup for normal recording.
102 case RECORD_WITH_PAINTING_DISABLED
:
103 painting_control
= ContentLayerClient::DISPLAY_LIST_PAINTING_DISABLED
;
105 case RECORD_WITH_CACHING_DISABLED
:
106 painting_control
= ContentLayerClient::DISPLAY_LIST_CACHING_DISABLED
;
108 case RECORD_WITH_CONSTRUCTION_DISABLED
:
109 painting_control
= ContentLayerClient::DISPLAY_LIST_CONSTRUCTION_DISABLED
;
112 // case RecordingSource::RECORD_WITH_SK_NULL_CANVAS should not be reached
116 int repeat_count
= 1;
117 if (slow_down_raster_scale_factor_for_debug_
> 1) {
118 repeat_count
= slow_down_raster_scale_factor_for_debug_
;
119 painting_control
= ContentLayerClient::DISPLAY_LIST_CACHING_DISABLED
;
121 for (int i
= 0; i
< repeat_count
; ++i
) {
123 DisplayItemList::Create(recorded_viewport_
, use_cached_picture_
);
124 painter
->PaintContentsToDisplayList(display_list_
.get(), recorded_viewport_
,
127 if (use_cached_picture_
)
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_
);
140 gfx::Size
DisplayListRecordingSource::GetSize() const {
144 void DisplayListRecordingSource::SetEmptyBounds() {
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 {
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
)
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;