This source file includes following definitions.
- m_inClipExpansion
- removeAllClientsFromCache
- removeClientFromCache
- applyResource
- applyStatefulResource
- tryPathOnlyClipping
- applyClippingToContext
- postApplyResource
- postApplyStatefulResource
- drawClipMaskContent
- asDisplayList
- calculateClipContentRepaintRect
- hitTestClipContent
- resourceBoundingBox
#include "config.h"
#include "core/rendering/svg/RenderSVGResourceClipper.h"
#include "RuntimeEnabledFeatures.h"
#include "SVGNames.h"
#include "core/frame/FrameView.h"
#include "core/frame/LocalFrame.h"
#include "core/rendering/HitTestResult.h"
#include "core/rendering/svg/SVGRenderingContext.h"
#include "core/rendering/svg/SVGResources.h"
#include "core/rendering/svg/SVGResourcesCache.h"
#include "core/svg/SVGUseElement.h"
#include "platform/graphics/DisplayList.h"
#include "platform/graphics/GraphicsContextStateSaver.h"
#include "wtf/TemporaryChange.h"
namespace WebCore {
const RenderSVGResourceType RenderSVGResourceClipper::s_resourceType = ClipperResourceType;
RenderSVGResourceClipper::RenderSVGResourceClipper(SVGClipPathElement* node)
: RenderSVGResourceContainer(node)
, m_inClipExpansion(false)
{
}
RenderSVGResourceClipper::~RenderSVGResourceClipper()
{
}
void RenderSVGResourceClipper::removeAllClientsFromCache(bool markForInvalidation)
{
m_clipContentDisplayList.clear();
m_clipBoundaries = FloatRect();
markAllClientsForInvalidation(markForInvalidation ? LayoutAndBoundariesInvalidation : ParentOnlyInvalidation);
}
void RenderSVGResourceClipper::removeClientFromCache(RenderObject* client, bool markForInvalidation)
{
ASSERT(client);
markClientForInvalidation(client, markForInvalidation ? BoundariesInvalidation : ParentOnlyInvalidation);
}
bool RenderSVGResourceClipper::applyResource(RenderObject*, RenderStyle*, GraphicsContext*&, unsigned short)
{
ASSERT_NOT_REACHED();
return false;
}
bool RenderSVGResourceClipper::applyStatefulResource(RenderObject* object, GraphicsContext*& context, ClipperContext& clipperContext)
{
ASSERT(object);
ASSERT(context);
clearInvalidationMask();
return applyClippingToContext(object, object->objectBoundingBox(), object->repaintRectInLocalCoordinates(), context, clipperContext);
}
bool RenderSVGResourceClipper::tryPathOnlyClipping(GraphicsContext* context,
const AffineTransform& animatedLocalTransform, const FloatRect& objectBoundingBox) {
if (!style()->svgStyle()->clipperResource().isEmpty())
return false;
WindRule clipRule = RULE_NONZERO;
Path clipPath = Path();
for (Element* childElement = ElementTraversal::firstWithin(*element()); childElement; childElement = ElementTraversal::nextSibling(*childElement)) {
RenderObject* renderer = childElement->renderer();
if (!renderer)
continue;
if (renderer->isSVGText())
return false;
if (!childElement->isSVGElement() || !toSVGElement(childElement)->isSVGGraphicsElement())
continue;
SVGGraphicsElement* styled = toSVGGraphicsElement(childElement);
RenderStyle* style = renderer->style();
if (!style || style->display() == NONE || style->visibility() != VISIBLE)
continue;
const SVGRenderStyle* svgStyle = style->svgStyle();
if (!svgStyle->clipperResource().isEmpty())
return false;
if (clipPath.isEmpty()) {
styled->toClipPath(clipPath);
clipRule = svgStyle->clipRule();
clipPath.setWindRule(clipRule);
continue;
}
if (RuntimeEnabledFeatures::pathOpsSVGClippingEnabled()) {
Path subPath;
styled->toClipPath(subPath);
subPath.setWindRule(svgStyle->clipRule());
if (!clipPath.unionPath(subPath))
return false;
} else {
return false;
}
}
if (toSVGClipPathElement(element())->clipPathUnits()->currentValue()->enumValue() == SVGUnitTypes::SVG_UNIT_TYPE_OBJECTBOUNDINGBOX) {
AffineTransform transform;
transform.translate(objectBoundingBox.x(), objectBoundingBox.y());
transform.scaleNonUniform(objectBoundingBox.width(), objectBoundingBox.height());
clipPath.transform(transform);
}
clipPath.transform(animatedLocalTransform);
if (clipPath.isEmpty())
clipPath.addRect(FloatRect());
context->clipPath(clipPath, clipRule);
return true;
}
bool RenderSVGResourceClipper::applyClippingToContext(RenderObject* target, const FloatRect& targetBoundingBox,
const FloatRect& repaintRect, GraphicsContext* context, ClipperContext& clipperContext)
{
ASSERT(target);
ASSERT(target->node());
ASSERT(context);
ASSERT(clipperContext.state == ClipperContext::NotAppliedState);
ASSERT_WITH_SECURITY_IMPLICATION(!needsLayout());
if (repaintRect.isEmpty() || m_inClipExpansion)
return false;
TemporaryChange<bool> inClipExpansionChange(m_inClipExpansion, true);
AffineTransform animatedLocalTransform = toSVGClipPathElement(element())->animatedLocalTransform();
if (!target->node()->isSVGElement()
&& toSVGClipPathElement(element())->clipPathUnits()->currentValue()->enumValue() == SVGUnitTypes::SVG_UNIT_TYPE_USERSPACEONUSE) {
ASSERT(style());
animatedLocalTransform.scale(style()->effectiveZoom());
}
if (tryPathOnlyClipping(context, animatedLocalTransform, targetBoundingBox)) {
clipperContext.state = ClipperContext::AppliedPathState;
return true;
}
clipperContext.state = ClipperContext::AppliedMaskState;
context->beginTransparencyLayer(1, &repaintRect);
{
GraphicsContextStateSaver maskContentSaver(*context);
context->concatCTM(animatedLocalTransform);
SVGResources* resources = SVGResourcesCache::cachedResourcesForRenderObject(this);
RenderSVGResourceClipper* clipPathClipper = 0;
ClipperContext clipPathClipperContext;
if (resources && (clipPathClipper = resources->clipper())) {
if (!clipPathClipper->applyClippingToContext(this, targetBoundingBox, repaintRect, context, clipPathClipperContext)) {
maskContentSaver.restore();
context->restoreLayer();
return false;
}
}
drawClipMaskContent(context, targetBoundingBox);
if (clipPathClipper)
clipPathClipper->postApplyStatefulResource(this, context, clipPathClipperContext);
}
context->beginLayer(1, CompositeSourceIn, &repaintRect);
return true;
}
void RenderSVGResourceClipper::postApplyResource(RenderObject*, GraphicsContext*&, unsigned short,
const Path*, const RenderSVGShape*) {
ASSERT_NOT_REACHED();
}
void RenderSVGResourceClipper::postApplyStatefulResource(RenderObject*, GraphicsContext*& context, ClipperContext& clipperContext)
{
switch (clipperContext.state) {
case ClipperContext::AppliedPathState:
break;
case ClipperContext::AppliedMaskState:
context->endLayer();
context->endLayer();
break;
default:
ASSERT_NOT_REACHED();
}
}
void RenderSVGResourceClipper::drawClipMaskContent(GraphicsContext* context, const FloatRect& targetBoundingBox)
{
ASSERT(context);
AffineTransform contentTransformation;
SVGUnitTypes::SVGUnitType contentUnits = toSVGClipPathElement(element())->clipPathUnits()->currentValue()->enumValue();
if (contentUnits == SVGUnitTypes::SVG_UNIT_TYPE_OBJECTBOUNDINGBOX) {
contentTransformation.translate(targetBoundingBox.x(), targetBoundingBox.y());
contentTransformation.scaleNonUniform(targetBoundingBox.width(), targetBoundingBox.height());
context->concatCTM(contentTransformation);
}
if (!m_clipContentDisplayList)
m_clipContentDisplayList = asDisplayList(context, contentTransformation);
ASSERT(m_clipContentDisplayList);
context->drawDisplayList(m_clipContentDisplayList.get());
}
PassRefPtr<DisplayList> RenderSVGResourceClipper::asDisplayList(GraphicsContext* context,
const AffineTransform& contentTransformation)
{
ASSERT(context);
ASSERT(frame());
context->beginRecording(strokeBoundingBox());
PaintBehavior oldBehavior = frame()->view()->paintBehavior();
frame()->view()->setPaintBehavior(oldBehavior | PaintBehaviorRenderingSVGMask);
for (SVGElement* childElement = Traversal<SVGElement>::firstChild(*element()); childElement; childElement = Traversal<SVGElement>::nextSibling(*childElement)) {
RenderObject* renderer = childElement->renderer();
if (!renderer)
continue;
RenderStyle* style = renderer->style();
if (!style || style->display() == NONE || style->visibility() != VISIBLE)
continue;
WindRule newClipRule = style->svgStyle()->clipRule();
bool isUseElement = isSVGUseElement(*childElement);
if (isUseElement) {
SVGUseElement& useElement = toSVGUseElement(*childElement);
renderer = useElement.rendererClipChild();
if (!renderer)
continue;
if (!useElement.hasAttribute(SVGNames::clip_ruleAttr))
newClipRule = renderer->style()->svgStyle()->clipRule();
}
if (!renderer->isSVGShape() && !renderer->isSVGText())
continue;
context->setFillRule(newClipRule);
if (isUseElement)
renderer = childElement->renderer();
SVGRenderingContext::renderSubtree(context, renderer, contentTransformation);
}
frame()->view()->setPaintBehavior(oldBehavior);
return context->endRecording();
}
void RenderSVGResourceClipper::calculateClipContentRepaintRect()
{
for (SVGElement* childElement = Traversal<SVGElement>::firstChild(*element()); childElement; childElement = Traversal<SVGElement>::nextSibling(*childElement)) {
RenderObject* renderer = childElement->renderer();
if (!renderer)
continue;
if (!renderer->isSVGShape() && !renderer->isSVGText() && !isSVGUseElement(*childElement))
continue;
RenderStyle* style = renderer->style();
if (!style || style->display() == NONE || style->visibility() != VISIBLE)
continue;
m_clipBoundaries.unite(renderer->localToParentTransform().mapRect(renderer->repaintRectInLocalCoordinates()));
}
m_clipBoundaries = toSVGClipPathElement(element())->animatedLocalTransform().mapRect(m_clipBoundaries);
}
bool RenderSVGResourceClipper::hitTestClipContent(const FloatRect& objectBoundingBox, const FloatPoint& nodeAtPoint)
{
FloatPoint point = nodeAtPoint;
if (!SVGRenderSupport::pointInClippingArea(this, point))
return false;
SVGClipPathElement* clipPathElement = toSVGClipPathElement(element());
if (clipPathElement->clipPathUnits()->currentValue()->enumValue() == SVGUnitTypes::SVG_UNIT_TYPE_OBJECTBOUNDINGBOX) {
AffineTransform transform;
transform.translate(objectBoundingBox.x(), objectBoundingBox.y());
transform.scaleNonUniform(objectBoundingBox.width(), objectBoundingBox.height());
point = transform.inverse().mapPoint(point);
}
point = clipPathElement->animatedLocalTransform().inverse().mapPoint(point);
for (SVGElement* childElement = Traversal<SVGElement>::firstChild(*element()); childElement; childElement = Traversal<SVGElement>::nextSibling(*childElement)) {
RenderObject* renderer = childElement->renderer();
if (!renderer)
continue;
if (!renderer->isSVGShape() && !renderer->isSVGText() && !isSVGUseElement(*childElement))
continue;
IntPoint hitPoint;
HitTestResult result(hitPoint);
if (renderer->nodeAtFloatPoint(HitTestRequest(HitTestRequest::SVGClipContent | HitTestRequest::ConfusingAndOftenMisusedDisallowShadowContent), result, point, HitTestForeground))
return true;
}
return false;
}
FloatRect RenderSVGResourceClipper::resourceBoundingBox(const RenderObject* object)
{
if (selfNeedsLayout())
return object->objectBoundingBox();
if (m_clipBoundaries.isEmpty())
calculateClipContentRepaintRect();
if (toSVGClipPathElement(element())->clipPathUnits()->currentValue()->enumValue() == SVGUnitTypes::SVG_UNIT_TYPE_OBJECTBOUNDINGBOX) {
FloatRect objectBoundingBox = object->objectBoundingBox();
AffineTransform transform;
transform.translate(objectBoundingBox.x(), objectBoundingBox.y());
transform.scaleNonUniform(objectBoundingBox.width(), objectBoundingBox.height());
return transform.mapRect(m_clipBoundaries);
}
return m_clipBoundaries;
}
}