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