summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/platform/graphics/paint/geometry_mapper.h
blob: cdc290ebb5025e27bc7eedc2e342865db5ee0f91 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef THIRD_PARTY_BLINK_RENDERER_PLATFORM_GRAPHICS_PAINT_GEOMETRY_MAPPER_H_
#define THIRD_PARTY_BLINK_RENDERER_PLATFORM_GRAPHICS_PAINT_GEOMETRY_MAPPER_H_

#include "base/optional.h"
#include "third_party/blink/renderer/platform/graphics/overlay_scrollbar_clip_behavior.h"
#include "third_party/blink/renderer/platform/graphics/paint/float_clip_rect.h"
#include "third_party/blink/renderer/platform/graphics/paint/property_tree_state.h"
#include "third_party/blink/renderer/platform/transforms/transformation_matrix.h"
#include "third_party/blink/renderer/platform/wtf/allocator/allocator.h"
#include "third_party/blink/renderer/platform/wtf/hash_map.h"

namespace blink {

// Clips can use FloatRect::Intersect or FloatRect::InclusiveIntersect.
enum InclusiveIntersectOrNot { kNonInclusiveIntersect, kInclusiveIntersect };

// Whether to expand the visual or clip rect to infinity when we meet any
// animating transform or filter when walking from a descendant state to an
// ancestor state, when mapping a visual rect or getting the accumulated clip
// rect. After we expanded the rect, we will still apply ancestor clips when
// continuing walking up the tree. TODO(crbug.com/1026653): Consider animation
// bounds instead of using infinite rect.
enum ExpandVisualRectForAnimationOrNot {
  kDontExpandVisualRectForAnimation,
  kExpandVisualRectForAnimation,
};

// GeometryMapper is a helper class for fast computations of transformed and
// visual rects in different PropertyTreeStates. The design document has a
// number of details on use cases, algorithmic definitions, and running times.
//
// NOTE: A GeometryMapper object is only valid for property trees that do not
// change. If any mutation occurs, a new GeometryMapper object must be allocated
// corresponding to the new state.
//
// Design document: http://bit.ly/28P4FDA
class PLATFORM_EXPORT GeometryMapper {
  STATIC_ONLY(GeometryMapper);

 public:
  // The return value of SourceToDestinationProjection. If the result is known
  // to be accumulation of 2d translations, |matrix| is nullptr, and
  // |translation_2d| is the accumulated 2d translation. Otherwise |matrix|
  // points to the accumulated projection, and |translation_2d| is zero.
  class Translation2DOrMatrix {
    DISALLOW_NEW();

   public:
    Translation2DOrMatrix() { DCHECK(IsIdentity()); }
    explicit Translation2DOrMatrix(const FloatSize& translation_2d)
        : translation_2d_(translation_2d) {
      DCHECK(IsIdentityOr2DTranslation());
    }
    explicit Translation2DOrMatrix(const TransformationMatrix& matrix)
        : matrix_(matrix) {
      DCHECK(!IsIdentityOr2DTranslation());
    }

    bool IsIdentity() const { return !matrix_ && translation_2d_.IsZero(); }
    bool IsIdentityOr2DTranslation() const { return !matrix_; }
    const FloatSize& Translation2D() const {
      DCHECK(IsIdentityOr2DTranslation());
      return translation_2d_;
    }
    const TransformationMatrix& Matrix() const {
      DCHECK(!IsIdentityOr2DTranslation());
      return *matrix_;
    }

    template <typename Rect>
    void MapRect(Rect& rect) const {
      if (LIKELY(IsIdentityOr2DTranslation()))
        MoveRect(rect, Translation2D());
      else
        rect = Matrix().MapRect(rect);
    }

    void MapQuad(FloatQuad& quad) const {
      if (LIKELY(IsIdentityOr2DTranslation()))
        quad.Move(Translation2D());
      else
        quad = Matrix().MapQuad(quad);
    }

    void MapFloatClipRect(FloatClipRect& rect) const {
      if (LIKELY(IsIdentityOr2DTranslation()))
        rect.MoveBy(FloatPoint(Translation2D()));
      else
        rect.Map(Matrix());
    }

    FloatPoint MapPoint(const FloatPoint& point) const {
      if (LIKELY(IsIdentityOr2DTranslation()))
        return point + Translation2D();
      return Matrix().MapPoint(point);
    }

    void PostTranslate(float x, float y) {
      if (LIKELY(IsIdentityOr2DTranslation()))
        translation_2d_.Expand(x, y);
      else
        matrix_->PostTranslate(x, y);
    }

    SkM44 ToSkM44() const { return TransformationMatrix::ToSkM44(Matrix()); }

