Chromium Code Reviews| 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/tile_priority.h" | |
| 6 | |
| 7 namespace { | |
| 8 | |
| 9 const double kMaxTimeToVisibleInSeconds = 1000.0; | |
| 10 | |
| 11 // TODO(qinmin): modify ui/range/Range.h to support template so that we | |
| 12 // don't need to define this. | |
| 13 struct Range { | |
| 14 Range(double start, double end) : start_(start), end_(end) {} | |
| 15 Range Intersects(const Range& other); | |
| 16 bool IsEmpty(); | |
| 17 double start_; | |
| 18 double end_; | |
| 19 }; | |
| 20 | |
| 21 Range Range::Intersects(const Range& other) { | |
| 22 start_ = std::max(start_, other.start_); | |
| 23 end_ = std::min(end_, other.end_); | |
| 24 return Range(start_, end_); | |
| 25 } | |
| 26 | |
| 27 bool Range::IsEmpty() { | |
| 28 return start_ >= end_; | |
| 29 } | |
| 30 | |
| 31 // Calculate a time range that |value| will be larger than |threshold| | |
| 32 // given the velocity of its change. | |
| 33 Range TimeRangeValueLargerThanThreshold( | |
| 34 int value, int threshold, double velocity) { | |
| 35 double minimum_time = 0; | |
| 36 double maximum_time = kMaxTimeToVisibleInSeconds; | |
| 37 | |
| 38 if (velocity > 0) { | |
| 39 if (value < threshold) | |
| 40 minimum_time = std::min(kMaxTimeToVisibleInSeconds, | |
| 41 (threshold - value) / velocity); | |
| 42 } else if (velocity <= 0) { | |
| 43 if (value < threshold) | |
| 44 minimum_time = kMaxTimeToVisibleInSeconds; | |
| 45 else if (velocity != 0) | |
| 46 maximum_time = std::min(maximum_time, (threshold - value) / velocity); | |
| 47 } | |
| 48 | |
| 49 return Range(minimum_time, maximum_time); | |
| 50 } | |
| 51 | |
| 52 } // namespace | |
| 53 | |
| 54 namespace cc { | |
| 55 | |
| 56 int TilePriority::manhattanDistance(const gfx::RectF& a, const gfx::RectF& b) { | |
|
enne (OOO)
2012/12/03 19:57:28
Can you write a quick unit test for this math?
qinmin
2012/12/04 00:37:30
Done.
| |
| 57 gfx::RectF c = gfx::UnionRects(a, b); | |
| 58 // Rects touching the edge of the screen should not be considered visible. | |
| 59 // So we add 1 pixel here to avoid that situation. | |
| 60 int x = static_cast<int>( | |
| 61 std::max(0.0f, c.width() - a.width() - b.width() + 1)); | |
| 62 int y = static_cast<int>( | |
| 63 std::max(0.0f, c.height() - a.height() - b.height() + 1)); | |
| 64 return (x + y); | |
| 65 } | |
| 66 | |
| 67 double TilePriority::TimeForBoundsToIntersect(gfx::RectF previous_bounds, | |
| 68 gfx::RectF current_bounds, | |
| 69 double time_delta, | |
| 70 gfx::RectF target_bounds) { | |
| 71 if (current_bounds.Intersects(target_bounds)) | |
| 72 return 0; | |
| 73 | |
| 74 if (previous_bounds.Intersects(target_bounds) || time_delta == 0) | |
| 75 return kMaxTimeToVisibleInSeconds; | |
| 76 | |
| 77 // As we are trying to solve the case of both scaling and scrolling, using | |
| 78 // a single coordinate with velocity is not enough. The logic here is to | |
| 79 // calculate the velocity for each edge. Then we calculate the time range that | |
| 80 // each edge will stay on the same side of the target bounds. If there is an | |
| 81 // overlap between these time ranges, the bounds must have intersect with | |
| 82 // each other during that period of time. | |
| 83 double velocity = | |
| 84 (current_bounds.right() - previous_bounds.right()) / time_delta; | |
| 85 Range range = TimeRangeValueLargerThanThreshold( | |
| 86 current_bounds.right(), target_bounds.x(), velocity); | |
| 87 | |
| 88 velocity = (current_bounds.x() - previous_bounds.x()) / time_delta; | |
| 89 range = range.Intersects(TimeRangeValueLargerThanThreshold( | |
| 90 -current_bounds.x(), -target_bounds.right(), -velocity)); | |
| 91 | |
| 92 | |
| 93 velocity = (current_bounds.y() - previous_bounds.y()) / time_delta; | |
| 94 range = range.Intersects(TimeRangeValueLargerThanThreshold( | |
| 95 -current_bounds.y(), -target_bounds.bottom(), -velocity)); | |
| 96 | |
| 97 velocity = (current_bounds.bottom() - previous_bounds.bottom()) / time_delta; | |
| 98 range = range.Intersects(TimeRangeValueLargerThanThreshold( | |
| 99 current_bounds.bottom(), target_bounds.y(), velocity)); | |
| 100 | |
| 101 return range.IsEmpty() ? kMaxTimeToVisibleInSeconds : range.start_; | |
| 102 } | |
| 103 | |
| 104 } // namespace cc | |
| OLD | NEW |