- add sources.
[platform/framework/web/crosswalk.git] / src / cc / resources / tile_priority.cc
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/tile_priority.h"
6
7 #include "base/values.h"
8 #include "cc/base/math_util.h"
9
10 namespace {
11
12 // TODO(qinmin): modify ui/range/Range.h to support template so that we
13 // don't need to define this.
14 struct Range {
15   Range(float start, float end) : start_(start), end_(end) {}
16   bool IsEmpty();
17   float start_;
18   float end_;
19 };
20
21 inline bool Intersects(const Range& a, const Range& b) {
22   return a.start_ < b.end_ && b.start_ < a.end_;
23 }
24
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_));
27 }
28
29 bool Range::IsEmpty() {
30   return start_ >= end_;
31 }
32
33 inline void IntersectNegativeHalfplane(Range* out,
34                                        float previous,
35                                        float current,
36                                        float target,
37                                        float time_delta) {
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);
42   else
43     out->end_ = std::min(out->end_, t);
44 }
45
46 inline void IntersectPositiveHalfplane(Range* out,
47                                        float previous,
48                                        float current,
49                                        float target,
50                                        float time_delta) {
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);
55   else
56     out->end_ = std::min(out->end_, t);
57 }
58
59 }  // namespace
60
61 namespace cc {
62
63 scoped_ptr<base::Value> WhichTreeAsValue(WhichTree tree) {
64   switch (tree) {
65   case ACTIVE_TREE:
66       return scoped_ptr<base::Value>(base::Value::CreateStringValue(
67           "ACTIVE_TREE"));
68   case PENDING_TREE:
69       return scoped_ptr<base::Value>(base::Value::CreateStringValue(
70           "PENDING_TREE"));
71   default:
72       DCHECK(false) << "Unrecognized WhichTree value " << tree;
73       return scoped_ptr<base::Value>(base::Value::CreateStringValue(
74           "<unknown WhichTree value>"));
75   }
76 }
77
78 scoped_ptr<base::Value> TileResolutionAsValue(
79     TileResolution resolution) {
80   switch (resolution) {
81   case LOW_RESOLUTION:
82     return scoped_ptr<base::Value>(base::Value::CreateStringValue(
83         "LOW_RESOLUTION"));
84   case HIGH_RESOLUTION:
85     return scoped_ptr<base::Value>(base::Value::CreateStringValue(
86         "HIGH_RESOLUTION"));
87   case NON_IDEAL_RESOLUTION:
88       return scoped_ptr<base::Value>(base::Value::CreateStringValue(
89         "NON_IDEAL_RESOLUTION"));
90   }
91   DCHECK(false) << "Unrecognized TileResolution value " << resolution;
92   return scoped_ptr<base::Value>(base::Value::CreateStringValue(
93       "<unknown TileResolution value>"));
94 }
95
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>();
104 }
105
106 float TilePriority::TimeForBoundsToIntersect(const gfx::RectF& previous_bounds,
107                                              const gfx::RectF& current_bounds,
108                                              float time_delta,
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())
115     return 0.0f;
116
117   const float kMaxTimeToVisibleInSeconds =
118       std::numeric_limits<float>::infinity();
119
120   if (time_delta == 0.0f)
121     return kMaxTimeToVisibleInSeconds;
122
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_;
143 }
144
145 scoped_ptr<base::Value> TileMemoryLimitPolicyAsValue(
146     TileMemoryLimitPolicy policy) {
147   switch (policy) {
148   case ALLOW_NOTHING:
149       return scoped_ptr<base::Value>(base::Value::CreateStringValue(
150           "ALLOW_NOTHING"));
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"));
157   case ALLOW_ANYTHING:
158       return scoped_ptr<base::Value>(base::Value::CreateStringValue(
159           "ALLOW_ANYTHING"));
160   default:
161       DCHECK(false) << "Unrecognized policy value";
162       return scoped_ptr<base::Value>(base::Value::CreateStringValue(
163           "<unknown>"));
164   }
165 }
166
167 scoped_ptr<base::Value> TreePriorityAsValue(TreePriority prio) {
168   switch (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"));
178   }
179   DCHECK(false) << "Unrecognized priority value " << prio;
180   return scoped_ptr<base::Value>(base::Value::CreateStringValue(
181       "<unknown>"));
182 }
183
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>();
194 }
195
196 }  // namespace cc