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