File: LayoutSVGResourceClipper.cpp

package info (click to toggle)
chromium-browser 57.0.2987.98-1~deb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 2,637,852 kB
  • ctags: 2,544,394
  • sloc: cpp: 12,815,961; ansic: 3,676,222; python: 1,147,112; asm: 526,608; java: 523,212; xml: 286,794; perl: 92,654; sh: 86,408; objc: 73,271; makefile: 27,698; cs: 18,487; yacc: 13,031; tcl: 12,957; pascal: 4,875; ml: 4,716; lex: 3,904; sql: 3,862; ruby: 1,982; lisp: 1,508; php: 1,368; exp: 404; awk: 325; csh: 117; jsp: 39; sed: 37
file content (312 lines) | stat: -rw-r--r-- 11,369 bytes parent folder | download
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
/*
 * Copyright (C) 2004, 2005, 2007, 2008 Nikolas Zimmermann <zimmermann@kde.org>
 * Copyright (C) 2004, 2005, 2006, 2007, 2008 Rob Buis <buis@kde.org>
 * Copyright (C) Research In Motion Limited 2009-2010. All rights reserved.
 * Copyright (C) 2011 Dirk Schulze <krit@webkit.org>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public License
 * along with this library; see the file COPYING.LIB.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

#include "core/layout/svg/LayoutSVGResourceClipper.h"

#include "core/dom/ElementTraversal.h"
#include "core/layout/HitTestResult.h"
#include "core/layout/svg/SVGLayoutSupport.h"
#include "core/paint/PaintInfo.h"
#include "core/svg/SVGGeometryElement.h"
#include "core/svg/SVGUseElement.h"
#include "platform/graphics/paint/SkPictureBuilder.h"
#include "third_party/skia/include/core/SkPicture.h"
#include "third_party/skia/include/pathops/SkPathOps.h"

namespace blink {

namespace {

enum class ClipStrategy { None, Mask, Path };

ClipStrategy modifyStrategyForClipPath(const ComputedStyle& style,
                                       ClipStrategy strategy) {
  // If the shape in the clip-path gets clipped too then fallback to masking.
  if (strategy != ClipStrategy::Path || !style.clipPath())
    return strategy;
  return ClipStrategy::Mask;
}

ClipStrategy determineClipStrategy(const SVGGraphicsElement& element) {
  const LayoutObject* layoutObject = element.layoutObject();
  if (!layoutObject)
    return ClipStrategy::None;
  const ComputedStyle& style = layoutObject->styleRef();
  if (style.display() == EDisplay::None ||
      style.visibility() != EVisibility::kVisible)
    return ClipStrategy::None;
  ClipStrategy strategy = ClipStrategy::None;
  // Only shapes, paths and texts are allowed for clipping.
  if (layoutObject->isSVGShape()) {
    strategy = ClipStrategy::Path;
  } else if (layoutObject->isSVGText()) {
    // Text requires masking.
    strategy = ClipStrategy::Mask;
  }
  return modifyStrategyForClipPath(style, strategy);
}

ClipStrategy determineClipStrategy(const SVGElement& element) {
  // <use> within <clipPath> have a restricted content model.
  // (https://drafts.fxtf.org/css-masking-1/#ClipPathElement)
  if (isSVGUseElement(element)) {
    const LayoutObject* useLayoutObject = element.layoutObject();
    if (!useLayoutObject ||
        useLayoutObject->styleRef().display() == EDisplay::None)
      return ClipStrategy::None;
    const SVGGraphicsElement* shapeElement =
        toSVGUseElement(element).visibleTargetGraphicsElementForClipping();
    if (!shapeElement)
      return ClipStrategy::None;
    ClipStrategy shapeStrategy = determineClipStrategy(*shapeElement);
    return modifyStrategyForClipPath(useLayoutObject->styleRef(),
                                     shapeStrategy);
  }
  if (!element.isSVGGraphicsElement())
    return ClipStrategy::None;
  return determineClipStrategy(toSVGGraphicsElement(element));
}

bool contributesToClip(const SVGElement& element) {
  return determineClipStrategy(element) != ClipStrategy::None;
}

void pathFromElement(const SVGElement& element, Path& clipPath) {
  if (isSVGGeometryElement(element))
    toSVGGeometryElement(element).toClipPath(clipPath);
  else if (isSVGUseElement(element))
    toSVGUseElement(element).toClipPath(clipPath);
}

}  // namespace

LayoutSVGResourceClipper::LayoutSVGResourceClipper(SVGClipPathElement* node)
    : LayoutSVGResourceContainer(node), m_inClipExpansion(false) {}

LayoutSVGResourceClipper::~LayoutSVGResourceClipper() {}

void LayoutSVGResourceClipper::removeAllClientsFromCache(
    bool markForInvalidation) {
  m_clipContentPath.clear();
  m_clipContentPicture.reset();
  m_localClipBounds = FloatRect();
  markAllClientsForInvalidation(markForInvalidation
                                    ? LayoutAndBoundariesInvalidation
                                    : ParentOnlyInvalidation);
}

void LayoutSVGResourceClipper::removeClientFromCache(LayoutObject* client,
                                                     bool markForInvalidation) {
  ASSERT(client);
  markClientForInvalidation(client, markForInvalidation
                                        ? BoundariesInvalidation
                                        : ParentOnlyInvalidation);
}

bool LayoutSVGResourceClipper::calculateClipContentPathIfNeeded() {
  if (!m_clipContentPath.isEmpty())
    return true;

  // If the current clip-path gets clipped itself, we have to fallback to
  // masking.
  if (styleRef().clipPath())
    return false;

  unsigned opCount = 0;
  bool usingBuilder = false;
  SkOpBuilder clipPathBuilder;

  for (const SVGElement& childElement :
       Traversal<SVGElement>::childrenOf(*element())) {
    ClipStrategy strategy = determineClipStrategy(childElement);
    if (strategy == ClipStrategy::None)
      continue;
    if (strategy == ClipStrategy::Mask) {
      m_clipContentPath.clear();
      return false;
    }

    // First clip shape.
    if (m_clipContentPath.isEmpty()) {
      pathFromElement(childElement, m_clipContentPath);
      continue;
    }

    // Multiple shapes require PathOps. In some degenerate cases PathOps can
    // exhibit quadratic behavior, so we cap the number of ops to a reasonable
    // count.
    const unsigned kMaxOps = 42;
    if (++opCount > kMaxOps) {
      m_clipContentPath.clear();
      return false;
    }

    // Second clip shape => start using the builder.
    if (!usingBuilder) {
      clipPathBuilder.add(m_clipContentPath.getSkPath(), kUnion_SkPathOp);
      usingBuilder = true;
    }

    Path subPath;
    pathFromElement(childElement, subPath);

    clipPathBuilder.add(subPath.getSkPath(), kUnion_SkPathOp);
  }

  if (usingBuilder) {
    SkPath resolvedPath;
    clipPathBuilder.resolve(&resolvedPath);
    m_clipContentPath = resolvedPath;
  }

  return true;
}

bool LayoutSVGResourceClipper::asPath(
    const AffineTransform& animatedLocalTransform,
    const FloatRect& referenceBox,
    Path& clipPath) {
  if (!calculateClipContentPathIfNeeded())
    return false;

  clipPath = m_clipContentPath;

  // We are able to represent the clip as a path. Continue with direct clipping,
  // and transform the content to userspace if necessary.
  if (clipPathUnits() == SVGUnitTypes::kSvgUnitTypeObjectboundingbox) {
    AffineTransform transform;
    transform.translate(referenceBox.x(), referenceBox.y());
    transform.scaleNonUniform(referenceBox.width(), referenceBox.height());
    clipPath.transform(transform);
  }

  // Transform path by animatedLocalTransform.
  clipPath.transform(animatedLocalTransform);
  return true;
}

sk_sp<const SkPicture> LayoutSVGResourceClipper::createContentPicture() {
  ASSERT(frame());
  if (m_clipContentPicture)
    return m_clipContentPicture;

  // Using strokeBoundingBox (instead of visualRectInLocalSVGCoordinates) to
  // avoid the intersection with local clips/mask, which may yield incorrect
  // results when mixing objectBoundingBox and userSpaceOnUse units
  // (http://crbug.com/294900).
  FloatRect bounds = strokeBoundingBox();

  SkPictureBuilder pictureBuilder(bounds, nullptr, nullptr);
  // Switch to a paint behavior where all children of this <clipPath> will be
  // laid out using special constraints:
  // - fill-opacity/stroke-opacity/opacity set to 1
  // - masker/filter not applied when laying out the children
  // - fill is set to the initial fill paint server (solid, black)
  // - stroke is set to the initial stroke paint server (none)
  PaintInfo info(pictureBuilder.context(), LayoutRect::infiniteIntRect(),
                 PaintPhaseForeground, GlobalPaintNormalPhase,
                 PaintLayerPaintingRenderingClipPathAsMask |
                     PaintLayerPaintingRenderingResourceSubtree);

  for (const SVGElement& childElement :
       Traversal<SVGElement>::childrenOf(*element())) {
    if (!contributesToClip(childElement))
      continue;
    // Use the LayoutObject of the direct child even if it is a <use>. In that
    // case, we will paint the targeted element indirectly.
    const LayoutObject* layoutObject = childElement.layoutObject();
    layoutObject->paint(info, IntPoint());
  }

  m_clipContentPicture = pictureBuilder.endRecording();
  return m_clipContentPicture;
}

void LayoutSVGResourceClipper::calculateLocalClipBounds() {
  // This is a rough heuristic to appraise the clip size and doesn't consider
  // clip on clip.
  for (const SVGElement& childElement :
       Traversal<SVGElement>::childrenOf(*element())) {
    if (!contributesToClip(childElement))
      continue;
    const LayoutObject* layoutObject = childElement.layoutObject();
    m_localClipBounds.unite(layoutObject->localToSVGParentTransform().mapRect(
        layoutObject->visualRectInLocalSVGCoordinates()));
  }
}

bool LayoutSVGResourceClipper::hitTestClipContent(
    const FloatRect& objectBoundingBox,
    const FloatPoint& nodeAtPoint) {
  FloatPoint point = nodeAtPoint;
  if (!SVGLayoutSupport::pointInClippingArea(*this, point))
    return false;

  if (clipPathUnits() == SVGUnitTypes::kSvgUnitTypeObjectboundingbox) {
    AffineTransform transform;
    transform.translate(objectBoundingBox.x(), objectBoundingBox.y());
    transform.scaleNonUniform(objectBoundingBox.width(),
                              objectBoundingBox.height());
    point = transform.inverse().mapPoint(point);
  }

  AffineTransform animatedLocalTransform =
      toSVGClipPathElement(element())->calculateTransform(
          SVGElement::IncludeMotionTransform);
  if (!animatedLocalTransform.isInvertible())
    return false;

  point = animatedLocalTransform.inverse().mapPoint(point);

  for (const SVGElement& childElement :
       Traversal<SVGElement>::childrenOf(*element())) {
    if (!contributesToClip(childElement))
      continue;
    IntPoint hitPoint;
    HitTestResult result(HitTestRequest::SVGClipContent, hitPoint);
    LayoutObject* layoutObject = childElement.layoutObject();
    if (layoutObject->nodeAtFloatPoint(result, point, HitTestForeground))
      return true;
  }
  return false;
}

FloatRect LayoutSVGResourceClipper::resourceBoundingBox(
    const FloatRect& referenceBox) {
  // The resource has not been layouted yet. Return the reference box.
  if (selfNeedsLayout())
    return referenceBox;

  if (m_localClipBounds.isEmpty())
    calculateLocalClipBounds();

  AffineTransform transform =
      toSVGClipPathElement(element())->calculateTransform(
          SVGElement::IncludeMotionTransform);
  if (clipPathUnits() == SVGUnitTypes::kSvgUnitTypeObjectboundingbox) {
    transform.translate(referenceBox.x(), referenceBox.y());
    transform.scaleNonUniform(referenceBox.width(), referenceBox.height());
  }
  return transform.mapRect(m_localClipBounds);
}

}  // namespace blink