initial import
[vuplus_webkit] / 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 COMPUTER, 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 COMPUTER, 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 #include <wtf/PassOwnPtr.h>
30
31 namespace WebCore {
32
33 TransformState& TransformState::operator=(const TransformState& other)
34 {
35     m_mapPoint = other.m_mapPoint;
36     m_mapQuad = other.m_mapQuad;
37     if (m_mapPoint)
38         m_lastPlanarPoint = other.m_lastPlanarPoint;
39     if (m_mapQuad)
40         m_lastPlanarQuad = other.m_lastPlanarQuad;
41     m_accumulatingTransform = other.m_accumulatingTransform;
42     m_direction = other.m_direction;
43     
44     m_accumulatedTransform.clear();
45
46     if (other.m_accumulatedTransform)
47         m_accumulatedTransform = adoptPtr(new TransformationMatrix(*other.m_accumulatedTransform));
48         
49     return *this;
50 }
51
52 void TransformState::move(int x, int y, TransformAccumulation accumulate)
53 {
54     if (m_accumulatingTransform && m_accumulatedTransform) {
55         // If we're accumulating into an existing transform, apply the translation.
56         if (m_direction == ApplyTransformDirection)
57             m_accumulatedTransform->translateRight(x, y);
58         else
59             m_accumulatedTransform->translate(x, y);
60         
61         // Then flatten if necessary.
62         if (accumulate == FlattenTransform)
63             flatten();
64     } else {
65         // Just move the point and, optionally, the quad.
66         if (m_direction == UnapplyInverseTransformDirection) {
67             x = -x;
68             y = -y;
69         }
70         if (m_mapPoint)
71             m_lastPlanarPoint.move(x, y);
72         if (m_mapQuad)
73             m_lastPlanarQuad.move(x, y);
74     }
75     m_accumulatingTransform = accumulate == AccumulateTransform;
76 }
77
78 // FIXME: We transform AffineTransform to TransformationMatrix. This is rather inefficient.
79 void TransformState::applyTransform(const AffineTransform& transformFromContainer, TransformAccumulation accumulate)
80 {
81     applyTransform(transformFromContainer.toTransformationMatrix(), accumulate);
82 }
83
84 void TransformState::applyTransform(const TransformationMatrix& transformFromContainer, TransformAccumulation accumulate)
85 {
86     // If we have an accumulated transform from last time, multiply in this transform
87     if (m_accumulatedTransform) {
88         if (m_direction == ApplyTransformDirection)
89             m_accumulatedTransform = adoptPtr(new TransformationMatrix(transformFromContainer * *m_accumulatedTransform));
90         else
91             m_accumulatedTransform->multiply(transformFromContainer);
92     } else if (accumulate == AccumulateTransform) {
93         // Make one if we started to accumulate
94         m_accumulatedTransform = adoptPtr(new TransformationMatrix(transformFromContainer));
95     }
96     
97     if (accumulate == FlattenTransform) {
98         const TransformationMatrix* finalTransform = m_accumulatedTransform ? m_accumulatedTransform.get() : &transformFromContainer;
99         flattenWithTransform(*finalTransform);
100     }
101     m_accumulatingTransform = accumulate == AccumulateTransform;
102 }
103
104 void TransformState::flatten()
105 {
106     if (!m_accumulatedTransform) {
107         m_accumulatingTransform = false;
108         return;
109     }
110     
111     flattenWithTransform(*m_accumulatedTransform);
112 }
113
114 FloatPoint TransformState::mappedPoint() const
115 {
116     if (!m_accumulatedTransform)
117         return m_lastPlanarPoint;
118
119     if (m_direction == ApplyTransformDirection)
120         return m_accumulatedTransform->mapPoint(m_lastPlanarPoint);
121
122     return m_accumulatedTransform->inverse().projectPoint(m_lastPlanarPoint);
123 }
124
125 FloatQuad TransformState::mappedQuad() const
126 {
127     if (!m_accumulatedTransform)
128         return m_lastPlanarQuad;
129
130     if (m_direction == ApplyTransformDirection)
131         return m_accumulatedTransform->mapQuad(m_lastPlanarQuad);
132
133     return m_accumulatedTransform->inverse().projectQuad(m_lastPlanarQuad);
134 }
135
136 void TransformState::flattenWithTransform(const TransformationMatrix& t)
137 {
138     if (m_direction == ApplyTransformDirection) {
139         if (m_mapPoint)
140             m_lastPlanarPoint = t.mapPoint(m_lastPlanarPoint);
141         if (m_mapQuad)
142             m_lastPlanarQuad = t.mapQuad(m_lastPlanarQuad);
143     } else {
144         TransformationMatrix inverseTransform = t.inverse();
145         if (m_mapPoint)
146             m_lastPlanarPoint = inverseTransform.projectPoint(m_lastPlanarPoint);
147         if (m_mapQuad)
148             m_lastPlanarQuad = inverseTransform.projectQuad(m_lastPlanarQuad);
149     }
150
151     // We could throw away m_accumulatedTransform if we wanted to here, but that
152     // would cause thrash when traversing hierarchies with alternating
153     // preserve-3d and flat elements.
154     if (m_accumulatedTransform)
155         m_accumulatedTransform->makeIdentity();
156     m_accumulatingTransform = false;
157 }
158
159 } // namespace WebCore