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 "base/threading/worker_pool.h"
10 #include "cc/layers/image_layer.h"
11 #include "content/browser/android/edge_effect.h"
12 #include "skia/ext/image_operations.h"
13 #include "ui/gfx/android/java_bitmap.h"
22 const float kEpsilon
= 1e-3f
;
24 SkBitmap
CreateSkBitmapFromAndroidResource(const char* name
, gfx::Size size
) {
25 base::android::ScopedJavaLocalRef
<jobject
> jobj
=
26 gfx::CreateJavaBitmapFromAndroidResource(name
, size
);
30 SkBitmap bitmap
= CreateSkBitmapFromJavaBitmap(gfx::JavaBitmap(jobj
.obj()));
34 return skia::ImageOperations::Resize(
35 bitmap
, skia::ImageOperations::RESIZE_BOX
, size
.width(), size
.height());
38 class OverscrollResources
{
40 OverscrollResources() {
41 TRACE_EVENT0("browser", "OverscrollResources::Create");
43 CreateSkBitmapFromAndroidResource("android:drawable/overscroll_edge",
46 CreateSkBitmapFromAndroidResource("android:drawable/overscroll_glow",
50 const SkBitmap
& edge_bitmap() { return edge_bitmap_
; }
51 const SkBitmap
& glow_bitmap() { return glow_bitmap_
; }
54 SkBitmap edge_bitmap_
;
55 SkBitmap glow_bitmap_
;
57 DISALLOW_COPY_AND_ASSIGN(OverscrollResources
);
60 // Leaky to allow access from a worker thread.
61 base::LazyInstance
<OverscrollResources
>::Leaky g_overscroll_resources
=
62 LAZY_INSTANCE_INITIALIZER
;
64 scoped_refptr
<cc::Layer
> CreateImageLayer(const SkBitmap
& bitmap
) {
65 scoped_refptr
<cc::ImageLayer
> layer
= cc::ImageLayer::Create();
66 layer
->SetBitmap(bitmap
);
70 bool IsApproxZero(float value
) {
71 return std::abs(value
) < kEpsilon
;
74 gfx::Vector2dF
ZeroSmallComponents(gfx::Vector2dF vector
) {
75 if (IsApproxZero(vector
.x()))
77 if (IsApproxZero(vector
.y()))
82 // Force loading of any necessary resources. This function is thread-safe.
83 void EnsureResources() {
84 g_overscroll_resources
.Get();
89 scoped_ptr
<OverscrollGlow
> OverscrollGlow::Create(bool enabled
) {
90 // Don't block the main thread with effect resource loading during creation.
91 // Effect instantiation is deferred until the effect overscrolls, in which
92 // case the main thread may block until the resource has loaded.
93 if (enabled
&& g_overscroll_resources
== NULL
)
94 base::WorkerPool::PostTask(FROM_HERE
, base::Bind(EnsureResources
), true);
96 return make_scoped_ptr(new OverscrollGlow(enabled
));
99 OverscrollGlow::OverscrollGlow(bool enabled
)
102 horizontal_overscroll_enabled_(true),
103 vertical_overscroll_enabled_(true) {}
105 OverscrollGlow::~OverscrollGlow() {
109 void OverscrollGlow::Enable() {
113 void OverscrollGlow::Disable() {
117 if (!enabled_
&& initialized_
) {
119 for (size_t i
= 0; i
< EdgeEffect::EDGE_COUNT
; ++i
)
120 edge_effects_
[i
]->Finish();
124 bool OverscrollGlow::OnOverscrolled(cc::Layer
* overscrolling_layer
,
125 base::TimeTicks current_time
,
126 gfx::Vector2dF overscroll
,
127 gfx::Vector2dF velocity
) {
128 DCHECK(overscrolling_layer
);
133 // The size of the glow determines the relative effect of the inputs; an
134 // empty-sized effect is effectively disabled.
138 if (!horizontal_overscroll_enabled_
) {
142 if (!vertical_overscroll_enabled_
) {
147 // Ignore sufficiently small values that won't meaningfuly affect animation.
148 overscroll
= ZeroSmallComponents(overscroll
);
149 velocity
= ZeroSmallComponents(velocity
);
151 if (overscroll
.IsZero()) {
153 Release(current_time
);
154 UpdateLayerAttachment(overscrolling_layer
);
156 return NeedsAnimate();
159 if (!InitializeIfNecessary())
162 if (!velocity
.IsZero()) {
163 // Release effects if scrolling has changed directions.
164 if (velocity
.x() * old_velocity_
.x() < 0)
165 ReleaseAxis(AXIS_X
, current_time
);
166 if (velocity
.y() * old_velocity_
.y() < 0)
167 ReleaseAxis(AXIS_Y
, current_time
);
169 Absorb(current_time
, velocity
, overscroll
, old_overscroll_
);
171 // Release effects when overscroll accumulation violates monotonicity.
172 if (overscroll
.x() * old_overscroll_
.x() < 0 ||
173 std::abs(overscroll
.x()) < std::abs(old_overscroll_
.x()))
174 ReleaseAxis(AXIS_X
, current_time
);
175 if (overscroll
.y() * old_overscroll_
.y() < 0 ||
176 std::abs(overscroll
.y()) < std::abs(old_overscroll_
.y()))
177 ReleaseAxis(AXIS_Y
, current_time
);
179 Pull(current_time
, overscroll
- old_overscroll_
);
182 old_velocity_
= velocity
;
183 old_overscroll_
= overscroll
;
185 UpdateLayerAttachment(overscrolling_layer
);
186 return NeedsAnimate();
189 bool OverscrollGlow::Animate(base::TimeTicks current_time
) {
190 if (!NeedsAnimate()) {
195 const gfx::SizeF sizes
[EdgeEffect::EDGE_COUNT
] = {
196 size_
, gfx::SizeF(size_
.height(), size_
.width()),
197 size_
, gfx::SizeF(size_
.height(), size_
.width())
200 for (size_t i
= 0; i
< EdgeEffect::EDGE_COUNT
; ++i
) {
201 if (edge_effects_
[i
]->Update(current_time
)) {
202 edge_effects_
[i
]->ApplyToLayers(sizes
[i
],
203 static_cast<EdgeEffect::Edge
>(i
));
207 if (!NeedsAnimate()) {
215 bool OverscrollGlow::NeedsAnimate() const {
216 if (!enabled_
|| !initialized_
)
218 for (size_t i
= 0; i
< EdgeEffect::EDGE_COUNT
; ++i
) {
219 if (!edge_effects_
[i
]->IsFinished())
225 void OverscrollGlow::UpdateLayerAttachment(cc::Layer
* parent
) {
230 if (!NeedsAnimate()) {
235 if (root_layer_
->parent() != parent
)
236 parent
->AddChild(root_layer_
);
239 void OverscrollGlow::Detach() {
241 root_layer_
->RemoveFromParent();
244 bool OverscrollGlow::InitializeIfNecessary() {
249 const SkBitmap
& edge
= g_overscroll_resources
.Get().edge_bitmap();
250 const SkBitmap
& glow
= g_overscroll_resources
.Get().glow_bitmap();
251 if (edge
.isNull() || glow
.isNull()) {
256 DCHECK(!root_layer_
);
257 root_layer_
= cc::Layer::Create();
258 for (size_t i
= 0; i
< EdgeEffect::EDGE_COUNT
; ++i
) {
259 scoped_refptr
<cc::Layer
> edge_layer
= CreateImageLayer(edge
);
260 scoped_refptr
<cc::Layer
> glow_layer
= CreateImageLayer(glow
);
261 root_layer_
->AddChild(edge_layer
);
262 root_layer_
->AddChild(glow_layer
);
263 edge_effects_
[i
] = make_scoped_ptr(new EdgeEffect(edge_layer
, glow_layer
));
270 void OverscrollGlow::Pull(base::TimeTicks current_time
,
271 gfx::Vector2dF overscroll_delta
) {
272 DCHECK(enabled_
&& initialized_
);
273 overscroll_delta
= ZeroSmallComponents(overscroll_delta
);
274 if (overscroll_delta
.IsZero())
277 gfx::Vector2dF overscroll_pull
= gfx::ScaleVector2d(overscroll_delta
,
279 1.f
/ size_
.height());
280 float edge_overscroll_pull
[EdgeEffect::EDGE_COUNT
] = {
281 min(overscroll_pull
.y(), 0.f
), // Top
282 min(overscroll_pull
.x(), 0.f
), // Left
283 max(overscroll_pull
.y(), 0.f
), // Bottom
284 max(overscroll_pull
.x(), 0.f
) // Right
287 for (size_t i
= 0; i
< EdgeEffect::EDGE_COUNT
; ++i
) {
288 if (!edge_overscroll_pull
[i
])
291 edge_effects_
[i
]->Pull(current_time
, std::abs(edge_overscroll_pull
[i
]));
292 GetOppositeEdge(i
)->Release(current_time
);
296 void OverscrollGlow::Absorb(base::TimeTicks current_time
,
297 gfx::Vector2dF velocity
,
298 gfx::Vector2dF overscroll
,
299 gfx::Vector2dF old_overscroll
) {
300 DCHECK(enabled_
&& initialized_
);
301 if (overscroll
.IsZero() || velocity
.IsZero())
304 // Only trigger on initial overscroll at a non-zero velocity
305 const float overscroll_velocities
[EdgeEffect::EDGE_COUNT
] = {
306 old_overscroll
.y() >= 0 && overscroll
.y() < 0 ? min(velocity
.y(), 0.f
) : 0,
307 old_overscroll
.x() >= 0 && overscroll
.x() < 0 ? min(velocity
.x(), 0.f
) : 0,
308 old_overscroll
.y() <= 0 && overscroll
.y() > 0 ? max(velocity
.y(), 0.f
) : 0,
309 old_overscroll
.x() <= 0 && overscroll
.x() > 0 ? max(velocity
.x(), 0.f
) : 0
312 for (size_t i
= 0; i
< EdgeEffect::EDGE_COUNT
; ++i
) {
313 if (!overscroll_velocities
[i
])
316 edge_effects_
[i
]->Absorb(current_time
, std::abs(overscroll_velocities
[i
]));
317 GetOppositeEdge(i
)->Release(current_time
);
321 void OverscrollGlow::Release(base::TimeTicks current_time
) {
322 DCHECK(initialized_
);
323 for (size_t i
= 0; i
< EdgeEffect::EDGE_COUNT
; ++i
) {
324 edge_effects_
[i
]->Release(current_time
);
326 old_overscroll_
= old_velocity_
= gfx::Vector2dF();
329 void OverscrollGlow::ReleaseAxis(Axis axis
, base::TimeTicks current_time
) {
330 DCHECK(initialized_
);
333 edge_effects_
[EdgeEffect::EDGE_LEFT
]->Release(current_time
);
334 edge_effects_
[EdgeEffect::EDGE_RIGHT
]->Release(current_time
);
335 old_overscroll_
.set_x(0);
336 old_velocity_
.set_x(0);
339 edge_effects_
[EdgeEffect::EDGE_TOP
]->Release(current_time
);
340 edge_effects_
[EdgeEffect::EDGE_BOTTOM
]->Release(current_time
);
341 old_overscroll_
.set_y(0);
342 old_velocity_
.set_y(0);
347 EdgeEffect
* OverscrollGlow::GetOppositeEdge(int edge_index
) {
348 DCHECK(initialized_
);
349 return edge_effects_
[(edge_index
+ 2) % EdgeEffect::EDGE_COUNT
].get();
352 } // namespace content