| 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" |
| 9 #include "ui/base/android/system_ui_resource_manager.h" |
| 8 | 10 |
| 9 namespace content { | 11 namespace content { |
| 10 | 12 |
| 11 namespace { | 13 namespace { |
| 12 | 14 |
| 13 enum State { | 15 enum State { |
| 14 STATE_IDLE = 0, | 16 STATE_IDLE = 0, |
| 15 STATE_PULL, | 17 STATE_PULL, |
| 16 STATE_ABSORB, | 18 STATE_ABSORB, |
| 17 STATE_RECEDE, | 19 STATE_RECEDE, |
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 111 return gfx::Size(window_size.width(), height); | 113 return gfx::Size(window_size.width(), height); |
| 112 case EdgeEffect::EDGE_LEFT: | 114 case EdgeEffect::EDGE_LEFT: |
| 113 case EdgeEffect::EDGE_RIGHT: | 115 case EdgeEffect::EDGE_RIGHT: |
| 114 return gfx::Size(window_size.height(), height); | 116 return gfx::Size(window_size.height(), height); |
| 115 default: | 117 default: |
| 116 NOTREACHED() << "Invalid edge: " << edge; | 118 NOTREACHED() << "Invalid edge: " << edge; |
| 117 return gfx::Size(); | 119 return gfx::Size(); |
| 118 }; | 120 }; |
| 119 } | 121 } |
| 120 | 122 |
| 121 void DisableLayer(cc::Layer* layer) { | 123 } // namespace |
| 122 DCHECK(layer); | |
| 123 layer->SetIsDrawable(false); | |
| 124 layer->SetTransform(gfx::Transform()); | |
| 125 layer->SetOpacity(1.f); | |
| 126 } | |
| 127 | 124 |
| 128 void UpdateLayer(cc::Layer* layer, | 125 class EdgeEffect::EffectLayer { |
| 129 EdgeEffect::Edge edge, | 126 public: |
| 130 const gfx::SizeF& window_size, | 127 EffectLayer(ui::SystemUIResourceManager::ResourceType resource_type, |
| 131 int offset, | 128 ui::SystemUIResourceManager* resource_manager) |
| 132 int height, | 129 : ui_resource_layer_(cc::UIResourceLayer::Create()), |
| 133 float opacity) { | 130 resource_type_(resource_type), |
| 134 DCHECK(layer); | 131 resource_manager_(resource_manager) {} |
| 135 layer->SetIsDrawable(true); | |
| 136 gfx::Size bounds = ComputeBounds(edge, window_size, height); | |
| 137 layer->SetTransformOrigin( | |
| 138 gfx::Point3F(bounds.width() * 0.5f, bounds.height() * 0.5f, 0)); | |
| 139 layer->SetTransform(ComputeTransform(edge, window_size, offset, height)); | |
| 140 layer->SetBounds(bounds); | |
| 141 layer->SetOpacity(Clamp(opacity, 0.f, 1.f)); | |
| 142 } | |
| 143 | 132 |
| 144 } // namespace | 133 ~EffectLayer() { ui_resource_layer_->RemoveFromParent(); } |
| 145 | 134 |
| 146 EdgeEffect::EdgeEffect(scoped_refptr<cc::Layer> edge, | 135 void SetParent(cc::Layer* parent) { |
| 147 scoped_refptr<cc::Layer> glow) | 136 if (ui_resource_layer_->parent() != parent) |
| 148 : edge_(edge) | 137 parent->AddChild(ui_resource_layer_); |
| 149 , glow_(glow) | 138 ui_resource_layer_->SetUIResourceId( |
| 150 , edge_alpha_(0) | 139 resource_manager_->GetUIResourceId(resource_type_)); |
| 151 , edge_scale_y_(0) | 140 } |
| 152 , glow_alpha_(0) | 141 |
| 153 , glow_scale_y_(0) | 142 void Disable() { |
| 154 , edge_alpha_start_(0) | 143 ui_resource_layer_->SetIsDrawable(false); |
| 155 , edge_alpha_finish_(0) | 144 ui_resource_layer_->SetTransform(gfx::Transform()); |
| 156 , edge_scale_y_start_(0) | 145 ui_resource_layer_->SetOpacity(1.f); |
| 157 , edge_scale_y_finish_(0) | 146 } |
| 158 , glow_alpha_start_(0) | 147 |
| 159 , glow_alpha_finish_(0) | 148 void Update(EdgeEffect::Edge edge, |
| 160 , glow_scale_y_start_(0) | 149 const gfx::SizeF& window_size, |
| 161 , glow_scale_y_finish_(0) | 150 int offset, |
| 162 , state_(STATE_IDLE) | 151 int height, |
| 163 , pull_distance_(0) { | 152 float opacity) { |
| 164 // Prevent the provided layers from drawing until the effect is activated. | 153 ui_resource_layer_->SetUIResourceId( |
| 165 DisableLayer(edge_.get()); | 154 resource_manager_->GetUIResourceId(resource_type_)); |
| 166 DisableLayer(glow_.get()); | 155 ui_resource_layer_->SetIsDrawable(true); |
| 156 gfx::Size bounds = ComputeBounds(edge, window_size, height); |
| 157 ui_resource_layer_->SetTransformOrigin( |
| 158 gfx::Point3F(bounds.width() * 0.5f, bounds.height() * 0.5f, 0)); |
| 159 ui_resource_layer_->SetTransform( |
| 160 ComputeTransform(edge, window_size, offset, height)); |
| 161 ui_resource_layer_->SetBounds(bounds); |
| 162 ui_resource_layer_->SetOpacity(Clamp(opacity, 0.f, 1.f)); |
| 163 } |
| 164 |
| 165 scoped_refptr<cc::UIResourceLayer> ui_resource_layer_; |
| 166 ui::SystemUIResourceManager::ResourceType resource_type_; |
| 167 ui::SystemUIResourceManager* resource_manager_; |
| 168 |
| 169 DISALLOW_COPY_AND_ASSIGN(EffectLayer); |
| 170 }; |
| 171 |
| 172 EdgeEffect::EdgeEffect(ui::SystemUIResourceManager* resource_manager) |
| 173 : edge_(new EffectLayer(ui::SystemUIResourceManager::OVERSCROLL_EDGE, |
| 174 resource_manager)), |
| 175 glow_(new EffectLayer(ui::SystemUIResourceManager::OVERSCROLL_GLOW, |
| 176 resource_manager)), |
| 177 edge_alpha_(0), |
| 178 edge_scale_y_(0), |
| 179 glow_alpha_(0), |
| 180 glow_scale_y_(0), |
| 181 edge_alpha_start_(0), |
| 182 edge_alpha_finish_(0), |
| 183 edge_scale_y_start_(0), |
| 184 edge_scale_y_finish_(0), |
| 185 glow_alpha_start_(0), |
| 186 glow_alpha_finish_(0), |
| 187 glow_scale_y_start_(0), |
| 188 glow_scale_y_finish_(0), |
| 189 state_(STATE_IDLE), |
| 190 pull_distance_(0) { |
| 167 } | 191 } |
| 168 | 192 |
| 169 EdgeEffect::~EdgeEffect() { } | 193 EdgeEffect::~EdgeEffect() { } |
| 170 | 194 |
| 171 bool EdgeEffect::IsFinished() const { | 195 bool EdgeEffect::IsFinished() const { |
| 172 return state_ == STATE_IDLE; | 196 return state_ == STATE_IDLE; |
| 173 } | 197 } |
| 174 | 198 |
| 175 void EdgeEffect::Finish() { | 199 void EdgeEffect::Finish() { |
| 176 DisableLayer(edge_.get()); | 200 edge_->Disable(); |
| 177 DisableLayer(glow_.get()); | 201 glow_->Disable(); |
| 178 pull_distance_ = 0; | 202 pull_distance_ = 0; |
| 179 state_ = STATE_IDLE; | 203 state_ = STATE_IDLE; |
| 180 } | 204 } |
| 181 | 205 |
| 182 void EdgeEffect::Pull(base::TimeTicks current_time, float delta_distance) { | 206 void EdgeEffect::Pull(base::TimeTicks current_time, float delta_distance) { |
| 183 if (state_ == STATE_PULL_DECAY && current_time - start_time_ < duration_) { | 207 if (state_ == STATE_PULL_DECAY && current_time - start_time_ < duration_) { |
| 184 return; | 208 return; |
| 185 } | 209 } |
| 186 if (state_ != STATE_PULL) { | 210 if (state_ != STATE_PULL) { |
| 187 glow_scale_y_ = kPullGlowBegin; | 211 glow_scale_y_ = kPullGlowBegin; |
| (...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 352 Edge edge, | 376 Edge edge, |
| 353 float edge_height, | 377 float edge_height, |
| 354 float glow_height, | 378 float glow_height, |
| 355 float offset) { | 379 float offset) { |
| 356 if (IsFinished()) | 380 if (IsFinished()) |
| 357 return; | 381 return; |
| 358 | 382 |
| 359 // An empty window size, while meaningless, is also relatively harmless, and | 383 // An empty window size, while meaningless, is also relatively harmless, and |
| 360 // will simply prevent any drawing of the layers. | 384 // will simply prevent any drawing of the layers. |
| 361 if (window_size.IsEmpty()) { | 385 if (window_size.IsEmpty()) { |
| 362 DisableLayer(edge_.get()); | 386 edge_->Disable(); |
| 363 DisableLayer(glow_.get()); | 387 glow_->Disable(); |
| 364 return; | 388 return; |
| 365 } | 389 } |
| 366 | 390 |
| 367 // Glow | 391 // Glow |
| 368 const int scaled_glow_height = static_cast<int>( | 392 const int scaled_glow_height = static_cast<int>( |
| 369 std::min(glow_height * glow_scale_y_ * kGlowHeightToWidthRatio * 0.6f, | 393 std::min(glow_height * glow_scale_y_ * kGlowHeightToWidthRatio * 0.6f, |
| 370 glow_height * kMaxGlowHeight) + 0.5f); | 394 glow_height * kMaxGlowHeight) + 0.5f); |
| 371 UpdateLayer( | 395 glow_->Update(edge, window_size, offset, scaled_glow_height, glow_alpha_); |
| 372 glow_.get(), edge, window_size, offset, scaled_glow_height, glow_alpha_); | |
| 373 | 396 |
| 374 // Edge | 397 // Edge |
| 375 const int scaled_edge_height = static_cast<int>(edge_height * edge_scale_y_); | 398 const int scaled_edge_height = static_cast<int>(edge_height * edge_scale_y_); |
| 376 UpdateLayer( | 399 edge_->Update(edge, window_size, offset, scaled_edge_height, edge_alpha_); |
| 377 edge_.get(), edge, window_size, offset, scaled_edge_height, edge_alpha_); | 400 } |
| 401 |
| 402 void EdgeEffect::SetParent(cc::Layer* parent) { |
| 403 edge_->SetParent(parent); |
| 404 glow_->SetParent(parent); |
| 378 } | 405 } |
| 379 | 406 |
| 380 } // namespace content | 407 } // namespace content |
| OLD | NEW |