RenderSVGResourceClipper.cpp [plain text]
#include "config.h"
#include "RenderSVGResourceClipper.h"
#include "ElementIterator.h"
#include "Frame.h"
#include "FrameView.h"
#include "HitTestRequest.h"
#include "HitTestResult.h"
#include "IntRect.h"
#include "RenderObject.h"
#include "RenderStyle.h"
#include "RenderView.h"
#include "SVGNames.h"
#include "SVGRenderingContext.h"
#include "SVGResources.h"
#include "SVGResourcesCache.h"
#include "SVGUseElement.h"
namespace WebCore {
RenderSVGResourceClipper::RenderSVGResourceClipper(SVGClipPathElement& element, RenderStyle&& style)
: RenderSVGResourceContainer(element, WTFMove(style))
{
}
RenderSVGResourceClipper::~RenderSVGResourceClipper()
{
}
void RenderSVGResourceClipper::removeAllClientsFromCache(bool markForInvalidation)
{
m_clipBoundaries = FloatRect();
m_clipper.clear();
markAllClientsForInvalidation(markForInvalidation ? LayoutAndBoundariesInvalidation : ParentOnlyInvalidation);
}
void RenderSVGResourceClipper::removeClientFromCache(RenderElement& client, bool markForInvalidation)
{
m_clipper.remove(&client);
markClientForInvalidation(client, markForInvalidation ? BoundariesInvalidation : ParentOnlyInvalidation);
}
bool RenderSVGResourceClipper::applyResource(RenderElement& renderer, const RenderStyle&, GraphicsContext*& context, unsigned short resourceMode)
{
ASSERT(context);
ASSERT_UNUSED(resourceMode, resourceMode == ApplyToDefaultMode);
return applyClippingToContext(renderer, renderer.objectBoundingBox(), renderer.repaintRectInLocalCoordinates(), *context);
}
bool RenderSVGResourceClipper::pathOnlyClipping(GraphicsContext& context, const AffineTransform& animatedLocalTransform, const FloatRect& objectBoundingBox)
{
if (!style().svgStyle().clipperResource().isEmpty())
return false;
WindRule clipRule = RULE_NONZERO;
Path clipPath = Path();
for (Node* childNode = clipPathElement().firstChild(); childNode; childNode = childNode->nextSibling()) {
RenderObject* renderer = childNode->renderer();
if (!renderer)
continue;
if (renderer->isSVGText())
return false;
if (!childNode->isSVGElement() || !downcast<SVGElement>(*childNode).isSVGGraphicsElement())
continue;
SVGGraphicsElement& styled = downcast<SVGGraphicsElement>(*childNode);
const RenderStyle& style = renderer->style();
if (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();
} else
return false;
}
if (clipPathElement().clipPathUnits() == 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(RenderElement& renderer, const FloatRect& objectBoundingBox, const FloatRect& repaintRect, GraphicsContext& context)
{
ClipperMaskImage& clipperMaskImage = addRendererToClipper(renderer);
bool shouldCreateClipperMaskImage = !clipperMaskImage;
AffineTransform animatedLocalTransform = clipPathElement().animatedLocalTransform();
if (shouldCreateClipperMaskImage && pathOnlyClipping(context, animatedLocalTransform, objectBoundingBox))
return true;
AffineTransform absoluteTransform = SVGRenderingContext::calculateTransformationToOutermostCoordinateSystem(renderer);
if (shouldCreateClipperMaskImage && !repaintRect.isEmpty()) {
clipperMaskImage = SVGRenderingContext::createImageBuffer(repaintRect, absoluteTransform, ColorSpaceSRGB, Unaccelerated);
if (!clipperMaskImage)
return false;
GraphicsContext& maskContext = clipperMaskImage->context();
maskContext.concatCTM(animatedLocalTransform);
auto* resources = SVGResourcesCache::cachedResourcesForRenderer(*this);
RenderSVGResourceClipper* clipper;
bool succeeded;
if (resources && (clipper = resources->clipper())) {
GraphicsContextStateSaver stateSaver(maskContext);
if (!clipper->applyClippingToContext(*this, objectBoundingBox, repaintRect, maskContext))
return false;
succeeded = drawContentIntoMaskImage(clipperMaskImage, objectBoundingBox);
} else
succeeded = drawContentIntoMaskImage(clipperMaskImage, objectBoundingBox);
if (!succeeded)
clipperMaskImage.reset();
}
if (!clipperMaskImage)
return false;
SVGRenderingContext::clipToImageBuffer(context, absoluteTransform, repaintRect, clipperMaskImage, shouldCreateClipperMaskImage);
return true;
}
bool RenderSVGResourceClipper::drawContentIntoMaskImage(const ClipperMaskImage& clipperMaskImage, const FloatRect& objectBoundingBox)
{
ASSERT(clipperMaskImage);
GraphicsContext& maskContext = clipperMaskImage->context();
AffineTransform maskContentTransformation;
if (clipPathElement().clipPathUnits() == SVGUnitTypes::SVG_UNIT_TYPE_OBJECTBOUNDINGBOX) {
maskContentTransformation.translate(objectBoundingBox.x(), objectBoundingBox.y());
maskContentTransformation.scaleNonUniform(objectBoundingBox.width(), objectBoundingBox.height());
maskContext.concatCTM(maskContentTransformation);
}
PaintBehavior oldBehavior = view().frameView().paintBehavior();
view().frameView().setPaintBehavior(oldBehavior | PaintBehaviorRenderingSVGMask);
for (auto& child : childrenOfType<SVGElement>(clipPathElement())) {
auto renderer = child.renderer();
if (!renderer)
continue;
if (renderer->needsLayout()) {
view().frameView().setPaintBehavior(oldBehavior);
return false;
}
const RenderStyle& style = renderer->style();
if (style.display() == NONE || style.visibility() != VISIBLE)
continue;
WindRule newClipRule = style.svgStyle().clipRule();
bool isUseElement = child.hasTagName(SVGNames::useTag);
if (isUseElement) {
SVGUseElement& useElement = downcast<SVGUseElement>(child);
renderer = useElement.rendererClipChild();
if (!renderer)
continue;
if (!useElement.hasAttributeWithoutSynchronization(SVGNames::clip_ruleAttr))
newClipRule = renderer->style().svgStyle().clipRule();
}
if (!renderer->isSVGShape() && !renderer->isSVGText())
continue;
maskContext.setFillRule(newClipRule);
SVGRenderingContext::renderSubtreeToImageBuffer(clipperMaskImage.get(), isUseElement ? *child.renderer() : *renderer, maskContentTransformation);
}
view().frameView().setPaintBehavior(oldBehavior);
return true;
}
void RenderSVGResourceClipper::calculateClipContentRepaintRect()
{
for (Node* childNode = clipPathElement().firstChild(); childNode; childNode = childNode->nextSibling()) {
RenderObject* renderer = childNode->renderer();
if (!childNode->isSVGElement() || !renderer)
continue;
if (!renderer->isSVGShape() && !renderer->isSVGText() && !childNode->hasTagName(SVGNames::useTag))
continue;
const RenderStyle& style = renderer->style();
if (style.display() == NONE || style.visibility() != VISIBLE)
continue;
m_clipBoundaries.unite(renderer->localToParentTransform().mapRect(renderer->repaintRectInLocalCoordinates()));
}
m_clipBoundaries = clipPathElement().animatedLocalTransform().mapRect(m_clipBoundaries);
}
ClipperMaskImage& RenderSVGResourceClipper::addRendererToClipper(const RenderObject& object)
{
return m_clipper.add(&object, ClipperMaskImage()).iterator->value;
}
bool RenderSVGResourceClipper::hitTestClipContent(const FloatRect& objectBoundingBox, const FloatPoint& nodeAtPoint)
{
FloatPoint point = nodeAtPoint;
if (!SVGRenderSupport::pointInClippingArea(*this, point))
return false;
if (clipPathElement().clipPathUnits() == SVGUnitTypes::SVG_UNIT_TYPE_OBJECTBOUNDINGBOX) {
AffineTransform transform;
transform.translate(objectBoundingBox.x(), objectBoundingBox.y());
transform.scaleNonUniform(objectBoundingBox.width(), objectBoundingBox.height());
point = transform.inverse().value_or(AffineTransform()).mapPoint(point);
}
point = clipPathElement().animatedLocalTransform().inverse().value_or(AffineTransform()).mapPoint(point);
for (Node* childNode = clipPathElement().firstChild(); childNode; childNode = childNode->nextSibling()) {
RenderObject* renderer = childNode->renderer();
if (!childNode->isSVGElement() || !renderer)
continue;
if (!renderer->isSVGShape() && !renderer->isSVGText() && !childNode->hasTagName(SVGNames::useTag))
continue;
IntPoint hitPoint;
HitTestResult result(hitPoint);
if (renderer->nodeAtFloatPoint(HitTestRequest(HitTestRequest::SVGClipContent | HitTestRequest::DisallowUserAgentShadowContent), result, point, HitTestForeground))
return true;
}
return false;
}
FloatRect RenderSVGResourceClipper::resourceBoundingBox(const RenderObject& object)
{
if (selfNeedsLayout()) {
addRendererToClipper(object);
return object.objectBoundingBox();
}
if (m_clipBoundaries.isEmpty())
calculateClipContentRepaintRect();
if (clipPathElement().clipPathUnits() == 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;
}
}