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/overscroll_glow.h"
7 #include "base/debug/trace_event.h"
8 #include "base/lazy_instance.h"
9 #include "cc/layers/image_layer.h"
10 #include "content/browser/android/edge_effect.h"
11 #include "ui/gfx/android/java_bitmap.h"
20 const float kEpsilon
= 1e-3f
;
22 class OverscrollResources
{
24 OverscrollResources() {
25 TRACE_EVENT0("browser", "OverscrollResources::Create");
27 gfx::CreateSkBitmapFromResource("android:drawable/overscroll_edge",
30 gfx::CreateSkBitmapFromResource("android:drawable/overscroll_glow",
34 const SkBitmap
& edge_bitmap() { return edge_bitmap_
; }
35 const SkBitmap
& glow_bitmap() { return glow_bitmap_
; }
38 SkBitmap edge_bitmap_
;
39 SkBitmap glow_bitmap_
;
41 DISALLOW_COPY_AND_ASSIGN(OverscrollResources
);
44 // Leaky to allow access from a worker thread.
45 base::LazyInstance
<OverscrollResources
>::Leaky g_overscroll_resources
=
46 LAZY_INSTANCE_INITIALIZER
;
48 scoped_refptr
<cc::Layer
> CreateImageLayer(const SkBitmap
& bitmap
) {
49 scoped_refptr
<cc::ImageLayer
> layer
= cc::ImageLayer::Create();
50 layer
->SetBitmap(bitmap
);
54 bool IsApproxZero(float value
) {
55 return std::abs(value
) < kEpsilon
;
58 gfx::Vector2dF
ZeroSmallComponents(gfx::Vector2dF vector
) {
59 if (IsApproxZero(vector
.x()))
61 if (IsApproxZero(vector
.y()))
68 scoped_ptr
<OverscrollGlow
> OverscrollGlow::Create(bool enabled
,
70 const SkBitmap
& edge
= g_overscroll_resources
.Get().edge_bitmap();
71 const SkBitmap
& glow
= g_overscroll_resources
.Get().glow_bitmap();
72 if (edge
.isNull() || glow
.isNull())
73 return scoped_ptr
<OverscrollGlow
>();
75 return make_scoped_ptr(new OverscrollGlow(enabled
, size
, edge
, glow
));
78 void OverscrollGlow::EnsureResources() {
79 g_overscroll_resources
.Get();
82 OverscrollGlow::OverscrollGlow(bool enabled
,
88 horizontal_overscroll_enabled_(true),
89 vertical_overscroll_enabled_(true),
90 root_layer_(cc::Layer::Create()) {
91 for (size_t i
= 0; i
< EdgeEffect::EDGE_COUNT
; ++i
) {
92 scoped_refptr
<cc::Layer
> edge_layer
= CreateImageLayer(edge
);
93 scoped_refptr
<cc::Layer
> glow_layer
= CreateImageLayer(glow
);
94 root_layer_
->AddChild(edge_layer
);
95 root_layer_
->AddChild(glow_layer
);
96 edge_effects_
[i
] = make_scoped_ptr(new EdgeEffect(edge_layer
, glow_layer
));
100 OverscrollGlow::~OverscrollGlow() {
101 root_layer_
->RemoveFromParent();
104 void OverscrollGlow::OnOverscrolled(base::TimeTicks current_time
,
105 gfx::Vector2dF overscroll
,
106 gfx::Vector2dF velocity
) {
110 // The size of the glow determines the relative effect of the inputs; an
111 // empty-sized effect is effectively disabled.
115 if (!horizontal_overscroll_enabled_
) {
119 if (!vertical_overscroll_enabled_
) {
124 // Ignore sufficiently small values that won't meaningfuly affect animation.
125 overscroll
= ZeroSmallComponents(overscroll
);
126 velocity
= ZeroSmallComponents(velocity
);
128 if (overscroll
.IsZero()) {
129 Release(current_time
);
133 if (!velocity
.IsZero()) {
134 // Release effects if scrolling has changed directions.
135 if (velocity
.x() * old_velocity_
.x() < 0)
136 ReleaseAxis(AXIS_X
, current_time
);
137 if (velocity
.y() * old_velocity_
.y() < 0)
138 ReleaseAxis(AXIS_Y
, current_time
);
140 Absorb(current_time
, velocity
, overscroll
, old_overscroll_
);
142 // Release effects when overscroll accumulation violates monotonicity.
143 if (overscroll
.x() * old_overscroll_
.x() < 0 ||
144 std::abs(overscroll
.x()) < std::abs(old_overscroll_
.x()))
145 ReleaseAxis(AXIS_X
, current_time
);
146 if (overscroll
.y() * old_overscroll_
.y() < 0 ||
147 std::abs(overscroll
.y()) < std::abs(old_overscroll_
.y()))
148 ReleaseAxis(AXIS_Y
, current_time
);
150 Pull(current_time
, overscroll
- old_overscroll_
);
153 old_velocity_
= velocity
;
154 old_overscroll_
= overscroll
;
157 void OverscrollGlow::Release(base::TimeTicks current_time
) {
158 for (size_t i
= 0; i
< EdgeEffect::EDGE_COUNT
; ++i
) {
159 edge_effects_
[i
]->Release(current_time
);
161 old_overscroll_
= old_velocity_
= gfx::Vector2dF();
164 bool OverscrollGlow::Animate(base::TimeTicks current_time
) {
168 const gfx::SizeF sizes
[EdgeEffect::EDGE_COUNT
] = {
169 size_
, gfx::SizeF(size_
.height(), size_
.width()),
170 size_
, gfx::SizeF(size_
.height(), size_
.width())
173 for (size_t i
= 0; i
< EdgeEffect::EDGE_COUNT
; ++i
) {
174 if (edge_effects_
[i
]->Update(current_time
)) {
175 edge_effects_
[i
]->ApplyToLayers(sizes
[i
],
176 static_cast<EdgeEffect::Edge
>(i
));
180 return NeedsAnimate();
183 void OverscrollGlow::SetEnabled(bool enabled
) {
184 if (enabled_
== enabled
)
188 for (size_t i
= 0; i
< EdgeEffect::EDGE_COUNT
; ++i
)
189 edge_effects_
[i
]->Finish();
193 bool OverscrollGlow::NeedsAnimate() const {
196 for (size_t i
= 0; i
< EdgeEffect::EDGE_COUNT
; ++i
) {
197 if (!edge_effects_
[i
]->IsFinished())
203 void OverscrollGlow::Pull(base::TimeTicks current_time
,
204 gfx::Vector2dF overscroll_delta
) {
205 overscroll_delta
= ZeroSmallComponents(overscroll_delta
);
206 if (overscroll_delta
.IsZero())
209 gfx::Vector2dF overscroll_pull
= gfx::ScaleVector2d(overscroll_delta
,
211 1.f
/ size_
.height());
212 float edge_overscroll_pull
[EdgeEffect::EDGE_COUNT
] = {
213 min(overscroll_pull
.y(), 0.f
), // Top
214 min(overscroll_pull
.x(), 0.f
), // Left
215 max(overscroll_pull
.y(), 0.f
), // Bottom
216 max(overscroll_pull
.x(), 0.f
) // Right
219 for (size_t i
= 0; i
< EdgeEffect::EDGE_COUNT
; ++i
) {
220 if (!edge_overscroll_pull
[i
])
223 edge_effects_
[i
]->Pull(current_time
, std::abs(edge_overscroll_pull
[i
]));
224 GetOppositeEdge(i
)->Release(current_time
);
228 void OverscrollGlow::Absorb(base::TimeTicks current_time
,
229 gfx::Vector2dF velocity
,
230 gfx::Vector2dF overscroll
,
231 gfx::Vector2dF old_overscroll
) {
232 if (overscroll
.IsZero() || velocity
.IsZero())
235 // Only trigger on initial overscroll at a non-zero velocity
236 const float overscroll_velocities
[EdgeEffect::EDGE_COUNT
] = {
237 old_overscroll
.y() >= 0 && overscroll
.y() < 0 ? min(velocity
.y(), 0.f
) : 0,
238 old_overscroll
.x() >= 0 && overscroll
.x() < 0 ? min(velocity
.x(), 0.f
) : 0,
239 old_overscroll
.y() <= 0 && overscroll
.y() > 0 ? max(velocity
.y(), 0.f
) : 0,
240 old_overscroll
.x() <= 0 && overscroll
.x() > 0 ? max(velocity
.x(), 0.f
) : 0
243 for (size_t i
= 0; i
< EdgeEffect::EDGE_COUNT
; ++i
) {
244 if (!overscroll_velocities
[i
])
247 edge_effects_
[i
]->Absorb(current_time
, std::abs(overscroll_velocities
[i
]));
248 GetOppositeEdge(i
)->Release(current_time
);
252 void OverscrollGlow::ReleaseAxis(Axis axis
, base::TimeTicks current_time
) {
255 edge_effects_
[EdgeEffect::EDGE_LEFT
]->Release(current_time
);
256 edge_effects_
[EdgeEffect::EDGE_RIGHT
]->Release(current_time
);
257 old_overscroll_
.set_x(0);
258 old_velocity_
.set_x(0);
261 edge_effects_
[EdgeEffect::EDGE_TOP
]->Release(current_time
);
262 edge_effects_
[EdgeEffect::EDGE_BOTTOM
]->Release(current_time
);
263 old_overscroll_
.set_y(0);
264 old_velocity_
.set_y(0);
269 EdgeEffect
* OverscrollGlow::GetOppositeEdge(int edge_index
) {
270 return edge_effects_
[(edge_index
+ 2) % EdgeEffect::EDGE_COUNT
].get();
273 } // namespace content