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

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

Issue 1057283003: Remove parts of //cc we aren't using (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: Created 5 years, 8 months 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 | « cc/resources/picture_layer_tiling.h ('k') | cc/resources/picture_layer_tiling_perftest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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_layer_tiling.h"
6
7 #include <algorithm>
8 #include <cmath>
9 #include <limits>
10 #include <set>
11
12 #include "base/logging.h"
13 #include "base/trace_event/trace_event.h"
14 #include "base/trace_event/trace_event_argument.h"
15 #include "cc/base/math_util.h"
16 #include "cc/resources/tile.h"
17 #include "cc/resources/tile_priority.h"
18 #include "ui/gfx/geometry/point_conversions.h"
19 #include "ui/gfx/geometry/rect_conversions.h"
20 #include "ui/gfx/geometry/safe_integer_conversions.h"
21 #include "ui/gfx/geometry/size_conversions.h"
22
23 namespace cc {
24 namespace {
25
26 const float kSoonBorderDistanceViewportPercentage = 0.15f;
27 const float kMaxSoonBorderDistanceInScreenPixels = 312.f;
28
29 } // namespace
30
31 scoped_ptr<PictureLayerTiling> PictureLayerTiling::Create(
32 float contents_scale,
33 scoped_refptr<RasterSource> raster_source,
34 PictureLayerTilingClient* client,
35 size_t max_tiles_for_interest_area,
36 float skewport_target_time_in_seconds,
37 int skewport_extrapolation_limit_in_content_pixels) {
38 return make_scoped_ptr(new PictureLayerTiling(
39 contents_scale, raster_source, client, max_tiles_for_interest_area,
40 skewport_target_time_in_seconds,
41 skewport_extrapolation_limit_in_content_pixels));
42 }
43
44 PictureLayerTiling::PictureLayerTiling(
45 float contents_scale,
46 scoped_refptr<RasterSource> raster_source,
47 PictureLayerTilingClient* client,
48 size_t max_tiles_for_interest_area,
49 float skewport_target_time_in_seconds,
50 int skewport_extrapolation_limit_in_content_pixels)
51 : max_tiles_for_interest_area_(max_tiles_for_interest_area),
52 skewport_target_time_in_seconds_(skewport_target_time_in_seconds),
53 skewport_extrapolation_limit_in_content_pixels_(
54 skewport_extrapolation_limit_in_content_pixels),
55 contents_scale_(contents_scale),
56 client_(client),
57 raster_source_(raster_source),
58 resolution_(NON_IDEAL_RESOLUTION),
59 tiling_data_(gfx::Size(), gfx::Size(), kBorderTexels),
60 can_require_tiles_for_activation_(false),
61 current_content_to_screen_scale_(0.f),
62 has_visible_rect_tiles_(false),
63 has_skewport_rect_tiles_(false),
64 has_soon_border_rect_tiles_(false),
65 has_eventually_rect_tiles_(false) {
66 DCHECK(!raster_source->IsSolidColor());
67 gfx::Size content_bounds = gfx::ToCeiledSize(
68 gfx::ScaleSize(raster_source_->GetSize(), contents_scale));
69 gfx::Size tile_size = client_->CalculateTileSize(content_bounds);
70
71 DCHECK(!gfx::ToFlooredSize(gfx::ScaleSize(raster_source_->GetSize(),
72 contents_scale)).IsEmpty())
73 << "Tiling created with scale too small as contents become empty."
74 << " Layer bounds: " << raster_source_->GetSize().ToString()
75 << " Contents scale: " << contents_scale;
76
77 tiling_data_.SetTilingSize(content_bounds);
78 tiling_data_.SetMaxTextureSize(tile_size);
79 }
80
81 PictureLayerTiling::~PictureLayerTiling() {
82 for (TileMap::const_iterator it = tiles_.begin(); it != tiles_.end(); ++it)
83 it->second->set_shared(false);
84 }
85
86 // static
87 float PictureLayerTiling::CalculateSoonBorderDistance(
88 const gfx::Rect& visible_rect_in_content_space,
89 float content_to_screen_scale) {
90 float max_dimension = std::max(visible_rect_in_content_space.width(),
91 visible_rect_in_content_space.height());
92 return std::min(
93 kMaxSoonBorderDistanceInScreenPixels / content_to_screen_scale,
94 max_dimension * kSoonBorderDistanceViewportPercentage);
95 }
96
97 Tile* PictureLayerTiling::CreateTile(int i,
98 int j,
99 const PictureLayerTiling* twin_tiling,
100 PictureLayerTiling* recycled_twin) {
101 // Can't have both a (pending or active) twin and a recycled twin tiling.
102 DCHECK_IMPLIES(twin_tiling, !recycled_twin);
103 DCHECK_IMPLIES(recycled_twin, !twin_tiling);
104 TileMapKey key(i, j);
105 DCHECK(tiles_.find(key) == tiles_.end());
106
107 gfx::Rect paint_rect = tiling_data_.TileBoundsWithBorder(i, j);
108 gfx::Rect tile_rect = paint_rect;
109 tile_rect.set_size(tiling_data_.max_texture_size());
110
111 // Check our twin for a valid tile.
112 if (twin_tiling &&
113 tiling_data_.max_texture_size() ==
114 twin_tiling->tiling_data_.max_texture_size()) {
115 if (Tile* candidate_tile = twin_tiling->TileAt(i, j)) {
116 gfx::Rect rect =
117 gfx::ScaleToEnclosingRect(paint_rect, 1.0f / contents_scale_);
118 const Region* invalidation = client_->GetPendingInvalidation();
119 if (!invalidation || !invalidation->Intersects(rect)) {
120 DCHECK(!candidate_tile->is_shared());
121 DCHECK_EQ(i, candidate_tile->tiling_i_index());
122 DCHECK_EQ(j, candidate_tile->tiling_j_index());
123 candidate_tile->set_shared(true);
124 tiles_[key] = candidate_tile;
125 return candidate_tile;
126 }
127 }
128 }
129
130 if (!raster_source_->CoversRect(tile_rect, contents_scale_))
131 return nullptr;
132
133 // Create a new tile because our twin didn't have a valid one.
134 scoped_refptr<Tile> tile = client_->CreateTile(contents_scale_, tile_rect);
135 DCHECK(!tile->is_shared());
136 tile->set_tiling_index(i, j);
137 tiles_[key] = tile;
138
139 if (recycled_twin) {
140 DCHECK(recycled_twin->tiles_.find(key) == recycled_twin->tiles_.end());
141 // Do what recycled_twin->CreateTile() would do.
142 tile->set_shared(true);
143 recycled_twin->tiles_[key] = tile;
144 }
145 return tile.get();
146 }
147
148 void PictureLayerTiling::CreateMissingTilesInLiveTilesRect() {
149 const PictureLayerTiling* twin_tiling =
150 client_->GetPendingOrActiveTwinTiling(this);
151 // There is no recycled twin during commit from the main thread which is when
152 // this occurs.
153 PictureLayerTiling* null_recycled_twin = nullptr;
154 DCHECK_EQ(null_recycled_twin, client_->GetRecycledTwinTiling(this));
155 bool include_borders = false;
156 for (TilingData::Iterator iter(
157 &tiling_data_, live_tiles_rect_, include_borders);
158 iter;
159 ++iter) {
160 TileMapKey key = iter.index();
161 TileMap::iterator find = tiles_.find(key);
162 if (find != tiles_.end())
163 continue;
164 CreateTile(key.first, key.second, twin_tiling, null_recycled_twin);
165 }
166
167 VerifyLiveTilesRect(false);
168 }
169
170 void PictureLayerTiling::CloneTilesAndPropertiesFrom(
171 const PictureLayerTiling& twin_tiling) {
172 DCHECK_EQ(&twin_tiling, client_->GetPendingOrActiveTwinTiling(this));
173
174 SetRasterSourceAndResize(twin_tiling.raster_source_);
175 DCHECK_EQ(twin_tiling.contents_scale_, contents_scale_);
176 DCHECK_EQ(twin_tiling.raster_source_, raster_source_);
177 DCHECK_EQ(twin_tiling.tile_size().ToString(), tile_size().ToString());
178
179 resolution_ = twin_tiling.resolution_;
180
181 SetLiveTilesRect(twin_tiling.live_tiles_rect());
182
183 // Recreate unshared tiles.
184 std::vector<TileMapKey> to_remove;
185 for (const auto& tile_map_pair : tiles_) {
186 TileMapKey key = tile_map_pair.first;
187 Tile* tile = tile_map_pair.second.get();
188 if (!tile->is_shared())
189 to_remove.push_back(key);
190 }
191 // The recycled twin does not exist since there is a pending twin (which is
192 // |twin_tiling|).
193 PictureLayerTiling* null_recycled_twin = nullptr;
194 DCHECK_EQ(null_recycled_twin, client_->GetRecycledTwinTiling(this));
195 for (const auto& key : to_remove) {
196 RemoveTileAt(key.first, key.second, null_recycled_twin);
197 CreateTile(key.first, key.second, &twin_tiling, null_recycled_twin);
198 }
199
200 // Create any missing tiles from the |twin_tiling|.
201 for (const auto& tile_map_pair : twin_tiling.tiles_) {
202 TileMapKey key = tile_map_pair.first;
203 Tile* tile = tile_map_pair.second.get();
204 if (!tile->is_shared())
205 CreateTile(key.first, key.second, &twin_tiling, null_recycled_twin);
206 }
207
208 DCHECK_EQ(twin_tiling.tiles_.size(), tiles_.size());
209 #if DCHECK_IS_ON()
210 for (const auto& tile_map_pair : tiles_)
211 DCHECK(tile_map_pair.second->is_shared());
212 VerifyLiveTilesRect(false);
213 #endif
214
215 UpdateTilePriorityRects(twin_tiling.current_content_to_screen_scale_,
216 twin_tiling.current_visible_rect_,
217 twin_tiling.current_skewport_rect_,
218 twin_tiling.current_soon_border_rect_,
219 twin_tiling.current_eventually_rect_,
220 twin_tiling.current_occlusion_in_layer_space_);
221 }
222
223 void PictureLayerTiling::SetRasterSourceAndResize(
224 scoped_refptr<RasterSource> raster_source) {
225 DCHECK(!raster_source->IsSolidColor());
226 gfx::Size old_layer_bounds = raster_source_->GetSize();
227 raster_source_.swap(raster_source);
228 gfx::Size new_layer_bounds = raster_source_->GetSize();
229 gfx::Size content_bounds =
230 gfx::ToCeiledSize(gfx::ScaleSize(new_layer_bounds, contents_scale_));
231 gfx::Size tile_size = client_->CalculateTileSize(content_bounds);
232
233 if (tile_size != tiling_data_.max_texture_size()) {
234 tiling_data_.SetTilingSize(content_bounds);
235 tiling_data_.SetMaxTextureSize(tile_size);
236 // When the tile size changes, the TilingData positions no longer work
237 // as valid keys to the TileMap, so just drop all tiles and clear the live
238 // tiles rect.
239 Reset();
240 return;
241 }
242
243 if (old_layer_bounds == new_layer_bounds)
244 return;
245
246 // The SetLiveTilesRect() method would drop tiles outside the new bounds,
247 // but may do so incorrectly if resizing the tiling causes the number of
248 // tiles in the tiling_data_ to change.
249 gfx::Rect content_rect(content_bounds);
250 int before_left = tiling_data_.TileXIndexFromSrcCoord(live_tiles_rect_.x());
251 int before_top = tiling_data_.TileYIndexFromSrcCoord(live_tiles_rect_.y());
252 int before_right =
253 tiling_data_.TileXIndexFromSrcCoord(live_tiles_rect_.right() - 1);
254 int before_bottom =
255 tiling_data_.TileYIndexFromSrcCoord(live_tiles_rect_.bottom() - 1);
256
257 // The live_tiles_rect_ is clamped to stay within the tiling size as we
258 // change it.
259 live_tiles_rect_.Intersect(content_rect);
260 tiling_data_.SetTilingSize(content_bounds);
261
262 int after_right = -1;
263 int after_bottom = -1;
264 if (!live_tiles_rect_.IsEmpty()) {
265 after_right =
266 tiling_data_.TileXIndexFromSrcCoord(live_tiles_rect_.right() - 1);
267 after_bottom =
268 tiling_data_.TileYIndexFromSrcCoord(live_tiles_rect_.bottom() - 1);
269 }
270
271 // There is no recycled twin since this is run on the pending tiling
272 // during commit, and on the active tree during activate.
273 PictureLayerTiling* null_recycled_twin = nullptr;
274 DCHECK_EQ(null_recycled_twin, client_->GetRecycledTwinTiling(this));
275
276 // Drop tiles outside the new layer bounds if the layer shrank.
277 for (int i = after_right + 1; i <= before_right; ++i) {
278 for (int j = before_top; j <= before_bottom; ++j)
279 RemoveTileAt(i, j, null_recycled_twin);
280 }
281 for (int i = before_left; i <= after_right; ++i) {
282 for (int j = after_bottom + 1; j <= before_bottom; ++j)
283 RemoveTileAt(i, j, null_recycled_twin);
284 }
285
286 // If the layer grew, the live_tiles_rect_ is not changed, but a new row
287 // and/or column of tiles may now exist inside the same live_tiles_rect_.
288 const PictureLayerTiling* twin_tiling =
289 client_->GetPendingOrActiveTwinTiling(this);
290 if (after_right > before_right) {
291 DCHECK_EQ(after_right, before_right + 1);
292 for (int j = before_top; j <= after_bottom; ++j)
293 CreateTile(after_right, j, twin_tiling, null_recycled_twin);
294 }
295 if (after_bottom > before_bottom) {
296 DCHECK_EQ(after_bottom, before_bottom + 1);
297 for (int i = before_left; i <= before_right; ++i)
298 CreateTile(i, after_bottom, twin_tiling, null_recycled_twin);
299 }
300 }
301
302 void PictureLayerTiling::Invalidate(const Region& layer_invalidation) {
303 if (live_tiles_rect_.IsEmpty())
304 return;
305 std::vector<TileMapKey> new_tile_keys;
306 gfx::Rect expanded_live_tiles_rect =
307 tiling_data_.ExpandRectIgnoringBordersToTileBounds(live_tiles_rect_);
308 for (Region::Iterator iter(layer_invalidation); iter.has_rect();
309 iter.next()) {
310 gfx::Rect layer_rect = iter.rect();
311 gfx::Rect content_rect =
312 gfx::ScaleToEnclosingRect(layer_rect, contents_scale_);
313 // Consider tiles inside the live tiles rect even if only their border
314 // pixels intersect the invalidation. But don't consider tiles outside
315 // the live tiles rect with the same conditions, as they won't exist.
316 int border_pixels = tiling_data_.border_texels();
317 content_rect.Inset(-border_pixels, -border_pixels);
318 // Avoid needless work by not bothering to invalidate where there aren't
319 // tiles.
320 content_rect.Intersect(expanded_live_tiles_rect);
321 if (content_rect.IsEmpty())
322 continue;
323 // Since the content_rect includes border pixels already, don't include
324 // borders when iterating to avoid double counting them.
325 bool include_borders = false;
326 for (TilingData::Iterator iter(
327 &tiling_data_, content_rect, include_borders);
328 iter;
329 ++iter) {
330 // There is no recycled twin for the pending tree during commit, or for
331 // the active tree during activation.
332 PictureLayerTiling* null_recycled_twin = nullptr;
333 DCHECK_EQ(null_recycled_twin, client_->GetRecycledTwinTiling(this));
334 if (RemoveTileAt(iter.index_x(), iter.index_y(), null_recycled_twin))
335 new_tile_keys.push_back(iter.index());
336 }
337 }
338
339 if (!new_tile_keys.empty()) {
340 // During commit from the main thread, invalidations can never be shared
341 // with the active tree since the active tree has different content there.
342 // And when invalidating an active-tree tiling, it means there was no
343 // pending tiling to clone from.
344 const PictureLayerTiling* null_twin_tiling = nullptr;
345 PictureLayerTiling* null_recycled_twin = nullptr;
346 DCHECK_EQ(null_recycled_twin, client_->GetRecycledTwinTiling(this));
347 for (size_t i = 0; i < new_tile_keys.size(); ++i) {
348 CreateTile(new_tile_keys[i].first, new_tile_keys[i].second,
349 null_twin_tiling, null_recycled_twin);
350 }
351 }
352 }
353
354 void PictureLayerTiling::SetRasterSourceOnTiles() {
355 // Shared (ie. non-invalidated) tiles on the pending tree are updated to use
356 // the new raster source. When this raster source is activated, the raster
357 // source will remain valid for shared tiles in the active tree.
358 for (TileMap::const_iterator it = tiles_.begin(); it != tiles_.end(); ++it)
359 it->second->set_raster_source(raster_source_);
360 VerifyLiveTilesRect(false);
361 }
362
363 PictureLayerTiling::CoverageIterator::CoverageIterator()
364 : tiling_(NULL),
365 current_tile_(NULL),
366 tile_i_(0),
367 tile_j_(0),
368 left_(0),
369 top_(0),
370 right_(-1),
371 bottom_(-1) {
372 }
373
374 PictureLayerTiling::CoverageIterator::CoverageIterator(
375 const PictureLayerTiling* tiling,
376 float dest_scale,
377 const gfx::Rect& dest_rect)
378 : tiling_(tiling),
379 dest_rect_(dest_rect),
380 dest_to_content_scale_(0),
381 current_tile_(NULL),
382 tile_i_(0),
383 tile_j_(0),
384 left_(0),
385 top_(0),
386 right_(-1),
387 bottom_(-1) {
388 DCHECK(tiling_);
389 if (dest_rect_.IsEmpty())
390 return;
391
392 dest_to_content_scale_ = tiling_->contents_scale_ / dest_scale;
393
394 gfx::Rect content_rect =
395 gfx::ScaleToEnclosingRect(dest_rect_,
396 dest_to_content_scale_,
397 dest_to_content_scale_);
398 // IndexFromSrcCoord clamps to valid tile ranges, so it's necessary to
399 // check for non-intersection first.
400 content_rect.Intersect(gfx::Rect(tiling_->tiling_size()));
401 if (content_rect.IsEmpty())
402 return;
403
404 left_ = tiling_->tiling_data_.TileXIndexFromSrcCoord(content_rect.x());
405 top_ = tiling_->tiling_data_.TileYIndexFromSrcCoord(content_rect.y());
406 right_ = tiling_->tiling_data_.TileXIndexFromSrcCoord(
407 content_rect.right() - 1);
408 bottom_ = tiling_->tiling_data_.TileYIndexFromSrcCoord(
409 content_rect.bottom() - 1);
410
411 tile_i_ = left_ - 1;
412 tile_j_ = top_;
413 ++(*this);
414 }
415
416 PictureLayerTiling::CoverageIterator::~CoverageIterator() {
417 }
418
419 PictureLayerTiling::CoverageIterator&
420 PictureLayerTiling::CoverageIterator::operator++() {
421 if (tile_j_ > bottom_)
422 return *this;
423
424 bool first_time = tile_i_ < left_;
425 bool new_row = false;
426 tile_i_++;
427 if (tile_i_ > right_) {
428 tile_i_ = left_;
429 tile_j_++;
430 new_row = true;
431 if (tile_j_ > bottom_) {
432 current_tile_ = NULL;
433 return *this;
434 }
435 }
436
437 current_tile_ = tiling_->TileAt(tile_i_, tile_j_);
438
439 // Calculate the current geometry rect. Due to floating point rounding
440 // and ToEnclosingRect, tiles might overlap in destination space on the
441 // edges.
442 gfx::Rect last_geometry_rect = current_geometry_rect_;
443
444 gfx::Rect content_rect = tiling_->tiling_data_.TileBounds(tile_i_, tile_j_);
445
446 current_geometry_rect_ =
447 gfx::ScaleToEnclosingRect(content_rect,
448 1 / dest_to_content_scale_,
449 1 / dest_to_content_scale_);
450
451 current_geometry_rect_.Intersect(dest_rect_);
452
453 if (first_time)
454 return *this;
455
456 // Iteration happens left->right, top->bottom. Running off the bottom-right
457 // edge is handled by the intersection above with dest_rect_. Here we make
458 // sure that the new current geometry rect doesn't overlap with the last.
459 int min_left;
460 int min_top;
461 if (new_row) {
462 min_left = dest_rect_.x();
463 min_top = last_geometry_rect.bottom();
464 } else {
465 min_left = last_geometry_rect.right();
466 min_top = last_geometry_rect.y();
467 }
468
469 int inset_left = std::max(0, min_left - current_geometry_rect_.x());
470 int inset_top = std::max(0, min_top - current_geometry_rect_.y());
471 current_geometry_rect_.Inset(inset_left, inset_top, 0, 0);
472
473 if (!new_row) {
474 DCHECK_EQ(last_geometry_rect.right(), current_geometry_rect_.x());
475 DCHECK_EQ(last_geometry_rect.bottom(), current_geometry_rect_.bottom());
476 DCHECK_EQ(last_geometry_rect.y(), current_geometry_rect_.y());
477 }
478
479 return *this;
480 }
481
482 gfx::Rect PictureLayerTiling::CoverageIterator::geometry_rect() const {
483 return current_geometry_rect_;
484 }
485
486 gfx::RectF PictureLayerTiling::CoverageIterator::texture_rect() const {
487 gfx::PointF tex_origin =
488 tiling_->tiling_data_.TileBoundsWithBorder(tile_i_, tile_j_).origin();
489
490 // Convert from dest space => content space => texture space.
491 gfx::RectF texture_rect(current_geometry_rect_);
492 texture_rect.Scale(dest_to_content_scale_,
493 dest_to_content_scale_);
494 texture_rect.Intersect(gfx::Rect(tiling_->tiling_size()));
495 if (texture_rect.IsEmpty())
496 return texture_rect;
497 texture_rect.Offset(-tex_origin.OffsetFromOrigin());
498
499 return texture_rect;
500 }
501
502 bool PictureLayerTiling::RemoveTileAt(int i,
503 int j,
504 PictureLayerTiling* recycled_twin) {
505 TileMap::iterator found = tiles_.find(TileMapKey(i, j));
506 if (found == tiles_.end())
507 return false;
508 found->second->set_shared(false);
509 tiles_.erase(found);
510 if (recycled_twin) {
511 // Recycled twin does not also have a recycled twin, so pass null.
512 recycled_twin->RemoveTileAt(i, j, nullptr);
513 }
514 return true;
515 }
516
517 void PictureLayerTiling::Reset() {
518 live_tiles_rect_ = gfx::Rect();
519 PictureLayerTiling* recycled_twin = client_->GetRecycledTwinTiling(this);
520 for (TileMap::const_iterator it = tiles_.begin(); it != tiles_.end(); ++it) {
521 it->second->set_shared(false);
522 if (recycled_twin)
523 recycled_twin->RemoveTileAt(it->first.first, it->first.second, nullptr);
524 }
525 tiles_.clear();
526 }
527
528 gfx::Rect PictureLayerTiling::ComputeSkewport(
529 double current_frame_time_in_seconds,
530 const gfx::Rect& visible_rect_in_content_space) const {
531 gfx::Rect skewport = visible_rect_in_content_space;
532 if (skewport.IsEmpty())
533 return skewport;
534
535 if (visible_rect_history_[1].frame_time_in_seconds == 0.0)
536 return skewport;
537
538 double time_delta = current_frame_time_in_seconds -
539 visible_rect_history_[1].frame_time_in_seconds;
540 if (time_delta == 0.0)
541 return skewport;
542
543 double extrapolation_multiplier =
544 skewport_target_time_in_seconds_ / time_delta;
545
546 int old_x = visible_rect_history_[1].visible_rect_in_content_space.x();
547 int old_y = visible_rect_history_[1].visible_rect_in_content_space.y();
548 int old_right =
549 visible_rect_history_[1].visible_rect_in_content_space.right();
550 int old_bottom =
551 visible_rect_history_[1].visible_rect_in_content_space.bottom();
552
553 int new_x = visible_rect_in_content_space.x();
554 int new_y = visible_rect_in_content_space.y();
555 int new_right = visible_rect_in_content_space.right();
556 int new_bottom = visible_rect_in_content_space.bottom();
557
558 // Compute the maximum skewport based on
559 // |skewport_extrapolation_limit_in_content_pixels_|.
560 gfx::Rect max_skewport = skewport;
561 max_skewport.Inset(-skewport_extrapolation_limit_in_content_pixels_,
562 -skewport_extrapolation_limit_in_content_pixels_);
563
564 // Inset the skewport by the needed adjustment.
565 skewport.Inset(extrapolation_multiplier * (new_x - old_x),
566 extrapolation_multiplier * (new_y - old_y),
567 extrapolation_multiplier * (old_right - new_right),
568 extrapolation_multiplier * (old_bottom - new_bottom));
569
570 // Ensure that visible rect is contained in the skewport.
571 skewport.Union(visible_rect_in_content_space);
572
573 // Clip the skewport to |max_skewport|. This needs to happen after the
574 // union in case intersecting would have left the empty rect.
575 skewport.Intersect(max_skewport);
576
577 return skewport;
578 }
579
580 bool PictureLayerTiling::ComputeTilePriorityRects(
581 const gfx::Rect& viewport_in_layer_space,
582 float ideal_contents_scale,
583 double current_frame_time_in_seconds,
584 const Occlusion& occlusion_in_layer_space) {
585 if (!NeedsUpdateForFrameAtTimeAndViewport(current_frame_time_in_seconds,
586 viewport_in_layer_space)) {
587 // This should never be zero for the purposes of has_ever_been_updated().
588 DCHECK_NE(current_frame_time_in_seconds, 0.0);
589 return false;
590 }
591
592 gfx::Rect visible_rect_in_content_space =
593 gfx::ScaleToEnclosingRect(viewport_in_layer_space, contents_scale_);
594
595 if (tiling_size().IsEmpty()) {
596 UpdateVisibleRectHistory(current_frame_time_in_seconds,
597 visible_rect_in_content_space);
598 last_viewport_in_layer_space_ = viewport_in_layer_space;
599 return false;
600 }
601
602 // Calculate the skewport.
603 gfx::Rect skewport = ComputeSkewport(current_frame_time_in_seconds,
604 visible_rect_in_content_space);
605 DCHECK(skewport.Contains(visible_rect_in_content_space));
606
607 // Calculate the eventually/live tiles rect.
608 gfx::Size tile_size = tiling_data_.max_texture_size();
609 int64 eventually_rect_area =
610 max_tiles_for_interest_area_ * tile_size.width() * tile_size.height();
611
612 gfx::Rect eventually_rect =
613 ExpandRectEquallyToAreaBoundedBy(visible_rect_in_content_space,
614 eventually_rect_area,
615 gfx::Rect(tiling_size()),
616 &expansion_cache_);
617
618 DCHECK(eventually_rect.IsEmpty() ||
619 gfx::Rect(tiling_size()).Contains(eventually_rect))
620 << "tiling_size: " << tiling_size().ToString()
621 << " eventually_rect: " << eventually_rect.ToString();
622
623 // Calculate the soon border rect.
624 float content_to_screen_scale = ideal_contents_scale / contents_scale_;
625 gfx::Rect soon_border_rect = visible_rect_in_content_space;
626 float border = CalculateSoonBorderDistance(visible_rect_in_content_space,
627 content_to_screen_scale);
628 soon_border_rect.Inset(-border, -border, -border, -border);
629
630 UpdateVisibleRectHistory(current_frame_time_in_seconds,
631 visible_rect_in_content_space);
632 last_viewport_in_layer_space_ = viewport_in_layer_space;
633
634 SetLiveTilesRect(eventually_rect);
635 UpdateTilePriorityRects(
636 content_to_screen_scale, visible_rect_in_content_space, skewport,
637 soon_border_rect, eventually_rect, occlusion_in_layer_space);
638 return true;
639 }
640
641 void PictureLayerTiling::UpdateTilePriorityRects(
642 float content_to_screen_scale,
643 const gfx::Rect& visible_rect_in_content_space,
644 const gfx::Rect& skewport,
645 const gfx::Rect& soon_border_rect,
646 const gfx::Rect& eventually_rect,
647 const Occlusion& occlusion_in_layer_space) {
648 current_visible_rect_ = visible_rect_in_content_space;
649 current_skewport_rect_ = skewport;
650 current_soon_border_rect_ = soon_border_rect;
651 current_eventually_rect_ = eventually_rect;
652 current_occlusion_in_layer_space_ = occlusion_in_layer_space;
653 current_content_to_screen_scale_ = content_to_screen_scale;
654
655 gfx::Rect tiling_rect(tiling_size());
656 has_visible_rect_tiles_ = tiling_rect.Intersects(current_visible_rect_);
657 has_skewport_rect_tiles_ = tiling_rect.Intersects(current_skewport_rect_);
658 has_soon_border_rect_tiles_ =
659 tiling_rect.Intersects(current_soon_border_rect_);
660 has_eventually_rect_tiles_ = tiling_rect.Intersects(current_eventually_rect_);
661 }
662
663 void PictureLayerTiling::SetLiveTilesRect(
664 const gfx::Rect& new_live_tiles_rect) {
665 DCHECK(new_live_tiles_rect.IsEmpty() ||
666 gfx::Rect(tiling_size()).Contains(new_live_tiles_rect))
667 << "tiling_size: " << tiling_size().ToString()
668 << " new_live_tiles_rect: " << new_live_tiles_rect.ToString();
669 if (live_tiles_rect_ == new_live_tiles_rect)
670 return;
671
672 PictureLayerTiling* recycled_twin = client_->GetRecycledTwinTiling(this);
673
674 // Iterate to delete all tiles outside of our new live_tiles rect.
675 for (TilingData::DifferenceIterator iter(&tiling_data_,
676 live_tiles_rect_,
677 new_live_tiles_rect);
678 iter;
679 ++iter) {
680 RemoveTileAt(iter.index_x(), iter.index_y(), recycled_twin);
681 }
682
683 const PictureLayerTiling* twin_tiling =
684 client_->GetPendingOrActiveTwinTiling(this);
685
686 // Iterate to allocate new tiles for all regions with newly exposed area.
687 for (TilingData::DifferenceIterator iter(&tiling_data_,
688 new_live_tiles_rect,
689 live_tiles_rect_);
690 iter;
691 ++iter) {
692 TileMapKey key(iter.index());
693 CreateTile(key.first, key.second, twin_tiling, recycled_twin);
694 }
695
696 live_tiles_rect_ = new_live_tiles_rect;
697 VerifyLiveTilesRect(false);
698 if (recycled_twin) {
699 recycled_twin->live_tiles_rect_ = live_tiles_rect_;
700 recycled_twin->VerifyLiveTilesRect(true);
701 }
702 }
703
704 void PictureLayerTiling::VerifyLiveTilesRect(bool is_on_recycle_tree) const {
705 #if DCHECK_IS_ON()
706 for (auto it = tiles_.begin(); it != tiles_.end(); ++it) {
707 if (!it->second.get())
708 continue;
709 DCHECK(it->first.first < tiling_data_.num_tiles_x())
710 << this << " " << it->first.first << "," << it->first.second
711 << " num_tiles_x " << tiling_data_.num_tiles_x() << " live_tiles_rect "
712 << live_tiles_rect_.ToString();
713 DCHECK(it->first.second < tiling_data_.num_tiles_y())
714 << this << " " << it->first.first << "," << it->first.second
715 << " num_tiles_y " << tiling_data_.num_tiles_y() << " live_tiles_rect "
716 << live_tiles_rect_.ToString();
717 DCHECK(tiling_data_.TileBounds(it->first.first, it->first.second)
718 .Intersects(live_tiles_rect_))
719 << this << " " << it->first.first << "," << it->first.second
720 << " tile bounds "
721 << tiling_data_.TileBounds(it->first.first, it->first.second).ToString()
722 << " live_tiles_rect " << live_tiles_rect_.ToString();
723 DCHECK_IMPLIES(is_on_recycle_tree, it->second->is_shared());
724 }
725 #endif
726 }
727
728 bool PictureLayerTiling::IsTileOccluded(const Tile* tile) const {
729 DCHECK(tile);
730
731 if (!current_occlusion_in_layer_space_.HasOcclusion())
732 return false;
733
734 gfx::Rect tile_query_rect =
735 gfx::IntersectRects(tile->content_rect(), current_visible_rect_);
736
737 // Explicitly check if the tile is outside the viewport. If so, we need to
738 // return false, since occlusion for this tile is unknown.
739 // TODO(vmpstr): Since the current visible rect is really a viewport in
740 // layer space, we should probably clip tile query rect to tiling bounds
741 // or live tiles rect.
742 if (tile_query_rect.IsEmpty())
743 return false;
744
745 if (contents_scale_ != 1.f) {
746 tile_query_rect =
747 gfx::ScaleToEnclosingRect(tile_query_rect, 1.0f / contents_scale_);
748 }
749
750 return current_occlusion_in_layer_space_.IsOccluded(tile_query_rect);
751 }
752
753 bool PictureLayerTiling::IsTileRequiredForActivationIfVisible(
754 const Tile* tile) const {
755 DCHECK_EQ(PENDING_TREE, client_->GetTree());
756
757 // This function assumes that the tile is visible (i.e. in the viewport). The
758 // caller needs to make sure that this condition is met to ensure we don't
759 // block activation on tiles outside of the viewport.
760
761 // If we are not allowed to mark tiles as required for activation, then don't
762 // do it.
763 if (!can_require_tiles_for_activation_)
764 return false;
765
766 if (resolution_ != HIGH_RESOLUTION)
767 return false;
768
769 if (IsTileOccluded(tile))
770 return false;
771
772 if (client_->RequiresHighResToDraw())
773 return true;
774
775 const PictureLayerTiling* twin_tiling =
776 client_->GetPendingOrActiveTwinTiling(this);
777 if (!twin_tiling)
778 return true;
779
780 if (twin_tiling->raster_source()->GetSize() != raster_source()->GetSize())
781 return true;
782
783 if (twin_tiling->current_visible_rect_ != current_visible_rect_)
784 return true;
785
786 Tile* twin_tile =
787 twin_tiling->TileAt(tile->tiling_i_index(), tile->tiling_j_index());
788 // If twin tile is missing, it might not have a recording, so we don't need
789 // this tile to be required for activation.
790 if (!twin_tile)
791 return false;
792
793 return true;
794 }
795
796 bool PictureLayerTiling::IsTileRequiredForDrawIfVisible(
797 const Tile* tile) const {
798 DCHECK_EQ(ACTIVE_TREE, client_->GetTree());
799
800 // This function assumes that the tile is visible (i.e. in the viewport).
801
802 if (resolution_ != HIGH_RESOLUTION)
803 return false;
804
805 if (IsTileOccluded(tile))
806 return false;
807
808 return true;
809 }
810
811 void PictureLayerTiling::UpdateTileAndTwinPriority(Tile* tile) const {
812 WhichTree tree = client_->GetTree();
813 WhichTree twin_tree = tree == ACTIVE_TREE ? PENDING_TREE : ACTIVE_TREE;
814
815 tile->SetPriority(tree, ComputePriorityForTile(tile));
816 UpdateRequiredStateForTile(tile, tree);
817
818 const PictureLayerTiling* twin_tiling =
819 client_->GetPendingOrActiveTwinTiling(this);
820 if (!tile->is_shared() || !twin_tiling) {
821 tile->SetPriority(twin_tree, TilePriority());
822 tile->set_is_occluded(twin_tree, false);
823 if (twin_tree == PENDING_TREE)
824 tile->set_required_for_activation(false);
825 else
826 tile->set_required_for_draw(false);
827 return;
828 }
829
830 tile->SetPriority(twin_tree, twin_tiling->ComputePriorityForTile(tile));
831 twin_tiling->UpdateRequiredStateForTile(tile, twin_tree);
832 }
833
834 void PictureLayerTiling::UpdateRequiredStateForTile(Tile* tile,
835 WhichTree tree) const {
836 if (tile->priority(tree).priority_bin == TilePriority::NOW) {
837 if (tree == PENDING_TREE) {
838 tile->set_required_for_activation(
839 IsTileRequiredForActivationIfVisible(tile));
840 } else {
841 tile->set_required_for_draw(IsTileRequiredForDrawIfVisible(tile));
842 }
843 tile->set_is_occluded(tree, IsTileOccluded(tile));
844 return;
845 }
846
847 // Non-NOW bin tiles are not required or occluded.
848 if (tree == PENDING_TREE)
849 tile->set_required_for_activation(false);
850 else
851 tile->set_required_for_draw(false);
852 tile->set_is_occluded(tree, false);
853 }
854
855 TilePriority PictureLayerTiling::ComputePriorityForTile(
856 const Tile* tile) const {
857 // TODO(vmpstr): See if this can be moved to iterators.
858 TilePriority::PriorityBin max_tile_priority_bin =
859 client_->GetMaxTilePriorityBin();
860
861 DCHECK_EQ(TileAt(tile->tiling_i_index(), tile->tiling_j_index()), tile);
862 gfx::Rect tile_bounds =
863 tiling_data_.TileBounds(tile->tiling_i_index(), tile->tiling_j_index());
864
865 if (max_tile_priority_bin <= TilePriority::NOW &&
866 current_visible_rect_.Intersects(tile_bounds)) {
867 return TilePriority(resolution_, TilePriority::NOW, 0);
868 }
869
870 DCHECK_GT(current_content_to_screen_scale_, 0.f);
871 float distance_to_visible =
872 current_visible_rect_.ManhattanInternalDistance(tile_bounds) *
873 current_content_to_screen_scale_;
874
875 if (max_tile_priority_bin <= TilePriority::SOON &&
876 (current_soon_border_rect_.Intersects(tile_bounds) ||
877 current_skewport_rect_.Intersects(tile_bounds))) {
878 return TilePriority(resolution_, TilePriority::SOON, distance_to_visible);
879 }
880
881 return TilePriority(resolution_, TilePriority::EVENTUALLY,
882 distance_to_visible);
883 }
884
885 void PictureLayerTiling::GetAllTilesAndPrioritiesForTracing(
886 std::map<const Tile*, TilePriority>* tile_map) const {
887 const PictureLayerTiling* twin_tiling =
888 client_->GetPendingOrActiveTwinTiling(this);
889 for (const auto& tile_pair : tiles_) {
890 const Tile* tile = tile_pair.second.get();
891 const TilePriority& priority = ComputePriorityForTile(tile);
892 const TilePriority& twin_priority =
893 twin_tiling ? twin_tiling->ComputePriorityForTile(tile)
894 : TilePriority();
895
896 // Store combined priority.
897 (*tile_map)[tile] = TilePriority(priority, twin_priority);
898 }
899 }
900
901 void PictureLayerTiling::AsValueInto(
902 base::trace_event::TracedValue* state) const {
903 state->SetInteger("num_tiles", tiles_.size());
904 state->SetDouble("content_scale", contents_scale_);
905 MathUtil::AddToTracedValue("visible_rect", current_visible_rect_, state);
906 MathUtil::AddToTracedValue("skewport_rect", current_skewport_rect_, state);
907 MathUtil::AddToTracedValue("soon_rect", current_soon_border_rect_, state);
908 MathUtil::AddToTracedValue("eventually_rect", current_eventually_rect_,
909 state);
910 MathUtil::AddToTracedValue("tiling_size", tiling_size(), state);
911 }
912
913 size_t PictureLayerTiling::GPUMemoryUsageInBytes() const {
914 size_t amount = 0;
915 for (TileMap::const_iterator it = tiles_.begin(); it != tiles_.end(); ++it) {
916 const Tile* tile = it->second.get();
917 amount += tile->GPUMemoryUsageInBytes();
918 }
919 return amount;
920 }
921
922 PictureLayerTiling::RectExpansionCache::RectExpansionCache()
923 : previous_target(0) {
924 }
925
926 namespace {
927
928 // This struct represents an event at which the expending rect intersects
929 // one of its boundaries. 4 intersection events will occur during expansion.
930 struct EdgeEvent {
931 enum { BOTTOM, TOP, LEFT, RIGHT } edge;
932 int* num_edges;
933 int distance;
934 };
935
936 // Compute the delta to expand from edges to cover target_area.
937 int ComputeExpansionDelta(int num_x_edges, int num_y_edges,
938 int width, int height,
939 int64 target_area) {
940 // Compute coefficients for the quadratic equation:
941 // a*x^2 + b*x + c = 0
942 int a = num_y_edges * num_x_edges;
943 int b = num_y_edges * width + num_x_edges * height;
944 int64 c = static_cast<int64>(width) * height - target_area;
945
946 // Compute the delta for our edges using the quadratic equation.
947 int delta =
948 (a == 0) ? -c / b : (-b + static_cast<int>(std::sqrt(
949 static_cast<int64>(b) * b - 4.0 * a * c))) /
950 (2 * a);
951 return std::max(0, delta);
952 }
953
954 } // namespace
955
956 gfx::Rect PictureLayerTiling::ExpandRectEquallyToAreaBoundedBy(
957 const gfx::Rect& starting_rect,
958 int64 target_area,
959 const gfx::Rect& bounding_rect,
960 RectExpansionCache* cache) {
961 if (starting_rect.IsEmpty())
962 return starting_rect;
963
964 if (cache &&
965 cache->previous_start == starting_rect &&
966 cache->previous_bounds == bounding_rect &&
967 cache->previous_target == target_area)
968 return cache->previous_result;
969
970 if (cache) {
971 cache->previous_start = starting_rect;
972 cache->previous_bounds = bounding_rect;
973 cache->previous_target = target_area;
974 }
975
976 DCHECK(!bounding_rect.IsEmpty());
977 DCHECK_GT(target_area, 0);
978
979 // Expand the starting rect to cover target_area, if it is smaller than it.
980 int delta = ComputeExpansionDelta(
981 2, 2, starting_rect.width(), starting_rect.height(), target_area);
982 gfx::Rect expanded_starting_rect = starting_rect;
983 if (delta > 0)
984 expanded_starting_rect.Inset(-delta, -delta);
985
986 gfx::Rect rect = IntersectRects(expanded_starting_rect, bounding_rect);
987 if (rect.IsEmpty()) {
988 // The starting_rect and bounding_rect are far away.
989 if (cache)
990 cache->previous_result = rect;
991 return rect;
992 }
993 if (delta >= 0 && rect == expanded_starting_rect) {
994 // The starting rect already covers the entire bounding_rect and isn't too
995 // large for the target_area.
996 if (cache)
997 cache->previous_result = rect;
998 return rect;
999 }
1000
1001 // Continue to expand/shrink rect to let it cover target_area.
1002
1003 // These values will be updated by the loop and uses as the output.
1004 int origin_x = rect.x();
1005 int origin_y = rect.y();
1006 int width = rect.width();
1007 int height = rect.height();
1008
1009 // In the beginning we will consider 2 edges in each dimension.
1010 int num_y_edges = 2;
1011 int num_x_edges = 2;
1012
1013 // Create an event list.
1014 EdgeEvent events[] = {
1015 { EdgeEvent::BOTTOM, &num_y_edges, rect.y() - bounding_rect.y() },
1016 { EdgeEvent::TOP, &num_y_edges, bounding_rect.bottom() - rect.bottom() },
1017 { EdgeEvent::LEFT, &num_x_edges, rect.x() - bounding_rect.x() },
1018 { EdgeEvent::RIGHT, &num_x_edges, bounding_rect.right() - rect.right() }
1019 };
1020
1021 // Sort the events by distance (closest first).
1022 if (events[0].distance > events[1].distance) std::swap(events[0], events[1]);
1023 if (events[2].distance > events[3].distance) std::swap(events[2], events[3]);
1024 if (events[0].distance > events[2].distance) std::swap(events[0], events[2]);
1025 if (events[1].distance > events[3].distance) std::swap(events[1], events[3]);
1026 if (events[1].distance > events[2].distance) std::swap(events[1], events[2]);
1027
1028 for (int event_index = 0; event_index < 4; event_index++) {
1029 const EdgeEvent& event = events[event_index];
1030
1031 int delta = ComputeExpansionDelta(
1032 num_x_edges, num_y_edges, width, height, target_area);
1033
1034 // Clamp delta to our event distance.
1035 if (delta > event.distance)
1036 delta = event.distance;
1037
1038 // Adjust the edge count for this kind of edge.
1039 --*event.num_edges;
1040
1041 // Apply the delta to the edges and edge events.
1042 for (int i = event_index; i < 4; i++) {
1043 switch (events[i].edge) {
1044 case EdgeEvent::BOTTOM:
1045 origin_y -= delta;
1046 height += delta;
1047 break;
1048 case EdgeEvent::TOP:
1049 height += delta;
1050 break;
1051 case EdgeEvent::LEFT:
1052 origin_x -= delta;
1053 width += delta;
1054 break;
1055 case EdgeEvent::RIGHT:
1056 width += delta;
1057 break;
1058 }
1059 events[i].distance -= delta;
1060 }
1061
1062 // If our delta is less then our event distance, we're done.
1063 if (delta < event.distance)
1064 break;
1065 }
1066
1067 gfx::Rect result(origin_x, origin_y, width, height);
1068 if (cache)
1069 cache->previous_result = result;
1070 return result;
1071 }
1072
1073 } // namespace cc
OLDNEW
« no previous file with comments | « cc/resources/picture_layer_tiling.h ('k') | cc/resources/picture_layer_tiling_perftest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698