    SkMatrix ToSkMatrix() const {
      if (LIKELY(IsIdentityOr2DTranslation())) {
        return SkMatrix::Translate(Translation2D().Width(),
                                   Translation2D().Height());
      }
      return SkMatrix(TransformationMatrix::ToSkMatrix44(Matrix()));
    }

    bool operator==(const Translation2DOrMatrix& other) {
      return translation_2d_ == other.translation_2d_ &&
             matrix_ == other.matrix_;
    }

    bool operator!=(const Translation2DOrMatrix& other) {
      return !(*this == other);
    }

   private:
    FloatSize translation_2d_;
    base::Optional<TransformationMatrix> matrix_;
  };

  // Returns the matrix that is suitable to map geometries on the source plane
  // to some backing in the destination plane.
  // Formal definition:
  //   output = flatten(destination_to_screen)^-1 * flatten(source_to_screen)
  // There are some cases that flatten(destination_to_screen) being
  // singular yet we can still define a reasonable projection, for example:
  // 1. Both nodes inherited a common singular flat ancestor:
  // 2. Both nodes are co-planar to a common singular ancestor:
  // Not every cases outlined above are supported!
  // Read implementation comments for specific restrictions.
  static Translation2DOrMatrix SourceToDestinationProjection(
      const TransformPaintPropertyNodeOrAlias& source,
      const TransformPaintPropertyNodeOrAlias& destination) {
    return SourceToDestinationProjection(source.Unalias(),
                                         destination.Unalias());
  }
  static Translation2DOrMatrix SourceToDestinationProjection(
      const TransformPaintPropertyNode& source,
      const TransformPaintPropertyNode& destination);

  // Same as SourceToDestinationProjection() except that it maps the rect
  // rather than returning the matrix.
  // |mapping_rect| is both input and output. Its type can be FloatRect,
  // LayoutRect or IntRect.
  template <typename Rect>
  static void SourceToDestinationRect(
      const TransformPaintPropertyNodeOrAlias& source,
      const TransformPaintPropertyNodeOrAlias& destination,
      Rect& mapping_rect) {
    SourceToDestinationRect(source.Unalias(), destination.Unalias(),
                            mapping_rect);
  }
  template <typename Rect>
  static void SourceToDestinationRect(
      const TransformPaintPropertyNode& source,
      const TransformPaintPropertyNode& destination,
      Rect& mapping_rect) {
    if (&source == &destination)
      return;

    // Fast-path optimization for mapping through just |source| when |source| is
    // a 2d translation.
    if (&destination == source.Parent() && source.IsIdentityOr2DTranslation()) {
      MoveRect(mapping_rect, source.Translation2D());
      return;
    }

    // Fast-path optimization for mapping through just |destination| when
    // |destination| is a 2d translation.
    if (&source == destination.Parent() &&
        destination.IsIdentityOr2DTranslation()) {
      MoveRect(mapping_rect, -destination.Translation2D());
      return;
    }

    bool has_animation = false;
    bool success = false;
    const auto& source_to_destination = SourceToDestinationProjectionInternal(
        source, destination, has_animation, success);
    if (!success)
      mapping_rect = Rect();
    else
      source_to_destination.MapRect(mapping_rect);
  }

  // Returns the clip rect between |local_state| and |ancestor_state|. The clip
  // rect is the total clip rect that should be applied when painting contents
  // of |local_state| in |ancestor_state| space. Because this clip rect applies
  // on contents of |local_state|, it's not affected by any effect nodes between
  // |local_state| and |ancestor_state|.
  //
  // The UnsnappedClipRect of any clip nodes is used, *not* the
  // PixelSnappedClipRect.
  //
  // Note that the clip of |ancestor_state| is *not* applied.
  //
  // The output FloatClipRect may contain false positives for rounded-ness
  // if a rounded clip is clipped out, and overly conservative results
  // in the presences of transforms.

  static FloatClipRect LocalToAncestorClipRect(
      const PropertyTreeStateOrAlias& local_state,
      const PropertyTreeStateOrAlias& ancestor_state,
      OverlayScrollbarClipBehavior behavior = kIgnoreOverlayScrollbarSize) {
    return LocalToAncestorClipRect(local_state.Unalias(),
                                   ancestor_state.Unalias(), behavior);
  }
  static FloatClipRect LocalToAncestorClipRect(
      const PropertyTreeState& local_state,
      const PropertyTreeState& ancestor_state,
      OverlayScrollbarClipBehavior = kIgnoreOverlayScrollbarSize);

