| OLD | NEW |
| (Empty) |
| 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. | |
| 4 | |
| 5 #include "content/browser/android/edge_effect.h" | |
| 6 | |
| 7 #include "cc/layers/layer.h" | |
| 8 #include "cc/layers/ui_resource_layer.h" | |
| 9 #include "content/browser/android/animation_utils.h" | |
| 10 #include "content/public/browser/android/compositor.h" | |
| 11 #include "ui/android/resources/resource_manager.h" | |
| 12 #include "ui/android/resources/system_ui_resource_type.h" | |
| 13 | |
| 14 namespace content { | |
| 15 | |
| 16 namespace { | |
| 17 | |
| 18 const ui::SystemUIResourceType kEdgeResourceId = ui::OVERSCROLL_EDGE; | |
| 19 const ui::SystemUIResourceType kGlowResourceId = ui::OVERSCROLL_GLOW; | |
| 20 | |
| 21 // Time it will take the effect to fully recede in ms | |
| 22 const int kRecedeTimeMs = 1000; | |
| 23 | |
| 24 // Time it will take before a pulled glow begins receding in ms | |
| 25 const int kPullTimeMs = 167; | |
| 26 | |
| 27 // Time it will take in ms for a pulled glow to decay before release | |
| 28 const int kPullDecayTimeMs = 1000; | |
| 29 | |
| 30 const float kMaxAlpha = 1.f; | |
| 31 const float kHeldEdgeScaleY = .5f; | |
| 32 | |
| 33 const float kMaxGlowHeight = 4.f; | |
| 34 | |
| 35 const float kPullGlowBegin = 1.f; | |
| 36 const float kPullEdgeBegin = 0.6f; | |
| 37 | |
| 38 // Min/max velocity that will be absorbed | |
| 39 const float kMinVelocity = 100.f; | |
| 40 const float kMaxVelocity = 10000.f; | |
| 41 | |
| 42 const float kEpsilon = 0.001f; | |
| 43 | |
| 44 const float kGlowHeightWidthRatio = 0.25f; | |
| 45 | |
| 46 // How much dragging should effect the height of the edge image. | |
| 47 // Number determined by user testing. | |
| 48 const int kPullDistanceEdgeFactor = 7; | |
| 49 | |
| 50 // How much dragging should effect the height of the glow image. | |
| 51 // Number determined by user testing. | |
| 52 const int kPullDistanceGlowFactor = 7; | |
| 53 const float kPullDistanceAlphaGlowFactor = 1.1f; | |
| 54 | |
| 55 const int kVelocityEdgeFactor = 8; | |
| 56 const int kVelocityGlowFactor = 12; | |
| 57 | |
| 58 const float kEdgeHeightAtMdpi = 12.f; | |
| 59 const float kGlowHeightAtMdpi = 128.f; | |
| 60 | |
| 61 } // namespace | |
| 62 | |
| 63 class EdgeEffect::EffectLayer { | |
| 64 public: | |
| 65 EffectLayer(ui::SystemUIResourceType resource_type, | |
| 66 ui::ResourceManager* resource_manager) | |
| 67 : ui_resource_layer_( | |
| 68 cc::UIResourceLayer::Create(Compositor::LayerSettings())), | |
| 69 resource_type_(resource_type), | |
| 70 resource_manager_(resource_manager) {} | |
| 71 | |
| 72 ~EffectLayer() { ui_resource_layer_->RemoveFromParent(); } | |
| 73 | |
| 74 void SetParent(cc::Layer* parent) { | |
| 75 if (ui_resource_layer_->parent() != parent) | |
| 76 parent->AddChild(ui_resource_layer_); | |
| 77 } | |
| 78 | |
| 79 void Disable() { ui_resource_layer_->SetIsDrawable(false); } | |
| 80 | |
| 81 void Update(const gfx::Size& size, | |
| 82 const gfx::Transform& transform, | |
| 83 float opacity) { | |
| 84 ui_resource_layer_->SetUIResourceId(resource_manager_->GetUIResourceId( | |
| 85 ui::ANDROID_RESOURCE_TYPE_SYSTEM, resource_type_)); | |
| 86 ui_resource_layer_->SetIsDrawable(true); | |
| 87 ui_resource_layer_->SetTransformOrigin( | |
| 88 gfx::Point3F(size.width() * 0.5f, 0, 0)); | |
| 89 ui_resource_layer_->SetTransform(transform); | |
| 90 ui_resource_layer_->SetBounds(size); | |
| 91 ui_resource_layer_->SetOpacity(Clamp(opacity, 0.f, 1.f)); | |
| 92 } | |
| 93 | |
| 94 scoped_refptr<cc::UIResourceLayer> ui_resource_layer_; | |
| 95 ui::SystemUIResourceType resource_type_; | |
| 96 ui::ResourceManager* resource_manager_; | |
| 97 | |
| 98 DISALLOW_COPY_AND_ASSIGN(EffectLayer); | |
| 99 }; | |
| 100 | |
| 101 EdgeEffect::EdgeEffect(ui::ResourceManager* resource_manager, | |
| 102 float device_scale_factor) | |
| 103 : edge_(new EffectLayer(kEdgeResourceId, resource_manager)), | |
| 104 glow_(new EffectLayer(kGlowResourceId, resource_manager)), | |
| 105 base_edge_height_(kEdgeHeightAtMdpi * device_scale_factor), | |
| 106 base_glow_height_(kGlowHeightAtMdpi * device_scale_factor), | |
| 107 edge_alpha_(0), | |
| 108 edge_scale_y_(0), | |
| 109 glow_alpha_(0), | |
| 110 glow_scale_y_(0), | |
| 111 edge_alpha_start_(0), | |
| 112 edge_alpha_finish_(0), | |
| 113 edge_scale_y_start_(0), | |
| 114 edge_scale_y_finish_(0), | |
| 115 glow_alpha_start_(0), | |
| 116 glow_alpha_finish_(0), | |
| 117 glow_scale_y_start_(0), | |
| 118 glow_scale_y_finish_(0), | |
| 119 state_(STATE_IDLE), | |
| 120 pull_distance_(0) { | |
| 121 } | |
| 122 | |
| 123 EdgeEffect::~EdgeEffect() { | |
| 124 } | |
| 125 | |
| 126 bool EdgeEffect::IsFinished() const { | |
| 127 return state_ == STATE_IDLE; | |
| 128 } | |
| 129 | |
| 130 void EdgeEffect::Finish() { | |
| 131 edge_->Disable(); | |
| 132 glow_->Disable(); | |
| 133 pull_distance_ = 0; | |
| 134 state_ = STATE_IDLE; | |
| 135 } | |
| 136 | |
| 137 void EdgeEffect::Pull(base::TimeTicks current_time, | |
| 138 float delta_distance, | |
| 139 float displacement) { | |
| 140 if (state_ == STATE_PULL_DECAY && current_time - start_time_ < duration_) { | |
| 141 return; | |
| 142 } | |
| 143 if (state_ != STATE_PULL) { | |
| 144 glow_scale_y_ = kPullGlowBegin; | |
| 145 } | |
| 146 state_ = STATE_PULL; | |
| 147 | |
| 148 start_time_ = current_time; | |
| 149 duration_ = base::TimeDelta::FromMilliseconds(kPullTimeMs); | |
| 150 | |
| 151 float abs_delta_distance = std::abs(delta_distance); | |
| 152 pull_distance_ += delta_distance; | |
| 153 float distance = std::abs(pull_distance_); | |
| 154 | |
| 155 edge_alpha_ = edge_alpha_start_ = Clamp(distance, kPullEdgeBegin, kMaxAlpha); | |
| 156 edge_scale_y_ = edge_scale_y_start_ = | |
| 157 Clamp(distance * kPullDistanceEdgeFactor, kHeldEdgeScaleY, 1.f); | |
| 158 | |
| 159 glow_alpha_ = glow_alpha_start_ = | |
| 160 std::min(kMaxAlpha, | |
| 161 glow_alpha_ + abs_delta_distance * kPullDistanceAlphaGlowFactor); | |
| 162 | |
| 163 float glow_change = abs_delta_distance; | |
| 164 if (delta_distance > 0 && pull_distance_ < 0) | |
| 165 glow_change = -glow_change; | |
| 166 if (pull_distance_ == 0) | |
| 167 glow_scale_y_ = 0; | |
| 168 | |
| 169 // Do not allow glow to get larger than kMaxGlowHeight. | |
| 170 glow_scale_y_ = glow_scale_y_start_ = | |
| 171 Clamp(glow_scale_y_ + glow_change * kPullDistanceGlowFactor, | |
| 172 0.f, | |
| 173 kMaxGlowHeight); | |
| 174 | |
| 175 edge_alpha_finish_ = edge_alpha_; | |
| 176 edge_scale_y_finish_ = edge_scale_y_; | |
| 177 glow_alpha_finish_ = glow_alpha_; | |
| 178 glow_scale_y_finish_ = glow_scale_y_; | |
| 179 } | |
| 180 | |
| 181 void EdgeEffect::Release(base::TimeTicks current_time) { | |
| 182 pull_distance_ = 0; | |
| 183 | |
| 184 if (state_ != STATE_PULL && state_ != STATE_PULL_DECAY) | |
| 185 return; | |
| 186 | |
| 187 state_ = STATE_RECEDE; | |
| 188 edge_alpha_start_ = edge_alpha_; | |
| 189 edge_scale_y_start_ = edge_scale_y_; | |
| 190 glow_alpha_start_ = glow_alpha_; | |
| 191 glow_scale_y_start_ = glow_scale_y_; | |
| 192 | |
| 193 edge_alpha_finish_ = 0.f; | |
| 194 edge_scale_y_finish_ = 0.f; | |
| 195 glow_alpha_finish_ = 0.f; | |
| 196 glow_scale_y_finish_ = 0.f; | |
| 197 | |
| 198 start_time_ = current_time; | |
| 199 duration_ = base::TimeDelta::FromMilliseconds(kRecedeTimeMs); | |
| 200 } | |
| 201 | |
| 202 void EdgeEffect::Absorb(base::TimeTicks current_time, float velocity) { | |
| 203 state_ = STATE_ABSORB; | |
| 204 velocity = Clamp(std::abs(velocity), kMinVelocity, kMaxVelocity); | |
| 205 | |
| 206 start_time_ = current_time; | |
| 207 // This should never be less than 1 millisecond. | |
| 208 duration_ = base::TimeDelta::FromMilliseconds(0.15f + (velocity * 0.02f)); | |
| 209 | |
| 210 // The edge should always be at least partially visible, regardless | |
| 211 // of velocity. | |
| 212 edge_alpha_start_ = 0.f; | |
| 213 edge_scale_y_ = edge_scale_y_start_ = 0.f; | |
| 214 // The glow depends more on the velocity, and therefore starts out | |
| 215 // nearly invisible. | |
| 216 glow_alpha_start_ = 0.3f; | |
| 217 glow_scale_y_start_ = 0.f; | |
| 218 | |
| 219 // Factor the velocity by 8. Testing on device shows this works best to | |
| 220 // reflect the strength of the user's scrolling. | |
| 221 edge_alpha_finish_ = Clamp(velocity * kVelocityEdgeFactor, 0.f, 1.f); | |
| 222 // Edge should never get larger than the size of its asset. | |
| 223 edge_scale_y_finish_ = | |
| 224 Clamp(velocity * kVelocityEdgeFactor, kHeldEdgeScaleY, 1.f); | |
| 225 | |
| 226 // Growth for the size of the glow should be quadratic to properly | |
| 227 // respond | |
| 228 // to a user's scrolling speed. The faster the scrolling speed, the more | |
| 229 // intense the effect should be for both the size and the saturation. | |
| 230 glow_scale_y_finish_ = | |
| 231 std::min(0.025f + (velocity * (velocity / 100) * 0.00015f), 1.75f); | |
| 232 // Alpha should change for the glow as well as size. | |
| 233 glow_alpha_finish_ = Clamp( | |
| 234 glow_alpha_start_, velocity * kVelocityGlowFactor * .00001f, kMaxAlpha); | |
| 235 } | |
| 236 | |
| 237 bool EdgeEffect::Update(base::TimeTicks current_time) { | |
| 238 if (IsFinished()) | |
| 239 return false; | |
| 240 | |
| 241 const double dt = (current_time - start_time_).InMilliseconds(); | |
| 242 const double t = std::min(dt / duration_.InMilliseconds(), 1.); | |
| 243 const float interp = static_cast<float>(Damp(t, 1.)); | |
| 244 | |
| 245 edge_alpha_ = Lerp(edge_alpha_start_, edge_alpha_finish_, interp); | |
| 246 edge_scale_y_ = Lerp(edge_scale_y_start_, edge_scale_y_finish_, interp); | |
| 247 glow_alpha_ = Lerp(glow_alpha_start_, glow_alpha_finish_, interp); | |
| 248 glow_scale_y_ = Lerp(glow_scale_y_start_, glow_scale_y_finish_, interp); | |
| 249 | |
| 250 if (t >= 1.f - kEpsilon) { | |
| 251 switch (state_) { | |
| 252 case STATE_ABSORB: | |
| 253 state_ = STATE_RECEDE; | |
| 254 start_time_ = current_time; | |
| 255 duration_ = base::TimeDelta::FromMilliseconds(kRecedeTimeMs); | |
| 256 | |
| 257 edge_alpha_start_ = edge_alpha_; | |
| 258 edge_scale_y_start_ = edge_scale_y_; | |
| 259 glow_alpha_start_ = glow_alpha_; | |
| 260 glow_scale_y_start_ = glow_scale_y_; | |
| 261 | |
| 262 // After absorb, the glow and edge should fade to nothing. | |
| 263 edge_alpha_finish_ = 0.f; | |
| 264 edge_scale_y_finish_ = 0.f; | |
| 265 glow_alpha_finish_ = 0.f; | |
| 266 glow_scale_y_finish_ = 0.f; | |
| 267 break; | |
| 268 case STATE_PULL: | |
| 269 state_ = STATE_PULL_DECAY; | |
| 270 start_time_ = current_time; | |
| 271 duration_ = base::TimeDelta::FromMilliseconds(kPullDecayTimeMs); | |
| 272 | |
| 273 edge_alpha_start_ = edge_alpha_; | |
| 274 edge_scale_y_start_ = edge_scale_y_; | |
| 275 glow_alpha_start_ = glow_alpha_; | |
| 276 glow_scale_y_start_ = glow_scale_y_; | |
| 277 | |
| 278 // After pull, the glow and edge should fade to nothing. | |
| 279 edge_alpha_finish_ = 0.f; | |
| 280 edge_scale_y_finish_ = 0.f; | |
| 281 glow_alpha_finish_ = 0.f; | |
| 282 glow_scale_y_finish_ = 0.f; | |
| 283 break; | |
| 284 case STATE_PULL_DECAY: { | |
| 285 // When receding, we want edge to decrease more slowly | |
| 286 // than the glow. | |
| 287 const float factor = | |
| 288 glow_scale_y_finish_ | |
| 289 ? 1 / (glow_scale_y_finish_ * glow_scale_y_finish_) | |
| 290 : std::numeric_limits<float>::max(); | |
| 291 edge_scale_y_ = | |
| 292 edge_scale_y_start_ + | |
| 293 (edge_scale_y_finish_ - edge_scale_y_start_) * interp * factor; | |
| 294 state_ = STATE_RECEDE; | |
| 295 } break; | |
| 296 case STATE_RECEDE: | |
| 297 Finish(); | |
| 298 break; | |
| 299 default: | |
| 300 break; | |
| 301 } | |
| 302 } | |
| 303 | |
| 304 if (state_ == STATE_RECEDE && glow_scale_y_ <= 0 && edge_scale_y_ <= 0) | |
| 305 Finish(); | |
| 306 | |
| 307 return !IsFinished(); | |
| 308 } | |
| 309 | |
| 310 float EdgeEffect::GetAlpha() const { | |
| 311 return IsFinished() ? 0.f : std::max(glow_alpha_, edge_alpha_); | |
| 312 } | |
| 313 | |
| 314 void EdgeEffect::ApplyToLayers(Edge edge, | |
| 315 const gfx::SizeF& viewport_size, | |
| 316 float offset) { | |
| 317 if (IsFinished()) | |
| 318 return; | |
| 319 | |
| 320 // An empty window size, while meaningless, is also relatively harmless, and | |
| 321 // will simply prevent any drawing of the layers. | |
| 322 if (viewport_size.IsEmpty()) { | |
| 323 edge_->Disable(); | |
| 324 glow_->Disable(); | |
| 325 return; | |
| 326 } | |
| 327 | |
| 328 gfx::SizeF size = ComputeOrientedSize(edge, viewport_size); | |
| 329 gfx::Transform transform = ComputeTransform(edge, viewport_size, offset); | |
| 330 | |
| 331 // Glow | |
| 332 const int scaled_glow_height = static_cast<int>( | |
| 333 std::min(base_glow_height_ * glow_scale_y_ * kGlowHeightWidthRatio * 0.6f, | |
| 334 base_glow_height_ * kMaxGlowHeight) + | |
| 335 0.5f); | |
| 336 const gfx::Size glow_size(size.width(), scaled_glow_height); | |
| 337 glow_->Update(glow_size, transform, glow_alpha_); | |
| 338 | |
| 339 // Edge | |
| 340 const int scaled_edge_height = | |
| 341 static_cast<int>(base_edge_height_ * edge_scale_y_); | |
| 342 const gfx::Size edge_size(size.width(), scaled_edge_height); | |
| 343 edge_->Update(edge_size, transform, edge_alpha_); | |
| 344 } | |
| 345 | |
| 346 void EdgeEffect::SetParent(cc::Layer* parent) { | |
| 347 edge_->SetParent(parent); | |
| 348 glow_->SetParent(parent); | |
| 349 } | |
| 350 | |
| 351 // static | |
| 352 void EdgeEffect::PreloadResources(ui::ResourceManager* resource_manager) { | |
| 353 DCHECK(resource_manager); | |
| 354 resource_manager->PreloadResource(ui::ANDROID_RESOURCE_TYPE_SYSTEM, | |
| 355 kEdgeResourceId); | |
| 356 resource_manager->PreloadResource(ui::ANDROID_RESOURCE_TYPE_SYSTEM, | |
| 357 kGlowResourceId); | |
| 358 } | |
| 359 | |
| 360 } // namespace content | |
| OLD | NEW |