Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(112)

Side by Side Diff: ui/gfx/compositor/layer.cc

Issue 7972023: Implicit animations through Layer. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: SetAnimator Created 9 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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/logging.h" 9 #include "base/logging.h"
10 #include "base/memory/scoped_ptr.h" 10 #include "base/memory/scoped_ptr.h"
11 #include "ui/base/animation/animation.h"
12 #include "ui/gfx/compositor/layer_animator.h"
11 #include "ui/gfx/canvas_skia.h" 13 #include "ui/gfx/canvas_skia.h"
12 #include "ui/gfx/point3.h" 14 #include "ui/gfx/point3.h"
13 15
14 namespace ui { 16 namespace ui {
15 17
16 Layer::Layer(Compositor* compositor) 18 Layer::Layer(Compositor* compositor)
17 : compositor_(compositor), 19 : compositor_(compositor),
18 parent_(NULL), 20 parent_(NULL),
19 visible_(true), 21 visible_(true),
20 can_have_texture_(true), 22 can_have_texture_(true),
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
64 } 66 }
65 67
66 bool Layer::Contains(const Layer* other) const { 68 bool Layer::Contains(const Layer* other) const {
67 for (const Layer* parent = other; parent; parent = parent->parent()) { 69 for (const Layer* parent = other; parent; parent = parent->parent()) {
68 if (parent == this) 70 if (parent == this)
69 return true; 71 return true;
70 } 72 }
71 return false; 73 return false;
72 } 74 }
73 75
76 void Layer::SetAnimation(Animation* animation) {
77 if (animation) {
78 if (!animator_.get())
79 animator_.reset(new LayerAnimator(this));
80 animation->Start();
81 animator_->SetAnimation(animation);
82 } else {
83 animator_.reset();
84 }
85 }
86
74 void Layer::SetTransform(const ui::Transform& transform) { 87 void Layer::SetTransform(const ui::Transform& transform) {
75 transform_ = transform; 88 if (animator_.get() && animator_->IsRunning()) {
76 89 animator_->AnimateTransform(transform);
77 if (parent() && fills_bounds_opaquely_) 90 return;
78 parent()->RecomputeHole(); 91 }
92 SetTransformImmediately(transform);
79 } 93 }
80 94
81 void Layer::SetBounds(const gfx::Rect& bounds) { 95 void Layer::SetBounds(const gfx::Rect& bounds) {
82 bounds_ = bounds; 96 if (animator_.get() && animator_->IsRunning()) {
97 if (bounds.size() == bounds_.size()) {
98 animator_->AnimateToPoint(bounds.origin());
99 return;
100 }
101 animator_->StopAnimatingToPoint();
102 }
103 SetBoundsImmediately(bounds);
104 }
83 105
84 if (parent() && fills_bounds_opaquely_) 106 void Layer::SetOpacity(float opacity) {
85 parent()->RecomputeHole(); 107 if (animator_.get() && animator_->IsRunning()) {
108 animator_->AnimateOpacity(opacity);
109 return;
110 }
111
112 SetOpacityImmediately(opacity);
86 } 113 }
87 114
88 bool Layer::ShouldDraw() { 115 bool Layer::ShouldDraw() {
89 return can_have_texture_ && GetCombinedOpacity() > 0.0f && 116 return can_have_texture_ && GetCombinedOpacity() > 0.0f &&
90 !hole_rect_.Contains(gfx::Rect(gfx::Point(0, 0), bounds_.size())); 117 !hole_rect_.Contains(gfx::Rect(gfx::Point(0, 0), bounds_.size()));
91 } 118 }
92 119
93 // static 120 // static
94 void Layer::ConvertPointToLayer(const Layer* source, 121 void Layer::ConvertPointToLayer(const Layer* source,
95 const Layer* target, 122 const Layer* target,
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after
203 230
204 void Layer::DrawTree() { 231 void Layer::DrawTree() {
205 if (!visible_) 232 if (!visible_)
206 return; 233 return;
207 234
208 Draw(); 235 Draw();
209 for (size_t i = 0; i < children_.size(); ++i) 236 for (size_t i = 0; i < children_.size(); ++i)
210 children_.at(i)->DrawTree(); 237 children_.at(i)->DrawTree();
211 } 238 }
212 239
213 void Layer::SetOpacity(float alpha) {
214 bool was_opaque = GetCombinedOpacity() == 1.0f;
215 opacity_ = alpha;
216 bool is_opaque = GetCombinedOpacity() == 1.0f;
217
218 // If our opacity has changed we need to recompute our hole, our parent's hole
219 // and the holes of all our descendants.
220 if (was_opaque != is_opaque) {
221 if (parent_)
222 parent_->RecomputeHole();
223 std::queue<Layer*> to_process;
224 to_process.push(this);
225 while (!to_process.empty()) {
226 Layer* current = to_process.front();
227 to_process.pop();
228 current->RecomputeHole();
229 for (size_t i = 0; i < current->children_.size(); ++i)
230 to_process.push(current->children_.at(i));
231 }
232 }
233 }
234
235 float Layer::GetCombinedOpacity() const { 240 float Layer::GetCombinedOpacity() const {
236 float opacity = opacity_; 241 float opacity = opacity_;
237 Layer* current = this->parent_; 242 Layer* current = this->parent_;
238 while (current) { 243 while (current) {
239 opacity *= current->opacity_; 244 opacity *= current->opacity_;
240 current = current->parent_; 245 current = current->parent_;
241 } 246 }
242 return opacity; 247 return opacity;
243 } 248 }
244 249
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
312 const Layer* p = this; 317 const Layer* p = this;
313 for (; p && p != ancestor; p = p->parent()) { 318 for (; p && p != ancestor; p = p->parent()) {
314 if (p->transform().HasChange()) 319 if (p->transform().HasChange())
315 transform->ConcatTransform(p->transform()); 320 transform->ConcatTransform(p->transform());
316 transform->ConcatTranslate(static_cast<float>(p->bounds().x()), 321 transform->ConcatTranslate(static_cast<float>(p->bounds().x()),
317 static_cast<float>(p->bounds().y())); 322 static_cast<float>(p->bounds().y()));
318 } 323 }
319 return p == ancestor; 324 return p == ancestor;
320 } 325 }
321 326
327 void Layer::SetBoundsImmediately(const gfx::Rect& bounds) {
328 bounds_ = bounds;
329
330 if (parent() && fills_bounds_opaquely_)
331 parent()->RecomputeHole();
332 }
333
334 void Layer::SetTransformImmediately(const ui::Transform& transform) {
335 transform_ = transform;
336
337 if (parent() && fills_bounds_opaquely_)
338 parent()->RecomputeHole();
339 }
340
341 void Layer::SetOpacityImmediately(float opacity) {
342 bool was_opaque = GetCombinedOpacity() == 1.0f;
343 opacity_ = opacity;
344 bool is_opaque = GetCombinedOpacity() == 1.0f;
345
346 // If our opacity has changed we need to recompute our hole, our parent's hole
347 // and the holes of all our descendants.
348 if (was_opaque != is_opaque) {
349 if (parent_)
350 parent_->RecomputeHole();
351 std::queue<Layer*> to_process;
352 to_process.push(this);
353 while (!to_process.empty()) {
354 Layer* current = to_process.front();
355 to_process.pop();
356 current->RecomputeHole();
357 for (size_t i = 0; i < current->children_.size(); ++i)
358 to_process.push(current->children_.at(i));
359 }
360 }
361 }
362
363 void Layer::SetBoundsFromAnimator(const gfx::Rect& bounds) {
364 SetBoundsImmediately(bounds);
365 }
366
367 void Layer::SetTransformFromAnimator(const Transform& transform) {
368 SetTransformImmediately(transform);
369 }
370
371 void Layer::SetOpacityFromAnimator(float opacity) {
372 SetOpacityImmediately(opacity);
373 }
374
322 } // namespace ui 375 } // namespace ui
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698