f0e2da3193c4e89911ff7fcb6db8ea84648790e7
[WebKit-https.git] / Source / WebCore / platform / graphics / transforms / TransformState.cpp
1 /*
2  * Copyright (C) 2011 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 #include "TransformState.h"
28
29 namespace WebCore {
30
31 TransformState& TransformState::operator=(const TransformState& other)
32 {
33     m_accumulatedOffset = other.m_accumulatedOffset;
34     m_mapPoint = other.m_mapPoint;
35     m_mapQuad = other.m_mapQuad;
36     if (m_mapPoint)
37         m_lastPlanarPoint = other.m_lastPlanarPoint;
38     if (m_mapQuad) {
39         m_lastPlanarQuad = other.m_lastPlanarQuad;
40         if (other.m_lastPlanarSecondaryQuad)
41             m_lastPlanarSecondaryQuad = std::make_unique<FloatQuad>(*other.m_lastPlanarSecondaryQuad);
42         else
43             m_lastPlanarSecondaryQuad = nullptr;
44     }
45     m_accumulatingTransform = other.m_accumulatingTransform;
46     m_direction = other.m_direction;
47     
48     m_accumulatedTransform = nullptr;
49
50     if (other.m_accumulatedTransform)
51         m_accumulatedTransform = std::make_unique<TransformationMatrix>(*other.m_accumulatedTransform);
52         
53     return *this;
54 }
55
56 void TransformState::translateTransform(const LayoutSize& offset)
57 {
58     if (m_direction == ApplyTransformDirection)
59         m_accumulatedTransform->translateRight(offset.width(), offset.height());
60     else
61         m_accumulatedTransform->translate(offset.width(), offset.height());
62 }
63
64 void TransformState::translateMappedCoordinates(const LayoutSize& offset)
65 {
66     LayoutSize adjustedOffset = (m_direction == ApplyTransformDirection) ? offset : -offset;
67     if (m_mapPoint)
68         m_lastPlanarPoint.move(adjustedOffset);
69     if (m_mapQuad) {
70         m_lastPlanarQuad.move(adjustedOffset);
71         if (m_lastPlanarSecondaryQuad)
72             m_lastPlanarSecondaryQuad->move(adjustedOffset);
73     }
74 }
75
76 void TransformState::move(const LayoutSize& offset, TransformAccumulation accumulate)
77 {
78     if (accumulate == FlattenTransform && !m_accumulatedTransform)
79         m_accumulatedOffset += offset;
80     else {
81         applyAccumulatedOffset();
82         if (m_accumulatingTransform && m_accumulatedTransform) {
83             // If we're accumulating into an existing transform, apply the translation.
84             translateTransform(offset);
85
86             // Then flatten if necessary.
87             if (accumulate == FlattenTransform)
88                 flatten();
89         } else
90             // Just move the point and/or quad.
91             translateMappedCoordinates(offset);
92     }
93     m_accumulatingTransform = accumulate == AccumulateTransform;
94 }
95
96 void TransformState::applyAccumulatedOffset()
97 {
98     LayoutSize offset = m_accumulatedOffset;
99     m_accumulatedOffset = LayoutSize();
100     if (!offset.isZero()) {
101         if (m_accumulatedTransform) {
102             translateTransform(offset);
103             flatten();
104         } else
105             translateMappedCoordinates(offset);
106     }
107 }
108
109 // FIXME: We transform AffineTransform to TransformationMatrix. This is rather inefficient.
110 void TransformState::applyTransform(const AffineTransform& transformFromContainer, TransformAccumulation accumulate, bool* wasClamped)
111 {
112     applyTransform(transformFromContainer.toTransformationMatrix(), accumulate, wasClamped);
113 }
114
115 void TransformState::applyTransform(const TransformationMatrix& transformFromContainer, TransformAccumulation accumulate, bool* wasClamped)
116 {
117     if (wasClamped)
118         *wasClamped = false;
119
120     if (transformFromContainer.isIntegerTranslation()) {
121         move(LayoutSize(transformFromContainer.e(), transformFromContainer.f()), accumulate);
122         return;
123     }
124
125     applyAccumulatedOffset();
126
127     // If we have an accumulated transform from last time, multiply in this transform
128     if (m_accumulatedTransform) {
129         if (m_direction == ApplyTransformDirection)
130             m_accumulatedTransform = std::make_unique<TransformationMatrix>(transformFromContainer * *m_accumulatedTransform);
131         else
132             m_accumulatedTransform->multiply(transformFromContainer);
133     } else if (accumulate == AccumulateTransform) {
134         // Make one if we started to accumulate
135         m_accumulatedTransform = std::make_unique<TransformationMatrix>(transformFromContainer);
136     }
137     
138     if (accumulate == FlattenTransform) {
139         const TransformationMatrix* finalTransform = m_accumulatedTransform ? m_accumulatedTransform.get() : &transformFromContainer;
140         flattenWithTransform(*finalTransform, wasClamped);
141     }
142     m_accumulatingTransform = accumulate == AccumulateTransform;
143 }
144
145 void TransformState::flatten(bool* wasClamped)
146 {
147     if (wasClamped)
148         *wasClamped = false;
149
150     applyAccumulatedOffset();
151
152     if (!m_accumulatedTransform) {
153         m_accumulatingTransform = false;
154         return;
155     }
156     
157     flattenWithTransform(*m_accumulatedTransform, wasClamped);
158 }
159
160 FloatPoint TransformState::mappedPoint(bool* wasClamped) const
161 {
162     if (wasClamped)
163         *wasClamped = false;
164
165     FloatPoint point = m_lastPlanarPoint;
166     point.move((m_direction == ApplyTransformDirection) ? m_accumulatedOffset : -m_accumulatedOffset);
167     if (!m_accumulatedTransform)
168         return point;
169
170     if (m_direction == ApplyTransformDirection)
171         return m_accumulatedTransform->mapPoint(point);
172
173     return m_accumulatedTransform->inverse().value_or(TransformationMatrix()).projectPoint(point, wasClamped);
174 }
175
176 FloatQuad TransformState::mappedQuad(bool* wasClamped) const
177 {
178     if (wasClamped)
179         *wasClamped = false;
180
181     FloatQuad quad = m_lastPlanarQuad;
182     mapQuad(quad, m_direction, wasClamped);
183     return quad;
184 }
185
186 Optional<FloatQuad> TransformState::mappedSecondaryQuad(bool* wasClamped) const
187 {
188     if (wasClamped)
189         *wasClamped = false;
190
191     if (!m_lastPlanarSecondaryQuad)
192         return Optional<FloatQuad>();
193
194     FloatQuad quad = *m_lastPlanarSecondaryQuad;
195     mapQuad(quad, m_direction, wasClamped);
196     return quad;
197 }
198
199 void TransformState::setLastPlanarSecondaryQuad(const FloatQuad* quad)
200 {
201     if (!quad) {
202         m_lastPlanarSecondaryQuad = nullptr;
203         return;
204     }
205     
206     // Map the quad back through any transform or offset back into the last flattening coordinate space.
207     FloatQuad backMappedQuad(*quad);
208     mapQuad(backMappedQuad, inverseDirection());
209     m_lastPlanarSecondaryQuad = std::make_unique<FloatQuad>(backMappedQuad);
210 }
211
212 void TransformState::mapQuad(FloatQuad& quad, TransformDirection direction, bool* wasClamped) const
213 {
214     quad.move((direction == ApplyTransformDirection) ? m_accumulatedOffset : -m_accumulatedOffset);
215     if (!m_accumulatedTransform)
216         return;
217
218     if (direction == ApplyTransformDirection)
219         quad = m_accumulatedTransform->mapQuad(quad);
220
221     quad = m_accumulatedTransform->inverse().value_or(TransformationMatrix()).projectQuad(quad, wasClamped);
222 }
223
224 void TransformState::flattenWithTransform(const TransformationMatrix& t, bool* wasClamped)
225 {
226     if (m_direction == ApplyTransformDirection) {
227         if (m_mapPoint)
228             m_lastPlanarPoint = t.mapPoint(m_lastPlanarPoint);
229         if (m_mapQuad) {
230             m_lastPlanarQuad = t.mapQuad(m_lastPlanarQuad);
231             if (m_lastPlanarSecondaryQuad)
232                 *m_lastPlanarSecondaryQuad = t.mapQuad(*m_lastPlanarSecondaryQuad);
233         }
234
235     } else {
236         TransformationMatrix inverseTransform = t.inverse().value_or(TransformationMatrix());
237         if (m_mapPoint)
238             m_lastPlanarPoint = inverseTransform.projectPoint(m_lastPlanarPoint);
239         if (m_mapQuad) {
240             m_lastPlanarQuad = inverseTransform.projectQuad(m_lastPlanarQuad, wasClamped);
241             if (m_lastPlanarSecondaryQuad)
242                 *m_lastPlanarSecondaryQuad = inverseTransform.projectQuad(*m_lastPlanarSecondaryQuad, wasClamped);
243         }
244     }
245
246     // We could throw away m_accumulatedTransform if we wanted to here, but that
247     // would cause thrash when traversing hierarchies with alternating
248     // preserve-3d and flat elements.
249     if (m_accumulatedTransform)
250         m_accumulatedTransform->makeIdentity();
251     m_accumulatingTransform = false;
252 }
253
254 } // namespace WebCore