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 "ui/android/resources/resource_manager.h"
11 #include "ui/android/resources/system_ui_resource_type.h"
17 const ui::SystemUIResourceType kEdgeResourceId
= ui::OVERSCROLL_EDGE
;
18 const ui::SystemUIResourceType kGlowResourceId
= ui::OVERSCROLL_GLOW
;
20 // Time it will take the effect to fully recede in ms
21 const int kRecedeTimeMs
= 1000;
23 // Time it will take before a pulled glow begins receding in ms
24 const int kPullTimeMs
= 167;
26 // Time it will take in ms for a pulled glow to decay before release
27 const int kPullDecayTimeMs
= 1000;
29 const float kMaxAlpha
= 1.f
;
30 const float kHeldEdgeScaleY
= .5f
;
32 const float kMaxGlowHeight
= 4.f
;
34 const float kPullGlowBegin
= 1.f
;
35 const float kPullEdgeBegin
= 0.6f
;
37 // Min/max velocity that will be absorbed
38 const float kMinVelocity
= 100.f
;
39 const float kMaxVelocity
= 10000.f
;
41 const float kEpsilon
= 0.001f
;
43 const float kGlowHeightWidthRatio
= 0.25f
;
45 // How much dragging should effect the height of the edge image.
46 // Number determined by user testing.
47 const int kPullDistanceEdgeFactor
= 7;
49 // How much dragging should effect the height of the glow image.
50 // Number determined by user testing.
51 const int kPullDistanceGlowFactor
= 7;
52 const float kPullDistanceAlphaGlowFactor
= 1.1f
;
54 const int kVelocityEdgeFactor
= 8;
55 const int kVelocityGlowFactor
= 12;
57 const float kEdgeHeightAtMdpi
= 12.f
;
58 const float kGlowHeightAtMdpi
= 128.f
;
62 class EdgeEffect::EffectLayer
{
64 EffectLayer(ui::SystemUIResourceType resource_type
,
65 ui::ResourceManager
* resource_manager
)
66 : ui_resource_layer_(cc::UIResourceLayer::Create()),
67 resource_type_(resource_type
),
68 resource_manager_(resource_manager
) {}
70 ~EffectLayer() { ui_resource_layer_
->RemoveFromParent(); }
72 void SetParent(cc::Layer
* parent
) {
73 if (ui_resource_layer_
->parent() != parent
)
74 parent
->AddChild(ui_resource_layer_
);
77 void Disable() { ui_resource_layer_
->SetIsDrawable(false); }
79 void Update(const gfx::Size
& size
,
80 const gfx::Transform
& transform
,
82 ui_resource_layer_
->SetUIResourceId(resource_manager_
->GetUIResourceId(
83 ui::ANDROID_RESOURCE_TYPE_SYSTEM
, resource_type_
));
84 ui_resource_layer_
->SetIsDrawable(true);
85 ui_resource_layer_
->SetTransformOrigin(
86 gfx::Point3F(size
.width() * 0.5f
, 0, 0));
87 ui_resource_layer_
->SetTransform(transform
);
88 ui_resource_layer_
->SetBounds(size
);
89 ui_resource_layer_
->SetOpacity(Clamp(opacity
, 0.f
, 1.f
));
92 scoped_refptr
<cc::UIResourceLayer
> ui_resource_layer_
;
93 ui::SystemUIResourceType resource_type_
;
94 ui::ResourceManager
* resource_manager_
;
96 DISALLOW_COPY_AND_ASSIGN(EffectLayer
);
99 EdgeEffect::EdgeEffect(ui::ResourceManager
* resource_manager
,
100 float device_scale_factor
)
101 : edge_(new EffectLayer(kEdgeResourceId
, resource_manager
)),
102 glow_(new EffectLayer(kGlowResourceId
, resource_manager
)),
103 base_edge_height_(kEdgeHeightAtMdpi
* device_scale_factor
),
104 base_glow_height_(kGlowHeightAtMdpi
* device_scale_factor
),
109 edge_alpha_start_(0),
110 edge_alpha_finish_(0),
111 edge_scale_y_start_(0),
112 edge_scale_y_finish_(0),
113 glow_alpha_start_(0),
114 glow_alpha_finish_(0),
115 glow_scale_y_start_(0),
116 glow_scale_y_finish_(0),
121 EdgeEffect::~EdgeEffect() {
124 bool EdgeEffect::IsFinished() const {
125 return state_
== STATE_IDLE
;
128 void EdgeEffect::Finish() {
135 void EdgeEffect::Pull(base::TimeTicks current_time
,
136 float delta_distance
,
137 float displacement
) {
138 if (state_
== STATE_PULL_DECAY
&& current_time
- start_time_
< duration_
) {
141 if (state_
!= STATE_PULL
) {
142 glow_scale_y_
= kPullGlowBegin
;
146 start_time_
= current_time
;
147 duration_
= base::TimeDelta::FromMilliseconds(kPullTimeMs
);
149 float abs_delta_distance
= std::abs(delta_distance
);
150 pull_distance_
+= delta_distance
;
151 float distance
= std::abs(pull_distance_
);
153 edge_alpha_
= edge_alpha_start_
= Clamp(distance
, kPullEdgeBegin
, kMaxAlpha
);
154 edge_scale_y_
= edge_scale_y_start_
=
155 Clamp(distance
* kPullDistanceEdgeFactor
, kHeldEdgeScaleY
, 1.f
);
157 glow_alpha_
= glow_alpha_start_
=
159 glow_alpha_
+ abs_delta_distance
* kPullDistanceAlphaGlowFactor
);
161 float glow_change
= abs_delta_distance
;
162 if (delta_distance
> 0 && pull_distance_
< 0)
163 glow_change
= -glow_change
;
164 if (pull_distance_
== 0)
167 // Do not allow glow to get larger than kMaxGlowHeight.
168 glow_scale_y_
= glow_scale_y_start_
=
169 Clamp(glow_scale_y_
+ glow_change
* kPullDistanceGlowFactor
,
173 edge_alpha_finish_
= edge_alpha_
;
174 edge_scale_y_finish_
= edge_scale_y_
;
175 glow_alpha_finish_
= glow_alpha_
;
176 glow_scale_y_finish_
= glow_scale_y_
;
179 void EdgeEffect::Release(base::TimeTicks current_time
) {
182 if (state_
!= STATE_PULL
&& state_
!= STATE_PULL_DECAY
)
185 state_
= STATE_RECEDE
;
186 edge_alpha_start_
= edge_alpha_
;
187 edge_scale_y_start_
= edge_scale_y_
;
188 glow_alpha_start_
= glow_alpha_
;
189 glow_scale_y_start_
= glow_scale_y_
;
191 edge_alpha_finish_
= 0.f
;
192 edge_scale_y_finish_
= 0.f
;
193 glow_alpha_finish_
= 0.f
;
194 glow_scale_y_finish_
= 0.f
;
196 start_time_
= current_time
;
197 duration_
= base::TimeDelta::FromMilliseconds(kRecedeTimeMs
);
200 void EdgeEffect::Absorb(base::TimeTicks current_time
, float velocity
) {
201 state_
= STATE_ABSORB
;
202 velocity
= Clamp(std::abs(velocity
), kMinVelocity
, kMaxVelocity
);
204 start_time_
= current_time
;
205 // This should never be less than 1 millisecond.
206 duration_
= base::TimeDelta::FromMilliseconds(0.15f
+ (velocity
* 0.02f
));
208 // The edge should always be at least partially visible, regardless
210 edge_alpha_start_
= 0.f
;
211 edge_scale_y_
= edge_scale_y_start_
= 0.f
;
212 // The glow depends more on the velocity, and therefore starts out
214 glow_alpha_start_
= 0.3f
;
215 glow_scale_y_start_
= 0.f
;
217 // Factor the velocity by 8. Testing on device shows this works best to
218 // reflect the strength of the user's scrolling.
219 edge_alpha_finish_
= Clamp(velocity
* kVelocityEdgeFactor
, 0.f
, 1.f
);
220 // Edge should never get larger than the size of its asset.
221 edge_scale_y_finish_
=
222 Clamp(velocity
* kVelocityEdgeFactor
, kHeldEdgeScaleY
, 1.f
);
224 // Growth for the size of the glow should be quadratic to properly
226 // to a user's scrolling speed. The faster the scrolling speed, the more
227 // intense the effect should be for both the size and the saturation.
228 glow_scale_y_finish_
=
229 std::min(0.025f
+ (velocity
* (velocity
/ 100) * 0.00015f
), 1.75f
);
230 // Alpha should change for the glow as well as size.
231 glow_alpha_finish_
= Clamp(
232 glow_alpha_start_
, velocity
* kVelocityGlowFactor
* .00001f
, kMaxAlpha
);
235 bool EdgeEffect::Update(base::TimeTicks current_time
) {
239 const double dt
= (current_time
- start_time_
).InMilliseconds();
240 const double t
= std::min(dt
/ duration_
.InMilliseconds(), 1.);
241 const float interp
= static_cast<float>(Damp(t
, 1.));
243 edge_alpha_
= Lerp(edge_alpha_start_
, edge_alpha_finish_
, interp
);
244 edge_scale_y_
= Lerp(edge_scale_y_start_
, edge_scale_y_finish_
, interp
);
245 glow_alpha_
= Lerp(glow_alpha_start_
, glow_alpha_finish_
, interp
);
246 glow_scale_y_
= Lerp(glow_scale_y_start_
, glow_scale_y_finish_
, interp
);
248 if (t
>= 1.f
- kEpsilon
) {
251 state_
= STATE_RECEDE
;
252 start_time_
= current_time
;
253 duration_
= base::TimeDelta::FromMilliseconds(kRecedeTimeMs
);
255 edge_alpha_start_
= edge_alpha_
;
256 edge_scale_y_start_
= edge_scale_y_
;
257 glow_alpha_start_
= glow_alpha_
;
258 glow_scale_y_start_
= glow_scale_y_
;
260 // After absorb, the glow and edge should fade to nothing.
261 edge_alpha_finish_
= 0.f
;
262 edge_scale_y_finish_
= 0.f
;
263 glow_alpha_finish_
= 0.f
;
264 glow_scale_y_finish_
= 0.f
;
267 state_
= STATE_PULL_DECAY
;
268 start_time_
= current_time
;
269 duration_
= base::TimeDelta::FromMilliseconds(kPullDecayTimeMs
);
271 edge_alpha_start_
= edge_alpha_
;
272 edge_scale_y_start_
= edge_scale_y_
;
273 glow_alpha_start_
= glow_alpha_
;
274 glow_scale_y_start_
= glow_scale_y_
;
276 // After pull, the glow and edge should fade to nothing.
277 edge_alpha_finish_
= 0.f
;
278 edge_scale_y_finish_
= 0.f
;
279 glow_alpha_finish_
= 0.f
;
280 glow_scale_y_finish_
= 0.f
;
282 case STATE_PULL_DECAY
: {
283 // When receding, we want edge to decrease more slowly
287 ? 1 / (glow_scale_y_finish_
* glow_scale_y_finish_
)
288 : std::numeric_limits
<float>::max();
290 edge_scale_y_start_
+
291 (edge_scale_y_finish_
- edge_scale_y_start_
) * interp
* factor
;
292 state_
= STATE_RECEDE
;
302 if (state_
== STATE_RECEDE
&& glow_scale_y_
<= 0 && edge_scale_y_
<= 0)
305 return !IsFinished();
308 float EdgeEffect::GetAlpha() const {
309 return IsFinished() ? 0.f
: std::max(glow_alpha_
, edge_alpha_
);
312 void EdgeEffect::ApplyToLayers(const gfx::SizeF
& size
,
313 const gfx::Transform
& transform
) {
317 // An empty window size, while meaningless, is also relatively harmless, and
318 // will simply prevent any drawing of the layers.
319 if (size
.IsEmpty()) {
326 const int scaled_glow_height
= static_cast<int>(
327 std::min(base_glow_height_
* glow_scale_y_
* kGlowHeightWidthRatio
* 0.6f
,
328 base_glow_height_
* kMaxGlowHeight
) +
330 const gfx::Size
glow_size(size
.width(), scaled_glow_height
);
331 glow_
->Update(glow_size
, transform
, glow_alpha_
);
334 const int scaled_edge_height
=
335 static_cast<int>(base_edge_height_
* edge_scale_y_
);
336 const gfx::Size
edge_size(size
.width(), scaled_edge_height
);
337 edge_
->Update(edge_size
, transform
, edge_alpha_
);
340 void EdgeEffect::SetParent(cc::Layer
* parent
) {
341 edge_
->SetParent(parent
);
342 glow_
->SetParent(parent
);
346 void EdgeEffect::PreloadResources(ui::ResourceManager
* resource_manager
) {
347 DCHECK(resource_manager
);
348 resource_manager
->PreloadResource(ui::ANDROID_RESOURCE_TYPE_SYSTEM
,
350 resource_manager
->PreloadResource(ui::ANDROID_RESOURCE_TYPE_SYSTEM
,
354 } // namespace content