Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / content / browser / android / edge_effect_l.cc
1 // Copyright 2014 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 "content/browser/android/edge_effect_l.h"
6
7 #include "cc/layers/ui_resource_layer.h"
8 #include "ui/base/android/system_ui_resource_manager.h"
9
10 namespace content {
11
12 namespace {
13
14 // Time it will take the effect to fully recede in ms
15 const int kRecedeTimeMs = 1000;
16
17 // Time it will take before a pulled glow begins receding in ms
18 const int kPullTimeMs = 167;
19
20 const float kMaxAlpha = 1.f;
21
22 const float kPullGlowBegin = 0.f;
23
24 // Min/max velocity that will be absorbed
25 const float kMinVelocity = 100.f;
26 const float kMaxVelocity = 10000.f;
27
28 const float kEpsilon = 0.001f;
29
30 const float kSin = 0.5f;    // sin(PI / 6)
31 const float kCos = 0.866f;  // cos(PI / 6);
32
33 // How much dragging should effect the height of the glow image.
34 // Number determined by user testing.
35 const float kPullDistanceAlphaGlowFactor = 1.1f;
36
37 const int kVelocityGlowFactor = 12;
38
39 const ui::SystemUIResourceManager::ResourceType kResourceType =
40     ui::SystemUIResourceManager::OVERSCROLL_GLOW_L;
41
42 template <typename T>
43 T Lerp(T a, T b, T t) {
44   return a + (b - a) * t;
45 }
46
47 template <typename T>
48 T Clamp(T value, T low, T high) {
49   return value < low ? low : (value > high ? high : value);
50 }
51
52 template <typename T>
53 T Damp(T input, T factor) {
54   T result;
55   if (factor == 1) {
56     result = 1 - (1 - input) * (1 - input);
57   } else {
58     result = 1 - std::pow(1 - input, 2 * factor);
59   }
60   return result;
61 }
62
63 }  // namespace
64
65 EdgeEffectL::EdgeEffectL(ui::SystemUIResourceManager* resource_manager)
66     : resource_manager_(resource_manager),
67       glow_(cc::UIResourceLayer::Create()),
68       glow_alpha_(0),
69       glow_scale_y_(0),
70       glow_alpha_start_(0),
71       glow_alpha_finish_(0),
72       glow_scale_y_start_(0),
73       glow_scale_y_finish_(0),
74       displacement_(0.5f),
75       target_displacement_(0.5f),
76       state_(STATE_IDLE),
77       pull_distance_(0) {
78   // Prevent the provided layers from drawing until the effect is activated.
79   glow_->SetIsDrawable(false);
80 }
81
82 EdgeEffectL::~EdgeEffectL() {
83   glow_->RemoveFromParent();
84 }
85
86 bool EdgeEffectL::IsFinished() const {
87   return state_ == STATE_IDLE;
88 }
89
90 void EdgeEffectL::Finish() {
91   glow_->SetIsDrawable(false);
92   pull_distance_ = 0;
93   state_ = STATE_IDLE;
94 }
95
96 void EdgeEffectL::Pull(base::TimeTicks current_time,
97                        float delta_distance,
98                        float displacement) {
99   target_displacement_ = displacement;
100   if (state_ == STATE_PULL_DECAY && current_time - start_time_ < duration_) {
101     return;
102   }
103   if (state_ != STATE_PULL) {
104     glow_scale_y_ = std::max(kPullGlowBegin, glow_scale_y_);
105   }
106   state_ = STATE_PULL;
107
108   start_time_ = current_time;
109   duration_ = base::TimeDelta::FromMilliseconds(kPullTimeMs);
110
111   float abs_delta_distance = std::abs(delta_distance);
112   pull_distance_ += delta_distance;
113
114   glow_alpha_ = glow_alpha_start_ = std::min(
115       kMaxAlpha,
116       glow_alpha_ + (abs_delta_distance * kPullDistanceAlphaGlowFactor));
117
118   if (pull_distance_ == 0) {
119     glow_scale_y_ = glow_scale_y_start_ = 0;
120   } else {
121     float scale = 1.f -
122                   1.f / std::sqrt(std::abs(pull_distance_) * bounds_.height()) -
123                   0.3f;
124     glow_scale_y_ = glow_scale_y_start_ = std::max(0.f, scale) / 0.7f;
125   }
126
127   glow_alpha_finish_ = glow_alpha_;
128   glow_scale_y_finish_ = glow_scale_y_;
129 }
130
131 void EdgeEffectL::Release(base::TimeTicks current_time) {
132   pull_distance_ = 0;
133
134   if (state_ != STATE_PULL && state_ != STATE_PULL_DECAY)
135     return;
136
137   state_ = STATE_RECEDE;
138   glow_alpha_start_ = glow_alpha_;
139   glow_scale_y_start_ = glow_scale_y_;
140
141   glow_alpha_finish_ = 0.f;
142   glow_scale_y_finish_ = 0.f;
143
144   start_time_ = current_time;
145   duration_ = base::TimeDelta::FromMilliseconds(kRecedeTimeMs);
146 }
147
148 void EdgeEffectL::Absorb(base::TimeTicks current_time, float velocity) {
149   state_ = STATE_ABSORB;
150
151   velocity = Clamp(std::abs(velocity), kMinVelocity, kMaxVelocity);
152
153   start_time_ = current_time;
154   // This should never be less than 1 millisecond.
155   duration_ = base::TimeDelta::FromMilliseconds(0.15f + (velocity * 0.02f));
156
157   // The glow depends more on the velocity, and therefore starts out
158   // nearly invisible.
159   glow_alpha_start_ = 0.3f;
160   glow_scale_y_start_ = std::max(glow_scale_y_, 0.f);
161
162   // Growth for the size of the glow should be quadratic to properly respond
163   // to a user's scrolling speed. The faster the scrolling speed, the more
164   // intense the effect should be for both the size and the saturation.
165   glow_scale_y_finish_ =
166       std::min(0.025f + (velocity * (velocity / 100) * 0.00015f) / 2.f, 1.f);
167   // Alpha should change for the glow as well as size.
168   glow_alpha_finish_ = Clamp(
169       glow_alpha_start_, velocity * kVelocityGlowFactor * .00001f, kMaxAlpha);
170   target_displacement_ = 0.5;
171 }
172
173 bool EdgeEffectL::Update(base::TimeTicks current_time) {
174   if (IsFinished())
175     return false;
176
177   const double dt = (current_time - start_time_).InMilliseconds();
178   const double t = std::min(dt / duration_.InMilliseconds(), 1.);
179   const float interp = static_cast<float>(Damp(t, 1.));
180
181   glow_alpha_ = Lerp(glow_alpha_start_, glow_alpha_finish_, interp);
182   glow_scale_y_ = Lerp(glow_scale_y_start_, glow_scale_y_finish_, interp);
183   displacement_ = (displacement_ + target_displacement_) / 2.f;
184
185   if (t >= 1.f - kEpsilon) {
186     switch (state_) {
187       case STATE_ABSORB:
188         state_ = STATE_RECEDE;
189         start_time_ = current_time;
190         duration_ = base::TimeDelta::FromMilliseconds(kRecedeTimeMs);
191
192         glow_alpha_start_ = glow_alpha_;
193         glow_scale_y_start_ = glow_scale_y_;
194
195         glow_alpha_finish_ = 0.f;
196         glow_scale_y_finish_ = 0.f;
197         break;
198       case STATE_PULL:
199         // Hold in this state until explicitly released.
200         break;
201       case STATE_PULL_DECAY:
202         state_ = STATE_RECEDE;
203         break;
204       case STATE_RECEDE:
205         Finish();
206         break;
207       default:
208         break;
209     }
210   }
211
212   bool one_last_frame = false;
213   if (state_ == STATE_RECEDE && glow_scale_y_ <= 0) {
214     Finish();
215     one_last_frame = true;
216   }
217
218   return !IsFinished() || one_last_frame;
219 }
220
221 void EdgeEffectL::ApplyToLayers(const gfx::SizeF& size,
222                                 const gfx::Transform& transform) {
223   if (IsFinished())
224     return;
225
226   // An empty window size, while meaningless, is also relatively harmless, and
227   // will simply prevent any drawing of the layers.
228   if (size.IsEmpty()) {
229     glow_->SetIsDrawable(false);
230     return;
231   }
232
233   const float r = size.width() * 0.75f / kSin;
234   const float y = kCos * r;
235   const float h = r - y;
236   bounds_ = gfx::Size(size.width(), (int)std::min(size.height(), h));
237   gfx::Size image_bounds(r, std::min(1.f, glow_scale_y_) * bounds_.height());
238
239   glow_->SetIsDrawable(true);
240   glow_->SetUIResourceId(resource_manager_->GetUIResourceId(kResourceType));
241   glow_->SetTransformOrigin(gfx::Point3F(bounds_.width() * 0.5f, 0, 0));
242   glow_->SetBounds(image_bounds);
243   glow_->SetContentsOpaque(false);
244   glow_->SetOpacity(Clamp(glow_alpha_, 0.f, 1.f));
245
246   const float displacement = Clamp(displacement_, 0.f, 1.f) - 0.5f;
247   const float displacement_offset_x = bounds_.width() * displacement * 0.5f;
248   const float image_offset_x = (bounds_.width() - image_bounds.width()) * 0.5f;
249   gfx::Transform offset_transform;
250   offset_transform.Translate(image_offset_x - displacement_offset_x, 0);
251   offset_transform.ConcatTransform(transform);
252   glow_->SetTransform(offset_transform);
253 }
254
255 void EdgeEffectL::SetParent(cc::Layer* parent) {
256   if (glow_->parent() != parent)
257     parent->AddChild(glow_);
258   glow_->SetUIResourceId(resource_manager_->GetUIResourceId(kResourceType));
259 }
260
261 // static
262 void EdgeEffectL::PreloadResources(
263     ui::SystemUIResourceManager* resource_manager) {
264   DCHECK(resource_manager);
265   resource_manager->PreloadResource(kResourceType);
266 }
267
268 }  // namespace content