OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "cc/resources/picture_pile.h" | |
6 | |
7 #include <algorithm> | |
8 #include <limits> | |
9 #include <vector> | |
10 | |
11 #include "cc/base/region.h" | |
12 #include "cc/resources/picture_pile_impl.h" | |
13 #include "skia/ext/analysis_canvas.h" | |
14 | |
15 namespace { | |
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 | |
18 // will be recorded. | |
19 const int kPixelDistanceToRecord = 8000; | |
20 // We don't perform solid color analysis on images that have more than 10 skia | |
21 // operations. | |
22 const int kOpCountThatIsOkToAnalyze = 10; | |
23 | |
24 // Dimensions of the tiles in this picture pile as well as the dimensions of | |
25 // the base picture in each tile. | |
26 const int kBasePictureSize = 512; | |
27 | |
28 // TODO(humper): The density threshold here is somewhat arbitrary; need a | |
29 // way to set // this from the command line so we can write a benchmark | |
30 // script and find a sweet spot. | |
31 const float kDensityThreshold = 0.5f; | |
32 | |
33 bool rect_sort_y(const gfx::Rect& r1, const gfx::Rect& r2) { | |
34 return r1.y() < r2.y() || (r1.y() == r2.y() && r1.x() < r2.x()); | |
35 } | |
36 | |
37 bool rect_sort_x(const gfx::Rect& r1, const gfx::Rect& r2) { | |
38 return r1.x() < r2.x() || (r1.x() == r2.x() && r1.y() < r2.y()); | |
39 } | |
40 | |
41 float PerformClustering(const std::vector<gfx::Rect>& tiles, | |
42 std::vector<gfx::Rect>* clustered_rects) { | |
43 // These variables track the record area and invalid area | |
44 // for the entire clustering | |
45 int total_record_area = 0; | |
46 int total_invalid_area = 0; | |
47 | |
48 // These variables track the record area and invalid area | |
49 // for the current cluster being constructed. | |
50 gfx::Rect cur_record_rect; | |
51 int cluster_record_area = 0, cluster_invalid_area = 0; | |
52 | |
53 for (std::vector<gfx::Rect>::const_iterator it = tiles.begin(); | |
54 it != tiles.end(); | |
55 it++) { | |
56 gfx::Rect invalid_tile = *it; | |
57 | |
58 // For each tile, we consider adding the invalid tile to the | |
59 // current record rectangle. Only add it if the amount of empty | |
60 // space created is below a density threshold. | |
61 int tile_area = invalid_tile.width() * invalid_tile.height(); | |
62 | |
63 gfx::Rect proposed_union = cur_record_rect; | |
64 proposed_union.Union(invalid_tile); | |
65 int proposed_area = proposed_union.width() * proposed_union.height(); | |
66 float proposed_density = | |
67 static_cast<float>(cluster_invalid_area + tile_area) / | |
68 static_cast<float>(proposed_area); | |
69 | |
70 if (proposed_density >= kDensityThreshold) { | |
71 // It's okay to add this invalid tile to the | |
72 // current recording rectangle. | |
73 cur_record_rect = proposed_union; | |
74 cluster_record_area = proposed_area; | |
75 cluster_invalid_area += tile_area; | |
76 total_invalid_area += tile_area; | |
77 } else { | |
78 // Adding this invalid tile to the current recording rectangle | |
79 // would exceed our badness threshold, so put the current rectangle | |
80 // in the list of recording rects, and start a new one. | |
81 clustered_rects->push_back(cur_record_rect); | |
82 total_record_area += cluster_record_area; | |
83 cur_record_rect = invalid_tile; | |
84 cluster_invalid_area = tile_area; | |
85 cluster_record_area = tile_area; | |
86 } | |
87 } | |
88 | |
89 DCHECK(!cur_record_rect.IsEmpty()); | |
90 clustered_rects->push_back(cur_record_rect); | |
91 total_record_area += cluster_record_area;; | |
92 | |
93 DCHECK_NE(total_record_area, 0); | |
94 | |
95 return static_cast<float>(total_invalid_area) / | |
96 static_cast<float>(total_record_area); | |
97 } | |
98 | |
99 void ClusterTiles(const std::vector<gfx::Rect>& invalid_tiles, | |
100 std::vector<gfx::Rect>* record_rects) { | |
101 TRACE_EVENT1("cc", "ClusterTiles", | |
102 "count", | |
103 invalid_tiles.size()); | |
104 if (invalid_tiles.size() <= 1) { | |
105 // Quickly handle the special case for common | |
106 // single-invalidation update, and also the less common | |
107 // case of no tiles passed in. | |
108 *record_rects = invalid_tiles; | |
109 return; | |
110 } | |
111 | |
112 // Sort the invalid tiles by y coordinate. | |
113 std::vector<gfx::Rect> invalid_tiles_vertical = invalid_tiles; | |
114 std::sort(invalid_tiles_vertical.begin(), | |
115 invalid_tiles_vertical.end(), | |
116 rect_sort_y); | |
117 | |
118 std::vector<gfx::Rect> vertical_clustering; | |
119 float vertical_density = | |
120 PerformClustering(invalid_tiles_vertical, &vertical_clustering); | |
121 | |
122 // If vertical density is optimal, then we can return early. | |
123 if (vertical_density == 1.f) { | |
124 *record_rects = vertical_clustering; | |
125 return; | |
126 } | |
127 | |
128 // Now try again with a horizontal sort, see which one is best | |
129 std::vector<gfx::Rect> invalid_tiles_horizontal = invalid_tiles; | |
130 std::sort(invalid_tiles_horizontal.begin(), | |
131 invalid_tiles_horizontal.end(), | |
132 rect_sort_x); | |
133 | |
134 std::vector<gfx::Rect> horizontal_clustering; | |
135 float horizontal_density = | |
136 PerformClustering(invalid_tiles_horizontal, &horizontal_clustering); | |
137 | |
138 if (vertical_density < horizontal_density) { | |
139 *record_rects = horizontal_clustering; | |
140 return; | |
141 } | |
142 | |
143 *record_rects = vertical_clustering; | |
144 } | |
145 | |
146 #ifdef NDEBUG | |
147 const bool kDefaultClearCanvasSetting = false; | |
148 #else | |
149 const bool kDefaultClearCanvasSetting = true; | |
150 #endif | |
151 | |
152 } // namespace | |
153 | |
154 namespace cc { | |
155 | |
156 PicturePile::PicturePile(float min_contents_scale, | |
157 const gfx::Size& tile_grid_size) | |
158 : min_contents_scale_(0), | |
159 slow_down_raster_scale_factor_for_debug_(0), | |
160 gather_pixel_refs_(false), | |
161 has_any_recordings_(false), | |
162 clear_canvas_with_debug_color_(kDefaultClearCanvasSetting), | |
163 requires_clear_(true), | |
164 is_solid_color_(false), | |
165 solid_color_(SK_ColorTRANSPARENT), | |
166 background_color_(SK_ColorTRANSPARENT), | |
167 pixel_record_distance_(kPixelDistanceToRecord), | |
168 is_suitable_for_gpu_rasterization_(true) { | |
169 tiling_.SetMaxTextureSize(gfx::Size(kBasePictureSize, kBasePictureSize)); | |
170 SetMinContentsScale(min_contents_scale); | |
171 SetTileGridSize(tile_grid_size); | |
172 } | |
173 | |
174 PicturePile::~PicturePile() { | |
175 } | |
176 | |
177 bool PicturePile::UpdateAndExpandInvalidation( | |
178 ContentLayerClient* painter, | |
179 Region* invalidation, | |
180 const gfx::Size& layer_size, | |
181 const gfx::Rect& visible_layer_rect, | |
182 int frame_number, | |
183 RecordingSource::RecordingMode recording_mode) { | |
184 gfx::Rect interest_rect = visible_layer_rect; | |
185 interest_rect.Inset(-pixel_record_distance_, -pixel_record_distance_); | |
186 recorded_viewport_ = interest_rect; | |
187 recorded_viewport_.Intersect(gfx::Rect(layer_size)); | |
188 | |
189 bool updated = ApplyInvalidationAndResize(interest_rect, invalidation, | |
190 layer_size, frame_number); | |
191 std::vector<gfx::Rect> invalid_tiles; | |
192 GetInvalidTileRects(interest_rect, &invalid_tiles); | |
193 std::vector<gfx::Rect> record_rects; | |
194 ClusterTiles(invalid_tiles, &record_rects); | |
195 | |
196 if (record_rects.empty()) | |
197 return updated; | |
198 | |
199 CreatePictures(painter, recording_mode, record_rects); | |
200 | |
201 DetermineIfSolidColor(); | |
202 | |
203 has_any_recordings_ = true; | |
204 DCHECK(CanRasterSlowTileCheck(recorded_viewport_)); | |
205 return true; | |
206 } | |
207 | |
208 bool PicturePile::ApplyInvalidationAndResize(const gfx::Rect& interest_rect, | |
209 Region* invalidation, | |
210 const gfx::Size& layer_size, | |
211 int frame_number) { | |
212 bool updated = false; | |
213 | |
214 Region synthetic_invalidation; | |
215 gfx::Size old_tiling_size = GetSize(); | |
216 if (old_tiling_size != layer_size) { | |
217 tiling_.SetTilingSize(layer_size); | |
218 updated = true; | |
219 } | |
220 | |
221 gfx::Rect interest_rect_over_tiles = | |
222 tiling_.ExpandRectToTileBounds(interest_rect); | |
223 | |
224 if (old_tiling_size != layer_size) { | |
225 gfx::Size min_tiling_size( | |
226 std::min(GetSize().width(), old_tiling_size.width()), | |
227 std::min(GetSize().height(), old_tiling_size.height())); | |
228 gfx::Size max_tiling_size( | |
229 std::max(GetSize().width(), old_tiling_size.width()), | |
230 std::max(GetSize().height(), old_tiling_size.height())); | |
231 | |
232 has_any_recordings_ = false; | |
233 | |
234 // Drop recordings that are outside the new or old layer bounds or that | |
235 // changed size. Newly exposed areas are considered invalidated. | |
236 // Previously exposed areas that are now outside of bounds also need to | |
237 // be invalidated, as they may become part of raster when scale < 1. | |
238 std::vector<PictureMapKey> to_erase; | |
239 int min_toss_x = tiling_.num_tiles_x(); | |
240 if (max_tiling_size.width() > min_tiling_size.width()) { | |
241 min_toss_x = | |
242 tiling_.FirstBorderTileXIndexFromSrcCoord(min_tiling_size.width()); | |
243 } | |
244 int min_toss_y = tiling_.num_tiles_y(); | |
245 if (max_tiling_size.height() > min_tiling_size.height()) { | |
246 min_toss_y = | |
247 tiling_.FirstBorderTileYIndexFromSrcCoord(min_tiling_size.height()); | |
248 } | |
249 for (const auto& key_picture_pair : picture_map_) { | |
250 const PictureMapKey& key = key_picture_pair.first; | |
251 if (key.first < min_toss_x && key.second < min_toss_y) { | |
252 has_any_recordings_ = true; | |
253 continue; | |
254 } | |
255 to_erase.push_back(key); | |
256 } | |
257 | |
258 for (size_t i = 0; i < to_erase.size(); ++i) | |
259 picture_map_.erase(to_erase[i]); | |
260 | |
261 // If a recording is dropped and not re-recorded below, invalidate that | |
262 // full recording to cause any raster tiles that would use it to be | |
263 // dropped. | |
264 // If the recording will be replaced below, invalidate newly exposed | |
265 // areas and previously exposed areas to force raster tiles that include the | |
266 // old recording to know there is new recording to display. | |
267 gfx::Rect min_tiling_rect_over_tiles = | |
268 tiling_.ExpandRectToTileBounds(gfx::Rect(min_tiling_size)); | |
269 if (min_toss_x < tiling_.num_tiles_x()) { | |
270 // The bounds which we want to invalidate are the tiles along the old | |
271 // edge of the pile when expanding, or the new edge of the pile when | |
272 // shrinking. In either case, it's the difference of the two, so we'll | |
273 // call this bounding box the DELTA EDGE RECT. | |
274 // | |
275 // In the picture below, the delta edge rect would be the bounding box of | |
276 // tiles {h,i,j}. |min_toss_x| would be equal to the horizontal index of | |
277 // the same tiles. | |
278 // | |
279 // min pile edge-v max pile edge-v | |
280 // ---------------+ - - - - - - - -+ | |
281 // mmppssvvyybbeeh|h . | |
282 // mmppssvvyybbeeh|h . | |
283 // nnqqttwwzzccffi|i . | |
284 // nnqqttwwzzccffi|i . | |
285 // oorruuxxaaddggj|j . | |
286 // oorruuxxaaddggj|j . | |
287 // ---------------+ - - - - - - - -+ <- min pile edge | |
288 // . | |
289 // - - - - - - - - - - - - - - - -+ <- max pile edge | |
290 // | |
291 // If you were to slide a vertical beam from the left edge of the | |
292 // delta edge rect toward the right, it would either hit the right edge | |
293 // of the delta edge rect, or the interest rect (expanded to the bounds | |
294 // of the tiles it touches). The same is true for a beam parallel to | |
295 // any of the four edges, sliding across the delta edge rect. We use | |
296 // the union of these four rectangles generated by these beams to | |
297 // determine which part of the delta edge rect is outside of the expanded | |
298 // interest rect. | |
299 // | |
300 // Case 1: Intersect rect is outside the delta edge rect. It can be | |
301 // either on the left or the right. The |left_rect| and |right_rect|, | |
302 // cover this case, one will be empty and one will cover the full | |
303 // delta edge rect. In the picture below, |left_rect| would cover the | |
304 // delta edge rect, and |right_rect| would be empty. | |
305 // +----------------------+ |^^^^^^^^^^^^^^^| | |
306 // |===> DELTA EDGE RECT | | | | |
307 // |===> | | INTEREST RECT | | |
308 // |===> | | | | |
309 // |===> | | | | |
310 // +----------------------+ |vvvvvvvvvvvvvvv| | |
311 // | |
312 // Case 2: Interest rect is inside the delta edge rect. It will always | |
313 // fill the entire delta edge rect horizontally since the old edge rect | |
314 // is a single tile wide, and the interest rect has been expanded to the | |
315 // bounds of the tiles it touches. In this case the |left_rect| and | |
316 // |right_rect| will be empty, but the case is handled by the |top_rect| | |
317 // and |bottom_rect|. In the picture below, neither the |top_rect| nor | |
318 // |bottom_rect| would empty, they would each cover the area of the old | |
319 // edge rect outside the expanded interest rect. | |
320 // +-----------------+ | |
321 // |:::::::::::::::::| | |
322 // |:::::::::::::::::| | |
323 // |vvvvvvvvvvvvvvvvv| | |
324 // | | | |
325 // +-----------------+ | |
326 // | INTEREST RECT | | |
327 // | | | |
328 // +-----------------+ | |
329 // | | | |
330 // | DELTA EDGE RECT | | |
331 // +-----------------+ | |
332 // | |
333 // Lastly, we need to consider tiles inside the expanded interest rect. | |
334 // For those tiles, we want to invalidate exactly the newly exposed | |
335 // pixels. In the picture below the tiles in the delta edge rect have | |
336 // been resized and the area covered by periods must be invalidated. The | |
337 // |exposed_rect| will cover exactly that area. | |
338 // v-min pile edge | |
339 // +---------+-------+ | |
340 // | ........| | |
341 // | ........| | |
342 // | DELTA EDGE.RECT.| | |
343 // | ........| | |
344 // | ........| | |
345 // | ........| | |
346 // | ........| | |
347 // | ........| | |
348 // | ........| | |
349 // +---------+-------+ | |
350 | |
351 int left = tiling_.TilePositionX(min_toss_x); | |
352 int right = left + tiling_.TileSizeX(min_toss_x); | |
353 int top = min_tiling_rect_over_tiles.y(); | |
354 int bottom = min_tiling_rect_over_tiles.bottom(); | |
355 | |
356 int left_until = std::min(interest_rect_over_tiles.x(), right); | |
357 int right_until = std::max(interest_rect_over_tiles.right(), left); | |
358 int top_until = std::min(interest_rect_over_tiles.y(), bottom); | |
359 int bottom_until = std::max(interest_rect_over_tiles.bottom(), top); | |
360 | |
361 int exposed_left = min_tiling_size.width(); | |
362 int exposed_left_until = max_tiling_size.width(); | |
363 int exposed_top = top; | |
364 int exposed_bottom = max_tiling_size.height(); | |
365 DCHECK_GE(exposed_left, left); | |
366 | |
367 gfx::Rect left_rect(left, top, left_until - left, bottom - top); | |
368 gfx::Rect right_rect(right_until, top, right - right_until, bottom - top); | |
369 gfx::Rect top_rect(left, top, right - left, top_until - top); | |
370 gfx::Rect bottom_rect( | |
371 left, bottom_until, right - left, bottom - bottom_until); | |
372 gfx::Rect exposed_rect(exposed_left, | |
373 exposed_top, | |
374 exposed_left_until - exposed_left, | |
375 exposed_bottom - exposed_top); | |
376 synthetic_invalidation.Union(left_rect); | |
377 synthetic_invalidation.Union(right_rect); | |
378 synthetic_invalidation.Union(top_rect); | |
379 synthetic_invalidation.Union(bottom_rect); | |
380 synthetic_invalidation.Union(exposed_rect); | |
381 } | |
382 if (min_toss_y < tiling_.num_tiles_y()) { | |
383 // The same thing occurs here as in the case above, but the invalidation | |
384 // rect is the bounding box around the bottom row of tiles in the min | |
385 // pile. This would be tiles {o,r,u,x,a,d,g,j} in the above picture. | |
386 | |
387 int top = tiling_.TilePositionY(min_toss_y); | |
388 int bottom = top + tiling_.TileSizeY(min_toss_y); | |
389 int left = min_tiling_rect_over_tiles.x(); | |
390 int right = min_tiling_rect_over_tiles.right(); | |
391 | |
392 int top_until = std::min(interest_rect_over_tiles.y(), bottom); | |
393 int bottom_until = std::max(interest_rect_over_tiles.bottom(), top); | |
394 int left_until = std::min(interest_rect_over_tiles.x(), right); | |
395 int right_until = std::max(interest_rect_over_tiles.right(), left); | |
396 | |
397 int exposed_top = min_tiling_size.height(); | |
398 int exposed_top_until = max_tiling_size.height(); | |
399 int exposed_left = left; | |
400 int exposed_right = max_tiling_size.width(); | |
401 DCHECK_GE(exposed_top, top); | |
402 | |
403 gfx::Rect left_rect(left, top, left_until - left, bottom - top); | |
404 gfx::Rect right_rect(right_until, top, right - right_until, bottom - top); | |
405 gfx::Rect top_rect(left, top, right - left, top_until - top); | |
406 gfx::Rect bottom_rect( | |
407 left, bottom_until, right - left, bottom - bottom_until); | |
408 gfx::Rect exposed_rect(exposed_left, | |
409 exposed_top, | |
410 exposed_right - exposed_left, | |
411 exposed_top_until - exposed_top); | |
412 synthetic_invalidation.Union(left_rect); | |
413 synthetic_invalidation.Union(right_rect); | |
414 synthetic_invalidation.Union(top_rect); | |
415 synthetic_invalidation.Union(bottom_rect); | |
416 synthetic_invalidation.Union(exposed_rect); | |
417 } | |
418 } | |
419 | |
420 // Detect cases where the full pile is invalidated, in this situation we | |
421 // can just drop/invalidate everything. | |
422 if (invalidation->Contains(gfx::Rect(old_tiling_size)) || | |
423 invalidation->Contains(gfx::Rect(GetSize()))) { | |
424 updated = !picture_map_.empty(); | |
425 picture_map_.clear(); | |
426 } else { | |
427 // Expand invalidation that is on tiles that aren't in the interest rect and | |
428 // will not be re-recorded below. These tiles are no longer valid and should | |
429 // be considerered fully invalid, so we can know to not keep around raster | |
430 // tiles that intersect with these recording tiles. | |
431 Region invalidation_expanded_to_full_tiles; | |
432 | |
433 for (Region::Iterator i(*invalidation); i.has_rect(); i.next()) { | |
434 gfx::Rect invalid_rect = i.rect(); | |
435 | |
436 // This rect covers the bounds (excluding borders) of all tiles whose | |
437 // bounds (including borders) touch the |interest_rect|. This matches | |
438 // the iteration of the |invalid_rect| below which includes borders when | |
439 // calling Invalidate() on pictures. | |
440 gfx::Rect invalid_rect_outside_interest_rect_tiles = | |
441 tiling_.ExpandRectToTileBounds(invalid_rect); | |
442 // We subtract the |interest_rect_over_tiles| which represents the bounds | |
443 // of tiles that will be re-recorded below. This matches the iteration of | |
444 // |interest_rect| below which includes borders. | |
445 // TODO(danakj): We should have a Rect-subtract-Rect-to-2-rects operator | |
446 // instead of using Rect::Subtract which gives you the bounding box of the | |
447 // subtraction. | |
448 invalid_rect_outside_interest_rect_tiles.Subtract( | |
449 interest_rect_over_tiles); | |
450 invalidation_expanded_to_full_tiles.Union( | |
451 invalid_rect_outside_interest_rect_tiles); | |
452 | |
453 // Split this inflated invalidation across tile boundaries and apply it | |
454 // to all tiles that it touches. | |
455 bool include_borders = true; | |
456 for (TilingData::Iterator iter(&tiling_, invalid_rect, include_borders); | |
457 iter; | |
458 ++iter) { | |
459 const PictureMapKey& key = iter.index(); | |
460 | |
461 PictureMap::iterator picture_it = picture_map_.find(key); | |
462 if (picture_it == picture_map_.end()) | |
463 continue; | |
464 | |
465 updated = true; | |
466 picture_map_.erase(key); | |
467 | |
468 // Invalidate drops the picture so the whole tile better be invalidated | |
469 // if it won't be re-recorded below. | |
470 DCHECK_IMPLIES(!tiling_.TileBounds(key.first, key.second) | |
471 .Intersects(interest_rect_over_tiles), | |
472 invalidation_expanded_to_full_tiles.Contains( | |
473 tiling_.TileBounds(key.first, key.second))); | |
474 } | |
475 } | |
476 invalidation->Union(invalidation_expanded_to_full_tiles); | |
477 } | |
478 | |
479 invalidation->Union(synthetic_invalidation); | |
480 return updated; | |
481 } | |
482 | |
483 void PicturePile::GetInvalidTileRects(const gfx::Rect& interest_rect, | |
484 std::vector<gfx::Rect>* invalid_tiles) { | |
485 // Make a list of all invalid tiles; we will attempt to | |
486 // cluster these into multiple invalidation regions. | |
487 bool include_borders = true; | |
488 for (TilingData::Iterator it(&tiling_, interest_rect, include_borders); it; | |
489 ++it) { | |
490 const PictureMapKey& key = it.index(); | |
491 if (picture_map_.find(key) == picture_map_.end()) | |
492 invalid_tiles->push_back(tiling_.TileBounds(key.first, key.second)); | |
493 } | |
494 } | |
495 | |
496 void PicturePile::CreatePictures(ContentLayerClient* painter, | |
497 RecordingSource::RecordingMode recording_mode, | |
498 const std::vector<gfx::Rect>& record_rects) { | |
499 for (const auto& record_rect : record_rects) { | |
500 gfx::Rect padded_record_rect = PadRect(record_rect); | |
501 | |
502 int repeat_count = std::max(1, slow_down_raster_scale_factor_for_debug_); | |
503 scoped_refptr<Picture> picture; | |
504 | |
505 for (int i = 0; i < repeat_count; i++) { | |
506 picture = Picture::Create(padded_record_rect, painter, tile_grid_size_, | |
507 gather_pixel_refs_, recording_mode); | |
508 // Note the '&&' with previous is-suitable state. | |
509 // This means that once a picture-pile becomes unsuitable for gpu | |
510 // rasterization due to some content, it will continue to be unsuitable | |
511 // even if that content is replaced by gpu-friendly content. | |
512 // This is an optimization to avoid iterating though all pictures in | |
513 // the pile after each invalidation. | |
514 if (is_suitable_for_gpu_rasterization_) { | |
515 const char* reason = nullptr; | |
516 is_suitable_for_gpu_rasterization_ &= | |
517 picture->IsSuitableForGpuRasterization(&reason); | |
518 | |
519 if (!is_suitable_for_gpu_rasterization_) { | |
520 TRACE_EVENT_INSTANT1("cc", "GPU Rasterization Veto", | |
521 TRACE_EVENT_SCOPE_THREAD, "reason", reason); | |
522 } | |
523 } | |
524 } | |
525 | |
526 bool found_tile_for_recorded_picture = false; | |
527 | |
528 bool include_borders = true; | |
529 for (TilingData::Iterator it(&tiling_, padded_record_rect, include_borders); | |
530 it; ++it) { | |
531 const PictureMapKey& key = it.index(); | |
532 gfx::Rect tile = PaddedRect(key); | |
533 if (padded_record_rect.Contains(tile)) { | |
534 picture_map_[key] = picture; | |
535 found_tile_for_recorded_picture = true; | |
536 } | |
537 } | |
538 DCHECK(found_tile_for_recorded_picture); | |
539 } | |
540 } | |
541 | |
542 scoped_refptr<RasterSource> PicturePile::CreateRasterSource( | |
543 bool can_use_lcd_text) const { | |
544 return scoped_refptr<RasterSource>( | |
545 PicturePileImpl::CreateFromPicturePile(this, can_use_lcd_text)); | |
546 } | |
547 | |
548 gfx::Size PicturePile::GetSize() const { | |
549 return tiling_.tiling_size(); | |
550 } | |
551 | |
552 void PicturePile::SetEmptyBounds() { | |
553 tiling_.SetTilingSize(gfx::Size()); | |
554 Clear(); | |
555 } | |
556 | |
557 void PicturePile::SetMinContentsScale(float min_contents_scale) { | |
558 DCHECK(min_contents_scale); | |
559 if (min_contents_scale_ == min_contents_scale) | |
560 return; | |
561 | |
562 // Picture contents are played back scaled. When the final contents scale is | |
563 // less than 1 (i.e. low res), then multiple recorded pixels will be used | |
564 // to raster one final pixel. To avoid splitting a final pixel across | |
565 // pictures (which would result in incorrect rasterization due to blending), a | |
566 // buffer margin is added so that any picture can be snapped to integral | |
567 // final pixels. | |
568 // | |
569 // For example, if a 1/4 contents scale is used, then that would be 3 buffer | |
570 // pixels, since that's the minimum number of pixels to add so that resulting | |
571 // content can be snapped to a four pixel aligned grid. | |
572 int buffer_pixels = static_cast<int>(ceil(1 / min_contents_scale) - 1); | |
573 buffer_pixels = std::max(0, buffer_pixels); | |
574 SetBufferPixels(buffer_pixels); | |
575 min_contents_scale_ = min_contents_scale; | |
576 } | |
577 | |
578 void PicturePile::SetSlowdownRasterScaleFactor(int factor) { | |
579 slow_down_raster_scale_factor_for_debug_ = factor; | |
580 } | |
581 | |
582 void PicturePile::SetGatherPixelRefs(bool gather_pixel_refs) { | |
583 gather_pixel_refs_ = gather_pixel_refs; | |
584 } | |
585 | |
586 void PicturePile::SetBackgroundColor(SkColor background_color) { | |
587 background_color_ = background_color; | |
588 } | |
589 | |
590 void PicturePile::SetRequiresClear(bool requires_clear) { | |
591 requires_clear_ = requires_clear; | |
592 } | |
593 | |
594 bool PicturePile::IsSuitableForGpuRasterization() const { | |
595 return is_suitable_for_gpu_rasterization_; | |
596 } | |
597 | |
598 void PicturePile::SetTileGridSize(const gfx::Size& tile_grid_size) { | |
599 DCHECK_GT(tile_grid_size.width(), 0); | |
600 DCHECK_GT(tile_grid_size.height(), 0); | |
601 | |
602 tile_grid_size_ = tile_grid_size; | |
603 } | |
604 | |
605 void PicturePile::SetUnsuitableForGpuRasterizationForTesting() { | |
606 is_suitable_for_gpu_rasterization_ = false; | |
607 } | |
608 | |
609 gfx::Size PicturePile::GetTileGridSizeForTesting() const { | |
610 return tile_grid_size_; | |
611 } | |
612 | |
613 bool PicturePile::CanRasterSlowTileCheck(const gfx::Rect& layer_rect) const { | |
614 bool include_borders = false; | |
615 for (TilingData::Iterator tile_iter(&tiling_, layer_rect, include_borders); | |
616 tile_iter; ++tile_iter) { | |
617 PictureMap::const_iterator map_iter = picture_map_.find(tile_iter.index()); | |
618 if (map_iter == picture_map_.end()) | |
619 return false; | |
620 } | |
621 return true; | |
622 } | |
623 | |
624 void PicturePile::DetermineIfSolidColor() { | |
625 is_solid_color_ = false; | |
626 solid_color_ = SK_ColorTRANSPARENT; | |
627 | |
628 if (picture_map_.empty()) { | |
629 return; | |
630 } | |
631 | |
632 PictureMap::const_iterator it = picture_map_.begin(); | |
633 const Picture* picture = it->second.get(); | |
634 | |
635 // Missing recordings due to frequent invalidations or being too far away | |
636 // from the interest rect will cause the a null picture to exist. | |
637 if (!picture) | |
638 return; | |
639 | |
640 // Don't bother doing more work if the first image is too complicated. | |
641 if (picture->ApproximateOpCount() > kOpCountThatIsOkToAnalyze) | |
642 return; | |
643 | |
644 // Make sure all of the mapped images point to the same picture. | |
645 for (++it; it != picture_map_.end(); ++it) { | |
646 if (it->second.get() != picture) | |
647 return; | |
648 } | |
649 | |
650 gfx::Size layer_size = GetSize(); | |
651 skia::AnalysisCanvas canvas(layer_size.width(), layer_size.height()); | |
652 | |
653 picture->Raster(&canvas, nullptr, Region(), 1.0f); | |
654 is_solid_color_ = canvas.GetColorIfSolid(&solid_color_); | |
655 } | |
656 | |
657 gfx::Rect PicturePile::PaddedRect(const PictureMapKey& key) const { | |
658 gfx::Rect tile = tiling_.TileBounds(key.first, key.second); | |
659 return PadRect(tile); | |
660 } | |
661 | |
662 gfx::Rect PicturePile::PadRect(const gfx::Rect& rect) const { | |
663 gfx::Rect padded_rect = rect; | |
664 padded_rect.Inset(-buffer_pixels(), -buffer_pixels(), -buffer_pixels(), | |
665 -buffer_pixels()); | |
666 return padded_rect; | |
667 } | |
668 | |
669 void PicturePile::Clear() { | |
670 picture_map_.clear(); | |
671 recorded_viewport_ = gfx::Rect(); | |
672 has_any_recordings_ = false; | |
673 is_solid_color_ = false; | |
674 } | |
675 | |
676 void PicturePile::SetBufferPixels(int new_buffer_pixels) { | |
677 if (new_buffer_pixels == buffer_pixels()) | |
678 return; | |
679 | |
680 Clear(); | |
681 tiling_.SetBorderTexels(new_buffer_pixels); | |
682 } | |
683 | |
684 } // namespace cc | |
OLD | NEW |