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 "ui/base/android/system_ui_resource_manager.h"
14 // Time it will take the effect to fully recede in ms
15 const int kRecedeTimeMs
= 1000;
17 // Time it will take before a pulled glow begins receding in ms
18 const int kPullTimeMs
= 167;
20 const float kMaxAlpha
= 1.f
;
22 const float kPullGlowBegin
= 0.f
;
24 // Min/max velocity that will be absorbed
25 const float kMinVelocity
= 100.f
;
26 const float kMaxVelocity
= 10000.f
;
28 const float kEpsilon
= 0.001f
;
30 const float kSin
= 0.5f
; // sin(PI / 6)
31 const float kCos
= 0.866f
; // cos(PI / 6);
33 // How much dragging should effect the height of the glow image.
34 // Number determined by user testing.
35 const float kPullDistanceAlphaGlowFactor
= 1.1f
;
37 const int kVelocityGlowFactor
= 12;
39 const ui::SystemUIResourceManager::ResourceType kResourceType
=
40 ui::SystemUIResourceManager::OVERSCROLL_GLOW_L
;
43 T
Lerp(T a
, T b
, T t
) {
44 return a
+ (b
- a
) * t
;
48 T
Clamp(T value
, T low
, T high
) {
49 return value
< low
? low
: (value
> high
? high
: value
);
53 T
Damp(T input
, T factor
) {
56 result
= 1 - (1 - input
) * (1 - input
);
58 result
= 1 - std::pow(1 - input
, 2 * factor
);
65 EdgeEffectL::EdgeEffectL(ui::SystemUIResourceManager
* resource_manager
)
66 : resource_manager_(resource_manager
),
67 glow_(cc::UIResourceLayer::Create()),
71 glow_alpha_finish_(0),
72 glow_scale_y_start_(0),
73 glow_scale_y_finish_(0),
75 target_displacement_(0.5f
),
78 // Prevent the provided layers from drawing until the effect is activated.
79 glow_
->SetIsDrawable(false);
82 EdgeEffectL::~EdgeEffectL() {
83 glow_
->RemoveFromParent();
86 bool EdgeEffectL::IsFinished() const {
87 return state_
== STATE_IDLE
;
90 void EdgeEffectL::Finish() {
91 glow_
->SetIsDrawable(false);
96 void EdgeEffectL::Pull(base::TimeTicks current_time
,
99 target_displacement_
= displacement
;
100 if (state_
== STATE_PULL_DECAY
&& current_time
- start_time_
< duration_
) {
103 if (state_
!= STATE_PULL
) {
104 glow_scale_y_
= std::max(kPullGlowBegin
, glow_scale_y_
);
108 start_time_
= current_time
;
109 duration_
= base::TimeDelta::FromMilliseconds(kPullTimeMs
);
111 float abs_delta_distance
= std::abs(delta_distance
);
112 pull_distance_
+= delta_distance
;
114 glow_alpha_
= glow_alpha_start_
= std::min(
116 glow_alpha_
+ (abs_delta_distance
* kPullDistanceAlphaGlowFactor
));
118 if (pull_distance_
== 0) {
119 glow_scale_y_
= glow_scale_y_start_
= 0;
122 1.f
/ std::sqrt(std::abs(pull_distance_
) * bounds_
.height()) -
124 glow_scale_y_
= glow_scale_y_start_
= std::max(0.f
, scale
) / 0.7f
;
127 glow_alpha_finish_
= glow_alpha_
;
128 glow_scale_y_finish_
= glow_scale_y_
;
131 void EdgeEffectL::Release(base::TimeTicks current_time
) {
134 if (state_
!= STATE_PULL
&& state_
!= STATE_PULL_DECAY
)
137 state_
= STATE_RECEDE
;
138 glow_alpha_start_
= glow_alpha_
;
139 glow_scale_y_start_
= glow_scale_y_
;
141 glow_alpha_finish_
= 0.f
;
142 glow_scale_y_finish_
= 0.f
;
144 start_time_
= current_time
;
145 duration_
= base::TimeDelta::FromMilliseconds(kRecedeTimeMs
);
148 void EdgeEffectL::Absorb(base::TimeTicks current_time
, float velocity
) {
149 state_
= STATE_ABSORB
;
151 velocity
= Clamp(std::abs(velocity
), kMinVelocity
, kMaxVelocity
);
153 start_time_
= current_time
;
154 // This should never be less than 1 millisecond.
155 duration_
= base::TimeDelta::FromMilliseconds(0.15f
+ (velocity
* 0.02f
));
157 // The glow depends more on the velocity, and therefore starts out
159 glow_alpha_start_
= 0.3f
;
160 glow_scale_y_start_
= std::max(glow_scale_y_
, 0.f
);
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;
173 bool EdgeEffectL::Update(base::TimeTicks current_time
) {
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.));
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
;
185 if (t
>= 1.f
- kEpsilon
) {
188 state_
= STATE_RECEDE
;
189 start_time_
= current_time
;
190 duration_
= base::TimeDelta::FromMilliseconds(kRecedeTimeMs
);
192 glow_alpha_start_
= glow_alpha_
;
193 glow_scale_y_start_
= glow_scale_y_
;
195 glow_alpha_finish_
= 0.f
;
196 glow_scale_y_finish_
= 0.f
;
199 // Hold in this state until explicitly released.
201 case STATE_PULL_DECAY
:
202 state_
= STATE_RECEDE
;
212 bool one_last_frame
= false;
213 if (state_
== STATE_RECEDE
&& glow_scale_y_
<= 0) {
215 one_last_frame
= true;
218 return !IsFinished() || one_last_frame
;
221 void EdgeEffectL::ApplyToLayers(const gfx::SizeF
& size
,
222 const gfx::Transform
& transform
) {
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);
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());
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
));
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
);
255 void EdgeEffectL::SetParent(cc::Layer
* parent
) {
256 if (glow_
->parent() != parent
)
257 parent
->AddChild(glow_
);
258 glow_
->SetUIResourceId(resource_manager_
->GetUIResourceId(kResourceType
));
262 void EdgeEffectL::PreloadResources(
263 ui::SystemUIResourceManager
* resource_manager
) {
264 DCHECK(resource_manager
);
265 resource_manager
->PreloadResource(kResourceType
);
268 } // namespace content