1 /*
2  * Copyright (C) 2009 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 #ifndef TransformState_h
27 #define TransformState_h
28 
29 #include "AffineTransform.h"
30 #include "FloatPoint.h"
31 #include "FloatQuad.h"
32 #include "IntSize.h"
33 #include "TransformationMatrix.h"
34 #include <wtf/PassRefPtr.h>
35 #include <wtf/OwnPtr.h>
36 #include <wtf/RefCounted.h>
37 
38 namespace WebCore {
39 
40 class TransformState {
41     WTF_MAKE_NONCOPYABLE(TransformState);
42 public:
43     enum TransformDirection { ApplyTransformDirection, UnapplyInverseTransformDirection };
44     enum TransformAccumulation { FlattenTransform, AccumulateTransform };
45 
46     // If quad is non-null, it will be mapped
47     TransformState(TransformDirection mappingDirection, const FloatPoint& p, const FloatQuad* quad = 0)
m_lastPlanarPoint(p)48         : m_lastPlanarPoint(p)
49         , m_accumulatingTransform(false)
50         , m_mapQuad(quad != 0)
51         , m_direction(mappingDirection)
52     {
53         if (quad)
54             m_lastPlanarQuad = *quad;
55     }
56 
57     void move(const IntSize& s, TransformAccumulation accumulate = FlattenTransform)
58     {
59         move(s.width(), s.height(), accumulate);
60     }
61 
62     void move(int x, int y, TransformAccumulation = FlattenTransform);
63     void applyTransform(const AffineTransform& transformFromContainer, TransformAccumulation = FlattenTransform);
64     void applyTransform(const TransformationMatrix& transformFromContainer, TransformAccumulation = FlattenTransform);
65     void flatten();
66 
67     // Return the coords of the point or quad in the last flattened layer
lastPlanarPoint()68     FloatPoint lastPlanarPoint() const { return m_lastPlanarPoint; }
lastPlanarQuad()69     FloatQuad lastPlanarQuad() const { return m_lastPlanarQuad; }
70 
71     // Return the point or quad mapped through the current transform
72     FloatPoint mappedPoint() const;
73     FloatQuad mappedQuad() const;
74 
75 private:
76     void flattenWithTransform(const TransformationMatrix&);
77 
78     FloatPoint m_lastPlanarPoint;
79     FloatQuad m_lastPlanarQuad;
80 
81     // We only allocate the transform if we need to
82     OwnPtr<TransformationMatrix> m_accumulatedTransform;
83     bool m_accumulatingTransform;
84     bool m_mapQuad;
85     TransformDirection m_direction;
86 };
87 
88 class HitTestingTransformState : public RefCounted<HitTestingTransformState> {
89 public:
create(const FloatPoint & p,const FloatQuad & quad)90     static PassRefPtr<HitTestingTransformState> create(const FloatPoint& p, const FloatQuad& quad)
91     {
92         return adoptRef(new HitTestingTransformState(p, quad));
93     }
94 
create(const HitTestingTransformState & other)95     static PassRefPtr<HitTestingTransformState> create(const HitTestingTransformState& other)
96     {
97         return adoptRef(new HitTestingTransformState(other));
98     }
99 
100     enum TransformAccumulation { FlattenTransform, AccumulateTransform };
101     void translate(int x, int y, TransformAccumulation);
102     void applyTransform(const TransformationMatrix& transformFromContainer, TransformAccumulation);
103 
104     FloatPoint mappedPoint() const;
105     FloatQuad mappedQuad() const;
106     void flatten();
107 
108     FloatPoint m_lastPlanarPoint;
109     FloatQuad m_lastPlanarQuad;
110     TransformationMatrix m_accumulatedTransform;
111     bool m_accumulatingTransform;
112 
113 private:
HitTestingTransformState(const FloatPoint & p,const FloatQuad & quad)114     HitTestingTransformState(const FloatPoint& p, const FloatQuad& quad)
115         : m_lastPlanarPoint(p)
116         , m_lastPlanarQuad(quad)
117         , m_accumulatingTransform(false)
118     {
119     }
120 
HitTestingTransformState(const HitTestingTransformState & other)121     HitTestingTransformState(const HitTestingTransformState& other)
122         : RefCounted<HitTestingTransformState>()
123         , m_lastPlanarPoint(other.m_lastPlanarPoint)
124         , m_lastPlanarQuad(other.m_lastPlanarQuad)
125         , m_accumulatedTransform(other.m_accumulatedTransform)
126         , m_accumulatingTransform(other.m_accumulatingTransform)
127     {
128     }
129 
130     void flattenWithTransform(const TransformationMatrix&);
131 };
132 
133 } // namespace WebCore
134 
135 #endif // TransformState_h
136