Blink roll 25b6bd3a7a131ffe68d809546ad1a20707915cdc:3a503f41ae42e5b79cfcd2ff10e65afde...
[chromium-blink-merge.git] / content / browser / android / edge_effect_l.cc
blob37f1cacdd02064b294f50562c92a77308097ede4
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.
5 #include "content/browser/android/edge_effect_l.h"
7 #include "cc/layers/ui_resource_layer.h"
8 #include "content/browser/android/animation_utils.h"
9 #include "ui/base/android/system_ui_resource_manager.h"
11 namespace content {
13 namespace {
15 // Time it will take the effect to fully recede in ms
16 const int kRecedeTimeMs = 600;
18 // Time it will take before a pulled glow begins receding in ms
19 const int kPullTimeMs = 167;
21 // Time it will take for a pulled glow to decay to partial strength before
22 // release
23 const int kPullDecayTimeMs = 2000;
25 const float kMaxAlpha = 0.5f;
27 const float kPullGlowBegin = 0.f;
29 // Min/max velocity that will be absorbed
30 const float kMinVelocity = 100.f;
31 const float kMaxVelocity = 10000.f;
33 const float kEpsilon = 0.001f;
35 const float kSin = 0.5f; // sin(PI / 6)
36 const float kCos = 0.866f; // cos(PI / 6);
38 // How much dragging should effect the height of the glow image.
39 // Number determined by user testing.
40 const float kPullDistanceAlphaGlowFactor = 0.8f;
42 const int kVelocityGlowFactor = 6;
44 const ui::SystemUIResourceType kResourceType = ui::OVERSCROLL_GLOW_L;
46 } // namespace
48 EdgeEffectL::EdgeEffectL(ui::SystemUIResourceManager* resource_manager)
49 : resource_manager_(resource_manager),
50 glow_(cc::UIResourceLayer::Create()),
51 glow_alpha_(0),
52 glow_scale_y_(0),
53 glow_alpha_start_(0),
54 glow_alpha_finish_(0),
55 glow_scale_y_start_(0),
56 glow_scale_y_finish_(0),
57 displacement_(0.5f),
58 target_displacement_(0.5f),
59 state_(STATE_IDLE),
60 pull_distance_(0) {
61 // Prevent the provided layers from drawing until the effect is activated.
62 glow_->SetIsDrawable(false);
65 EdgeEffectL::~EdgeEffectL() {
66 glow_->RemoveFromParent();
69 bool EdgeEffectL::IsFinished() const {
70 return state_ == STATE_IDLE;
73 void EdgeEffectL::Finish() {
74 glow_->SetIsDrawable(false);
75 pull_distance_ = 0;
76 state_ = STATE_IDLE;
79 void EdgeEffectL::Pull(base::TimeTicks current_time,
80 float delta_distance,
81 float displacement) {
82 target_displacement_ = displacement;
83 if (state_ == STATE_PULL_DECAY && current_time - start_time_ < duration_) {
84 return;
86 if (state_ != STATE_PULL) {
87 glow_scale_y_ = std::max(kPullGlowBegin, glow_scale_y_);
89 state_ = STATE_PULL;
91 start_time_ = current_time;
92 duration_ = base::TimeDelta::FromMilliseconds(kPullTimeMs);
94 float abs_delta_distance = std::abs(delta_distance);
95 pull_distance_ += delta_distance;
97 glow_alpha_ = glow_alpha_start_ = std::min(
98 kMaxAlpha,
99 glow_alpha_ + (abs_delta_distance * kPullDistanceAlphaGlowFactor));
101 if (pull_distance_ == 0) {
102 glow_scale_y_ = glow_scale_y_start_ = 0;
103 } else {
104 float scale = 1.f -
105 1.f / std::sqrt(std::abs(pull_distance_) * bounds_.height()) -
106 0.3f;
107 glow_scale_y_ = glow_scale_y_start_ = std::max(0.f, scale) / 0.7f;
110 glow_alpha_finish_ = glow_alpha_;
111 glow_scale_y_finish_ = glow_scale_y_;
114 void EdgeEffectL::Release(base::TimeTicks current_time) {
115 pull_distance_ = 0;
117 if (state_ != STATE_PULL && state_ != STATE_PULL_DECAY)
118 return;
120 state_ = STATE_RECEDE;
121 glow_alpha_start_ = glow_alpha_;
122 glow_scale_y_start_ = glow_scale_y_;
124 glow_alpha_finish_ = 0.f;
125 glow_scale_y_finish_ = 0.f;
127 start_time_ = current_time;
128 duration_ = base::TimeDelta::FromMilliseconds(kRecedeTimeMs);
131 void EdgeEffectL::Absorb(base::TimeTicks current_time, float velocity) {
132 state_ = STATE_ABSORB;
134 velocity = Clamp(std::abs(velocity), kMinVelocity, kMaxVelocity);
136 start_time_ = current_time;
137 // This should never be less than 1 millisecond.
138 duration_ = base::TimeDelta::FromMilliseconds(0.15f + (velocity * 0.02f));
140 // The glow depends more on the velocity, and therefore starts out
141 // nearly invisible.
142 glow_alpha_start_ = 0.3f;
143 glow_scale_y_start_ = std::max(glow_scale_y_, 0.f);
145 // Growth for the size of the glow should be quadratic to properly respond
146 // to a user's scrolling speed. The faster the scrolling speed, the more
147 // intense the effect should be for both the size and the saturation.
148 glow_scale_y_finish_ =
149 std::min(0.025f + (velocity * (velocity / 100) * 0.00015f) / 2.f, 1.f);
150 // Alpha should change for the glow as well as size.
151 glow_alpha_finish_ = Clamp(
152 glow_alpha_start_, velocity * kVelocityGlowFactor * .00001f, kMaxAlpha);
153 target_displacement_ = 0.5;
156 bool EdgeEffectL::Update(base::TimeTicks current_time) {
157 if (IsFinished())
158 return false;
160 const double dt = (current_time - start_time_).InMilliseconds();
161 const double t = std::min(dt / duration_.InMilliseconds(), 1.);
162 const float interp = static_cast<float>(Damp(t, 1.));
164 glow_alpha_ = Lerp(glow_alpha_start_, glow_alpha_finish_, interp);
165 glow_scale_y_ = Lerp(glow_scale_y_start_, glow_scale_y_finish_, interp);
166 displacement_ = (displacement_ + target_displacement_) / 2.f;
168 if (t >= 1.f - kEpsilon) {
169 switch (state_) {
170 case STATE_ABSORB:
171 state_ = STATE_RECEDE;
172 start_time_ = current_time;
173 duration_ = base::TimeDelta::FromMilliseconds(kRecedeTimeMs);
175 glow_alpha_start_ = glow_alpha_;
176 glow_scale_y_start_ = glow_scale_y_;
178 glow_alpha_finish_ = 0.f;
179 glow_scale_y_finish_ = 0.f;
180 break;
181 case STATE_PULL:
182 state_ = STATE_PULL_DECAY;
183 start_time_ = current_time;
184 duration_ = base::TimeDelta::FromMilliseconds(kPullDecayTimeMs);
186 glow_alpha_start_ = glow_alpha_;
187 glow_scale_y_start_ = glow_scale_y_;
189 // After pull, the glow should fade to nothing.
190 glow_alpha_finish_ = 0.f;
191 glow_scale_y_finish_ = 0.f;
192 break;
193 case STATE_PULL_DECAY:
194 state_ = STATE_RECEDE;
195 break;
196 case STATE_RECEDE:
197 Finish();
198 break;
199 default:
200 break;
204 bool one_last_frame = false;
205 if (state_ == STATE_RECEDE && glow_scale_y_ <= 0) {
206 Finish();
207 one_last_frame = true;
210 return !IsFinished() || one_last_frame;
213 void EdgeEffectL::ApplyToLayers(const gfx::SizeF& size,
214 const gfx::Transform& transform) {
215 if (IsFinished())
216 return;
218 // An empty window size, while meaningless, is also relatively harmless, and
219 // will simply prevent any drawing of the layers.
220 if (size.IsEmpty()) {
221 glow_->SetIsDrawable(false);
222 return;
225 const float r = size.width() * 0.75f / kSin;
226 const float y = kCos * r;
227 const float h = r - y;
228 const float o_r = size.height() * 0.75f / kSin;
229 const float o_y = kCos * o_r;
230 const float o_h = o_r - o_y;
231 const float base_glow_scale = h > 0.f ? std::min(o_h / h, 1.f) : 1.f;
232 bounds_ = gfx::Size(size.width(), (int)std::min(size.height(), h));
233 gfx::Size image_bounds(
234 r, std::min(1.f, glow_scale_y_) * base_glow_scale * bounds_.height());
236 glow_->SetIsDrawable(true);
237 glow_->SetUIResourceId(resource_manager_->GetUIResourceId(kResourceType));
238 glow_->SetTransformOrigin(gfx::Point3F(bounds_.width() * 0.5f, 0, 0));
239 glow_->SetBounds(image_bounds);
240 glow_->SetContentsOpaque(false);
241 glow_->SetOpacity(Clamp(glow_alpha_, 0.f, 1.f));
243 const float displacement = Clamp(displacement_, 0.f, 1.f) - 0.5f;
244 const float displacement_offset_x = bounds_.width() * displacement * 0.5f;
245 const float image_offset_x = (bounds_.width() - image_bounds.width()) * 0.5f;
246 gfx::Transform offset_transform;
247 offset_transform.Translate(image_offset_x - displacement_offset_x, 0);
248 offset_transform.ConcatTransform(transform);
249 glow_->SetTransform(offset_transform);
252 void EdgeEffectL::SetParent(cc::Layer* parent) {
253 if (glow_->parent() != parent)
254 parent->AddChild(glow_);
257 // static
258 void EdgeEffectL::PreloadResources(
259 ui::SystemUIResourceManager* resource_manager) {
260 DCHECK(resource_manager);
261 resource_manager->PreloadResource(kResourceType);
264 } // namespace content