  // Maps from a rect in |local_state| to its visual rect in |ancestor_state|.
  // If there is no effect node between |local_state| (included) and
  // |ancestor_state| (not included), the result is computed by multiplying the
  // rect by its combined transform between |local_state| and |ancestor_space|,
  // then flattening into 2D space, then intersecting by the clip for
  // |local_state|'s clips. If there are any pixel-moving effect nodes between
  // |local_state| and |ancestor_state|, for each segment of states separated
  // by the effect nodes, we'll execute the above process and map the result
  // rect with the effect.
  //
  // Note that the clip of |ancestor_state| is *not* applied.
  //
  // DCHECK fails if any of the paint property tree nodes in |local_state| are
  // not equal to or a descendant of that in |ancestor_state|.
  //
  // |mapping_rect| is both input and output.
  //
  // The output FloatClipRect may contain false positives for rounded-ness
  // if a rounded clip is clipped out, and overly conservative results
  // in the presences of transforms.
  //
  // Returns true if the mapped rect is non-empty. (Note: this has special
  // meaning in the presence of inclusive intersection.)
  //
  // Note: if inclusive intersection is specified, then the
  // GeometryMapperClipCache is bypassed (the GeometryMapperTRansformCache is
  // still used, however).
  //
  // If kInclusiveIntersect is set, clipping operations will
  // use FloatRect::InclusiveIntersect, and the return value of
  // InclusiveIntersect will be propagated to the return value of this method.
  // Otherwise, clipping operations will use LayoutRect::intersect, and the
  // return value will be true only if the clipped rect has non-zero area.
  // See the documentation for FloatRect::InclusiveIntersect for more
  // information.
  static bool LocalToAncestorVisualRect(
      const PropertyTreeStateOrAlias& local_state,
      const PropertyTreeStateOrAlias& ancestor_state,
      FloatClipRect& mapping_rect,
      OverlayScrollbarClipBehavior clip = kIgnoreOverlayScrollbarSize,
      InclusiveIntersectOrNot intersect = kNonInclusiveIntersect,
      ExpandVisualRectForAnimationOrNot animation =
          kDontExpandVisualRectForAnimation) {
    return LocalToAncestorVisualRect(local_state.Unalias(),
                                     ancestor_state.Unalias(), mapping_rect,
                                     clip, intersect, animation);
  }
  static bool LocalToAncestorVisualRect(
      const PropertyTreeState& local_state,
      const PropertyTreeState& ancestor_state,
      FloatClipRect& mapping_rect,
      OverlayScrollbarClipBehavior = kIgnoreOverlayScrollbarSize,
      InclusiveIntersectOrNot = kNonInclusiveIntersect,
      ExpandVisualRectForAnimationOrNot = kDontExpandVisualRectForAnimation);

  static void ClearCache();

 private:
  // The internal methods do the same things as their public counterparts, but
  // take an extra |success| parameter which indicates if the function is
  // successful on return. See comments of the public functions for failure
  // conditions.

  static Translation2DOrMatrix SourceToDestinationProjectionInternal(
      const TransformPaintPropertyNode& source,
      const TransformPaintPropertyNode& destination,
      bool& has_animation,
      bool& success);

  static FloatClipRect LocalToAncestorClipRectInternal(
      const ClipPaintPropertyNode& descendant,
      const ClipPaintPropertyNode& ancestor_clip,
      const TransformPaintPropertyNode& ancestor_transform,
      OverlayScrollbarClipBehavior,
      InclusiveIntersectOrNot,
      ExpandVisualRectForAnimationOrNot,
      bool& success);

  // The return value has the same meaning as that for
  // LocalToAncestorVisualRect.
  static bool LocalToAncestorVisualRectInternal(
      const PropertyTreeState& local_state,
      const PropertyTreeState& ancestor_state,
      FloatClipRect& mapping_rect,
      OverlayScrollbarClipBehavior,
      InclusiveIntersectOrNot,
      ExpandVisualRectForAnimationOrNot,
      bool& success);

  // The return value has the same meaning as that for
  // LocalToAncestorVisualRect.
  static bool SlowLocalToAncestorVisualRectWithEffects(
      const PropertyTreeState& local_state,
      const PropertyTreeState& ancestor_state,
      FloatClipRect& mapping_rect,
      OverlayScrollbarClipBehavior,
      InclusiveIntersectOrNot,
      ExpandVisualRectForAnimationOrNot,
      bool& success);

  static void MoveRect(FloatRect& rect, const FloatSize& delta) {
    rect.Move(delta.Width(), delta.Height());
  }

  static void MoveRect(LayoutRect& rect, const FloatSize& delta) {
    rect.Move(LayoutSize(delta.Width(), delta.Height()));
  }

  static void MoveRect(IntRect& rect, const FloatSize& delta) {
    auto float_rect = FloatRect(rect);
    MoveRect(float_rect, delta);
    rect = EnclosingIntRect(float_rect);
  }

  friend class GeometryMapperTest;
  friend class PaintLayerClipperTest;
};

}  // namespace blink

#endif  // THIRD_PARTY_BLINK_RENDERER_PLATFORM_GRAPHICS_PAINT_GEOMETRY_MAPPER_H_