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 "content/public/browser/android/compositor.h"
10 #include "ui/android/resources/resource_manager.h"
11 #include "ui/android/resources/system_ui_resource_type.h"
12 #include "ui/gfx/geometry/size_conversions.h"
18 // Time it will take the effect to fully recede in ms
19 const int kRecedeTimeMs
= 600;
21 // Time it will take before a pulled glow begins receding in ms
22 const int kPullTimeMs
= 167;
24 // Time it will take for a pulled glow to decay to partial strength before
26 const int kPullDecayTimeMs
= 2000;
28 const float kMaxAlpha
= 0.5f
;
30 const float kPullGlowBegin
= 0.f
;
32 // Min/max velocity that will be absorbed
33 const float kMinVelocity
= 100.f
;
34 const float kMaxVelocity
= 10000.f
;
36 const float kEpsilon
= 0.001f
;
38 const float kSin
= 0.5f
; // sin(PI / 6)
39 const float kCos
= 0.866f
; // cos(PI / 6);
41 // How much dragging should effect the height of the glow image.
42 // Number determined by user testing.
43 const float kPullDistanceAlphaGlowFactor
= 0.8f
;
45 const int kVelocityGlowFactor
= 6;
47 const ui::SystemUIResourceType kResourceId
= ui::OVERSCROLL_GLOW_L
;
51 EdgeEffectL::EdgeEffectL(ui::ResourceManager
* resource_manager
)
52 : resource_manager_(resource_manager
),
53 glow_(cc::UIResourceLayer::Create(Compositor::LayerSettings())),
57 glow_alpha_finish_(0),
58 glow_scale_y_start_(0),
59 glow_scale_y_finish_(0),
61 target_displacement_(0.5f
),
64 // Prevent the provided layers from drawing until the effect is activated.
65 glow_
->SetIsDrawable(false);
68 EdgeEffectL::~EdgeEffectL() {
69 glow_
->RemoveFromParent();
72 bool EdgeEffectL::IsFinished() const {
73 return state_
== STATE_IDLE
;
76 void EdgeEffectL::Finish() {
77 glow_
->SetIsDrawable(false);
82 void EdgeEffectL::Pull(base::TimeTicks current_time
,
85 target_displacement_
= displacement
;
86 if (state_
== STATE_PULL_DECAY
&& current_time
- start_time_
< duration_
) {
89 if (state_
!= STATE_PULL
) {
90 glow_scale_y_
= std::max(kPullGlowBegin
, glow_scale_y_
);
94 start_time_
= current_time
;
95 duration_
= base::TimeDelta::FromMilliseconds(kPullTimeMs
);
97 float abs_delta_distance
= std::abs(delta_distance
);
98 pull_distance_
+= delta_distance
;
100 glow_alpha_
= glow_alpha_start_
= std::min(
102 glow_alpha_
+ (abs_delta_distance
* kPullDistanceAlphaGlowFactor
));
104 if (pull_distance_
== 0) {
105 glow_scale_y_
= glow_scale_y_start_
= 0;
108 1.f
/ std::sqrt(std::abs(pull_distance_
) * bounds_
.height()) -
110 glow_scale_y_
= glow_scale_y_start_
= std::max(0.f
, scale
) / 0.7f
;
113 glow_alpha_finish_
= glow_alpha_
;
114 glow_scale_y_finish_
= glow_scale_y_
;
117 void EdgeEffectL::Release(base::TimeTicks current_time
) {
120 if (state_
!= STATE_PULL
&& state_
!= STATE_PULL_DECAY
)
123 state_
= STATE_RECEDE
;
124 glow_alpha_start_
= glow_alpha_
;
125 glow_scale_y_start_
= glow_scale_y_
;
127 glow_alpha_finish_
= 0.f
;
128 glow_scale_y_finish_
= 0.f
;
130 start_time_
= current_time
;
131 duration_
= base::TimeDelta::FromMilliseconds(kRecedeTimeMs
);
134 void EdgeEffectL::Absorb(base::TimeTicks current_time
, float velocity
) {
135 state_
= STATE_ABSORB
;
137 velocity
= Clamp(std::abs(velocity
), kMinVelocity
, kMaxVelocity
);
139 start_time_
= current_time
;
140 // This should never be less than 1 millisecond.
141 duration_
= base::TimeDelta::FromMilliseconds(0.15f
+ (velocity
* 0.02f
));
143 // The glow depends more on the velocity, and therefore starts out
145 glow_alpha_start_
= 0.3f
;
146 glow_scale_y_start_
= std::max(glow_scale_y_
, 0.f
);
148 // Growth for the size of the glow should be quadratic to properly respond
149 // to a user's scrolling speed. The faster the scrolling speed, the more
150 // intense the effect should be for both the size and the saturation.
151 glow_scale_y_finish_
=
152 std::min(0.025f
+ (velocity
* (velocity
/ 100) * 0.00015f
) / 2.f
, 1.f
);
153 // Alpha should change for the glow as well as size.
154 glow_alpha_finish_
= Clamp(
155 glow_alpha_start_
, velocity
* kVelocityGlowFactor
* .00001f
, kMaxAlpha
);
156 target_displacement_
= 0.5;
159 bool EdgeEffectL::Update(base::TimeTicks current_time
) {
163 const double dt
= (current_time
- start_time_
).InMilliseconds();
164 const double t
= std::min(dt
/ duration_
.InMilliseconds(), 1.);
165 const float interp
= static_cast<float>(Damp(t
, 1.));
167 glow_alpha_
= Lerp(glow_alpha_start_
, glow_alpha_finish_
, interp
);
168 glow_scale_y_
= Lerp(glow_scale_y_start_
, glow_scale_y_finish_
, interp
);
169 displacement_
= (displacement_
+ target_displacement_
) / 2.f
;
171 if (t
>= 1.f
- kEpsilon
) {
174 state_
= STATE_RECEDE
;
175 start_time_
= current_time
;
176 duration_
= base::TimeDelta::FromMilliseconds(kRecedeTimeMs
);
178 glow_alpha_start_
= glow_alpha_
;
179 glow_scale_y_start_
= glow_scale_y_
;
181 glow_alpha_finish_
= 0.f
;
182 glow_scale_y_finish_
= 0.f
;
185 state_
= STATE_PULL_DECAY
;
186 start_time_
= current_time
;
187 duration_
= base::TimeDelta::FromMilliseconds(kPullDecayTimeMs
);
189 glow_alpha_start_
= glow_alpha_
;
190 glow_scale_y_start_
= glow_scale_y_
;
192 // After pull, the glow should fade to nothing.
193 glow_alpha_finish_
= 0.f
;
194 glow_scale_y_finish_
= 0.f
;
196 case STATE_PULL_DECAY
:
197 state_
= STATE_RECEDE
;
207 bool one_last_frame
= false;
208 if (state_
== STATE_RECEDE
&& glow_scale_y_
<= 0) {
210 one_last_frame
= true;
213 return !IsFinished() || one_last_frame
;
216 float EdgeEffectL::GetAlpha() const {
217 return IsFinished() ? 0.f
: glow_alpha_
;
220 void EdgeEffectL::ApplyToLayers(Edge edge
,
221 const gfx::SizeF
& viewport_size
,
226 // An empty viewport, while meaningless, is also relatively harmless, and will
227 // simply prevent any drawing of the layers.
228 if (viewport_size
.IsEmpty()) {
229 glow_
->SetIsDrawable(false);
233 gfx::SizeF size
= ComputeOrientedSize(edge
, viewport_size
);
234 const float r
= size
.width() * 0.75f
/ kSin
;
235 const float y
= kCos
* r
;
236 const float h
= r
- y
;
237 const float o_r
= size
.height() * 0.75f
/ kSin
;
238 const float o_y
= kCos
* o_r
;
239 const float o_h
= o_r
- o_y
;
240 const float base_glow_scale
= h
> 0.f
? std::min(o_h
/ h
, 1.f
) : 1.f
;
241 bounds_
= gfx::Size(size
.width(), (int)std::min(size
.height(), h
));
242 gfx::Size
image_bounds(
243 r
, std::min(1.f
, glow_scale_y_
) * base_glow_scale
* bounds_
.height());
245 // Compute the displaced image rect. This includes both the horizontal
246 // offset from the |displacement_| factor, as well as the vertical edge offset
247 // provided by the method call.
248 const float displacement
= Clamp(displacement_
, 0.f
, 1.f
) - 0.5f
;
249 const float displacement_offset_x
= bounds_
.width() * displacement
* 0.5f
;
250 const float image_offset_x
= (bounds_
.width() - image_bounds
.width()) * 0.5f
;
251 gfx::RectF
image_rect(image_bounds
);
252 image_rect
.Offset(image_offset_x
- displacement_offset_x
, -std::abs(offset
));
254 // Clip the image rect against the viewport. If either rect is empty there's
255 // no need to draw anything further.
256 gfx::RectF
clipped_rect(size
.width(), size
.height());
257 clipped_rect
.Intersect(image_rect
);
258 if (clipped_rect
.IsEmpty() || image_rect
.IsEmpty()) {
259 glow_
->SetIsDrawable(false);
263 // Compute the logical UV coordinates of the clipped rect relative to the
264 // displaced image rect.
265 gfx::PointF clipped_top_left
= clipped_rect
.origin();
266 gfx::PointF clipped_bottom_right
= clipped_rect
.bottom_right();
267 gfx::PointF
uv_top_left(
268 (clipped_top_left
.x() - image_rect
.x()) / image_rect
.width(),
269 (clipped_top_left
.y() - image_rect
.y()) / image_rect
.height());
270 gfx::PointF
uv_bottom_right(
271 (clipped_bottom_right
.x() - image_rect
.x()) / image_rect
.width(),
272 (clipped_bottom_right
.y() - image_rect
.y()) / image_rect
.height());
273 glow_
->SetUV(uv_top_left
, uv_bottom_right
);
275 // There's no need to use the provided |offset| when computing the transform;
276 // the offset is built in to the computed UV coordinates.
277 glow_
->SetTransform(ComputeTransform(edge
, viewport_size
, 0));
279 glow_
->SetIsDrawable(true);
280 glow_
->SetUIResourceId(resource_manager_
->GetUIResourceId(
281 ui::ANDROID_RESOURCE_TYPE_SYSTEM
, kResourceId
));
282 glow_
->SetTransformOrigin(gfx::Point3F(bounds_
.width() * 0.5f
, 0, 0));
283 glow_
->SetBounds(gfx::ToRoundedSize(clipped_rect
.size()));
284 glow_
->SetContentsOpaque(false);
285 glow_
->SetOpacity(Clamp(glow_alpha_
, 0.f
, 1.f
));
288 void EdgeEffectL::SetParent(cc::Layer
* parent
) {
289 if (glow_
->parent() != parent
)
290 parent
->AddChild(glow_
);
294 void EdgeEffectL::PreloadResources(ui::ResourceManager
* resource_manager
) {
295 DCHECK(resource_manager
);
296 resource_manager
->PreloadResource(ui::ANDROID_RESOURCE_TYPE_SYSTEM
,
300 } // namespace content