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

Side by Side Diff: chrome/browser/ui/touch/animation/screen_rotation.cc

Issue 7273073: Animated Rotation (Closed) Base URL: http://git.chromium.org/git/chromium.git@trunk
Patch Set: Listen for painting to complete rather than posting tasks. Created 9 years, 4 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
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "chrome/browser/ui/touch/animation/screen_rotation.h"
6
7 #include "base/debug/trace_event.h"
8 #include "base/message_loop.h"
9 #include "base/task.h"
10 #include "ui/base/animation/slide_animation.h"
11 #include "ui/gfx/compositor/layer.h"
12 #include "ui/gfx/interpolated_transform.h"
13 #include "ui/gfx/rect.h"
14 #include "ui/gfx/transform.h"
15 #include "views/paint_lock.h"
16 #include "views/view.h"
17
18 namespace {
19 const int kDefaultTransitionDurationMs = 350;
20
21 } // namespace
22
23 ////////////////////////////////////////////////////////////////////////////////
24 // ScreenRotationListener public:
25 //
26
27 ScreenRotationListener::~ScreenRotationListener() {
28 }
29
30 ////////////////////////////////////////////////////////////////////////////////
31 // ScreenRotation public:
32 //
33
34 ScreenRotation::ScreenRotation(views::View* view,
35 ScreenRotationListener* listener,
36 float old_degrees,
37 float new_degrees)
38 : view_(view),
39 listener_(listener),
40 old_degrees_(old_degrees),
41 new_degrees_(new_degrees),
42 last_t_(0.0),
43 duration_(kDefaultTransitionDurationMs),
44 animation_started_(false),
45 animation_stopped_(false) {
46 DCHECK(view);
47 DCHECK(listener);
48 // Screen rotations are trigged as a result of a call to SetTransform which
49 // causes a paint to be scheduled to occur. At this point, the paint has been
50 // scheduled, but has not yet been started. We will listen for this paint to
51 // be completed before we start animating.
52 view->AddPaintListener(this);
53 }
54
55 ScreenRotation::~ScreenRotation() {
56 view_->RemovePaintListener(this);
57 }
58
59 void ScreenRotation::Finalize() {
60 ui::Transform final_transform = view_->GetTransform();
61 gfx::Rect final_bounds(0, 0, new_size_.width(), new_size_.height());
62 listener_->OnScreenRotationCompleted(final_transform, final_bounds);
63 }
64
65 void ScreenRotation::SetTarget(float degrees) {
66 if (new_degrees_ == degrees)
67 return;
68
69 new_degrees_ = degrees;
70 Init();
71 }
72
73 void ScreenRotation::OnPainted(views::View* view) {
74 // Do nothing, there should be a call to OnComposited coming.
75 }
76
77 void ScreenRotation::OnComposited(views::View* view) {
78 DoPendingWork();
79 }
80
81 ////////////////////////////////////////////////////////////////////////////////
82 // ScreenRotation private:
83 //
84
85 void ScreenRotation::AnimationProgressed(const ui::Animation* anim) {
86 TRACE_EVENT0("ScreenRotation", "step");
87 if (!interpolated_transform_.get() || !view_->layer())
88 return;
89
90 last_t_ = static_cast<float>(anim->GetCurrentValue());
91 view_->layer()->SetTransform(interpolated_transform_->Interpolate(last_t_));
92 view_->layer()->compositor()->SchedulePaint();
93 }
94
95 void ScreenRotation::AnimationEnded(const ui::Animation* anim) {
96 TRACE_EVENT_END0("ScreenRotation", "ScreenRotation");
97 // TODO(vollick) massage matrix so that entries sufficiently close
98 // to 0, 1, or -1 are clamped to these values. The idea is to fight
99 // accumulated numeric error due to successive rotations.
100 if (view_->layer()) {
101 ui::Transform xform = view_->layer()->transform();
102 gfx::Point origin;
103 xform.TransformPoint(origin);
104 ui::Transform translation;
105 translation.SetTranslate(new_origin_.x() - origin.x(),
106 new_origin_.y() - origin.y());
107 xform.ConcatTransform(translation);
108 view_->layer()->SetTransform(xform);
109 view_->layer()->compositor()->SchedulePaint();
110 }
111 animation_stopped_ = true;
112 }
113
114 void ScreenRotation::Init() {
115 TRACE_EVENT0("ScreenRotation", "init");
116 if (!view_->layer()) {
117 view_->SetPaintToLayer(true);
118 }
119
120 // can't proceed without a layer.
121 if (!view_->layer())
122 return;
123
124 ui::Transform current_transform = view_->layer()->transform();
125 int degrees = new_degrees_ - old_degrees_;
126 degrees = NormalizeAngle(degrees);
127
128 // No rotation required.
129 if (degrees == 0)
130 return;
131
132 gfx::Point old_pivot;
133 gfx::Point new_pivot;
134 int width = view_->layer()->bounds().width();
135 int height = view_->layer()->bounds().height();
136
137 switch (degrees) {
138 case 90:
139 new_origin_ = new_pivot = gfx::Point(width, 0);
140 new_size_.SetSize(height, width);
141 break;
142 case -90:
143 new_origin_ = new_pivot = gfx::Point(0, height);
144 new_size_.SetSize(height, width);
145 break;
146 case 180:
147 duration_ = 550;
148 new_pivot = old_pivot = gfx::Point(width / 2, height / 2);
149 new_origin_.SetPoint(width, height);
150 new_size_.SetSize(width, height);
151 break;
152 }
153
154 // Convert points to world space.
155 current_transform.TransformPoint(old_pivot);
156 current_transform.TransformPoint(new_pivot);
157 current_transform.TransformPoint(new_origin_);
158
159 scoped_ptr<ui::InterpolatedTransform> rotation(
160 new ui::InterpolatedTransformAboutPivot(
161 old_pivot,
162 new ui::InterpolatedRotation(0, degrees)));
163
164 scoped_ptr<ui::InterpolatedTransform> translation(
165 new ui::InterpolatedTranslation(
166 gfx::Point(0, 0),
167 gfx::Point(new_pivot.x() - old_pivot.x(),
168 new_pivot.y() - old_pivot.y())));
169
170 float scale_factor = 0.9f;
171 scoped_ptr<ui::InterpolatedTransform> scale_down(
172 new ui::InterpolatedScale(1.0f, scale_factor, 0.0f, 0.5f));
173
174 scoped_ptr<ui::InterpolatedTransform> scale_up(
175 new ui::InterpolatedScale(1.0f, 1.0f / scale_factor, 0.5f, 1.0f));
176
177 scoped_ptr<ui::InterpolatedTransform> transition(
178 new ui::InterpolatedConstantTransform(current_transform));
179
180 scale_up->SetChild(scale_down.release());
181 translation->SetChild(scale_up.release());
182 rotation->SetChild(translation.release());
183 transition->SetChild(rotation.release());
184
185 if (interpolated_transform_.get()) {
186 // We are in the middle of a transition. In this case, we need to create
187 // an interpolated transform that gets us from where we are to the target
188 // transform.
189 ui::Transform target = transition->Interpolate(1.0);
190 interpolated_transform_.reset(
191 new ui::InterpolatedTRSTransform(
192 current_transform, target, last_t_, 1.0));
193 } else {
194 interpolated_transform_.reset(transition.release());
195 }
196 }
197
198 void ScreenRotation::Start() {
199 TRACE_EVENT_BEGIN0("ScreenRotation", "ScreenRotation");
200 Init();
201 if (interpolated_transform_.get()) {
202 paint_lock_.reset(new views::PaintLock(view_));
203 animation_.reset(new ui::SlideAnimation(this));
204 animation_->SetTweenType(ui::Tween::LINEAR);
205 animation_->SetSlideDuration(duration_);
206 animation_->Show();
207 animation_started_ = true;
208 } else {
209 Finalize();
210 }
211 }
212
213 int ScreenRotation::NormalizeAngle(int degrees) {
214 while (degrees <= -180) degrees += 360;
215 while (degrees > 180) degrees -= 360;
216 return degrees;
217 }
218
219 void ScreenRotation::DoPendingWork() {
220 if (!animation_started_)
221 Start();
222 else if (animation_stopped_) {
223 view_->RemovePaintListener(this);
224 Finalize();
225 }
226 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698