Roll src/third_party/skia 99c7c07:4af6580
[chromium-blink-merge.git] / cc / resources / display_list_recording_source.cc
blob1c16aca2c19dba3ee865488ccf193749e5ed74b6
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_PAINTING_DISABLED:
101 painting_control = ContentLayerClient::DISPLAY_LIST_PAINTING_DISABLED;
102 break;
103 case RECORD_WITH_CACHING_DISABLED:
104 painting_control = ContentLayerClient::DISPLAY_LIST_CACHING_DISABLED;
105 break;
106 case RECORD_WITH_CONSTRUCTION_DISABLED:
107 painting_control = ContentLayerClient::DISPLAY_LIST_CONSTRUCTION_DISABLED;
108 break;
109 default:
110 // case RecordingSource::RECORD_WITH_SK_NULL_CANVAS should not be reached
111 NOTREACHED();
114 int repeat_count = 1;
115 if (slow_down_raster_scale_factor_for_debug_ > 1) {
116 repeat_count = slow_down_raster_scale_factor_for_debug_;
117 painting_control = ContentLayerClient::DISPLAY_LIST_CACHING_DISABLED;
119 for (int i = 0; i < repeat_count; ++i) {
120 const bool use_cached_picture = true;
121 display_list_ =
122 DisplayItemList::Create(recorded_viewport_, use_cached_picture);
123 painter->PaintContentsToDisplayList(display_list_.get(), recorded_viewport_,
124 painting_control);
126 display_list_->CreateAndCacheSkPicture();
128 is_suitable_for_gpu_rasterization_ =
129 display_list_->IsSuitableForGpuRasterization();
130 DetermineIfSolidColor();
131 display_list_->EmitTraceSnapshot();
132 if (gather_pixel_refs_)
133 display_list_->GatherPixelRefs(grid_cell_size_);
135 return true;
138 gfx::Size DisplayListRecordingSource::GetSize() const {
139 return size_;
142 void DisplayListRecordingSource::SetEmptyBounds() {
143 size_ = gfx::Size();
144 Clear();
147 void DisplayListRecordingSource::SetSlowdownRasterScaleFactor(int factor) {
148 slow_down_raster_scale_factor_for_debug_ = factor;
151 void DisplayListRecordingSource::SetGatherPixelRefs(bool gather_pixel_refs) {
152 gather_pixel_refs_ = gather_pixel_refs;
155 void DisplayListRecordingSource::SetBackgroundColor(SkColor background_color) {
156 background_color_ = background_color;
159 void DisplayListRecordingSource::SetRequiresClear(bool requires_clear) {
160 requires_clear_ = requires_clear;
163 void DisplayListRecordingSource::SetUnsuitableForGpuRasterizationForTesting() {
164 is_suitable_for_gpu_rasterization_ = false;
167 bool DisplayListRecordingSource::IsSuitableForGpuRasterization() const {
168 return is_suitable_for_gpu_rasterization_;
171 scoped_refptr<RasterSource> DisplayListRecordingSource::CreateRasterSource(
172 bool can_use_lcd_text) const {
173 return scoped_refptr<RasterSource>(
174 DisplayListRasterSource::CreateFromDisplayListRecordingSource(
175 this, can_use_lcd_text));
178 gfx::Size DisplayListRecordingSource::GetTileGridSizeForTesting() const {
179 return gfx::Size();
182 void DisplayListRecordingSource::DetermineIfSolidColor() {
183 DCHECK(display_list_.get());
184 is_solid_color_ = false;
185 solid_color_ = SK_ColorTRANSPARENT;
187 if (display_list_->ApproximateOpCount() > kOpCountThatIsOkToAnalyze)
188 return;
190 gfx::Size layer_size = GetSize();
191 skia::AnalysisCanvas canvas(layer_size.width(), layer_size.height());
192 display_list_->Raster(&canvas, nullptr, 1.f);
193 is_solid_color_ = canvas.GetColorIfSolid(&solid_color_);
196 void DisplayListRecordingSource::Clear() {
197 recorded_viewport_ = gfx::Rect();
198 display_list_ = NULL;
199 is_solid_color_ = false;
202 } // namespace cc