OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "config.h" |
| 6 #include "core/paint/SVGClipPainter.h" |
| 7 |
| 8 #include "core/dom/ElementTraversal.h" |
| 9 #include "core/layout/PaintInfo.h" |
| 10 #include "core/layout/svg/LayoutSVGResourceClipper.h" |
| 11 #include "core/layout/svg/SVGResources.h" |
| 12 #include "core/layout/svg/SVGResourcesCache.h" |
| 13 #include "core/paint/CompositingRecorder.h" |
| 14 #include "core/paint/TransformRecorder.h" |
| 15 #include "platform/graphics/paint/ClipPathDisplayItem.h" |
| 16 #include "platform/graphics/paint/CompositingDisplayItem.h" |
| 17 #include "platform/graphics/paint/DisplayItemList.h" |
| 18 #include "platform/graphics/paint/DrawingDisplayItem.h" |
| 19 #include "wtf/TemporaryChange.h" |
| 20 |
| 21 namespace blink { |
| 22 |
| 23 bool SVGClipPainter::applyStatefulResource(LayoutObject* object, GraphicsContext
* context, ClipperState& clipperState) |
| 24 { |
| 25 ASSERT(object); |
| 26 ASSERT(context); |
| 27 |
| 28 m_clip.clearInvalidationMask(); |
| 29 |
| 30 return applyClippingToContext(object, object->objectBoundingBox(), object->p
aintInvalidationRectInLocalCoordinates(), context, clipperState); |
| 31 } |
| 32 |
| 33 class SVGClipExpansionCycleHelper { |
| 34 public: |
| 35 SVGClipExpansionCycleHelper(LayoutSVGResourceClipper& clip) : m_clip(clip) {
clip.beginClipExpansion(); } |
| 36 ~SVGClipExpansionCycleHelper() { m_clip.endClipExpansion(); } |
| 37 private: |
| 38 LayoutSVGResourceClipper& m_clip; |
| 39 }; |
| 40 |
| 41 bool SVGClipPainter::applyClippingToContext(LayoutObject* target, const FloatRec
t& targetBoundingBox, |
| 42 const FloatRect& paintInvalidationRect, GraphicsContext* context, ClipperSta
te& clipperState) |
| 43 { |
| 44 ASSERT(target); |
| 45 ASSERT(context); |
| 46 ASSERT(clipperState == ClipperNotApplied); |
| 47 ASSERT_WITH_SECURITY_IMPLICATION(!m_clip.needsLayout()); |
| 48 |
| 49 if (paintInvalidationRect.isEmpty() || m_clip.hasCycle()) |
| 50 return false; |
| 51 |
| 52 SVGClipExpansionCycleHelper inClipExpansionChange(m_clip); |
| 53 |
| 54 AffineTransform animatedLocalTransform = toSVGClipPathElement(m_clip.element
())->calculateAnimatedLocalTransform(); |
| 55 // When drawing a clip for non-SVG elements, the CTM does not include the zo
om factor. |
| 56 // In this case, we need to apply the zoom scale explicitly - but only for c
lips with |
| 57 // userSpaceOnUse units (the zoom is accounted for objectBoundingBox-resolve
d lengths). |
| 58 if (!target->isSVG() && m_clip.clipPathUnits() == SVGUnitTypes::SVG_UNIT_TYP
E_USERSPACEONUSE) { |
| 59 ASSERT(m_clip.style()); |
| 60 animatedLocalTransform.scale(m_clip.style()->effectiveZoom()); |
| 61 } |
| 62 |
| 63 // First, try to apply the clip as a clipPath. |
| 64 if (m_clip.tryPathOnlyClipping(target->displayItemClient(), context, animate
dLocalTransform, targetBoundingBox)) { |
| 65 clipperState = ClipperAppliedPath; |
| 66 return true; |
| 67 } |
| 68 |
| 69 // Fall back to masking. |
| 70 clipperState = ClipperAppliedMask; |
| 71 |
| 72 // Begin compositing the clip mask. |
| 73 CompositingRecorder::beginCompositing(context, target->displayItemClient(),
SkXfermode::kSrcOver_Mode, 1, &paintInvalidationRect); |
| 74 { |
| 75 TransformRecorder recorder(*context, target->displayItemClient(), animat
edLocalTransform); |
| 76 |
| 77 // clipPath can also be clipped by another clipPath. |
| 78 SVGResources* resources = SVGResourcesCache::cachedResourcesForLayoutObj
ect(&m_clip); |
| 79 LayoutSVGResourceClipper* clipPathClipper = resources ? resources->clipp
er() : 0; |
| 80 ClipperState clipPathClipperState = ClipperNotApplied; |
| 81 if (clipPathClipper && !SVGClipPainter(*clipPathClipper).applyClippingTo
Context(&m_clip, targetBoundingBox, paintInvalidationRect, context, clipPathClip
perState)) { |
| 82 // End the clip mask's compositor. |
| 83 CompositingRecorder::endCompositing(context, target->displayItemClie
nt()); |
| 84 return false; |
| 85 } |
| 86 |
| 87 drawClipMaskContent(context, target->displayItemClient(), targetBounding
Box); |
| 88 |
| 89 if (clipPathClipper) |
| 90 SVGClipPainter(*clipPathClipper).postApplyStatefulResource(&m_clip,
context, clipPathClipperState); |
| 91 } |
| 92 |
| 93 // Masked content layer start. |
| 94 CompositingRecorder::beginCompositing(context, target->displayItemClient(),
SkXfermode::kSrcIn_Mode, 1, &paintInvalidationRect); |
| 95 |
| 96 return true; |
| 97 } |
| 98 |
| 99 void SVGClipPainter::postApplyStatefulResource(LayoutObject* target, GraphicsCon
text* context, ClipperState& clipperState) |
| 100 { |
| 101 switch (clipperState) { |
| 102 case ClipperAppliedPath: |
| 103 // Path-only clipping, no layers to restore but we need to emit an end t
o the clip path display item. |
| 104 if (RuntimeEnabledFeatures::slimmingPaintEnabled()) { |
| 105 context->displayItemList()->add(EndClipPathDisplayItem::create(targe
t->displayItemClient())); |
| 106 } else { |
| 107 EndClipPathDisplayItem endClipPathDisplayItem(target->displayItemCli
ent()); |
| 108 endClipPathDisplayItem.replay(context); |
| 109 } |
| 110 break; |
| 111 case ClipperAppliedMask: |
| 112 // Transfer content -> clip mask (SrcIn) |
| 113 CompositingRecorder::endCompositing(context, target->displayItemClient()
); |
| 114 |
| 115 // Transfer clip mask -> bg (SrcOver) |
| 116 CompositingRecorder::endCompositing(context, target->displayItemClient()
); |
| 117 break; |
| 118 default: |
| 119 ASSERT_NOT_REACHED(); |
| 120 } |
| 121 } |
| 122 |
| 123 void SVGClipPainter::drawClipMaskContent(GraphicsContext* context, DisplayItemCl
ient client, const FloatRect& targetBoundingBox) |
| 124 { |
| 125 ASSERT(context); |
| 126 |
| 127 AffineTransform contentTransformation; |
| 128 RefPtr<const SkPicture> clipContentPicture = m_clip.createContentPicture(con
tentTransformation, targetBoundingBox); |
| 129 |
| 130 TransformRecorder recorder(*context, client, contentTransformation); |
| 131 if (RuntimeEnabledFeatures::slimmingPaintEnabled()) { |
| 132 ASSERT(context->displayItemList()); |
| 133 context->displayItemList()->add(DrawingDisplayItem::create(client, Displ
ayItem::SVGClip, clipContentPicture)); |
| 134 } else { |
| 135 DrawingDisplayItem clipPicture(client, DisplayItem::SVGClip, clipContent
Picture); |
| 136 clipPicture.replay(context); |
| 137 } |
| 138 } |
| 139 |
| 140 } |
OLD | NEW |