Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(241)

Side by Side Diff: cc/resources/picture_pile.cc

Issue 78883002: Cluster invalidation rectangles into coherent regions to lessen SkPicture re-recording (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: A few more nits that I missed in the last upload Created 7 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2012 The Chromium Authors. All rights reserved. 1 // Copyright 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "cc/resources/picture_pile.h" 5 #include "cc/resources/picture_pile.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <limits> 8 #include <limits>
9 #include <vector> 9 #include <vector>
10 10
11 #include "cc/base/region.h" 11 #include "cc/base/region.h"
12 #include "cc/debug/rendering_stats_instrumentation.h" 12 #include "cc/debug/rendering_stats_instrumentation.h"
13 #include "cc/resources/picture_pile_impl.h" 13 #include "cc/resources/picture_pile_impl.h"
14 14
15 namespace { 15 namespace {
16 // Layout pixel buffer around the visible layer rect to record. Any base 16 // Layout pixel buffer around the visible layer rect to record. Any base
17 // picture that intersects the visible layer rect expanded by this distance 17 // picture that intersects the visible layer rect expanded by this distance
18 // will be recorded. 18 // will be recorded.
19 const int kPixelDistanceToRecord = 8000; 19 const int kPixelDistanceToRecord = 8000;
20
21 // TODO(humper): The density threshold here is somewhat arbitrary; need a
22 // way to set // this from the command line so we can write a benchmark
23 // script and find a sweet spot.
24 const float kDensityTreshold = 0.5f;
25
26 bool rect_sort_y(const gfx::Rect &r1, const gfx::Rect &r2) {
27 return r1.y() < r2.y() || (r1.y() == r2.y() && r1.x() < r2.x());
28 }
29
30 bool rect_sort_x(const gfx::Rect &r1, const gfx::Rect &r2) {
31 return r1.x() < r2.x() || (r1.x() == r2.x() && r1.y() < r2.y());
32 }
33
20 } // namespace 34 } // namespace
21 35
22 namespace cc { 36 namespace cc {
23 37
24 PicturePile::PicturePile() { 38 PicturePile::PicturePile() {
25 } 39 }
26 40
27 PicturePile::~PicturePile() { 41 PicturePile::~PicturePile() {
28 } 42 }
29 43
44 float do_clustering(const std::vector<gfx::Rect>& tiles,
vmpstr 2013/11/21 19:47:59 You can move all of the non member functions to th
humper 2013/11/21 20:51:14 Done.
45 std::vector<gfx::Rect>* clustered_rects) {
46 TRACE_EVENT0("cc.debug", "do_clustering");
47
48 // These variables track the record area and invalid area
49 // for the entire clustering
50 int total_record_area = 0;
51 int total_invalid_area = 0;
52
53 // These variables track the record area and invalid area
54 // for the current cluster being constructed.
55 gfx::Rect cur_record_rect;
56 int total_area = 0, invalid_area = 0;
57
58 for (std::vector<gfx::Rect>::const_iterator it = tiles.begin();
59 it != tiles.end();
60 it++) {
61 gfx::Rect invalid_tile = *it;
62
63 // For each tile, we consider adding the invalid tile to the
64 // current record rectangle. Only add it if the amount of empty
65 // space created is below a density threshold.
66 int tile_area = invalid_tile.width() * invalid_tile.height();
67
68 gfx::Rect proposed_union = cur_record_rect;
69 proposed_union.Union(invalid_tile);
70 int proposed_area = proposed_union.width() * proposed_union.height();
71 float proposed_density =
72 static_cast<float>(invalid_area + tile_area) /
73 static_cast<float>(proposed_area);
74
75 if (proposed_density >= kDensityTreshold) {
76 // It's okay to add this invalid tile to the
77 // current recording rectangle.
78 cur_record_rect = proposed_union;
79 total_area = proposed_area;
80 invalid_area += tile_area;
81 total_invalid_area += tile_area;
82 } else {
83 // Adding this invalid tile to the current recording rectangle
84 // would exceed our badness threshold, so put the current rectangle
85 // in the list of recording rects, and start a new one.
86 clustered_rects->push_back(cur_record_rect);
87 total_record_area += total_area;
88 cur_record_rect = invalid_tile;
89 invalid_area = tile_area;
90 total_area = tile_area;
91 }
92 }
93
94 if (!cur_record_rect.IsEmpty()) {
95 clustered_rects->push_back(cur_record_rect);
96 total_record_area += total_area;
97 }
98
99 if (total_record_area == 0) {
100 return 1;
101 } else {
102 return static_cast<float>(total_invalid_area) /
103 static_cast<float>(total_record_area);
104 }
105 }
106
107 float ClusterTiles(const std::vector<gfx::Rect>& invalid_tiles,
108 std::vector<gfx::Rect>* record_rects) {
109 TRACE_EVENT1("cc.debug", "ClusterTiles",
110 "number of tiles",
111 invalid_tiles.size());
112
113 if (invalid_tiles.size() <= 1) {
114 // Quickly handle the special case for common
115 // single-invalidation update, and also the less common
116 // case of no tiles passed in.
117 *record_rects = invalid_tiles;
118 return 1;
119 }
120
121 // Sort the invalid tiles by y coordinate.
122 std::vector<gfx::Rect> invalid_tiles_vertical = invalid_tiles;
123 std::sort(invalid_tiles_vertical.begin(),
124 invalid_tiles_vertical.end(),
125 rect_sort_y);
126
127 float vertical_density;
128 std::vector<gfx::Rect> vertical_clustering;
129 vertical_density = do_clustering(invalid_tiles_vertical,
130 &vertical_clustering);
131
132 // Now try again with a horizontal sort, see which one is best
133 // TODO(humper): Heuristics for skipping this step?
vmpstr 2013/11/21 19:47:59 I think we should either just skip the horizontal
humper 2013/11/21 20:51:14 I'm concerned about sites that have two long verti
134 std::vector<gfx::Rect> invalid_tiles_horizontal = invalid_tiles;
135 std::sort(invalid_tiles_vertical.begin(),
136 invalid_tiles_vertical.end(),
137 rect_sort_x);
138
139 float horizontal_density;
140 std::vector<gfx::Rect> horizontal_clustering;
141 horizontal_density = do_clustering(invalid_tiles_vertical,
142 &horizontal_clustering);
143
144 if (vertical_density < horizontal_density) {
145 *record_rects = horizontal_clustering;
146 return horizontal_density;
147 } else {
148 *record_rects = vertical_clustering;
149 return vertical_density;
150 }
151 }
152
30 bool PicturePile::Update( 153 bool PicturePile::Update(
31 ContentLayerClient* painter, 154 ContentLayerClient* painter,
32 SkColor background_color, 155 SkColor background_color,
33 bool contents_opaque, 156 bool contents_opaque,
34 const Region& invalidation, 157 const Region& invalidation,
35 gfx::Rect visible_layer_rect, 158 gfx::Rect visible_layer_rect,
36 RenderingStatsInstrumentation* stats_instrumentation) { 159 RenderingStatsInstrumentation* stats_instrumentation) {
37 background_color_ = background_color; 160 background_color_ = background_color;
38 contents_opaque_ = contents_opaque; 161 contents_opaque_ = contents_opaque;
39 162
(...skipping 14 matching lines...) Expand all
54 const PictureMapKey& key = iter.index(); 177 const PictureMapKey& key = iter.index();
55 178
56 PictureMap::iterator picture_it = picture_map_.find(key); 179 PictureMap::iterator picture_it = picture_map_.find(key);
57 if (picture_it == picture_map_.end()) 180 if (picture_it == picture_map_.end())
58 continue; 181 continue;
59 182
60 invalidated = picture_it->second.Invalidate() || invalidated; 183 invalidated = picture_it->second.Invalidate() || invalidated;
61 } 184 }
62 } 185 }
63 186
64 gfx::Rect record_rect; 187 // Make a list of all invalid tiles; we will attempt to
188 // cluster these into multiple invalidation regions.
189 std::vector<gfx::Rect> invalid_tiles;
190
65 for (TilingData::Iterator it(&tiling_, interest_rect); 191 for (TilingData::Iterator it(&tiling_, interest_rect);
66 it; ++it) { 192 it; ++it) {
67 const PictureMapKey& key = it.index(); 193 const PictureMapKey& key = it.index();
68 const PictureInfo& info = picture_map_[key]; 194 const PictureInfo& info = picture_map_[key];
69 if (!info.picture.get()) { 195 if (!info.picture.get()) {
70 gfx::Rect tile = PaddedRect(key); 196 gfx::Rect tile = tiling_.TileBounds(key.first, key.second);
71 record_rect.Union(tile); 197 invalid_tiles.push_back(tile);
72 } 198 }
73 } 199 }
74 200
75 if (record_rect.IsEmpty()) { 201 std::vector<gfx::Rect> record_rects;
202 ClusterTiles(invalid_tiles, &record_rects);
203
204 if (record_rects.empty()) {
76 if (invalidated) 205 if (invalidated)
77 UpdateRecordedRegion(); 206 UpdateRecordedRegion();
78 return invalidated; 207 return invalidated;
79 } 208 }
80 209
81 int repeat_count = std::max(1, slow_down_raster_scale_factor_for_debug_); 210 for (std::vector<gfx::Rect>::iterator it = record_rects.begin();
82 scoped_refptr<Picture> picture = Picture::Create(record_rect); 211 it != record_rects.end();
212 it++) {
213 gfx::Rect record_rect = *it;
214 record_rect.Inset(-buffer_pixels(), -buffer_pixels(),
215 -buffer_pixels(), -buffer_pixels());
83 216
84 { 217 int repeat_count = std::max(1, slow_down_raster_scale_factor_for_debug_);
85 base::TimeDelta best_duration = base::TimeDelta::FromInternalValue( 218 scoped_refptr<Picture> picture = Picture::Create(record_rect);
86 std::numeric_limits<int64>::max()); 219
87 for (int i = 0; i < repeat_count; i++) { 220 {
88 base::TimeTicks start_time = stats_instrumentation->StartRecording(); 221 base::TimeDelta best_duration = base::TimeDelta::FromInternalValue(
89 picture->Record(painter, tile_grid_info_); 222 std::numeric_limits<int64>::max());
90 base::TimeDelta duration = 223 for (int i = 0; i < repeat_count; i++) {
91 stats_instrumentation->EndRecording(start_time); 224 base::TimeTicks start_time = stats_instrumentation->StartRecording();
92 best_duration = std::min(duration, best_duration); 225 picture->Record(painter, tile_grid_info_);
226 base::TimeDelta duration =
227 stats_instrumentation->EndRecording(start_time);
228 best_duration = std::min(duration, best_duration);
229 }
230 int recorded_pixel_count =
231 picture->LayerRect().width() * picture->LayerRect().height();
232 stats_instrumentation->AddRecord(best_duration, recorded_pixel_count);
233 if (num_raster_threads_ > 1)
234 picture->GatherPixelRefs(tile_grid_info_);
235 picture->CloneForDrawing(num_raster_threads_);
93 } 236 }
94 int recorded_pixel_count =
95 picture->LayerRect().width() * picture->LayerRect().height();
96 stats_instrumentation->AddRecord(best_duration, recorded_pixel_count);
97 if (num_raster_threads_ > 1)
98 picture->GatherPixelRefs(tile_grid_info_);
99 picture->CloneForDrawing(num_raster_threads_);
100 }
101 237
102 for (TilingData::Iterator it(&tiling_, record_rect); 238 for (TilingData::Iterator it(&tiling_, record_rect);
103 it; ++it) { 239 it; ++it) {
104 const PictureMapKey& key = it.index(); 240 const PictureMapKey& key = it.index();
105 gfx::Rect tile = PaddedRect(key); 241 gfx::Rect tile = PaddedRect(key);
106 if (record_rect.Contains(tile)) { 242 if (record_rect.Contains(tile)) {
107 PictureInfo& info = picture_map_[key]; 243 PictureInfo& info = picture_map_[key];
108 info.picture = picture; 244 info.picture = picture;
245 }
109 } 246 }
110 } 247 }
111 248
112 UpdateRecordedRegion(); 249 UpdateRecordedRegion();
113 return true; 250 return true;
114 } 251 }
115 252
116 } // namespace cc 253 } // namespace cc
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698