RenderSVGResource.cpp [plain text]
#include "config.h"
#include "RenderSVGResource.h"
#include "Frame.h"
#include "FrameView.h"
#include "RenderSVGResourceClipper.h"
#include "RenderSVGResourceFilter.h"
#include "RenderSVGResourceMasker.h"
#include "RenderSVGResourceSolidColor.h"
#include "RenderView.h"
#include "SVGResources.h"
#include "SVGResourcesCache.h"
#include "SVGURIReference.h"
namespace WebCore {
static inline bool inheritColorFromParentStyleIfNeeded(RenderElement& object, bool applyToFill, Color& color)
{
if (color.isValid())
return true;
if (!object.parent())
return false;
const SVGRenderStyle& parentSVGStyle = object.parent()->style().svgStyle();
color = applyToFill ? parentSVGStyle.fillPaintColor() : parentSVGStyle.strokePaintColor();
return true;
}
static inline RenderSVGResource* requestPaintingResource(RenderSVGResourceMode mode, RenderElement& renderer, const RenderStyle& style, Color& fallbackColor)
{
const SVGRenderStyle& svgStyle = style.svgStyle();
bool isRenderingMask = renderer.view().frameView().paintBehavior().contains(PaintBehavior::RenderingSVGMask);
if (mode == RenderSVGResourceMode::ApplyToFill) {
if (isRenderingMask) {
RenderSVGResourceSolidColor* colorResource = RenderSVGResource::sharedSolidPaintingResource();
colorResource->setColor(SVGRenderStyle::initialFillPaintColor());
return colorResource;
}
if (!svgStyle.hasFill())
return nullptr;
} else {
if (!svgStyle.hasStroke() || isRenderingMask)
return nullptr;
}
bool applyToFill = mode == RenderSVGResourceMode::ApplyToFill;
SVGPaintType paintType = applyToFill ? svgStyle.fillPaintType() : svgStyle.strokePaintType();
if (paintType == SVGPaintType::None)
return nullptr;
Color color;
switch (paintType) {
case SVGPaintType::CurrentColor:
case SVGPaintType::RGBColor:
case SVGPaintType::URICurrentColor:
case SVGPaintType::URIRGBColor:
color = applyToFill ? svgStyle.fillPaintColor() : svgStyle.strokePaintColor();
break;
default:
break;
}
if (style.insideLink() == InsideLink::InsideVisited) {
SVGPaintType visitedPaintType = applyToFill ? svgStyle.visitedLinkFillPaintType() : svgStyle.visitedLinkStrokePaintType();
if (visitedPaintType < SVGPaintType::URINone && visitedPaintType != SVGPaintType::CurrentColor) {
const Color& visitedColor = applyToFill ? svgStyle.visitedLinkFillPaintColor() : svgStyle.visitedLinkStrokePaintColor();
if (visitedColor.isValid())
color = visitedColor.colorWithAlpha(color.alphaAsFloat());
}
}
RenderSVGResourceSolidColor* colorResource = RenderSVGResource::sharedSolidPaintingResource();
if (paintType < SVGPaintType::URINone) {
if (!inheritColorFromParentStyleIfNeeded(renderer, applyToFill, color))
return nullptr;
colorResource->setColor(color);
return colorResource;
}
auto* resources = SVGResourcesCache::cachedResourcesForRenderer(renderer);
if (!resources) {
if (paintType == SVGPaintType::URINone || !inheritColorFromParentStyleIfNeeded(renderer, applyToFill, color))
return nullptr;
colorResource->setColor(color);
return colorResource;
}
RenderSVGResource* uriResource = mode == RenderSVGResourceMode::ApplyToFill ? resources->fill() : resources->stroke();
if (!uriResource) {
if (!inheritColorFromParentStyleIfNeeded(renderer, applyToFill, color))
return nullptr;
colorResource->setColor(color);
return colorResource;
}
fallbackColor = color;
return uriResource;
}
RenderSVGResource* RenderSVGResource::fillPaintingResource(RenderElement& renderer, const RenderStyle& style, Color& fallbackColor)
{
return requestPaintingResource(RenderSVGResourceMode::ApplyToFill, renderer, style, fallbackColor);
}
RenderSVGResource* RenderSVGResource::strokePaintingResource(RenderElement& renderer, const RenderStyle& style, Color& fallbackColor)
{
return requestPaintingResource(RenderSVGResourceMode::ApplyToStroke, renderer, style, fallbackColor);
}
RenderSVGResourceSolidColor* RenderSVGResource::sharedSolidPaintingResource()
{
static RenderSVGResourceSolidColor* s_sharedSolidPaintingResource = 0;
if (!s_sharedSolidPaintingResource)
s_sharedSolidPaintingResource = new RenderSVGResourceSolidColor;
return s_sharedSolidPaintingResource;
}
static inline void removeFromCacheAndInvalidateDependencies(RenderElement& renderer, bool needsLayout)
{
if (auto* resources = SVGResourcesCache::cachedResourcesForRenderer(renderer)) {
if (RenderSVGResourceFilter* filter = resources->filter())
filter->removeClientFromCache(renderer);
if (RenderSVGResourceMasker* masker = resources->masker())
masker->removeClientFromCache(renderer);
if (RenderSVGResourceClipper* clipper = resources->clipper())
clipper->removeClientFromCache(renderer);
}
if (!is<SVGElement>(renderer.element()))
return;
auto* dependencies = renderer.document().accessSVGExtensions().setOfElementsReferencingTarget(downcast<SVGElement>(*renderer.element()));
if (!dependencies)
return;
for (auto* element : *dependencies) {
if (auto* renderer = element->renderer()) {
static NeverDestroyed<HashSet<SVGElement*>> invalidatingDependencies;
if (UNLIKELY(!invalidatingDependencies.get().add(element).isNewEntry)) {
continue;
}
RenderSVGResource::markForLayoutAndParentResourceInvalidation(*renderer, needsLayout);
invalidatingDependencies.get().remove(element);
}
}
}
void RenderSVGResource::markForLayoutAndParentResourceInvalidation(RenderObject& object, bool needsLayout)
{
ASSERT(object.node());
if (needsLayout && !object.renderTreeBeingDestroyed())
object.setNeedsLayout();
if (is<RenderElement>(object))
removeFromCacheAndInvalidateDependencies(downcast<RenderElement>(object), needsLayout);
auto current = object.parent();
while (current) {
removeFromCacheAndInvalidateDependencies(*current, needsLayout);
if (is<RenderSVGResourceContainer>(*current)) {
downcast<RenderSVGResourceContainer>(*current).removeAllClientsFromCache();
break;
}
current = current->parent();
}
}
}