Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(958)

Unified Diff: content/browser/android/edge_effect_l.cc

Issue 367173003: [Android] Implementation of overscroll effect for Android L (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix build Created 6 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « content/browser/android/edge_effect_l.h ('k') | content/browser/android/overscroll_glow.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: content/browser/android/edge_effect_l.cc
diff --git a/content/browser/android/edge_effect_l.cc b/content/browser/android/edge_effect_l.cc
new file mode 100644
index 0000000000000000000000000000000000000000..3d32378fce966f24849a71109f933495bd094422
--- /dev/null
+++ b/content/browser/android/edge_effect_l.cc
@@ -0,0 +1,341 @@
+// Copyright 2014 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "content/browser/android/edge_effect_l.h"
+
+#include "base/debug/trace_event.h"
+#include "base/lazy_instance.h"
+#include "cc/layers/image_layer.h"
+#include "third_party/skia/include/core/SkBitmap.h"
+#include "third_party/skia/include/core/SkCanvas.h"
+#include "third_party/skia/include/core/SkPaint.h"
+#include "third_party/skia/include/effects/SkPorterDuff.h"
+#include "ui/gfx/screen.h"
+
+namespace content {
+
+namespace {
+
+// Time it will take the effect to fully recede in ms
+const int kRecedeTime = 1000;
+
+// Time it will take before a pulled glow begins receding in ms
+const int kPullTime = 167;
+
+const float kMaxAlpha = 1.f;
+
+const float kPullGlowBegin = 0.f;
+
+// Min/max velocity that will be absorbed
+const float kMinVelocity = 100.f;
+const float kMaxVelocity = 10000.f;
+
+const float kEpsilon = 0.001f;
+
+const float kSin = 0.5f; // sin(PI / 6)
+const float kCos = 0.866f; // cos(PI / 6);
+
+// How much dragging should effect the height of the glow image.
+// Number determined by user testing.
+const float kPullDistanceAlphaGlowFactor = 1.1f;
+
+const int kVelocityGlowFactor = 12;
+
+template <typename T>
+T Lerp(T a, T b, T t) {
+ return a + (b - a) * t;
+}
+
+template <typename T>
+T Clamp(T value, T low, T high) {
+ return value < low ? low : (value > high ? high : value);
+}
+
+template <typename T>
+T Damp(T input, T factor) {
+ T result;
+ if (factor == 1) {
+ result = 1 - (1 - input) * (1 - input);
+ } else {
+ result = 1 - std::pow(1 - input, 2 * factor);
+ }
+ return result;
+}
+
+void DisableLayer(cc::Layer* layer) {
+ DCHECK(layer);
+ layer->SetIsDrawable(false);
+ layer->SetTransform(gfx::Transform());
+ layer->SetOpacity(1.f);
+}
+
+scoped_refptr<cc::Layer> CreateImageLayer(const SkBitmap& bitmap) {
+ scoped_refptr<cc::ImageLayer> layer = cc::ImageLayer::Create();
+ layer->SetBitmap(bitmap);
+ return layer;
+}
+
+class EdgeEffectLResources {
+ public:
+ EdgeEffectLResources() {
+ TRACE_EVENT0("browser", "EdgeEffectLResources::Create");
+ // TODO(jdduke): Fetch the theme color from the platform as follows:
+ /*final TypedArray a = context.obtainStyledAttributes(
+ com.android.internal.R.styleable.EdgeEffect);
+ final int themeColor = a.getColor(
+ com.android.internal.R.styleable.EdgeEffect_colorPrimary, 0xff666666);*/
+ const int kThemeColor = 0xff666666;
+
+ SkPaint paint;
+ paint.setAntiAlias(true);
+ paint.setColor((kThemeColor & 0xffffff) | 0x33000000);
+ paint.setStyle(SkPaint::kFill_Style);
+
+ gfx::Size screen_bounds =
+ gfx::Screen::GetNativeScreen()->GetPrimaryDisplay().GetSizeInPixel();
+ const float width = std::max(screen_bounds.width(), screen_bounds.height());
+ const float radius = width * 0.75f / kSin;
aelias_OOO_until_Jul13 2014/07/17 20:23:17 Looks like this ends up multiplying by 1.5, for ov
jdduke (slow) 2014/07/17 22:24:12 Hmm, the (raw sized) effect should be larger than
jdduke (slow) 2014/08/11 19:51:10 1.5 is right, but the variable name |radius| is mi
+ const float y = kCos * radius;
+ const float height = radius - y;
+ gfx::Size bounds(radius, height);
+ SkRect arc_rect = SkRect::MakeXYWH(
+ -radius / 2.f, -radius - y, radius * 2.f, radius * 2.f);
+ if (!glow_bitmap_.allocPixels(
+ SkImageInfo::MakeN32Premul(bounds.width(), bounds.height()))) {
+ LOG(FATAL) << " Failed to allocate bitmap of size " << bounds.width()
+ << "x" << bounds.height();
+ }
+
+ SkCanvas canvas(glow_bitmap_);
+ canvas.clipRect(SkRect::MakeXYWH(0, 0, bounds.width(), bounds.height()));
+ canvas.drawArc(arc_rect, 45, 90, true, paint);
+ }
+
+ const SkBitmap& glow_bitmap() const { return glow_bitmap_; }
+
+ private:
+ SkBitmap glow_bitmap_;
+
+ DISALLOW_COPY_AND_ASSIGN(EdgeEffectLResources);
+};
+
+// Leaky to allow access from a worker thread.
+base::LazyInstance<EdgeEffectLResources>::Leaky g_edge_effect_resources =
+ LAZY_INSTANCE_INITIALIZER;
+
+} // namespace
+
+// static
+scoped_ptr<EdgeEffectL> EdgeEffectL::Create(cc::Layer* root_layer) {
+ DCHECK(root_layer);
+
+ const SkBitmap& glow = g_edge_effect_resources.Get().glow_bitmap();
+ if (glow.isNull())
+ return scoped_ptr<EdgeEffectL>();
+
+ scoped_refptr<cc::Layer> glow_layer = CreateImageLayer(glow);
+ root_layer->AddChild(glow_layer);
+ return make_scoped_ptr(new EdgeEffectL(glow_layer));
+}
+
+EdgeEffectL::EdgeEffectL(const scoped_refptr<cc::Layer>& glow)
+ : glow_(glow)
+ , glow_alpha_(0)
+ , glow_scale_y_(0)
+ , glow_alpha_start_(0)
+ , glow_alpha_finish_(0)
+ , glow_scale_y_start_(0)
+ , glow_scale_y_finish_(0)
+ , displacement_(0.5f)
+ , target_displacement_(0.5f)
+ , state_(STATE_IDLE)
+ , pull_distance_(0) {
+ // Prevent the provided layers from drawing until the effect is activated.
+ DisableLayer(glow_.get());
+ glow_->SetBlendMode(SkXfermode::kSrcATop_Mode);
+}
+
+EdgeEffectL::~EdgeEffectL() { }
+
+bool EdgeEffectL::IsFinished() const {
+ return state_ == STATE_IDLE;
+}
+
+void EdgeEffectL::Finish() {
+ DisableLayer(glow_.get());
+ pull_distance_ = 0;
+ state_ = STATE_IDLE;
+}
+
+void EdgeEffectL::Pull(base::TimeTicks current_time, float delta_distance) {
+ Pull(current_time, delta_distance, 0.5f);
+}
+
+void EdgeEffectL::Pull(base::TimeTicks current_time,
+ float delta_distance,
+ float displacement) {
+ target_displacement_ = displacement;
+ if (state_ == STATE_PULL_DECAY && current_time - start_time_ < duration_) {
+ return;
+ }
+ if (state_ != STATE_PULL) {
+ glow_scale_y_ = std::max(kPullGlowBegin, glow_scale_y_);
+ }
+ state_ = STATE_PULL;
+
+ start_time_ = current_time;
+ duration_ = base::TimeDelta::FromMilliseconds(kPullTime);
+
+ float abs_delta_distance = std::abs(delta_distance);
+ pull_distance_ += delta_distance;
+
+ glow_alpha_ = glow_alpha_start_ = std::min(
+ kMaxAlpha,
+ glow_alpha_ + (abs_delta_distance * kPullDistanceAlphaGlowFactor));
+
+ if (pull_distance_ == 0) {
+ glow_scale_y_ = glow_scale_y_start_ = 0;
+ } else {
+ float scale = 1.f -
+ 1.f / std::sqrt(std::abs(pull_distance_) * bounds_.height()) -
+ 0.3f;
+ glow_scale_y_ = glow_scale_y_start_ = std::max(0.f, scale) / 0.7f;
+ }
+
+ glow_alpha_finish_ = glow_alpha_;
+ glow_scale_y_finish_ = glow_scale_y_;
+}
+
+void EdgeEffectL::Release(base::TimeTicks current_time) {
+ pull_distance_ = 0;
+
+ if (state_ != STATE_PULL && state_ != STATE_PULL_DECAY)
+ return;
+
+ state_ = STATE_RECEDE;
+ glow_alpha_start_ = glow_alpha_;
+ glow_scale_y_start_ = glow_scale_y_;
+
+ glow_alpha_finish_ = 0.f;
+ glow_scale_y_finish_ = 0.f;
+
+ start_time_ = current_time;
+ duration_ = base::TimeDelta::FromMilliseconds(kRecedeTime);
+}
+
+void EdgeEffectL::Absorb(base::TimeTicks current_time, float velocity) {
+ state_ = STATE_ABSORB;
+
+ velocity = Clamp(std::abs(velocity), kMinVelocity, kMaxVelocity);
+
+ start_time_ = current_time;
+ // This should never be less than 1 millisecond.
+ duration_ = base::TimeDelta::FromMilliseconds(0.15f + (velocity * 0.02f));
+
+ // The glow depends more on the velocity, and therefore starts out
+ // nearly invisible.
+ glow_alpha_start_ = 0.3f;
+ glow_scale_y_start_ = std::max(glow_scale_y_, 0.f);
+
+ // Growth for the size of the glow should be quadratic to properly
+ // respond
+ // to a user's scrolling speed. The faster the scrolling speed, the more
+ // intense the effect should be for both the size and the saturation.
+ glow_scale_y_finish_ = std::min(
+ 0.025f + (velocity * (velocity / 100) * 0.00015f) / 2.f, 1.f);
+ // Alpha should change for the glow as well as size.
+ glow_alpha_finish_ = Clamp(glow_alpha_start_,
+ velocity * kVelocityGlowFactor * .00001f,
+ kMaxAlpha);
+ target_displacement_ = 0.5;
+}
+
+bool EdgeEffectL::Update(base::TimeTicks current_time) {
+ if (IsFinished())
+ return false;
+
+ const double dt = (current_time - start_time_).InMilliseconds();
+ const double t = std::min(dt / duration_.InMilliseconds(), 1.);
+ const float interp = static_cast<float>(Damp(t, 1.));
+
+ glow_alpha_ = Lerp(glow_alpha_start_, glow_alpha_finish_, interp);
+ glow_scale_y_ = Lerp(glow_scale_y_start_, glow_scale_y_finish_, interp);
+ displacement_ = (displacement_ + target_displacement_) / 2.f;
+
+ if (t >= 1.f - kEpsilon) {
+ switch (state_) {
+ case STATE_ABSORB:
+ state_ = STATE_RECEDE;
+ start_time_ = current_time;
+ duration_ = base::TimeDelta::FromMilliseconds(kRecedeTime);
+
+ glow_alpha_start_ = glow_alpha_;
+ glow_scale_y_start_ = glow_scale_y_;
+
+ glow_alpha_finish_ = 0.f;
+ glow_scale_y_finish_ = 0.f;
+ break;
+ case STATE_PULL:
+ // Hold in this state until explicitly released.
+ break;
+ case STATE_PULL_DECAY:
+ state_ = STATE_RECEDE;
+ break;
+ case STATE_RECEDE:
+ Finish();
+ break;
+ default:
+ break;
+ }
+ }
+
+ bool one_last_frame = false;
+ if (state_ == STATE_RECEDE && glow_scale_y_ <= 0) {
+ Finish();
+ // TODO(jdduke): Check |one_last_frame_| behavior.
+ one_last_frame = true;
+ }
+
+ return !IsFinished() || one_last_frame;
+}
+
+void EdgeEffectL::ApplyToLayers(const gfx::SizeF& size,
+ const gfx::Transform& transform) {
+ if (IsFinished())
+ return;
+
+ // An empty window size, while meaningless, is also relatively harmless, and
+ // will simply prevent any drawing of the layers.
+ if (size.IsEmpty()) {
+ DisableLayer(glow_.get());
+ return;
+ }
+
+ const float r = size.width() * 0.75f / kSin;
+ const float y = kCos * r;
+ const float h = r - y;
+ bounds_ = gfx::Size(size.width(), (int) std::min(size.height(), h));
+ gfx::Size image_bounds(r, std::min(1.f, glow_scale_y_) * bounds_.height());
+
+ glow_->SetIsDrawable(true);
+ glow_->SetTransformOrigin(gfx::Point3F(bounds_.width() * 0.5f, 0, 0));
+ glow_->SetBounds(image_bounds);
+ glow_->SetOpacity(Clamp(glow_alpha_, 0.f, 1.f));
+
+ const float displacement = Clamp(displacement_, 0.f, 1.f) - 0.5f;
+ const float displacement_offset_x = bounds_.width() * displacement * 0.5f;
+ const float image_offset_x = (bounds_.width() - image_bounds.width()) * 0.5f;
+ gfx::Transform offset_transform;
+ offset_transform.Translate(image_offset_x - displacement_offset_x, 0);
+ offset_transform.ConcatTransform(transform);
+ glow_->SetTransform(offset_transform);
+}
+
+// static
+void EdgeEffectL::EnsureResources() {
+ g_edge_effect_resources.Get();
+}
+
+} // namespace content
+
« no previous file with comments | « content/browser/android/edge_effect_l.h ('k') | content/browser/android/overscroll_glow.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698