e4a3f66602fc38d71fd3b078f908a1b141b48da5
[WebKit-https.git] / Source / WebCore / platform / graphics / ca / win / PlatformCAAnimationWin.cpp
1 /*
2  * Copyright (C) 2014 Apple Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
24  */
25
26 #include "config.h"
27
28 #if PLATFORM(WIN) && USE(CA)
29 #include "PlatformCAAnimationWin.h"
30
31 #include "FloatConversion.h"
32 #include "TimingFunction.h"
33 #include <QuartzCore/CACFAnimation.h>
34 #include <QuartzCore/CACFTiming.h>
35 #include <QuartzCore/CACFTimingFunction.h>
36 #include <QuartzCore/CACFValueFunction.h>
37 #include <QuartzCore/CACFVector.h>
38 #include <wtf/text/WTFString.h>
39
40 using namespace WebCore;
41
42 static CFStringRef toCACFFillModeType(PlatformCAAnimation::FillModeType type)
43 {
44     switch (type) {
45     case PlatformCAAnimation::NoFillMode:
46     case PlatformCAAnimation::Forwards: return kCACFFillModeForwards;
47     case PlatformCAAnimation::Backwards: return kCACFFillModeBackwards;
48     case PlatformCAAnimation::Both: return kCACFFillModeBoth;
49     }
50     ASSERT_NOT_REACHED();
51     return 0;
52 }
53
54 static PlatformCAAnimation::FillModeType fromCACFFillModeType(CFStringRef string)
55 {
56     if (string == kCACFFillModeBackwards)
57         return PlatformCAAnimation::Backwards;
58
59     if (string == kCACFFillModeBoth)
60         return PlatformCAAnimation::Both;
61
62     return PlatformCAAnimation::Forwards;
63 }
64
65 static CFStringRef toCACFValueFunctionType(PlatformCAAnimation::ValueFunctionType type)
66 {
67     switch (type) {
68     case PlatformCAAnimation::NoValueFunction: return 0;
69     case PlatformCAAnimation::RotateX: return kCACFValueFunctionRotateX;
70     case PlatformCAAnimation::RotateY: return kCACFValueFunctionRotateY;
71     case PlatformCAAnimation::RotateZ: return kCACFValueFunctionRotateZ;
72     case PlatformCAAnimation::ScaleX: return kCACFValueFunctionScaleX;
73     case PlatformCAAnimation::ScaleY: return kCACFValueFunctionScaleY;
74     case PlatformCAAnimation::ScaleZ: return kCACFValueFunctionScaleZ;
75     case PlatformCAAnimation::Scale: return kCACFValueFunctionScale;
76     case PlatformCAAnimation::TranslateX: return kCACFValueFunctionTranslateX;
77     case PlatformCAAnimation::TranslateY: return kCACFValueFunctionTranslateY;
78     case PlatformCAAnimation::TranslateZ: return kCACFValueFunctionTranslateZ;
79     case PlatformCAAnimation::Translate: return kCACFValueFunctionTranslate;
80     }
81     ASSERT_NOT_REACHED();
82     return 0;
83 }
84
85 static PlatformCAAnimation::ValueFunctionType fromCACFValueFunctionType(CFStringRef string)
86 {
87     if (string == kCACFValueFunctionRotateX)
88         return PlatformCAAnimation::RotateX;
89
90     if (string == kCACFValueFunctionRotateY)
91         return PlatformCAAnimation::RotateY;
92
93     if (string == kCACFValueFunctionRotateZ)
94         return PlatformCAAnimation::RotateZ;
95
96     if (string == kCACFValueFunctionScaleX)
97         return PlatformCAAnimation::ScaleX;
98
99     if (string == kCACFValueFunctionScaleY)
100         return PlatformCAAnimation::ScaleY;
101
102     if (string == kCACFValueFunctionScaleZ)
103         return PlatformCAAnimation::ScaleZ;
104
105     if (string == kCACFValueFunctionScale)
106         return PlatformCAAnimation::Scale;
107
108     if (string == kCACFValueFunctionTranslateX)
109         return PlatformCAAnimation::TranslateX;
110
111     if (string == kCACFValueFunctionTranslateY)
112         return PlatformCAAnimation::TranslateY;
113
114     if (string == kCACFValueFunctionTranslateZ)
115         return PlatformCAAnimation::TranslateZ;
116
117     if (string == kCACFValueFunctionTranslate)
118         return PlatformCAAnimation::Translate;
119
120     return PlatformCAAnimation::NoValueFunction;
121 }
122
123 static RetainPtr<CACFTimingFunctionRef> toCACFTimingFunction(const TimingFunction* timingFunction, bool reverse)
124 {
125     ASSERT(timingFunction);
126     if (timingFunction->isCubicBezierTimingFunction()) {
127         RefPtr<CubicBezierTimingFunction> reversed;
128         const CubicBezierTimingFunction* ctf = static_cast<const CubicBezierTimingFunction*>(timingFunction);
129
130         if (reverse) {
131             reversed = ctf->createReversed();
132             ctf = reversed.get();
133         }
134
135         float x1 = static_cast<float>(ctf->x1());
136         float y1 = static_cast<float>(ctf->y1());
137         float x2 = static_cast<float>(ctf->x2());
138         float y2 = static_cast<float>(ctf->y2());
139         return adoptCF(CACFTimingFunctionCreate(x1, y1, x2, y2));
140     }
141     
142     return CACFTimingFunctionGetFunctionWithName(kCACFTimingFunctionLinear);
143 }
144
145 Ref<PlatformCAAnimation> PlatformCAAnimationWin::create(AnimationType type, const String& keyPath)
146 {
147     return adoptRef(*new PlatformCAAnimationWin(type, keyPath));
148 }
149
150 Ref<PlatformCAAnimation> PlatformCAAnimationWin::create(PlatformAnimationRef animation)
151 {
152     return adoptRef(*new PlatformCAAnimationWin(animation));
153 }
154
155 PlatformCAAnimationWin::PlatformCAAnimationWin(AnimationType type, const String& keyPath)
156     : PlatformCAAnimation(type)
157 {
158     if (type == Basic)
159         m_animation = adoptCF(CACFAnimationCreate(kCACFBasicAnimation));
160     else
161         m_animation = adoptCF(CACFAnimationCreate(kCACFKeyframeAnimation));
162     
163     CACFAnimationSetKeyPath(m_animation.get(), keyPath.createCFString().get());
164 }
165
166 PlatformCAAnimationWin::PlatformCAAnimationWin(PlatformAnimationRef animation)
167 {
168     if (CACFAnimationGetClass(animation) == kCACFBasicAnimation)
169         setType(Basic);
170     else if (CACFAnimationGetClass(animation) == kCACFKeyframeAnimation)
171         setType(Keyframe);
172     else {
173         ASSERT_NOT_REACHED();
174         return;
175     }
176     
177     m_animation = animation;
178 }
179
180 Ref<PlatformCAAnimation> PlatformCAAnimationWin::copy() const
181 {
182     auto animation = create(animationType(), keyPath());
183     
184     animation->setBeginTime(beginTime());
185     animation->setDuration(duration());
186     animation->setSpeed(speed());
187     animation->setTimeOffset(timeOffset());
188     animation->setRepeatCount(repeatCount());
189     animation->setAutoreverses(autoreverses());
190     animation->setFillMode(fillMode());
191     animation->setRemovedOnCompletion(isRemovedOnCompletion());
192     animation->setAdditive(isAdditive());
193     animation->copyTimingFunctionFrom(*this);
194     if (valueFunction())
195         animation->setValueFunction(valueFunction());
196     
197     // Copy the specific Basic or Keyframe values
198     if (animationType() == Keyframe) {
199         animation->copyValuesFrom(*this);
200         animation->copyKeyTimesFrom(*this);
201         animation->copyTimingFunctionsFrom(*this);
202     } else {
203         animation->copyFromValueFrom(*this);
204         animation->copyToValueFrom(*this);
205     }
206     
207     return animation;
208 }
209
210 PlatformCAAnimationWin::~PlatformCAAnimationWin() = default;
211
212 PlatformAnimationRef PlatformCAAnimationWin::platformAnimation() const
213 {
214     return m_animation.get();
215 }
216
217 String PlatformCAAnimationWin::keyPath() const
218 {
219     return CACFAnimationGetKeyPath(m_animation.get());
220 }
221
222 CFTimeInterval PlatformCAAnimationWin::beginTime() const
223 {
224     return CACFAnimationGetBeginTime(m_animation.get());
225 }
226
227 void PlatformCAAnimationWin::setBeginTime(CFTimeInterval value)
228 {
229     CACFAnimationSetBeginTime(m_animation.get(), value);
230 }
231
232 CFTimeInterval PlatformCAAnimationWin::duration() const
233 {
234     return CACFAnimationGetDuration(m_animation.get());
235 }
236
237 void PlatformCAAnimationWin::setDuration(CFTimeInterval value)
238 {
239     CACFAnimationSetDuration(m_animation.get(), value);
240 }
241
242 float PlatformCAAnimationWin::speed() const
243 {
244     return CACFAnimationGetSpeed(m_animation.get());
245 }
246
247 void PlatformCAAnimationWin::setSpeed(float value)
248 {
249     CACFAnimationSetSpeed(m_animation.get(), value);
250 }
251
252 CFTimeInterval PlatformCAAnimationWin::timeOffset() const
253 {
254     return CACFAnimationGetTimeOffset(m_animation.get());
255 }
256
257 void PlatformCAAnimationWin::setTimeOffset(CFTimeInterval value)
258 {
259     CACFAnimationSetTimeOffset(m_animation.get(), value);
260 }
261
262 float PlatformCAAnimationWin::repeatCount() const
263 {
264     return CACFAnimationGetRepeatCount(m_animation.get());
265 }
266
267 void PlatformCAAnimationWin::setRepeatCount(float value)
268 {
269     CACFAnimationSetRepeatCount(m_animation.get(), value);
270 }
271
272 bool PlatformCAAnimationWin::autoreverses() const
273 {
274     return CACFAnimationGetAutoreverses(m_animation.get());
275 }
276
277 void PlatformCAAnimationWin::setAutoreverses(bool value)
278 {
279     CACFAnimationSetAutoreverses(m_animation.get(), value);
280 }
281
282 PlatformCAAnimationWin::FillModeType PlatformCAAnimationWin::fillMode() const
283 {
284     return fromCACFFillModeType(CACFAnimationGetFillMode(m_animation.get()));
285 }
286
287 void PlatformCAAnimationWin::setFillMode(FillModeType value)
288 {
289     CACFAnimationSetFillMode(m_animation.get(), toCACFFillModeType(value));
290 }
291
292 void PlatformCAAnimationWin::setTimingFunction(const TimingFunction* value, bool reverse)
293 {
294     UNUSED_PARAM(reverse);
295     CACFAnimationSetTimingFunction(m_animation.get(), toCACFTimingFunction(value, reverse).get());
296 }
297
298 void PlatformCAAnimationWin::copyTimingFunctionFrom(const PlatformCAAnimation& value)
299 {
300     CACFTimingFunctionRef timingFunc = CACFAnimationGetTimingFunction(downcast<PlatformCAAnimationWin>(value).m_animation.get());
301     if (timingFunc)
302         CACFAnimationSetTimingFunction(m_animation.get(), timingFunc);
303 }
304
305 bool PlatformCAAnimationWin::isRemovedOnCompletion() const
306 {
307     return CACFAnimationIsRemovedOnCompletion(m_animation.get());
308 }
309
310 void PlatformCAAnimationWin::setRemovedOnCompletion(bool value)
311 {
312     CACFAnimationSetRemovedOnCompletion(m_animation.get(), value);
313 }
314
315 bool PlatformCAAnimationWin::isAdditive() const
316 {
317     return CACFAnimationIsAdditive(m_animation.get());
318 }
319
320 void PlatformCAAnimationWin::setAdditive(bool value)
321 {
322     CACFAnimationSetAdditive(m_animation.get(), value);
323 }
324
325 PlatformCAAnimation::ValueFunctionType PlatformCAAnimationWin::valueFunction() const
326 {
327     CACFValueFunctionRef func = CACFAnimationGetValueFunction(m_animation.get());
328     return func ? fromCACFValueFunctionType(CACFValueFunctionGetName(func)) : NoValueFunction;
329 }
330
331 void PlatformCAAnimationWin::setValueFunction(ValueFunctionType value)
332 {
333     CFStringRef valueString = toCACFValueFunctionType(value);
334     CACFAnimationSetValueFunction(m_animation.get(), valueString ? CACFValueFunctionGetFunctionWithName(valueString) : 0);
335 }
336
337 void PlatformCAAnimationWin::setFromValue(float value)
338 {
339     if (animationType() != Basic)
340         return;
341
342     RetainPtr<CFNumberRef> v = adoptCF(CFNumberCreate(0, kCFNumberFloatType, &value));
343     CACFAnimationSetFromValue(m_animation.get(), v.get());
344 }
345
346 void PlatformCAAnimationWin::setFromValue(const WebCore::TransformationMatrix& value)
347 {
348     if (animationType() != Basic)
349         return;
350     
351     RetainPtr<CACFVectorRef> v = adoptCF(CACFVectorCreateTransform(value));
352     CACFAnimationSetFromValue(m_animation.get(), v.get());
353 }
354
355 void PlatformCAAnimationWin::setFromValue(const FloatPoint3D& value)
356 {
357     if (animationType() != Basic)
358         return;
359
360     CGFloat a[3] = { value.x(), value.y(), value.z() };
361     RetainPtr<CACFVectorRef> v = adoptCF(CACFVectorCreate(3, a));
362     CACFAnimationSetFromValue(m_animation.get(), v.get());
363 }
364
365 void PlatformCAAnimationWin::setFromValue(const WebCore::Color& value)
366 {
367     if (animationType() != Basic)
368         return;
369
370     CGFloat a[4] = { value.red(), value.green(), value.blue(), value.alpha() };
371     RetainPtr<CACFVectorRef> v = adoptCF(CACFVectorCreate(4, a));
372     CACFAnimationSetFromValue(m_animation.get(), v.get());
373 }
374
375 void PlatformCAAnimationWin::setFromValue(const FilterOperation*, int)
376 {
377     // FIXME: Hardware filter animation not implemented on Windows
378 }
379
380 void PlatformCAAnimationWin::copyFromValueFrom(const PlatformCAAnimation& value)
381 {
382     if (animationType() != Basic || value.animationType() != Basic)
383         return;
384     
385     CACFAnimationSetFromValue(m_animation.get(), CACFAnimationGetFromValue(downcast<PlatformCAAnimationWin>(value).platformAnimation()));
386 }
387
388 void PlatformCAAnimationWin::setToValue(float value)
389 {
390     if (animationType() != Basic)
391         return;
392
393     RetainPtr<CFNumberRef> v = adoptCF(CFNumberCreate(0, kCFNumberFloatType, &value));
394     CACFAnimationSetToValue(m_animation.get(), v.get());
395 }
396
397 void PlatformCAAnimationWin::setToValue(const WebCore::TransformationMatrix& value)
398 {
399     if (animationType() != Basic)
400         return;
401
402     RetainPtr<CACFVectorRef> v = adoptCF(CACFVectorCreateTransform(value));
403     CACFAnimationSetToValue(m_animation.get(), v.get());
404 }
405
406 void PlatformCAAnimationWin::setToValue(const FloatPoint3D& value)
407 {
408     if (animationType() != Basic)
409         return;
410
411     CGFloat a[3] = { value.x(), value.y(), value.z() };
412     RetainPtr<CACFVectorRef> v = adoptCF(CACFVectorCreate(3, a));
413     CACFAnimationSetToValue(m_animation.get(), v.get());
414 }
415
416 void PlatformCAAnimationWin::setToValue(const WebCore::Color& value)
417 {
418     if (animationType() != Basic)
419         return;
420
421     CGFloat a[4] = { value.red(), value.green(), value.blue(), value.alpha() };
422     RetainPtr<CACFVectorRef> v = adoptCF(CACFVectorCreate(4, a));
423     CACFAnimationSetToValue(m_animation.get(), v.get());
424 }
425
426 void PlatformCAAnimationWin::setToValue(const FilterOperation*, int)
427 {
428     // FIXME: Hardware filter animation not implemented on Windows
429 }
430
431 void PlatformCAAnimationWin::copyToValueFrom(const PlatformCAAnimation& value)
432 {
433     if (animationType() != Basic || value.animationType() != Basic)
434         return;
435         
436     CACFAnimationSetToValue(m_animation.get(), CACFAnimationGetToValue(downcast<PlatformCAAnimationWin>(value).platformAnimation()));
437 }
438
439 // Keyframe-animation properties.
440 void PlatformCAAnimationWin::setValues(const Vector<float>& value)
441 {
442     if (animationType() != Keyframe)
443         return;
444
445     RetainPtr<CFMutableArrayRef> array = adoptCF(CFArrayCreateMutable(0, value.size(), &kCFTypeArrayCallBacks));
446     for (size_t i = 0; i < value.size(); ++i) {
447         RetainPtr<CFNumberRef> v = adoptCF(CFNumberCreate(0, kCFNumberFloatType, &value[i]));
448         CFArrayAppendValue(array.get(), v.get());
449     }
450
451     CACFAnimationSetValues(m_animation.get(), array.get());
452 }
453
454 void PlatformCAAnimationWin::setValues(const Vector<WebCore::TransformationMatrix>& value)
455 {
456     if (animationType() != Keyframe)
457         return;
458
459     RetainPtr<CFMutableArrayRef> array = adoptCF(CFArrayCreateMutable(0, value.size(), &kCFTypeArrayCallBacks));
460     for (size_t i = 0; i < value.size(); ++i) {
461         RetainPtr<CACFVectorRef> v = adoptCF(CACFVectorCreateTransform(value[i]));
462         CFArrayAppendValue(array.get(), v.get());
463     }
464
465     CACFAnimationSetValues(m_animation.get(), array.get());
466 }
467
468 void PlatformCAAnimationWin::setValues(const Vector<FloatPoint3D>& value)
469 {
470     if (animationType() != Keyframe)
471         return;
472         
473     RetainPtr<CFMutableArrayRef> array = adoptCF(CFArrayCreateMutable(0, value.size(), &kCFTypeArrayCallBacks));
474     for (size_t i = 0; i < value.size(); ++i) {
475         CGFloat a[3] = { value[i].x(), value[i].y(), value[i].z() };
476         RetainPtr<CACFVectorRef> v = adoptCF(CACFVectorCreate(3, a));
477         CFArrayAppendValue(array.get(), v.get());
478     }
479
480     CACFAnimationSetValues(m_animation.get(), array.get());
481 }
482
483 void PlatformCAAnimationWin::setValues(const Vector<WebCore::Color>& value)
484 {
485     if (animationType() != Keyframe)
486         return;
487         
488     RetainPtr<CFMutableArrayRef> array = adoptCF(CFArrayCreateMutable(0, value.size(), &kCFTypeArrayCallBacks));
489     for (size_t i = 0; i < value.size(); ++i) {
490         CGFloat a[4] = { value[i].red(), value[i].green(), value[i].blue(), value[i].alpha() };
491         RetainPtr<CACFVectorRef> v = adoptCF(CACFVectorCreate(4, a));
492         CFArrayAppendValue(array.get(), v.get());
493     }
494
495     CACFAnimationSetValues(m_animation.get(), array.get());
496 }
497
498 void PlatformCAAnimationWin::setValues(const Vector<RefPtr<FilterOperation> >&, int)
499 {
500     // FIXME: Hardware filter animation not implemented on Windows
501 }
502
503 void PlatformCAAnimationWin::copyValuesFrom(const PlatformCAAnimation& value)
504 {
505     if (animationType() != Keyframe || value.animationType() != Keyframe)
506         return;
507     
508     CACFAnimationSetValues(m_animation.get(), CACFAnimationGetValues(downcast<PlatformCAAnimationWin>(value).platformAnimation()));
509 }
510
511 void PlatformCAAnimationWin::setKeyTimes(const Vector<float>& value)
512 {
513     if (animationType() != Keyframe)
514         return;
515         
516     RetainPtr<CFMutableArrayRef> array = adoptCF(CFArrayCreateMutable(0, value.size(), &kCFTypeArrayCallBacks));
517     for (size_t i = 0; i < value.size(); ++i) {
518         RetainPtr<CFNumberRef> v = adoptCF(CFNumberCreate(0, kCFNumberFloatType, &value[i]));
519         CFArrayAppendValue(array.get(), v.get());
520     }
521
522     CACFAnimationSetKeyTimes(m_animation.get(), array.get());
523 }
524
525 void PlatformCAAnimationWin::copyKeyTimesFrom(const PlatformCAAnimation& value)
526 {
527     if (animationType() != Keyframe)
528         return;
529
530     CACFAnimationSetKeyTimes(m_animation.get(), CACFAnimationGetKeyTimes(downcast<PlatformCAAnimationWin>(value).platformAnimation()));
531 }
532
533 void PlatformCAAnimationWin::setTimingFunctions(const Vector<const TimingFunction*>& value, bool reverse)
534 {
535     UNUSED_PARAM(reverse);
536     if (animationType() != Keyframe)
537         return;
538
539     RetainPtr<CFMutableArrayRef> array = adoptCF(CFArrayCreateMutable(0, value.size(), &kCFTypeArrayCallBacks));
540     for (size_t i = 0; i < value.size(); ++i) {
541         RetainPtr<CFNumberRef> v = adoptCF(CFNumberCreate(0, kCFNumberFloatType, &value[i]));
542         CFArrayAppendValue(array.get(), toCACFTimingFunction(value[i], reverse).get());
543     }
544
545     CACFAnimationSetTimingFunctions(m_animation.get(), array.get());
546 }
547
548 void PlatformCAAnimationWin::copyTimingFunctionsFrom(const PlatformCAAnimation& value)
549 {
550     CACFAnimationSetTimingFunctions(m_animation.get(), CACFAnimationGetTimingFunctions(downcast<PlatformCAAnimationWin>(value).platformAnimation()));
551 }
552
553 #endif // PLATFORM(WIN)