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 // TODO(qinmin): modify ui/range/Range.h to support template so that we | |
10 // don't need to define this. | |
11 struct Range { | |
12 Range(double start, double end) : start_(start), end_(end) {} | |
13 Range Intersects(const Range& other); | |
14 bool IsEmpty(); | |
15 double start_; | |
16 double end_; | |
17 }; | |
18 | |
19 Range Range::Intersects(const Range& other) { | |
20 start_ = std::max(start_, other.start_); | |
21 end_ = std::min(end_, other.end_); | |
22 return Range(start_, end_); | |
23 } | |
24 | |
25 bool Range::IsEmpty() { | |
26 return start_ >= end_; | |
27 } | |
28 | |
29 // Calculate a time range that |value| will be larger than |threshold| | |
30 // given the velocity of its change. | |
31 Range TimeRangeValueLargerThanThreshold( | |
32 int value, int threshold, double velocity) { | |
33 double minimum_time = 0; | |
34 double maximum_time = cc::TilePriority::kMaxTimeToVisibleInSeconds; | |
35 | |
36 if (velocity > 0) { | |
37 if (value < threshold) | |
38 minimum_time = std::min(cc::TilePriority::kMaxTimeToVisibleInSeconds, | |
39 (threshold - value) / velocity); | |
40 } else if (velocity <= 0) { | |
41 if (value < threshold) | |
42 minimum_time = cc::TilePriority::kMaxTimeToVisibleInSeconds; | |
43 else if (velocity != 0) | |
44 maximum_time = std::min(maximum_time, (threshold - value) / velocity); | |
45 } | |
46 | |
47 return Range(minimum_time, maximum_time); | |
48 } | |
49 | |
50 } // namespace | |
51 | |
52 namespace cc { | |
53 | |
54 const double TilePriority::kMaxTimeToVisibleInSeconds = 1000; | |
55 | |
56 int TilePriority::manhattanDistance(const gfx::RectF& a, const gfx::RectF& b) { | |
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) | |
enne (OOO)
2012/12/04 01:13:27
Why is it the case that if the previous bounds but
qinmin
2012/12/04 01:39:53
so if the previous bounds intersect, and the curre
| |
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 |