| 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_layer.h" |
| 10 #include "ui/base/android/system_ui_resource_manager.h" |
| 8 | 11 |
| 9 namespace content { | 12 namespace content { |
| 10 | 13 |
| 11 namespace { | 14 namespace { |
| 12 | 15 |
| 13 enum State { | 16 enum State { |
| 14 STATE_IDLE = 0, | 17 STATE_IDLE = 0, |
| 15 STATE_PULL, | 18 STATE_PULL, |
| 16 STATE_ABSORB, | 19 STATE_ABSORB, |
| 17 STATE_RECEDE, | 20 STATE_RECEDE, |
| (...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 134 DCHECK(layer); | 137 DCHECK(layer); |
| 135 layer->SetIsDrawable(true); | 138 layer->SetIsDrawable(true); |
| 136 gfx::Size bounds = ComputeBounds(edge, window_size, height); | 139 gfx::Size bounds = ComputeBounds(edge, window_size, height); |
| 137 layer->SetTransformOrigin( | 140 layer->SetTransformOrigin( |
| 138 gfx::Point3F(bounds.width() * 0.5f, bounds.height() * 0.5f, 0)); | 141 gfx::Point3F(bounds.width() * 0.5f, bounds.height() * 0.5f, 0)); |
| 139 layer->SetTransform(ComputeTransform(edge, window_size, offset, height)); | 142 layer->SetTransform(ComputeTransform(edge, window_size, offset, height)); |
| 140 layer->SetBounds(bounds); | 143 layer->SetBounds(bounds); |
| 141 layer->SetOpacity(Clamp(opacity, 0.f, 1.f)); | 144 layer->SetOpacity(Clamp(opacity, 0.f, 1.f)); |
| 142 } | 145 } |
| 143 | 146 |
| 144 } // namespace | 147 class EdgeEffectLayer : public ui::SystemUIResourceLayer, |
| 148 public EdgeEffect::LayerWrapper { |
| 149 public: |
| 150 static scoped_ptr<EdgeEffectLayer> Create( |
| 151 ui::SystemUIResourceManager::Type type, |
| 152 ui::SystemUIResourceManager* resource_manager) { |
| 153 return make_scoped_ptr(new EdgeEffectLayer(type, resource_manager)); |
| 154 } |
| 145 | 155 |
| 146 EdgeEffect::EdgeEffect(scoped_refptr<cc::Layer> edge, | 156 virtual ~EdgeEffectLayer() { |
| 147 scoped_refptr<cc::Layer> glow) | 157 resource_manager_->Unsubscribe(type_, this); |
| 148 : edge_(edge) | 158 ui_resource_layer_->RemoveFromParent(); |
| 149 , glow_(glow) | 159 } |
| 150 , edge_alpha_(0) | 160 |
| 151 , edge_scale_y_(0) | 161 // ui::SystemUIResourceLayer implementation. |
| 152 , glow_alpha_(0) | 162 virtual void SetUIResourceId(cc::UIResourceId id) OVERRIDE { |
| 153 , glow_scale_y_(0) | 163 ui_resource_id_ = id; |
| 154 , edge_alpha_start_(0) | 164 ui_resource_layer_->SetUIResourceId(id); |
| 155 , edge_alpha_finish_(0) | 165 } |
| 156 , edge_scale_y_start_(0) | 166 |
| 157 , edge_scale_y_finish_(0) | 167 // EdgeEffect::LayerWrapper implementation. |
| 158 , glow_alpha_start_(0) | 168 virtual cc::Layer* layer() OVERRIDE { return ui_resource_layer_.get(); } |
| 159 , glow_alpha_finish_(0) | 169 |
| 160 , glow_scale_y_start_(0) | 170 virtual void AddToLayer(cc::Layer* parent) OVERRIDE { |
| 161 , glow_scale_y_finish_(0) | 171 if (ui_resource_layer_->parent() != parent) |
| 162 , state_(STATE_IDLE) | 172 parent->AddChild(ui_resource_layer_); |
| 163 , pull_distance_(0) { | 173 ui_resource_layer_->SetUIResourceId(ui_resource_id_); |
| 174 } |
| 175 |
| 176 private: |
| 177 EdgeEffectLayer(ui::SystemUIResourceManager::Type type, |
| 178 ui::SystemUIResourceManager* resource_manager) |
| 179 : ui_resource_id_(0), |
| 180 ui_resource_layer_(cc::UIResourceLayer::Create()), |
| 181 type_(type), |
| 182 resource_manager_(resource_manager) { |
| 183 resource_manager_->Subscribe(type_, this); |
| 184 } |
| 185 |
| 186 cc::UIResourceId ui_resource_id_; |
| 187 scoped_refptr<cc::UIResourceLayer> ui_resource_layer_; |
| 188 ui::SystemUIResourceManager::Type type_; |
| 189 ui::SystemUIResourceManager* resource_manager_; |
| 190 |
| 191 DISALLOW_COPY_AND_ASSIGN(EdgeEffectLayer); |
| 192 }; |
| 193 |
| 194 } // namespace |
| 195 |
| 196 EdgeEffect::EdgeEffect(ui::SystemUIResourceManager* resource_manager) |
| 197 : edge_alpha_(0), |
| 198 edge_scale_y_(0), |
| 199 glow_alpha_(0), |
| 200 glow_scale_y_(0), |
| 201 edge_alpha_start_(0), |
| 202 edge_alpha_finish_(0), |
| 203 edge_scale_y_start_(0), |
| 204 edge_scale_y_finish_(0), |
| 205 glow_alpha_start_(0), |
| 206 glow_alpha_finish_(0), |
| 207 glow_scale_y_start_(0), |
| 208 glow_scale_y_finish_(0), |
| 209 state_(STATE_IDLE), |
| 210 pull_distance_(0) { |
| 211 edge_ = EdgeEffectLayer::Create(ui::SystemUIResourceManager::OVERSCROLL_EDGE, |
| 212 resource_manager); |
| 213 glow_ = EdgeEffectLayer::Create(ui::SystemUIResourceManager::OVERSCROLL_GLOW, |
| 214 resource_manager); |
| 164 // Prevent the provided layers from drawing until the effect is activated. | 215 // Prevent the provided layers from drawing until the effect is activated. |
| 165 DisableLayer(edge_.get()); | 216 DisableLayer(edge_->layer()); |
| 166 DisableLayer(glow_.get()); | 217 DisableLayer(glow_->layer()); |
| 167 } | 218 } |
| 168 | 219 |
| 169 EdgeEffect::~EdgeEffect() { } | 220 EdgeEffect::~EdgeEffect() { } |
| 170 | 221 |
| 171 bool EdgeEffect::IsFinished() const { | 222 bool EdgeEffect::IsFinished() const { |
| 172 return state_ == STATE_IDLE; | 223 return state_ == STATE_IDLE; |
| 173 } | 224 } |
| 174 | 225 |
| 175 void EdgeEffect::Finish() { | 226 void EdgeEffect::Finish() { |
| 176 DisableLayer(edge_.get()); | 227 DisableLayer(edge_->layer()); |
| 177 DisableLayer(glow_.get()); | 228 DisableLayer(glow_->layer()); |
| 178 pull_distance_ = 0; | 229 pull_distance_ = 0; |
| 179 state_ = STATE_IDLE; | 230 state_ = STATE_IDLE; |
| 180 } | 231 } |
| 181 | 232 |
| 182 void EdgeEffect::Pull(base::TimeTicks current_time, float delta_distance) { | 233 void EdgeEffect::Pull(base::TimeTicks current_time, float delta_distance) { |
| 183 if (state_ == STATE_PULL_DECAY && current_time - start_time_ < duration_) { | 234 if (state_ == STATE_PULL_DECAY && current_time - start_time_ < duration_) { |
| 184 return; | 235 return; |
| 185 } | 236 } |
| 186 if (state_ != STATE_PULL) { | 237 if (state_ != STATE_PULL) { |
| 187 glow_scale_y_ = kPullGlowBegin; | 238 glow_scale_y_ = kPullGlowBegin; |
| (...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 352 Edge edge, | 403 Edge edge, |
| 353 float edge_height, | 404 float edge_height, |
| 354 float glow_height, | 405 float glow_height, |
| 355 float offset) { | 406 float offset) { |
| 356 if (IsFinished()) | 407 if (IsFinished()) |
| 357 return; | 408 return; |
| 358 | 409 |
| 359 // An empty window size, while meaningless, is also relatively harmless, and | 410 // An empty window size, while meaningless, is also relatively harmless, and |
| 360 // will simply prevent any drawing of the layers. | 411 // will simply prevent any drawing of the layers. |
| 361 if (window_size.IsEmpty()) { | 412 if (window_size.IsEmpty()) { |
| 362 DisableLayer(edge_.get()); | 413 DisableLayer(edge_->layer()); |
| 363 DisableLayer(glow_.get()); | 414 DisableLayer(glow_->layer()); |
| 364 return; | 415 return; |
| 365 } | 416 } |
| 366 | 417 |
| 367 // Glow | 418 // Glow |
| 368 const int scaled_glow_height = static_cast<int>( | 419 const int scaled_glow_height = static_cast<int>( |
| 369 std::min(glow_height * glow_scale_y_ * kGlowHeightToWidthRatio * 0.6f, | 420 std::min(glow_height * glow_scale_y_ * kGlowHeightToWidthRatio * 0.6f, |
| 370 glow_height * kMaxGlowHeight) + 0.5f); | 421 glow_height * kMaxGlowHeight) + 0.5f); |
| 371 UpdateLayer( | 422 UpdateLayer(glow_->layer(), |
| 372 glow_.get(), edge, window_size, offset, scaled_glow_height, glow_alpha_); | 423 edge, |
| 424 window_size, |
| 425 offset, |
| 426 scaled_glow_height, |
| 427 glow_alpha_); |
| 373 | 428 |
| 374 // Edge | 429 // Edge |
| 375 const int scaled_edge_height = static_cast<int>(edge_height * edge_scale_y_); | 430 const int scaled_edge_height = static_cast<int>(edge_height * edge_scale_y_); |
| 376 UpdateLayer( | 431 UpdateLayer(edge_->layer(), |
| 377 edge_.get(), edge, window_size, offset, scaled_edge_height, edge_alpha_); | 432 edge, |
| 433 window_size, |
| 434 offset, |
| 435 scaled_edge_height, |
| 436 edge_alpha_); |
| 437 } |
| 438 |
| 439 void EdgeEffect::AddEffectsToLayer(cc::Layer* parent) { |
| 440 edge_->AddToLayer(parent); |
| 441 glow_->AddToLayer(parent); |
| 378 } | 442 } |
| 379 | 443 |
| 380 } // namespace content | 444 } // namespace content |
| OLD | NEW |