Remove PlatformFile from profile_browsertest
[chromium-blink-merge.git] / content / browser / android / overscroll_glow.cc
blobf4a0168cd5568e4f183f6fb620e9a7cb8d3d9982
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"
15 using std::max;
16 using std::min;
18 namespace content {
20 namespace {
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);
27 if (jobj.is_null())
28 return SkBitmap();
30 SkBitmap bitmap = CreateSkBitmapFromJavaBitmap(gfx::JavaBitmap(jobj.obj()));
31 if (bitmap.isNull())
32 return bitmap;
34 return skia::ImageOperations::Resize(
35 bitmap, skia::ImageOperations::RESIZE_BOX, size.width(), size.height());
38 class OverscrollResources {
39 public:
40 OverscrollResources() {
41 TRACE_EVENT0("browser", "OverscrollResources::Create");
42 edge_bitmap_ =
43 CreateSkBitmapFromAndroidResource("android:drawable/overscroll_edge",
44 gfx::Size(128, 12));
45 glow_bitmap_ =
46 CreateSkBitmapFromAndroidResource("android:drawable/overscroll_glow",
47 gfx::Size(128, 64));
50 const SkBitmap& edge_bitmap() { return edge_bitmap_; }
51 const SkBitmap& glow_bitmap() { return glow_bitmap_; }
53 private:
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);
67 return layer;
70 bool IsApproxZero(float value) {
71 return std::abs(value) < kEpsilon;
74 gfx::Vector2dF ZeroSmallComponents(gfx::Vector2dF vector) {
75 if (IsApproxZero(vector.x()))
76 vector.set_x(0);
77 if (IsApproxZero(vector.y()))
78 vector.set_y(0);
79 return vector;
82 // Force loading of any necessary resources. This function is thread-safe.
83 void EnsureResources() {
84 g_overscroll_resources.Get();
87 } // namespace
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)
100 : enabled_(enabled),
101 initialized_(false),
102 horizontal_overscroll_enabled_(true),
103 vertical_overscroll_enabled_(true) {}
105 OverscrollGlow::~OverscrollGlow() {
106 Detach();
109 void OverscrollGlow::Enable() {
110 enabled_ = true;
113 void OverscrollGlow::Disable() {
114 if (!enabled_)
115 return;
116 enabled_ = false;
117 if (!enabled_ && initialized_) {
118 Detach();
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);
130 if (!enabled_)
131 return false;
133 // The size of the glow determines the relative effect of the inputs; an
134 // empty-sized effect is effectively disabled.
135 if (size_.IsEmpty())
136 return false;
138 if (!horizontal_overscroll_enabled_) {
139 overscroll.set_x(0);
140 velocity.set_x(0);
142 if (!vertical_overscroll_enabled_) {
143 overscroll.set_y(0);
144 velocity.set_y(0);
147 // Ignore sufficiently small values that won't meaningfuly affect animation.
148 overscroll = ZeroSmallComponents(overscroll);
149 velocity = ZeroSmallComponents(velocity);
151 if (overscroll.IsZero()) {
152 if (initialized_) {
153 Release(current_time);
154 UpdateLayerAttachment(overscrolling_layer);
156 return NeedsAnimate();
159 if (!InitializeIfNecessary())
160 return false;
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_);
170 } else {
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()) {
191 Detach();
192 return false;
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()) {
208 Detach();
209 return false;
212 return true;
215 bool OverscrollGlow::NeedsAnimate() const {
216 if (!enabled_ || !initialized_)
217 return false;
218 for (size_t i = 0; i < EdgeEffect::EDGE_COUNT; ++i) {
219 if (!edge_effects_[i]->IsFinished())
220 return true;
222 return false;
225 void OverscrollGlow::UpdateLayerAttachment(cc::Layer* parent) {
226 DCHECK(parent);
227 if (!root_layer_)
228 return;
230 if (!NeedsAnimate()) {
231 Detach();
232 return;
235 if (root_layer_->parent() != parent)
236 parent->AddChild(root_layer_);
239 void OverscrollGlow::Detach() {
240 if (root_layer_)
241 root_layer_->RemoveFromParent();
244 bool OverscrollGlow::InitializeIfNecessary() {
245 DCHECK(enabled_);
246 if (initialized_)
247 return true;
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()) {
252 Disable();
253 return false;
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));
266 initialized_ = true;
267 return true;
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())
275 return;
277 gfx::Vector2dF overscroll_pull = gfx::ScaleVector2d(overscroll_delta,
278 1.f / size_.width(),
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])
289 continue;
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())
302 return;
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])
314 continue;
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_);
331 switch (axis) {
332 case AXIS_X:
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);
337 break;
338 case AXIS_Y:
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);
343 break;
347 EdgeEffect* OverscrollGlow::GetOppositeEdge(int edge_index) {
348 DCHECK(initialized_);
349 return edge_effects_[(edge_index + 2) % EdgeEffect::EDGE_COUNT].get();
352 } // namespace content