1 /*
2  * Copyright (C) 2012 Google 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. AND ITS CONTRIBUTORS ``AS IS'' AND
14  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16  * DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR
17  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
19  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
20  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
21  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
22  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23  */
24 
25 #include "third_party/blink/renderer/platform/animation/animation_translation_util.h"
26 
27 #include "third_party/blink/renderer/platform/animation/compositor_transform_operations.h"
28 #include "third_party/blink/renderer/platform/transforms/interpolated_transform_operation.h"
29 #include "third_party/blink/renderer/platform/transforms/matrix_3d_transform_operation.h"
30 #include "third_party/blink/renderer/platform/transforms/matrix_transform_operation.h"
31 #include "third_party/blink/renderer/platform/transforms/perspective_transform_operation.h"
32 #include "third_party/blink/renderer/platform/transforms/rotate_transform_operation.h"
33 #include "third_party/blink/renderer/platform/transforms/scale_transform_operation.h"
34 #include "third_party/blink/renderer/platform/transforms/skew_transform_operation.h"
35 #include "third_party/blink/renderer/platform/transforms/transform_operations.h"
36 #include "third_party/blink/renderer/platform/transforms/transformation_matrix.h"
37 #include "third_party/blink/renderer/platform/transforms/translate_transform_operation.h"
38 
39 namespace blink {
40 
ToCompositorTransformOperations(const TransformOperations & transform_operations,CompositorTransformOperations * out_transform_operations)41 void ToCompositorTransformOperations(
42     const TransformOperations& transform_operations,
43     CompositorTransformOperations* out_transform_operations) {
44   // We need to do a deep copy the transformOperations may contain ref pointers
45   // to TransformOperation objects.
46   for (const auto& operation : transform_operations.Operations()) {
47     switch (operation->GetType()) {
48       case TransformOperation::kScaleX:
49       case TransformOperation::kScaleY:
50       case TransformOperation::kScaleZ:
51       case TransformOperation::kScale3D:
52       case TransformOperation::kScale: {
53         auto* transform =
54             static_cast<const ScaleTransformOperation*>(operation.get());
55         out_transform_operations->AppendScale(transform->X(), transform->Y(),
56                                               transform->Z());
57         break;
58       }
59       case TransformOperation::kTranslateX:
60       case TransformOperation::kTranslateY:
61       case TransformOperation::kTranslateZ:
62       case TransformOperation::kTranslate3D:
63       case TransformOperation::kTranslate: {
64         auto* transform =
65             static_cast<const TranslateTransformOperation*>(operation.get());
66         DCHECK(transform->X().IsFixed() && transform->Y().IsFixed());
67         out_transform_operations->AppendTranslate(
68             transform->X().Value(), transform->Y().Value(), transform->Z());
69         break;
70       }
71       case TransformOperation::kRotateX:
72       case TransformOperation::kRotateY:
73       case TransformOperation::kRotate3D:
74       case TransformOperation::kRotate: {
75         auto* transform =
76             static_cast<const RotateTransformOperation*>(operation.get());
77         out_transform_operations->AppendRotate(
78             transform->X(), transform->Y(), transform->Z(), transform->Angle());
79         break;
80       }
81       case TransformOperation::kSkewX:
82       case TransformOperation::kSkewY:
83       case TransformOperation::kSkew: {
84         auto* transform =
85             static_cast<const SkewTransformOperation*>(operation.get());
86         out_transform_operations->AppendSkew(transform->AngleX(),
87                                              transform->AngleY());
88         break;
89       }
90       case TransformOperation::kMatrix: {
91         auto* transform =
92             static_cast<const MatrixTransformOperation*>(operation.get());
93         TransformationMatrix m = transform->Matrix();
94         out_transform_operations->AppendMatrix(
95             TransformationMatrix::ToSkMatrix44(m));
96         break;
97       }
98       case TransformOperation::kMatrix3D: {
99         auto* transform =
100             static_cast<const Matrix3DTransformOperation*>(operation.get());
101         TransformationMatrix m = transform->Matrix();
102         out_transform_operations->AppendMatrix(
103             TransformationMatrix::ToSkMatrix44(m));
104         break;
105       }
106       case TransformOperation::kPerspective: {
107         auto* transform =
108             static_cast<const PerspectiveTransformOperation*>(operation.get());
109         out_transform_operations->AppendPerspective(transform->Perspective());
110         break;
111       }
112       case TransformOperation::kRotateAroundOrigin:
113       case TransformOperation::kInterpolated: {
114         TransformationMatrix m;
115         operation->Apply(m, FloatSize());
116         out_transform_operations->AppendMatrix(
117             TransformationMatrix::ToSkMatrix44(m));
118         break;
119       }
120       case TransformOperation::kIdentity:
121         out_transform_operations->AppendIdentity();
122         break;
123       default:
124         NOTREACHED();
125         break;
126     }  // switch
127   }    // for each operation
128 }
129 
130 }  // namespace blink
131