1 // Copyright (c) 2013 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.h"
7 #include "cc/layers/layer.h"
8 #include "cc/layers/ui_resource_layer.h"
9 #include "content/browser/android/animation_utils.h"
10 #include "content/public/browser/android/compositor.h"
11 #include "ui/android/resources/resource_manager.h"
12 #include "ui/android/resources/system_ui_resource_type.h"
18 const ui::SystemUIResourceType kEdgeResourceId
= ui::OVERSCROLL_EDGE
;
19 const ui::SystemUIResourceType kGlowResourceId
= ui::OVERSCROLL_GLOW
;
21 // Time it will take the effect to fully recede in ms
22 const int kRecedeTimeMs
= 1000;
24 // Time it will take before a pulled glow begins receding in ms
25 const int kPullTimeMs
= 167;
27 // Time it will take in ms for a pulled glow to decay before release
28 const int kPullDecayTimeMs
= 1000;
30 const float kMaxAlpha
= 1.f
;
31 const float kHeldEdgeScaleY
= .5f
;
33 const float kMaxGlowHeight
= 4.f
;
35 const float kPullGlowBegin
= 1.f
;
36 const float kPullEdgeBegin
= 0.6f
;
38 // Min/max velocity that will be absorbed
39 const float kMinVelocity
= 100.f
;
40 const float kMaxVelocity
= 10000.f
;
42 const float kEpsilon
= 0.001f
;
44 const float kGlowHeightWidthRatio
= 0.25f
;
46 // How much dragging should effect the height of the edge image.
47 // Number determined by user testing.
48 const int kPullDistanceEdgeFactor
= 7;
50 // How much dragging should effect the height of the glow image.
51 // Number determined by user testing.
52 const int kPullDistanceGlowFactor
= 7;
53 const float kPullDistanceAlphaGlowFactor
= 1.1f
;
55 const int kVelocityEdgeFactor
= 8;
56 const int kVelocityGlowFactor
= 12;
58 const float kEdgeHeightAtMdpi
= 12.f
;
59 const float kGlowHeightAtMdpi
= 128.f
;
63 class EdgeEffect::EffectLayer
{
65 EffectLayer(ui::SystemUIResourceType resource_type
,
66 ui::ResourceManager
* resource_manager
)
68 cc::UIResourceLayer::Create(Compositor::LayerSettings())),
69 resource_type_(resource_type
),
70 resource_manager_(resource_manager
) {}
72 ~EffectLayer() { ui_resource_layer_
->RemoveFromParent(); }
74 void SetParent(cc::Layer
* parent
) {
75 if (ui_resource_layer_
->parent() != parent
)
76 parent
->AddChild(ui_resource_layer_
);
79 void Disable() { ui_resource_layer_
->SetIsDrawable(false); }
81 void Update(const gfx::Size
& size
,
82 const gfx::Transform
& transform
,
84 ui_resource_layer_
->SetUIResourceId(resource_manager_
->GetUIResourceId(
85 ui::ANDROID_RESOURCE_TYPE_SYSTEM
, resource_type_
));
86 ui_resource_layer_
->SetIsDrawable(true);
87 ui_resource_layer_
->SetTransformOrigin(
88 gfx::Point3F(size
.width() * 0.5f
, 0, 0));
89 ui_resource_layer_
->SetTransform(transform
);
90 ui_resource_layer_
->SetBounds(size
);
91 ui_resource_layer_
->SetOpacity(Clamp(opacity
, 0.f
, 1.f
));
94 scoped_refptr
<cc::UIResourceLayer
> ui_resource_layer_
;
95 ui::SystemUIResourceType resource_type_
;
96 ui::ResourceManager
* resource_manager_
;
98 DISALLOW_COPY_AND_ASSIGN(EffectLayer
);
101 EdgeEffect::EdgeEffect(ui::ResourceManager
* resource_manager
,
102 float device_scale_factor
)
103 : edge_(new EffectLayer(kEdgeResourceId
, resource_manager
)),
104 glow_(new EffectLayer(kGlowResourceId
, resource_manager
)),
105 base_edge_height_(kEdgeHeightAtMdpi
* device_scale_factor
),
106 base_glow_height_(kGlowHeightAtMdpi
* device_scale_factor
),
111 edge_alpha_start_(0),
112 edge_alpha_finish_(0),
113 edge_scale_y_start_(0),
114 edge_scale_y_finish_(0),
115 glow_alpha_start_(0),
116 glow_alpha_finish_(0),
117 glow_scale_y_start_(0),
118 glow_scale_y_finish_(0),
123 EdgeEffect::~EdgeEffect() {
126 bool EdgeEffect::IsFinished() const {
127 return state_
== STATE_IDLE
;
130 void EdgeEffect::Finish() {
137 void EdgeEffect::Pull(base::TimeTicks current_time
,
138 float delta_distance
,
139 float displacement
) {
140 if (state_
== STATE_PULL_DECAY
&& current_time
- start_time_
< duration_
) {
143 if (state_
!= STATE_PULL
) {
144 glow_scale_y_
= kPullGlowBegin
;
148 start_time_
= current_time
;
149 duration_
= base::TimeDelta::FromMilliseconds(kPullTimeMs
);
151 float abs_delta_distance
= std::abs(delta_distance
);
152 pull_distance_
+= delta_distance
;
153 float distance
= std::abs(pull_distance_
);
155 edge_alpha_
= edge_alpha_start_
= Clamp(distance
, kPullEdgeBegin
, kMaxAlpha
);
156 edge_scale_y_
= edge_scale_y_start_
=
157 Clamp(distance
* kPullDistanceEdgeFactor
, kHeldEdgeScaleY
, 1.f
);
159 glow_alpha_
= glow_alpha_start_
=
161 glow_alpha_
+ abs_delta_distance
* kPullDistanceAlphaGlowFactor
);
163 float glow_change
= abs_delta_distance
;
164 if (delta_distance
> 0 && pull_distance_
< 0)
165 glow_change
= -glow_change
;
166 if (pull_distance_
== 0)
169 // Do not allow glow to get larger than kMaxGlowHeight.
170 glow_scale_y_
= glow_scale_y_start_
=
171 Clamp(glow_scale_y_
+ glow_change
* kPullDistanceGlowFactor
,
175 edge_alpha_finish_
= edge_alpha_
;
176 edge_scale_y_finish_
= edge_scale_y_
;
177 glow_alpha_finish_
= glow_alpha_
;
178 glow_scale_y_finish_
= glow_scale_y_
;
181 void EdgeEffect::Release(base::TimeTicks current_time
) {
184 if (state_
!= STATE_PULL
&& state_
!= STATE_PULL_DECAY
)
187 state_
= STATE_RECEDE
;
188 edge_alpha_start_
= edge_alpha_
;
189 edge_scale_y_start_
= edge_scale_y_
;
190 glow_alpha_start_
= glow_alpha_
;
191 glow_scale_y_start_
= glow_scale_y_
;
193 edge_alpha_finish_
= 0.f
;
194 edge_scale_y_finish_
= 0.f
;
195 glow_alpha_finish_
= 0.f
;
196 glow_scale_y_finish_
= 0.f
;
198 start_time_
= current_time
;
199 duration_
= base::TimeDelta::FromMilliseconds(kRecedeTimeMs
);
202 void EdgeEffect::Absorb(base::TimeTicks current_time
, float velocity
) {
203 state_
= STATE_ABSORB
;
204 velocity
= Clamp(std::abs(velocity
), kMinVelocity
, kMaxVelocity
);
206 start_time_
= current_time
;
207 // This should never be less than 1 millisecond.
208 duration_
= base::TimeDelta::FromMilliseconds(0.15f
+ (velocity
* 0.02f
));
210 // The edge should always be at least partially visible, regardless
212 edge_alpha_start_
= 0.f
;
213 edge_scale_y_
= edge_scale_y_start_
= 0.f
;
214 // The glow depends more on the velocity, and therefore starts out
216 glow_alpha_start_
= 0.3f
;
217 glow_scale_y_start_
= 0.f
;
219 // Factor the velocity by 8. Testing on device shows this works best to
220 // reflect the strength of the user's scrolling.
221 edge_alpha_finish_
= Clamp(velocity
* kVelocityEdgeFactor
, 0.f
, 1.f
);
222 // Edge should never get larger than the size of its asset.
223 edge_scale_y_finish_
=
224 Clamp(velocity
* kVelocityEdgeFactor
, kHeldEdgeScaleY
, 1.f
);
226 // Growth for the size of the glow should be quadratic to properly
228 // to a user's scrolling speed. The faster the scrolling speed, the more
229 // intense the effect should be for both the size and the saturation.
230 glow_scale_y_finish_
=
231 std::min(0.025f
+ (velocity
* (velocity
/ 100) * 0.00015f
), 1.75f
);
232 // Alpha should change for the glow as well as size.
233 glow_alpha_finish_
= Clamp(
234 glow_alpha_start_
, velocity
* kVelocityGlowFactor
* .00001f
, kMaxAlpha
);
237 bool EdgeEffect::Update(base::TimeTicks current_time
) {
241 const double dt
= (current_time
- start_time_
).InMilliseconds();
242 const double t
= std::min(dt
/ duration_
.InMilliseconds(), 1.);
243 const float interp
= static_cast<float>(Damp(t
, 1.));
245 edge_alpha_
= Lerp(edge_alpha_start_
, edge_alpha_finish_
, interp
);
246 edge_scale_y_
= Lerp(edge_scale_y_start_
, edge_scale_y_finish_
, interp
);
247 glow_alpha_
= Lerp(glow_alpha_start_
, glow_alpha_finish_
, interp
);
248 glow_scale_y_
= Lerp(glow_scale_y_start_
, glow_scale_y_finish_
, interp
);
250 if (t
>= 1.f
- kEpsilon
) {
253 state_
= STATE_RECEDE
;
254 start_time_
= current_time
;
255 duration_
= base::TimeDelta::FromMilliseconds(kRecedeTimeMs
);
257 edge_alpha_start_
= edge_alpha_
;
258 edge_scale_y_start_
= edge_scale_y_
;
259 glow_alpha_start_
= glow_alpha_
;
260 glow_scale_y_start_
= glow_scale_y_
;
262 // After absorb, the glow and edge should fade to nothing.
263 edge_alpha_finish_
= 0.f
;
264 edge_scale_y_finish_
= 0.f
;
265 glow_alpha_finish_
= 0.f
;
266 glow_scale_y_finish_
= 0.f
;
269 state_
= STATE_PULL_DECAY
;
270 start_time_
= current_time
;
271 duration_
= base::TimeDelta::FromMilliseconds(kPullDecayTimeMs
);
273 edge_alpha_start_
= edge_alpha_
;
274 edge_scale_y_start_
= edge_scale_y_
;
275 glow_alpha_start_
= glow_alpha_
;
276 glow_scale_y_start_
= glow_scale_y_
;
278 // After pull, the glow and edge should fade to nothing.
279 edge_alpha_finish_
= 0.f
;
280 edge_scale_y_finish_
= 0.f
;
281 glow_alpha_finish_
= 0.f
;
282 glow_scale_y_finish_
= 0.f
;
284 case STATE_PULL_DECAY
: {
285 // When receding, we want edge to decrease more slowly
289 ? 1 / (glow_scale_y_finish_
* glow_scale_y_finish_
)
290 : std::numeric_limits
<float>::max();
292 edge_scale_y_start_
+
293 (edge_scale_y_finish_
- edge_scale_y_start_
) * interp
* factor
;
294 state_
= STATE_RECEDE
;
304 if (state_
== STATE_RECEDE
&& glow_scale_y_
<= 0 && edge_scale_y_
<= 0)
307 return !IsFinished();
310 float EdgeEffect::GetAlpha() const {
311 return IsFinished() ? 0.f
: std::max(glow_alpha_
, edge_alpha_
);
314 void EdgeEffect::ApplyToLayers(const gfx::SizeF
& size
,
315 const gfx::Transform
& transform
) {
319 // An empty window size, while meaningless, is also relatively harmless, and
320 // will simply prevent any drawing of the layers.
321 if (size
.IsEmpty()) {
328 const int scaled_glow_height
= static_cast<int>(
329 std::min(base_glow_height_
* glow_scale_y_
* kGlowHeightWidthRatio
* 0.6f
,
330 base_glow_height_
* kMaxGlowHeight
) +
332 const gfx::Size
glow_size(size
.width(), scaled_glow_height
);
333 glow_
->Update(glow_size
, transform
, glow_alpha_
);
336 const int scaled_edge_height
=
337 static_cast<int>(base_edge_height_
* edge_scale_y_
);
338 const gfx::Size
edge_size(size
.width(), scaled_edge_height
);
339 edge_
->Update(edge_size
, transform
, edge_alpha_
);
342 void EdgeEffect::SetParent(cc::Layer
* parent
) {
343 edge_
->SetParent(parent
);
344 glow_
->SetParent(parent
);
348 void EdgeEffect::PreloadResources(ui::ResourceManager
* resource_manager
) {
349 DCHECK(resource_manager
);
350 resource_manager
->PreloadResource(ui::ANDROID_RESOURCE_TYPE_SYSTEM
,
352 resource_manager
->PreloadResource(ui::ANDROID_RESOURCE_TYPE_SYSTEM
,
356 } // namespace content