| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "ui/gfx/compositor/layer.h" | 5 #include "ui/gfx/compositor/layer.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "base/debug/trace_event.h" | 9 #include "base/debug/trace_event.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| (...skipping 14 matching lines...) Expand all Loading... |
| 25 | 25 |
| 26 namespace { | 26 namespace { |
| 27 | 27 |
| 28 const float EPSILON = 1e-3f; | 28 const float EPSILON = 1e-3f; |
| 29 | 29 |
| 30 bool IsApproximateMultilpleOf(float value, float base) { | 30 bool IsApproximateMultilpleOf(float value, float base) { |
| 31 float remainder = fmod(fabs(value), base); | 31 float remainder = fmod(fabs(value), base); |
| 32 return remainder < EPSILON || base - remainder < EPSILON; | 32 return remainder < EPSILON || base - remainder < EPSILON; |
| 33 } | 33 } |
| 34 | 34 |
| 35 const ui::Layer* GetRoot(const ui::Layer* layer) { |
| 36 return layer->parent() ? GetRoot(layer->parent()) : layer; |
| 37 } |
| 38 |
| 35 } // namespace | 39 } // namespace |
| 36 | 40 |
| 37 namespace ui { | 41 namespace ui { |
| 38 | 42 |
| 39 Layer::Layer(Compositor* compositor) | 43 Layer::Layer() |
| 40 : type_(LAYER_HAS_TEXTURE), | 44 : type_(LAYER_HAS_TEXTURE), |
| 41 compositor_(compositor), | 45 compositor_(NULL), |
| 42 parent_(NULL), | 46 parent_(NULL), |
| 43 visible_(true), | 47 visible_(true), |
| 44 fills_bounds_opaquely_(true), | 48 fills_bounds_opaquely_(true), |
| 45 recompute_hole_(false), | 49 recompute_hole_(false), |
| 46 layer_updated_externally_(false), | 50 layer_updated_externally_(false), |
| 47 opacity_(1.0f), | 51 opacity_(1.0f), |
| 48 delegate_(NULL) { | 52 delegate_(NULL) { |
| 49 #if defined(USE_WEBKIT_COMPOSITOR) | 53 #if defined(USE_WEBKIT_COMPOSITOR) |
| 50 CreateWebLayer(); | 54 CreateWebLayer(); |
| 51 #endif | 55 #endif |
| 52 } | 56 } |
| 53 | 57 |
| 54 Layer::Layer(Compositor* compositor, LayerType type) | 58 Layer::Layer(LayerType type) |
| 55 : type_(type), | 59 : type_(type), |
| 56 compositor_(compositor), | 60 compositor_(NULL), |
| 57 parent_(NULL), | 61 parent_(NULL), |
| 58 visible_(true), | 62 visible_(true), |
| 59 fills_bounds_opaquely_(true), | 63 fills_bounds_opaquely_(true), |
| 60 recompute_hole_(false), | 64 recompute_hole_(false), |
| 61 layer_updated_externally_(false), | 65 layer_updated_externally_(false), |
| 62 opacity_(1.0f), | 66 opacity_(1.0f), |
| 63 delegate_(NULL) { | 67 delegate_(NULL) { |
| 64 #if defined(USE_WEBKIT_COMPOSITOR) | 68 #if defined(USE_WEBKIT_COMPOSITOR) |
| 65 CreateWebLayer(); | 69 CreateWebLayer(); |
| 66 #endif | 70 #endif |
| 67 } | 71 } |
| 68 | 72 |
| 69 Layer::~Layer() { | 73 Layer::~Layer() { |
| 74 if (compositor_) |
| 75 compositor_->SetRootLayer(NULL); |
| 70 if (parent_) | 76 if (parent_) |
| 71 parent_->Remove(this); | 77 parent_->Remove(this); |
| 72 for (size_t i = 0; i < children_.size(); ++i) | 78 for (size_t i = 0; i < children_.size(); ++i) |
| 73 children_[i]->parent_ = NULL; | 79 children_[i]->parent_ = NULL; |
| 74 #if defined(USE_WEBKIT_COMPOSITOR) | 80 #if defined(USE_WEBKIT_COMPOSITOR) |
| 75 web_layer_.removeFromParent(); | 81 web_layer_.removeFromParent(); |
| 76 #endif | 82 #endif |
| 77 } | 83 } |
| 78 | 84 |
| 79 Compositor* Layer::GetCompositor() { | 85 Compositor* Layer::GetCompositor() { |
| 80 return compositor_ ? compositor_ : parent_ ? parent_->GetCompositor() : NULL; | 86 return GetRoot(this)->compositor_; |
| 81 } | 87 } |
| 82 | 88 |
| 83 void Layer::SetCompositor(Compositor* compositor) { | 89 void Layer::SetCompositor(Compositor* compositor) { |
| 84 // This function must only be called once, with a valid compositor, and only | 90 // This function must only be called to set the compositor on the root layer, |
| 85 // for the compositor's root layer. | 91 // or to reset it. |
| 86 DCHECK(!compositor_); | 92 DCHECK(!compositor || !compositor_); |
| 87 DCHECK(compositor); | 93 DCHECK(!compositor || compositor->root_layer() == this); |
| 88 DCHECK_EQ(compositor->root_layer(), this); | 94 DCHECK(!parent_); |
| 89 compositor_ = compositor; | 95 compositor_ = compositor; |
| 90 } | 96 } |
| 91 | 97 |
| 92 void Layer::Add(Layer* child) { | 98 void Layer::Add(Layer* child) { |
| 99 DCHECK(!child->compositor_); |
| 93 if (child->parent_) | 100 if (child->parent_) |
| 94 child->parent_->Remove(child); | 101 child->parent_->Remove(child); |
| 95 child->parent_ = this; | 102 child->parent_ = this; |
| 96 children_.push_back(child); | 103 children_.push_back(child); |
| 97 #if defined(USE_WEBKIT_COMPOSITOR) | 104 #if defined(USE_WEBKIT_COMPOSITOR) |
| 98 web_layer_.addChild(child->web_layer_); | 105 web_layer_.addChild(child->web_layer_); |
| 99 #endif | 106 #endif |
| 100 | 107 |
| 101 SetNeedsToRecomputeHole(); | 108 SetNeedsToRecomputeHole(); |
| 102 } | 109 } |
| (...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 228 !hole_rect_.Contains(gfx::Rect(gfx::Point(0, 0), bounds_.size())); | 235 !hole_rect_.Contains(gfx::Rect(gfx::Point(0, 0), bounds_.size())); |
| 229 } | 236 } |
| 230 | 237 |
| 231 // static | 238 // static |
| 232 void Layer::ConvertPointToLayer(const Layer* source, | 239 void Layer::ConvertPointToLayer(const Layer* source, |
| 233 const Layer* target, | 240 const Layer* target, |
| 234 gfx::Point* point) { | 241 gfx::Point* point) { |
| 235 if (source == target) | 242 if (source == target) |
| 236 return; | 243 return; |
| 237 | 244 |
| 238 const Layer* root_layer = source->compositor()->root_layer(); | 245 const Layer* root_layer = GetRoot(source); |
| 239 CHECK_EQ(root_layer, target->compositor()->root_layer()); | 246 CHECK_EQ(root_layer, GetRoot(target)); |
| 240 | 247 |
| 241 if (source != root_layer) | 248 if (source != root_layer) |
| 242 source->ConvertPointForAncestor(root_layer, point); | 249 source->ConvertPointForAncestor(root_layer, point); |
| 243 if (target != root_layer) | 250 if (target != root_layer) |
| 244 target->ConvertPointFromAncestor(root_layer, point); | 251 target->ConvertPointFromAncestor(root_layer, point); |
| 245 } | 252 } |
| 246 | 253 |
| 247 void Layer::SetFillsBoundsOpaquely(bool fills_bounds_opaquely) { | 254 void Layer::SetFillsBoundsOpaquely(bool fills_bounds_opaquely) { |
| 248 if (fills_bounds_opaquely_ == fills_bounds_opaquely) | 255 if (fills_bounds_opaquely_ == fills_bounds_opaquely) |
| 249 return; | 256 return; |
| (...skipping 474 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 724 0, | 731 0, |
| 725 0, | 732 0, |
| 726 static_cast<float>(bounds_.width())/texture_cc->size().width(), | 733 static_cast<float>(bounds_.width())/texture_cc->size().width(), |
| 727 static_cast<float>(bounds_.height())/texture_cc->size().height()); | 734 static_cast<float>(bounds_.height())/texture_cc->size().height()); |
| 728 texture_layer.setUVRect(rect); | 735 texture_layer.setUVRect(rect); |
| 729 } | 736 } |
| 730 } | 737 } |
| 731 #endif | 738 #endif |
| 732 | 739 |
| 733 } // namespace ui | 740 } // namespace ui |
| OLD | NEW |