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