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 {
RenderSVGResourceType RenderSVGResourceClipper::s_resourceType = ClipperResourceType;
RenderSVGResourceClipper::RenderSVGResourceClipper(SVGClipPathElement& element, PassRef<RenderStyle> style)
: RenderSVGResourceContainer(element, WTF::move(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() || !toSVGElement(childNode)->isSVGGraphicsElement())
continue;
SVGGraphicsElement* styled = toSVGGraphicsElement(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)
{
bool missingClipperData = !m_clipper.contains(&renderer);
if (missingClipperData)
m_clipper.set(&renderer, std::make_unique<ClipperData>());
bool shouldCreateClipData = false;
AffineTransform animatedLocalTransform = clipPathElement().animatedLocalTransform();
ClipperData* clipperData = m_clipper.get(&renderer);
if (!clipperData->clipMaskImage) {
if (pathOnlyClipping(context, animatedLocalTransform, objectBoundingBox))
return true;
shouldCreateClipData = true;
}
AffineTransform absoluteTransform;
SVGRenderingContext::calculateTransformationToOutermostCoordinateSystem(renderer, absoluteTransform);
if (shouldCreateClipData && !repaintRect.isEmpty()) {
if (!SVGRenderingContext::createImageBuffer(repaintRect, absoluteTransform, clipperData->clipMaskImage, ColorSpaceDeviceRGB, Unaccelerated))
return false;
GraphicsContext* maskContext = clipperData->clipMaskImage->context();
ASSERT(maskContext);
maskContext->concatCTM(animatedLocalTransform);
SVGResources* resources = SVGResourcesCache::cachedResourcesForRenderObject(*this);
RenderSVGResourceClipper* clipper;
bool succeeded;
if (resources && (clipper = resources->clipper())) {
GraphicsContextStateSaver stateSaver(*maskContext);
if (!clipper->applyClippingToContext(*this, objectBoundingBox, repaintRect, maskContext))
return false;
succeeded = drawContentIntoMaskImage(clipperData, objectBoundingBox);
} else
succeeded = drawContentIntoMaskImage(clipperData, objectBoundingBox);
if (!succeeded)
clipperData->clipMaskImage.reset();
}
if (!clipperData->clipMaskImage)
return false;
SVGRenderingContext::clipToImageBuffer(context, absoluteTransform, repaintRect, clipperData->clipMaskImage, missingClipperData);
return true;
}
bool RenderSVGResourceClipper::drawContentIntoMaskImage(ClipperData* clipperData, const FloatRect& objectBoundingBox)
{
ASSERT(clipperData);
ASSERT(clipperData->clipMaskImage);
GraphicsContext* maskContext = clipperData->clipMaskImage->context();
ASSERT(maskContext);
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 = toSVGUseElement(child);
renderer = useElement.rendererClipChild();
if (!renderer)
continue;
if (!useElement.hasAttribute(SVGNames::clip_ruleAttr))
newClipRule = renderer->style().svgStyle().clipRule();
}
if (!renderer->isSVGShape() && !renderer->isSVGText())
continue;
maskContext->setFillRule(newClipRule);
SVGRenderingContext::renderSubtreeToImageBuffer(clipperData->clipMaskImage.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);
}
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().mapPoint(point);
}
point = clipPathElement().animatedLocalTransform().inverse().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::DisallowShadowContent), result, point, HitTestForeground))
return true;
}
return false;
}
FloatRect RenderSVGResourceClipper::resourceBoundingBox(const RenderObject& object)
{
if (selfNeedsLayout())
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;
}
}