OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef GeometryMapper_h | 5 #ifndef GeometryMapper_h |
6 #define GeometryMapper_h | 6 #define GeometryMapper_h |
7 | 7 |
8 #include "platform/graphics/paint/FloatClipRect.h" | 8 #include "platform/graphics/paint/FloatClipRect.h" |
9 #include "platform/graphics/paint/PropertyTreeState.h" | 9 #include "platform/graphics/paint/PropertyTreeState.h" |
10 #include "platform/transforms/TransformationMatrix.h" | 10 #include "platform/transforms/TransformationMatrix.h" |
11 #include "wtf/HashMap.h" | 11 #include "wtf/HashMap.h" |
12 | 12 |
13 namespace blink { | 13 namespace blink { |
14 | 14 |
15 // GeometryMapper is a helper class for fast computations of transformed and | 15 // GeometryMapper is a helper class for fast computations of transformed and |
16 // visual rects in different PropertyTreeStates. The design document has a | 16 // visual rects in different PropertyTreeStates. The design document has a |
17 // number of details on use cases, algorithmic definitions, and running times. | 17 // number of details on use cases, algorithmic definitions, and running times. |
18 // | 18 // |
19 // NOTE: A GeometryMapper object is only valid for property trees that do not | 19 // NOTE: A GeometryMapper object is only valid for property trees that do not |
20 // change. If any mutation occurs, a new GeometryMapper object must be allocated | 20 // change. If any mutation occurs, a new GeometryMapper object must be allocated |
21 // corresponding to the new state. | 21 // corresponding to the new state. |
22 // | 22 // |
23 // ** WARNING** Callers to the methods below may not assume that any const | |
24 // references returned remain const across multiple calls into GeometryMapper. | |
25 // If needed, callers must store local copies of the return values. | |
26 // | |
27 // Design document: http://bit.ly/28P4FDA | 23 // Design document: http://bit.ly/28P4FDA |
28 // | 24 // |
29 // TODO(chrishtr): take effect tree into account. | 25 // TODO(chrishtr): take effect tree into account. |
30 class PLATFORM_EXPORT GeometryMapper { | 26 class PLATFORM_EXPORT GeometryMapper { |
31 public: | 27 public: |
32 static std::unique_ptr<GeometryMapper> create() { | 28 static std::unique_ptr<GeometryMapper> create() { |
33 return WTF::wrapUnique(new GeometryMapper()); | 29 return WTF::wrapUnique(new GeometryMapper()); |
34 } | 30 } |
35 | 31 |
36 // The runtime of m calls among localToAncestorVisualRect, localToAncestorRect | 32 // The runtime of m calls among localToAncestorVisualRect, localToAncestorRect |
37 // or ancestorToLocalRect with the same |ancestorState| parameter is | 33 // or ancestorToLocalRect with the same |ancestorState| parameter is |
38 // guaranteed to be O(n + m), where n is the number of transform and clip | 34 // guaranteed to be O(n + m), where n is the number of transform and clip |
39 // nodes in their respective property trees. | 35 // nodes in their respective property trees. |
40 | 36 |
41 // If the clips and transforms of |sourceState| are equal to or descendants of | 37 // If the clips and transforms of |sourceState| are equal to or descendants of |
42 // those of |destinationState|, returns the same value as | 38 // those of |destinationState|, returns the same value as |
43 // localToAncestorVisualRect. Otherwise, maps the input rect to the | 39 // localToAncestorVisualRect. Otherwise, maps the input rect to the |
44 // transform state which is the lowest common ancestor of | 40 // transform state which is the lowest common ancestor of |
45 // |sourceState.transform| and |destinationState.transform|, then multiplies | 41 // |sourceState.transform| and |destinationState.transform|, then multiplies |
46 // it by the the inverse transform mapping from the lowest common ancestor to | 42 // it by the the inverse transform mapping from the lowest common ancestor to |
47 // |destinationState.transform|. | 43 // |destinationState.transform|. |
48 // | 44 // |
49 // DCHECK fails if the clip of |destinationState| is not an ancestor of the | 45 // DCHECK fails if the clip of |destinationState| is not an ancestor of the |
50 // clip of |sourceState|, or the inverse transform is not invertible. | 46 // clip of |sourceState|, or the inverse transform is not invertible. |
51 // | 47 FloatClipRect sourceToDestinationVisualRect( |
52 // |mappingRect| is both input and output. | 48 const FloatRect&, |
53 void sourceToDestinationVisualRect(const PropertyTreeState& sourceState, | 49 const PropertyTreeState& sourceState, |
54 const PropertyTreeState& destinationState, | 50 const PropertyTreeState& destinationState); |
55 FloatRect& mappingRect); | |
56 | 51 |
57 // Same as sourceToDestinationVisualRect() except that only transforms are | 52 // Same as sourceToDestinationVisualRect() except that only transforms are |
58 // applied. | 53 // applied. |
59 // | 54 FloatRect sourceToDestinationRect( |
60 // |mappingRect| is both input and output. | 55 const FloatRect&, |
61 void sourceToDestinationRect( | |
62 const TransformPaintPropertyNode* sourceTransformNode, | 56 const TransformPaintPropertyNode* sourceTransformNode, |
63 const TransformPaintPropertyNode* destinationTransformNode, | 57 const TransformPaintPropertyNode* destinationTransformNode); |
64 FloatRect& mappingRect); | |
65 | 58 |
66 // Maps from a rect in |localTransformSpace| to its visual rect in | 59 // Maps from a rect in |localTransformSpace| to its visual rect in |
67 // |ancestorState|. This is computed by multiplying the rect by its combined | 60 // |ancestorState|. This is computed by multiplying the rect by its combined |
68 // transform between |localTransformSpace| and |ancestorSpace|, then | 61 // transform between |localTransformSpace| and |ancestorSpace|, then |
69 // flattening into 2D space, then intersecting by the "clip visual rect" for | 62 // flattening into 2D space, then intersecting by the "clip visual rect" for |
70 // |localTransformState|'s clips. See above for the definition of "clip visual | 63 // |localTransformState|'s clips. See above for the definition of "clip visual |
71 // rect". | 64 // rect". |
72 // | 65 // |
73 // Note that the clip of |ancestorState| is *not* applied. | 66 // Note that the clip of |ancestorState| is *not* applied. |
74 // | 67 // |
75 // DCHECK fails if any of the paint property tree nodes in | 68 // DCHECK fails if any of the paint property tree nodes in |
76 // |localTransformState| are not equal to or a descendant of that in | 69 // |localTransformState| are not equal to or a descendant of that in |
77 // |ancestorState|. | 70 // |ancestorState|. |
78 // | 71 FloatClipRect localToAncestorVisualRect( |
79 // |mappingRect| is both input and output. | 72 const FloatRect&, |
80 void localToAncestorVisualRect(const PropertyTreeState& localTransformState, | 73 const PropertyTreeState& localTransformState, |
81 const PropertyTreeState& ancestorState, | 74 const PropertyTreeState& ancestorState); |
82 FloatRect& mappingRect); | |
83 | 75 |
84 // Maps from a rect in |localTransformNode| space to its transformed rect in | 76 // Maps from a rect in |localTransformNode| space to its transformed rect in |
85 // |ancestorTransformNode| space. This is computed by multiplying the rect by | 77 // |ancestorTransformNode| space. This is computed by multiplying the rect by |
86 // the combined transform between |localTransformNode| and | 78 // the combined transform between |localTransformNode| and |
87 // |ancestorTransformNode|, then flattening into 2D space. | 79 // |ancestorTransformNode|, then flattening into 2D space. |
88 // | 80 // |
89 // DCHECK fails if |localTransformNode| is not equal to or a descendant of | 81 // DCHECK fails if |localTransformNode| is not equal to or a descendant of |
90 // |ancestorTransformNode|. | 82 // |ancestorTransformNode|. |
91 // | 83 FloatRect localToAncestorRect( |
92 //|mappingRect| is both input and output. | 84 const FloatRect&, |
93 void localToAncestorRect( | |
94 const TransformPaintPropertyNode* localTransformNode, | 85 const TransformPaintPropertyNode* localTransformNode, |
95 const TransformPaintPropertyNode* ancestorTransformNode, | 86 const TransformPaintPropertyNode* ancestorTransformNode); |
96 FloatRect& mappingRect); | |
97 | 87 |
98 // Maps from a rect in |ancestorTransformNode| space to its transformed rect | 88 // Maps from a rect in |ancestorTransformNode| space to its transformed rect |
99 // in |localTransformNode| space. This is computed by multiplying the rect by | 89 // in |localTransformNode| space. This is computed by multiplying the rect by |
100 // the inverse combined transform between |localTransformNode| and | 90 // the inverse combined transform between |localTransformNode| and |
101 // |ancestorTransformNode|, if the transform is invertible. | 91 // |ancestorTransformNode|, if the transform is invertible. |
102 // | 92 // |
103 // DCHECK fails if the combined transform is not invertible, or | 93 // DCHECK fails if the combined transform is not invertible, or |
104 // |localTransformNode| is not equal to or a descendant of | 94 // |localTransformNode| is not equal to or a descendant of |
105 // |ancestorTransformNode|. | 95 // |ancestorTransformNode|. |
106 // | 96 FloatRect ancestorToLocalRect( |
107 // |mappingRect| is both input and output. | 97 const FloatRect&, |
108 void ancestorToLocalRect( | |
109 const TransformPaintPropertyNode* ancestorTransformNode, | 98 const TransformPaintPropertyNode* ancestorTransformNode, |
110 const TransformPaintPropertyNode* localTransformNode, | 99 const TransformPaintPropertyNode* localTransformNode); |
111 FloatRect& mappingRect); | |
112 | 100 |
113 // Returns the matrix used in |LocalToAncestorRect|. DCHECK fails iff | 101 // Returns the matrix used in |LocalToAncestorRect|. DCHECK fails iff |
114 // |localTransformNode| is not equal to or a descendant of | 102 // |localTransformNode| is not equal to or a descendant of |
115 // |ancestorTransformNode|. | 103 // |ancestorTransformNode|. |
116 const TransformationMatrix& localToAncestorMatrix( | 104 const TransformationMatrix& localToAncestorMatrix( |
117 const TransformPaintPropertyNode* localTransformNode, | 105 const TransformPaintPropertyNode* localTransformNode, |
118 const TransformPaintPropertyNode* ancestorTransformNode); | 106 const TransformPaintPropertyNode* ancestorTransformNode); |
119 | 107 |
120 // Returns the "clip visual rect" between |localTransformState| and | 108 // Returns the "clip visual rect" between |localTransformState| and |
121 // |ancestorState|. See above for the definition of "clip visual rect". | 109 // |ancestorState|. See above for the definition of "clip visual rect". |
122 FloatClipRect localToAncestorClipRect( | 110 FloatClipRect localToAncestorClipRect( |
123 const PropertyTreeState& localTransformState, | 111 const PropertyTreeState& localTransformState, |
124 const PropertyTreeState& ancestorState); | 112 const PropertyTreeState& ancestorState); |
125 | 113 |
126 // Like localToAncestorClipRect, except it can handle destination transform | 114 // Like localToAncestorClipRect, except it can handle destination transform |
127 // spaces which are not direct ancestors of the source transform space. | 115 // spaces which are not direct ancestors of the source transform space. |
128 const FloatClipRect& sourceToDestinationClipRect( | 116 FloatClipRect sourceToDestinationClipRect( |
129 const PropertyTreeState& sourceState, | 117 const PropertyTreeState& sourceState, |
130 const PropertyTreeState& destinationState); | 118 const PropertyTreeState& destinationState); |
131 | 119 |
132 // Returns the lowest common ancestor in the paint property tree. | 120 // Returns the lowest common ancestor in the paint property tree. |
133 template <typename NodeType> | 121 template <typename NodeType> |
134 static PLATFORM_EXPORT const NodeType* lowestCommonAncestor(const NodeType*, | 122 static PLATFORM_EXPORT const NodeType* lowestCommonAncestor(const NodeType*, |
135 const NodeType*); | 123 const NodeType*); |
136 | 124 |
137 void clearCache(); | 125 void clearCache(); |
138 | 126 |
139 protected: | 127 protected: |
140 GeometryMapper() {} | 128 GeometryMapper() {} |
141 | 129 |
142 private: | 130 private: |
143 // The internal methods do the same things as their public counterparts, but | 131 // The internal methods do the same things as their public counterparts, but |
144 // take an extra |success| parameter which indicates if the function is | 132 // take an extra |success| parameter which indicates if the function is |
145 // successful on return. See comments of the public functions for failure | 133 // successful on return. See comments of the public functions for failure |
146 // conditions. | 134 // conditions. |
147 | 135 |
148 void sourceToDestinationVisualRectInternal( | 136 FloatClipRect sourceToDestinationVisualRectInternal( |
| 137 const FloatRect&, |
149 const PropertyTreeState& sourceState, | 138 const PropertyTreeState& sourceState, |
150 const PropertyTreeState& destinationState, | 139 const PropertyTreeState& destinationState, |
151 FloatRect& mappingRect, | |
152 bool& success); | 140 bool& success); |
153 | 141 |
154 void localToAncestorVisualRectInternal( | 142 FloatClipRect localToAncestorVisualRectInternal( |
| 143 const FloatRect&, |
155 const PropertyTreeState& localTransformState, | 144 const PropertyTreeState& localTransformState, |
156 const PropertyTreeState& ancestorState, | 145 const PropertyTreeState& ancestorState, |
157 FloatRect& mappingRect, | |
158 bool& success); | 146 bool& success); |
159 | 147 |
160 void localToAncestorRectInternal( | 148 FloatRect localToAncestorRectInternal( |
| 149 const FloatRect&, |
161 const TransformPaintPropertyNode* localTransformNode, | 150 const TransformPaintPropertyNode* localTransformNode, |
162 const TransformPaintPropertyNode* ancestorTransformNode, | 151 const TransformPaintPropertyNode* ancestorTransformNode, |
163 FloatRect&, | |
164 bool& success); | 152 bool& success); |
165 | 153 |
166 const TransformationMatrix& localToAncestorMatrixInternal( | 154 const TransformationMatrix& localToAncestorMatrixInternal( |
167 const TransformPaintPropertyNode* localTransformNode, | 155 const TransformPaintPropertyNode* localTransformNode, |
168 const TransformPaintPropertyNode* ancestorTransformNode, | 156 const TransformPaintPropertyNode* ancestorTransformNode, |
169 bool& success); | 157 bool& success); |
170 | 158 |
171 const FloatClipRect& localToAncestorClipRectInternal( | 159 const FloatClipRect& localToAncestorClipRectInternal( |
172 const ClipPaintPropertyNode* descendant, | 160 const ClipPaintPropertyNode* descendant, |
173 const ClipPaintPropertyNode* ancestorClip, | 161 const ClipPaintPropertyNode* ancestorClip, |
174 const TransformPaintPropertyNode* ancestorTransform, | 162 const TransformPaintPropertyNode* ancestorTransform, |
175 bool& success); | 163 bool& success); |
176 | 164 |
177 const FloatClipRect& sourceToDestinationClipRectInternal( | 165 FloatClipRect sourceToDestinationClipRectInternal( |
178 const PropertyTreeState& sourceState, | 166 const PropertyTreeState& sourceState, |
179 const PropertyTreeState& destinationState, | 167 const PropertyTreeState& destinationState, |
180 bool& success); | 168 bool& success); |
181 | 169 |
182 void slowLocalToAncestorVisualRectWithEffects( | 170 FloatClipRect slowLocalToAncestorVisualRectWithEffects( |
| 171 const FloatRect&, |
183 const PropertyTreeState& localState, | 172 const PropertyTreeState& localState, |
184 const PropertyTreeState& ancestorState, | 173 const PropertyTreeState& ancestorState, |
185 FloatRect& mappingRect, | |
186 bool& success); | 174 bool& success); |
187 | 175 |
188 friend class GeometryMapperTest; | 176 friend class GeometryMapperTest; |
189 friend class PaintLayerClipperTest; | 177 friend class PaintLayerClipperTest; |
190 | 178 |
191 // These are used to represent various return values of the above | |
192 // methods. | |
193 const TransformationMatrix m_identity; | 179 const TransformationMatrix m_identity; |
194 const FloatClipRect m_infiniteClip; | 180 const FloatClipRect m_infiniteClip; |
195 FloatClipRect m_tempRect; | |
196 | 181 |
197 DISALLOW_COPY_AND_ASSIGN(GeometryMapper); | 182 DISALLOW_COPY_AND_ASSIGN(GeometryMapper); |
198 }; | 183 }; |
199 | 184 |
200 } // namespace blink | 185 } // namespace blink |
201 | 186 |
202 #endif // GeometryMapper_h | 187 #endif // GeometryMapper_h |
OLD | NEW |