| OLD | NEW |
| 1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 2012 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 "config.h" | 5 #include "config.h" |
| 6 | 6 |
| 7 #include "CCKeyframedAnimationCurve.h" | 7 #include "CCKeyframedAnimationCurve.h" |
| 8 | 8 |
| 9 #include <wtf/OwnPtr.h> | |
| 10 | |
| 11 using WebKit::WebTransformationMatrix; | 9 using WebKit::WebTransformationMatrix; |
| 12 | 10 |
| 13 namespace cc { | 11 namespace cc { |
| 14 | 12 |
| 15 namespace { | 13 namespace { |
| 16 | 14 |
| 17 template <class Keyframe> | 15 template <class Keyframe> |
| 18 void insertKeyframe(PassOwnPtr<Keyframe> popKeyframe, OwnPtrVector<Keyframe>& ke
yframes) | 16 void insertKeyframe(scoped_ptr<Keyframe> keyframe, ScopedPtrVector<Keyframe>& ke
yframes) |
| 19 { | 17 { |
| 20 OwnPtr<Keyframe> keyframe = popKeyframe; | |
| 21 | |
| 22 // Usually, the keyframes will be added in order, so this loop would be unne
cessary and | 18 // Usually, the keyframes will be added in order, so this loop would be unne
cessary and |
| 23 // we should skip it if possible. | 19 // we should skip it if possible. |
| 24 if (!keyframes.isEmpty() && keyframe->time() < keyframes.last()->time()) { | 20 if (!keyframes.isEmpty() && keyframe->time() < keyframes.last()->time()) { |
| 25 for (size_t i = 0; i < keyframes.size(); ++i) { | 21 for (size_t i = 0; i < keyframes.size(); ++i) { |
| 26 if (keyframe->time() < keyframes[i]->time()) { | 22 if (keyframe->time() < keyframes[i]->time()) { |
| 27 keyframes.insert(i, keyframe.release()); | 23 keyframes.insert(i, keyframe.Pass()); |
| 28 return; | 24 return; |
| 29 } | 25 } |
| 30 } | 26 } |
| 31 } | 27 } |
| 32 | 28 |
| 33 keyframes.append(keyframe.release()); | 29 keyframes.append(keyframe.Pass()); |
| 34 } | 30 } |
| 35 | 31 |
| 36 PassOwnPtr<CCTimingFunction> cloneTimingFunction(const CCTimingFunction* timingF
unction) | 32 scoped_ptr<CCTimingFunction> cloneTimingFunction(const CCTimingFunction* timingF
unction) |
| 37 { | 33 { |
| 38 ASSERT(timingFunction); | 34 ASSERT(timingFunction); |
| 39 OwnPtr<CCAnimationCurve> curve(timingFunction->clone()); | 35 scoped_ptr<CCAnimationCurve> curve(timingFunction->clone()); |
| 40 return adoptPtr(static_cast<CCTimingFunction*>(curve.leakPtr())); | 36 return scoped_ptr<CCTimingFunction>(static_cast<CCTimingFunction*>(curve.rel
ease())); |
| 41 } | 37 } |
| 42 | 38 |
| 43 } // namespace | 39 } // namespace |
| 44 | 40 |
| 45 CCKeyframe::CCKeyframe(double time, PassOwnPtr<CCTimingFunction> timingFunction) | 41 CCKeyframe::CCKeyframe(double time, scoped_ptr<CCTimingFunction> timingFunction) |
| 46 : m_time(time) | 42 : m_time(time) |
| 47 , m_timingFunction(timingFunction) | 43 , m_timingFunction(timingFunction.Pass()) |
| 48 { | 44 { |
| 49 } | 45 } |
| 50 | 46 |
| 51 CCKeyframe::~CCKeyframe() | 47 CCKeyframe::~CCKeyframe() |
| 52 { | 48 { |
| 53 } | 49 } |
| 54 | 50 |
| 55 double CCKeyframe::time() const | 51 double CCKeyframe::time() const |
| 56 { | 52 { |
| 57 return m_time; | 53 return m_time; |
| 58 } | 54 } |
| 59 | 55 |
| 60 const CCTimingFunction* CCKeyframe::timingFunction() const | 56 const CCTimingFunction* CCKeyframe::timingFunction() const |
| 61 { | 57 { |
| 62 return m_timingFunction.get(); | 58 return m_timingFunction.get(); |
| 63 } | 59 } |
| 64 | 60 |
| 65 PassOwnPtr<CCFloatKeyframe> CCFloatKeyframe::create(double time, float value, Pa
ssOwnPtr<CCTimingFunction> timingFunction) | 61 scoped_ptr<CCFloatKeyframe> CCFloatKeyframe::create(double time, float value, sc
oped_ptr<CCTimingFunction> timingFunction) |
| 66 { | 62 { |
| 67 return adoptPtr(new CCFloatKeyframe(time, value, timingFunction)); | 63 return make_scoped_ptr(new CCFloatKeyframe(time, value, timingFunction.Pass(
))); |
| 68 } | 64 } |
| 69 | 65 |
| 70 CCFloatKeyframe::CCFloatKeyframe(double time, float value, PassOwnPtr<CCTimingFu
nction> timingFunction) | 66 CCFloatKeyframe::CCFloatKeyframe(double time, float value, scoped_ptr<CCTimingFu
nction> timingFunction) |
| 71 : CCKeyframe(time, timingFunction) | 67 : CCKeyframe(time, timingFunction.Pass()) |
| 72 , m_value(value) | 68 , m_value(value) |
| 73 { | 69 { |
| 74 } | 70 } |
| 75 | 71 |
| 76 CCFloatKeyframe::~CCFloatKeyframe() | 72 CCFloatKeyframe::~CCFloatKeyframe() |
| 77 { | 73 { |
| 78 } | 74 } |
| 79 | 75 |
| 80 float CCFloatKeyframe::value() const | 76 float CCFloatKeyframe::value() const |
| 81 { | 77 { |
| 82 return m_value; | 78 return m_value; |
| 83 } | 79 } |
| 84 | 80 |
| 85 PassOwnPtr<CCFloatKeyframe> CCFloatKeyframe::clone() const | 81 scoped_ptr<CCFloatKeyframe> CCFloatKeyframe::clone() const |
| 86 { | 82 { |
| 87 return CCFloatKeyframe::create(time(), value(), timingFunction() ? cloneTimi
ngFunction(timingFunction()) : nullptr); | 83 scoped_ptr<CCTimingFunction> func; |
| 84 if (timingFunction()) |
| 85 func = cloneTimingFunction(timingFunction()); |
| 86 return CCFloatKeyframe::create(time(), value(), func.Pass()); |
| 88 } | 87 } |
| 89 | 88 |
| 90 PassOwnPtr<CCTransformKeyframe> CCTransformKeyframe::create(double time, const W
ebKit::WebTransformOperations& value, PassOwnPtr<CCTimingFunction> timingFunctio
n) | 89 scoped_ptr<CCTransformKeyframe> CCTransformKeyframe::create(double time, const W
ebKit::WebTransformOperations& value, scoped_ptr<CCTimingFunction> timingFunctio
n) |
| 91 { | 90 { |
| 92 return adoptPtr(new CCTransformKeyframe(time, value, timingFunction)); | 91 return make_scoped_ptr(new CCTransformKeyframe(time, value, timingFunction.P
ass())); |
| 93 } | 92 } |
| 94 | 93 |
| 95 CCTransformKeyframe::CCTransformKeyframe(double time, const WebKit::WebTransform
Operations& value, PassOwnPtr<CCTimingFunction> timingFunction) | 94 CCTransformKeyframe::CCTransformKeyframe(double time, const WebKit::WebTransform
Operations& value, scoped_ptr<CCTimingFunction> timingFunction) |
| 96 : CCKeyframe(time, timingFunction) | 95 : CCKeyframe(time, timingFunction.Pass()) |
| 97 , m_value(value) | 96 , m_value(value) |
| 98 { | 97 { |
| 99 } | 98 } |
| 100 | 99 |
| 101 CCTransformKeyframe::~CCTransformKeyframe() | 100 CCTransformKeyframe::~CCTransformKeyframe() |
| 102 { | 101 { |
| 103 } | 102 } |
| 104 | 103 |
| 105 const WebKit::WebTransformOperations& CCTransformKeyframe::value() const | 104 const WebKit::WebTransformOperations& CCTransformKeyframe::value() const |
| 106 { | 105 { |
| 107 return m_value; | 106 return m_value; |
| 108 } | 107 } |
| 109 | 108 |
| 110 PassOwnPtr<CCTransformKeyframe> CCTransformKeyframe::clone() const | 109 scoped_ptr<CCTransformKeyframe> CCTransformKeyframe::clone() const |
| 111 { | 110 { |
| 112 return CCTransformKeyframe::create(time(), value(), timingFunction() ? clone
TimingFunction(timingFunction()) : nullptr); | 111 scoped_ptr<CCTimingFunction> func; |
| 112 if (timingFunction()) |
| 113 func = cloneTimingFunction(timingFunction()); |
| 114 return CCTransformKeyframe::create(time(), value(), func.Pass()); |
| 113 } | 115 } |
| 114 | 116 |
| 115 PassOwnPtr<CCKeyframedFloatAnimationCurve> CCKeyframedFloatAnimationCurve::creat
e() | 117 scoped_ptr<CCKeyframedFloatAnimationCurve> CCKeyframedFloatAnimationCurve::creat
e() |
| 116 { | 118 { |
| 117 return adoptPtr(new CCKeyframedFloatAnimationCurve); | 119 return make_scoped_ptr(new CCKeyframedFloatAnimationCurve); |
| 118 } | 120 } |
| 119 | 121 |
| 120 CCKeyframedFloatAnimationCurve::CCKeyframedFloatAnimationCurve() | 122 CCKeyframedFloatAnimationCurve::CCKeyframedFloatAnimationCurve() |
| 121 { | 123 { |
| 122 } | 124 } |
| 123 | 125 |
| 124 CCKeyframedFloatAnimationCurve::~CCKeyframedFloatAnimationCurve() | 126 CCKeyframedFloatAnimationCurve::~CCKeyframedFloatAnimationCurve() |
| 125 { | 127 { |
| 126 } | 128 } |
| 127 | 129 |
| 128 void CCKeyframedFloatAnimationCurve::addKeyframe(PassOwnPtr<CCFloatKeyframe> key
frame) | 130 void CCKeyframedFloatAnimationCurve::addKeyframe(scoped_ptr<CCFloatKeyframe> key
frame) |
| 129 { | 131 { |
| 130 insertKeyframe(keyframe, m_keyframes); | 132 insertKeyframe(keyframe.Pass(), m_keyframes); |
| 131 } | 133 } |
| 132 | 134 |
| 133 double CCKeyframedFloatAnimationCurve::duration() const | 135 double CCKeyframedFloatAnimationCurve::duration() const |
| 134 { | 136 { |
| 135 return m_keyframes.last()->time() - m_keyframes.first()->time(); | 137 return m_keyframes.last()->time() - m_keyframes.first()->time(); |
| 136 } | 138 } |
| 137 | 139 |
| 138 PassOwnPtr<CCAnimationCurve> CCKeyframedFloatAnimationCurve::clone() const | 140 scoped_ptr<CCAnimationCurve> CCKeyframedFloatAnimationCurve::clone() const |
| 139 { | 141 { |
| 140 OwnPtr<CCKeyframedFloatAnimationCurve> toReturn(CCKeyframedFloatAnimationCur
ve::create()); | 142 scoped_ptr<CCKeyframedFloatAnimationCurve> toReturn(CCKeyframedFloatAnimatio
nCurve::create()); |
| 141 for (size_t i = 0; i < m_keyframes.size(); ++i) | 143 for (size_t i = 0; i < m_keyframes.size(); ++i) |
| 142 toReturn->addKeyframe(m_keyframes[i]->clone()); | 144 toReturn->addKeyframe(m_keyframes[i]->clone()); |
| 143 return toReturn.release(); | 145 return toReturn.PassAs<CCAnimationCurve>(); |
| 144 } | 146 } |
| 145 | 147 |
| 146 float CCKeyframedFloatAnimationCurve::getValue(double t) const | 148 float CCKeyframedFloatAnimationCurve::getValue(double t) const |
| 147 { | 149 { |
| 148 if (t <= m_keyframes.first()->time()) | 150 if (t <= m_keyframes.first()->time()) |
| 149 return m_keyframes.first()->value(); | 151 return m_keyframes.first()->value(); |
| 150 | 152 |
| 151 if (t >= m_keyframes.last()->time()) | 153 if (t >= m_keyframes.last()->time()) |
| 152 return m_keyframes.last()->value(); | 154 return m_keyframes.last()->value(); |
| 153 | 155 |
| 154 size_t i = 0; | 156 size_t i = 0; |
| 155 for (; i < m_keyframes.size() - 1; ++i) { | 157 for (; i < m_keyframes.size() - 1; ++i) { |
| 156 if (t < m_keyframes[i+1]->time()) | 158 if (t < m_keyframes[i+1]->time()) |
| 157 break; | 159 break; |
| 158 } | 160 } |
| 159 | 161 |
| 160 float progress = static_cast<float>((t - m_keyframes[i]->time()) / (m_keyfra
mes[i+1]->time() - m_keyframes[i]->time())); | 162 float progress = static_cast<float>((t - m_keyframes[i]->time()) / (m_keyfra
mes[i+1]->time() - m_keyframes[i]->time())); |
| 161 | 163 |
| 162 if (m_keyframes[i]->timingFunction()) | 164 if (m_keyframes[i]->timingFunction()) |
| 163 progress = m_keyframes[i]->timingFunction()->getValue(progress); | 165 progress = m_keyframes[i]->timingFunction()->getValue(progress); |
| 164 | 166 |
| 165 return m_keyframes[i]->value() + (m_keyframes[i+1]->value() - m_keyframes[i]
->value()) * progress; | 167 return m_keyframes[i]->value() + (m_keyframes[i+1]->value() - m_keyframes[i]
->value()) * progress; |
| 166 } | 168 } |
| 167 | 169 |
| 168 PassOwnPtr<CCKeyframedTransformAnimationCurve> CCKeyframedTransformAnimationCurv
e::create() | 170 scoped_ptr<CCKeyframedTransformAnimationCurve> CCKeyframedTransformAnimationCurv
e::create() |
| 169 { | 171 { |
| 170 return adoptPtr(new CCKeyframedTransformAnimationCurve); | 172 return make_scoped_ptr(new CCKeyframedTransformAnimationCurve); |
| 171 } | 173 } |
| 172 | 174 |
| 173 CCKeyframedTransformAnimationCurve::CCKeyframedTransformAnimationCurve() | 175 CCKeyframedTransformAnimationCurve::CCKeyframedTransformAnimationCurve() |
| 174 { | 176 { |
| 175 } | 177 } |
| 176 | 178 |
| 177 CCKeyframedTransformAnimationCurve::~CCKeyframedTransformAnimationCurve() | 179 CCKeyframedTransformAnimationCurve::~CCKeyframedTransformAnimationCurve() |
| 178 { | 180 { |
| 179 } | 181 } |
| 180 | 182 |
| 181 void CCKeyframedTransformAnimationCurve::addKeyframe(PassOwnPtr<CCTransformKeyfr
ame> keyframe) | 183 void CCKeyframedTransformAnimationCurve::addKeyframe(scoped_ptr<CCTransformKeyfr
ame> keyframe) |
| 182 { | 184 { |
| 183 insertKeyframe(keyframe, m_keyframes); | 185 insertKeyframe(keyframe.Pass(), m_keyframes); |
| 184 } | 186 } |
| 185 | 187 |
| 186 double CCKeyframedTransformAnimationCurve::duration() const | 188 double CCKeyframedTransformAnimationCurve::duration() const |
| 187 { | 189 { |
| 188 return m_keyframes.last()->time() - m_keyframes.first()->time(); | 190 return m_keyframes.last()->time() - m_keyframes.first()->time(); |
| 189 } | 191 } |
| 190 | 192 |
| 191 PassOwnPtr<CCAnimationCurve> CCKeyframedTransformAnimationCurve::clone() const | 193 scoped_ptr<CCAnimationCurve> CCKeyframedTransformAnimationCurve::clone() const |
| 192 { | 194 { |
| 193 OwnPtr<CCKeyframedTransformAnimationCurve> toReturn(CCKeyframedTransformAnim
ationCurve::create()); | 195 scoped_ptr<CCKeyframedTransformAnimationCurve> toReturn(CCKeyframedTransform
AnimationCurve::create()); |
| 194 for (size_t i = 0; i < m_keyframes.size(); ++i) | 196 for (size_t i = 0; i < m_keyframes.size(); ++i) |
| 195 toReturn->addKeyframe(m_keyframes[i]->clone()); | 197 toReturn->addKeyframe(m_keyframes[i]->clone()); |
| 196 return toReturn.release(); | 198 return toReturn.PassAs<CCAnimationCurve>(); |
| 197 } | 199 } |
| 198 | 200 |
| 199 WebTransformationMatrix CCKeyframedTransformAnimationCurve::getValue(double t) c
onst | 201 WebTransformationMatrix CCKeyframedTransformAnimationCurve::getValue(double t) c
onst |
| 200 { | 202 { |
| 201 if (t <= m_keyframes.first()->time()) | 203 if (t <= m_keyframes.first()->time()) |
| 202 return m_keyframes.first()->value().apply(); | 204 return m_keyframes.first()->value().apply(); |
| 203 | 205 |
| 204 if (t >= m_keyframes.last()->time()) | 206 if (t >= m_keyframes.last()->time()) |
| 205 return m_keyframes.last()->value().apply(); | 207 return m_keyframes.last()->value().apply(); |
| 206 | 208 |
| 207 size_t i = 0; | 209 size_t i = 0; |
| 208 for (; i < m_keyframes.size() - 1; ++i) { | 210 for (; i < m_keyframes.size() - 1; ++i) { |
| 209 if (t < m_keyframes[i+1]->time()) | 211 if (t < m_keyframes[i+1]->time()) |
| 210 break; | 212 break; |
| 211 } | 213 } |
| 212 | 214 |
| 213 double progress = (t - m_keyframes[i]->time()) / (m_keyframes[i+1]->time() -
m_keyframes[i]->time()); | 215 double progress = (t - m_keyframes[i]->time()) / (m_keyframes[i+1]->time() -
m_keyframes[i]->time()); |
| 214 | 216 |
| 215 if (m_keyframes[i]->timingFunction()) | 217 if (m_keyframes[i]->timingFunction()) |
| 216 progress = m_keyframes[i]->timingFunction()->getValue(progress); | 218 progress = m_keyframes[i]->timingFunction()->getValue(progress); |
| 217 | 219 |
| 218 return m_keyframes[i+1]->value().blend(m_keyframes[i]->value(), progress); | 220 return m_keyframes[i+1]->value().blend(m_keyframes[i]->value(), progress); |
| 219 } | 221 } |
| 220 | 222 |
| 221 } // namespace cc | 223 } // namespace cc |
| OLD | NEW |