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.
5 #include "cc/resources/tile_priority.h"
7 #include "base/values.h"
8 #include "cc/base/math_util.h"
12 // TODO(qinmin): modify ui/range/Range.h to support template so that we
13 // don't need to define this.
15 Range(float start, float end) : start_(start), end_(end) {}
21 inline bool Intersects(const Range& a, const Range& b) {
22 return a.start_ < b.end_ && b.start_ < a.end_;
25 inline Range Intersect(const Range& a, const Range& b) {
26 return Range(std::max(a.start_, b.start_), std::min(a.end_, b.end_));
29 bool Range::IsEmpty() {
30 return start_ >= end_;
33 inline void IntersectNegativeHalfplane(Range* out,
38 float time_per_dist = time_delta / (current - previous);
39 float t = (target - current) * time_per_dist;
40 if (time_per_dist > 0.0f)
41 out->start_ = std::max(out->start_, t);
43 out->end_ = std::min(out->end_, t);
46 inline void IntersectPositiveHalfplane(Range* out,
51 float time_per_dist = time_delta / (current - previous);
52 float t = (target - current) * time_per_dist;
53 if (time_per_dist < 0.0f)
54 out->start_ = std::max(out->start_, t);
56 out->end_ = std::min(out->end_, t);
63 scoped_ptr<base::Value> WhichTreeAsValue(WhichTree tree) {
66 return scoped_ptr<base::Value>(base::Value::CreateStringValue(
69 return scoped_ptr<base::Value>(base::Value::CreateStringValue(
72 DCHECK(false) << "Unrecognized WhichTree value " << tree;
73 return scoped_ptr<base::Value>(base::Value::CreateStringValue(
74 "<unknown WhichTree value>"));
78 scoped_ptr<base::Value> TileResolutionAsValue(
79 TileResolution resolution) {
82 return scoped_ptr<base::Value>(base::Value::CreateStringValue(
85 return scoped_ptr<base::Value>(base::Value::CreateStringValue(
87 case NON_IDEAL_RESOLUTION:
88 return scoped_ptr<base::Value>(base::Value::CreateStringValue(
89 "NON_IDEAL_RESOLUTION"));
91 DCHECK(false) << "Unrecognized TileResolution value " << resolution;
92 return scoped_ptr<base::Value>(base::Value::CreateStringValue(
93 "<unknown TileResolution value>"));
96 scoped_ptr<base::Value> TilePriority::AsValue() const {
97 scoped_ptr<base::DictionaryValue> state(new base::DictionaryValue());
98 state->Set("resolution", TileResolutionAsValue(resolution).release());
99 state->Set("time_to_visible_in_seconds",
100 MathUtil::AsValueSafely(time_to_visible_in_seconds).release());
101 state->Set("distance_to_visible_in_pixels",
102 MathUtil::AsValueSafely(distance_to_visible_in_pixels).release());
103 return state.PassAs<base::Value>();
106 float TilePriority::TimeForBoundsToIntersect(const gfx::RectF& previous_bounds,
107 const gfx::RectF& current_bounds,
109 const gfx::RectF& target_bounds) {
110 // Perform an intersection test explicitly between current and target.
111 if (current_bounds.x() < target_bounds.right() &&
112 current_bounds.y() < target_bounds.bottom() &&
113 target_bounds.x() < current_bounds.right() &&
114 target_bounds.y() < current_bounds.bottom())
117 const float kMaxTimeToVisibleInSeconds =
118 std::numeric_limits<float>::infinity();
120 if (time_delta == 0.0f)
121 return kMaxTimeToVisibleInSeconds;
123 // As we are trying to solve the case of both scaling and scrolling, using
124 // a single coordinate with velocity is not enough. The logic here is to
125 // calculate the velocity for each edge. Then we calculate the time range that
126 // each edge will stay on the same side of the target bounds. If there is an
127 // overlap between these time ranges, the bounds must have intersect with
128 // each other during that period of time.
129 Range range(0.0f, kMaxTimeToVisibleInSeconds);
130 IntersectPositiveHalfplane(
131 &range, previous_bounds.x(), current_bounds.x(),
132 target_bounds.right(), time_delta);
133 IntersectNegativeHalfplane(
134 &range, previous_bounds.right(), current_bounds.right(),
135 target_bounds.x(), time_delta);
136 IntersectPositiveHalfplane(
137 &range, previous_bounds.y(), current_bounds.y(),
138 target_bounds.bottom(), time_delta);
139 IntersectNegativeHalfplane(
140 &range, previous_bounds.bottom(), current_bounds.bottom(),
141 target_bounds.y(), time_delta);
142 return range.IsEmpty() ? kMaxTimeToVisibleInSeconds : range.start_;
145 scoped_ptr<base::Value> TileMemoryLimitPolicyAsValue(
146 TileMemoryLimitPolicy policy) {
149 return scoped_ptr<base::Value>(base::Value::CreateStringValue(
151 case ALLOW_ABSOLUTE_MINIMUM:
152 return scoped_ptr<base::Value>(base::Value::CreateStringValue(
153 "ALLOW_ABSOLUTE_MINIMUM"));
154 case ALLOW_PREPAINT_ONLY:
155 return scoped_ptr<base::Value>(base::Value::CreateStringValue(
156 "ALLOW_PREPAINT_ONLY"));
158 return scoped_ptr<base::Value>(base::Value::CreateStringValue(
161 DCHECK(false) << "Unrecognized policy value";
162 return scoped_ptr<base::Value>(base::Value::CreateStringValue(
167 scoped_ptr<base::Value> TreePriorityAsValue(TreePriority prio) {
169 case SAME_PRIORITY_FOR_BOTH_TREES:
170 return scoped_ptr<base::Value>(base::Value::CreateStringValue(
171 "SAME_PRIORITY_FOR_BOTH_TREES"));
172 case SMOOTHNESS_TAKES_PRIORITY:
173 return scoped_ptr<base::Value>(base::Value::CreateStringValue(
174 "SMOOTHNESS_TAKES_PRIORITY"));
175 case NEW_CONTENT_TAKES_PRIORITY:
176 return scoped_ptr<base::Value>(base::Value::CreateStringValue(
177 "NEW_CONTENT_TAKES_PRIORITY"));
179 DCHECK(false) << "Unrecognized priority value " << prio;
180 return scoped_ptr<base::Value>(base::Value::CreateStringValue(
184 scoped_ptr<base::Value> GlobalStateThatImpactsTilePriority::AsValue() const {
185 scoped_ptr<base::DictionaryValue> state(new base::DictionaryValue());
186 state->Set("memory_limit_policy",
187 TileMemoryLimitPolicyAsValue(memory_limit_policy).release());
188 state->SetInteger("memory_limit_in_bytes", memory_limit_in_bytes);
189 state->SetInteger("unused_memory_limit_in_bytes",
190 unused_memory_limit_in_bytes);
191 state->SetInteger("num_resources_limit", num_resources_limit);
192 state->Set("tree_priority", TreePriorityAsValue(tree_priority).release());
193 return state.PassAs<base::Value>();