This source file includes following definitions.
- isChildAllowed
- removeAllClientsFromCache
- removeClientFromCache
- buildPrimitives
- adjustScaleForMaximumImageSize
- createImageBuffer
- applyResource
- postApplyResource
- resourceBoundingBox
- primitiveAttributeChanged
- drawingRegion
#include "config.h"
#include "core/rendering/svg/RenderSVGResourceFilter.h"
#include "core/frame/Settings.h"
#include "core/rendering/svg/RenderSVGResourceFilterPrimitive.h"
#include "core/rendering/svg/SVGRenderingContext.h"
#include "core/svg/SVGFilterPrimitiveStandardAttributes.h"
#include "platform/graphics/UnacceleratedImageBufferSurface.h"
#include "platform/graphics/filters/SkiaImageFilterBuilder.h"
#include "platform/graphics/filters/SourceAlpha.h"
#include "platform/graphics/filters/SourceGraphic.h"
using namespace std;
namespace WebCore {
const RenderSVGResourceType RenderSVGResourceFilter::s_resourceType = FilterResourceType;
RenderSVGResourceFilter::RenderSVGResourceFilter(SVGFilterElement* node)
: RenderSVGResourceContainer(node)
{
}
RenderSVGResourceFilter::~RenderSVGResourceFilter()
{
m_filter.clear();
}
bool RenderSVGResourceFilter::isChildAllowed(RenderObject* child, RenderStyle*) const
{
return child->isSVGResourceFilterPrimitive();
}
void RenderSVGResourceFilter::removeAllClientsFromCache(bool markForInvalidation)
{
m_filter.clear();
markAllClientsForInvalidation(markForInvalidation ? LayoutAndBoundariesInvalidation : ParentOnlyInvalidation);
}
void RenderSVGResourceFilter::removeClientFromCache(RenderObject* client, bool markForInvalidation)
{
ASSERT(client);
if (FilterData* filterData = m_filter.get(client)) {
if (filterData->savedContext)
filterData->state = FilterData::MarkedForRemoval;
else
m_filter.remove(client);
}
markClientForInvalidation(client, markForInvalidation ? BoundariesInvalidation : ParentOnlyInvalidation);
}
PassRefPtr<SVGFilterBuilder> RenderSVGResourceFilter::buildPrimitives(SVGFilter* filter)
{
SVGFilterElement* filterElement = toSVGFilterElement(element());
FloatRect targetBoundingBox = filter->targetBoundingBox();
RefPtr<SVGFilterBuilder> builder = SVGFilterBuilder::create(SourceGraphic::create(filter), SourceAlpha::create(filter));
for (SVGElement* element = Traversal<SVGElement>::firstChild(*filterElement); element; element = Traversal<SVGElement>::nextSibling(*element)) {
if (!element->isFilterEffect() || !element->renderer())
continue;
SVGFilterPrimitiveStandardAttributes* effectElement = static_cast<SVGFilterPrimitiveStandardAttributes*>(element);
RefPtr<FilterEffect> effect = effectElement->build(builder.get(), filter);
if (!effect) {
builder->clearEffects();
return nullptr;
}
builder->appendEffectToEffectReferences(effect, effectElement->renderer());
effectElement->setStandardAttributes(effect.get());
effect->setEffectBoundaries(SVGLengthContext::resolveRectangle<SVGFilterPrimitiveStandardAttributes>(effectElement, filterElement->primitiveUnits()->currentValue()->enumValue(), targetBoundingBox));
effect->setOperatingColorSpace(
effectElement->renderer()->style()->svgStyle()->colorInterpolationFilters() == CI_LINEARRGB ? ColorSpaceLinearRGB : ColorSpaceDeviceRGB);
builder->add(AtomicString(effectElement->result()->currentValue()->value()), effect);
}
return builder.release();
}
void RenderSVGResourceFilter::adjustScaleForMaximumImageSize(const FloatSize& size, FloatSize& filterScale)
{
FloatSize scaledSize(size);
scaledSize.scale(filterScale.width(), filterScale.height());
float scaledArea = scaledSize.width() * scaledSize.height();
if (scaledArea <= FilterEffect::maxFilterArea())
return;
filterScale.scale(sqrt(FilterEffect::maxFilterArea() / scaledArea));
}
static bool createImageBuffer(const Filter* filter, OwnPtr<ImageBuffer>& imageBuffer)
{
IntRect paintRect = filter->sourceImageRect();
if (paintRect.isEmpty())
return false;
OwnPtr<ImageBufferSurface> surface = adoptPtr(new UnacceleratedImageBufferSurface(paintRect.size()));
if (!surface->isValid())
return false;
OwnPtr<ImageBuffer> image = ImageBuffer::create(surface.release());
GraphicsContext* imageContext = image->context();
ASSERT(imageContext);
imageContext->translate(-paintRect.x(), -paintRect.y());
imageContext->concatCTM(filter->absoluteTransform());
imageBuffer = image.release();
return true;
}
bool RenderSVGResourceFilter::applyResource(RenderObject* object, RenderStyle*, GraphicsContext*& context, unsigned short resourceMode)
{
ASSERT(object);
ASSERT(context);
ASSERT_UNUSED(resourceMode, resourceMode == ApplyToDefaultMode);
clearInvalidationMask();
bool deferredFiltersEnabled = object->document().settings()->deferredFiltersEnabled();
if (deferredFiltersEnabled) {
if (m_objects.contains(object))
return false;
} else if (m_filter.contains(object)) {
FilterData* filterData = m_filter.get(object);
if (filterData->state == FilterData::PaintingSource || filterData->state == FilterData::Applying)
filterData->state = FilterData::CycleDetected;
return false;
}
OwnPtr<FilterData> filterData(adoptPtr(new FilterData));
FloatRect targetBoundingBox = object->objectBoundingBox();
SVGFilterElement* filterElement = toSVGFilterElement(element());
filterData->boundaries = SVGLengthContext::resolveRectangle<SVGFilterElement>(filterElement, filterElement->filterUnits()->currentValue()->enumValue(), targetBoundingBox);
if (filterData->boundaries.isEmpty())
return false;
AffineTransform absoluteTransform;
SVGRenderingContext::calculateDeviceSpaceTransformation(object, absoluteTransform);
if (!absoluteTransform.isInvertible())
return false;
FloatSize filterScale;
if (filterElement->hasAttribute(SVGNames::filterResAttr)) {
filterScale = FloatSize(
filterElement->filterResX()->currentValue()->value() / filterData->boundaries.width(),
filterElement->filterResY()->currentValue()->value() / filterData->boundaries.height());
} else {
filterScale = FloatSize(absoluteTransform.xScale(), absoluteTransform.yScale());
}
adjustScaleForMaximumImageSize(filterData->boundaries.size(), filterScale);
filterData->drawingRegion = object->strokeBoundingBox();
filterData->drawingRegion.intersect(filterData->boundaries);
FloatRect absoluteDrawingRegion = filterData->drawingRegion;
if (!deferredFiltersEnabled)
absoluteDrawingRegion.scale(filterScale.width(), filterScale.height());
IntRect intDrawingRegion = enclosingIntRect(absoluteDrawingRegion);
bool primitiveBoundingBoxMode = filterElement->primitiveUnits()->currentValue()->enumValue() == SVGUnitTypes::SVG_UNIT_TYPE_OBJECTBOUNDINGBOX;
filterData->shearFreeAbsoluteTransform = AffineTransform();
if (!deferredFiltersEnabled)
filterData->shearFreeAbsoluteTransform.scale(filterScale.width(), filterScale.height());
filterData->filter = SVGFilter::create(filterData->shearFreeAbsoluteTransform, intDrawingRegion, targetBoundingBox, filterData->boundaries, primitiveBoundingBoxMode);
filterData->builder = buildPrimitives(filterData->filter.get());
if (!filterData->builder)
return false;
FilterEffect* lastEffect = filterData->builder->lastEffect();
if (!lastEffect)
return false;
lastEffect->determineFilterPrimitiveSubregion(ClipToFilterRegion);
if (deferredFiltersEnabled) {
SkiaImageFilterBuilder builder(context);
m_objects.add(object);
RefPtr<ImageFilter> imageFilter = builder.build(lastEffect, ColorSpaceDeviceRGB);
FloatRect boundaries = enclosingIntRect(filterData->boundaries);
context->save();
context->clipRect(boundaries);
if (filterElement->hasAttribute(SVGNames::filterResAttr)) {
FloatSize size = context->getCTM().mapSize(boundaries.size());
FloatSize filterResScale(
filterElement->filterResX()->currentValue()->value() / size.width(),
filterElement->filterResY()->currentValue()->value() / size.height());
context->scale(filterResScale);
AffineTransform resizeMatrix;
resizeMatrix.scale(1 / filterResScale.width(), 1 / filterResScale.height());
imageFilter = builder.buildTransform(resizeMatrix, imageFilter.get());
}
AffineTransform ctm = context->getCTM();
if (ctm.b() || ctm.c()) {
AffineTransform scaleAndTranslate;
scaleAndTranslate.translate(ctm.e(), ctm.f());
scaleAndTranslate.scale(ctm.xScale(), ctm.yScale());
ASSERT(scaleAndTranslate.isInvertible());
AffineTransform shearAndRotate = scaleAndTranslate.inverse();
shearAndRotate.multiply(ctm);
context->setCTM(scaleAndTranslate);
imageFilter = builder.buildTransform(shearAndRotate, imageFilter.get());
}
context->beginLayer(1, CompositeSourceOver, &boundaries, ColorFilterNone, imageFilter.get());
return true;
}
if (filterData->drawingRegion.isEmpty()) {
ASSERT(!m_filter.contains(object));
filterData->savedContext = context;
m_filter.set(object, filterData.release());
return false;
}
OwnPtr<ImageBuffer> sourceGraphic;
if (!createImageBuffer(filterData->filter.get(), sourceGraphic)) {
ASSERT(!m_filter.contains(object));
filterData->savedContext = context;
m_filter.set(object, filterData.release());
return false;
}
GraphicsContext* sourceGraphicContext = sourceGraphic->context();
ASSERT(sourceGraphicContext);
filterData->sourceGraphicBuffer = sourceGraphic.release();
filterData->savedContext = context;
context = sourceGraphicContext;
ASSERT(!m_filter.contains(object));
m_filter.set(object, filterData.release());
return true;
}
void RenderSVGResourceFilter::postApplyResource(RenderObject* object, GraphicsContext*& context, unsigned short resourceMode, const Path*, const RenderSVGShape*)
{
ASSERT(object);
ASSERT(context);
ASSERT_UNUSED(resourceMode, resourceMode == ApplyToDefaultMode);
if (object->document().settings()->deferredFiltersEnabled()) {
context->endLayer();
context->restore();
m_objects.remove(object);
return;
}
FilterData* filterData = m_filter.get(object);
if (!filterData)
return;
switch (filterData->state) {
case FilterData::MarkedForRemoval:
m_filter.remove(object);
return;
case FilterData::CycleDetected:
case FilterData::Applying:
filterData->state = FilterData::PaintingSource;
return;
case FilterData::PaintingSource:
if (!filterData->savedContext) {
removeClientFromCache(object);
return;
}
context = filterData->savedContext;
filterData->savedContext = 0;
break;
case FilterData::Built: { }
}
FilterEffect* lastEffect = filterData->builder->lastEffect();
if (lastEffect && !filterData->boundaries.isEmpty() && !lastEffect->filterPrimitiveSubregion().isEmpty()) {
if (filterData->state != FilterData::Built)
filterData->filter->setSourceImage(filterData->sourceGraphicBuffer.release());
if (!lastEffect->hasResult()) {
filterData->state = FilterData::Applying;
lastEffect->apply();
lastEffect->correctFilterResultIfNeeded();
lastEffect->transformResultColorSpace(ColorSpaceDeviceRGB);
}
filterData->state = FilterData::Built;
ImageBuffer* resultImage = lastEffect->asImageBuffer();
if (resultImage) {
context->drawImageBuffer(resultImage, filterData->filter->mapAbsoluteRectToLocalRect(lastEffect->absolutePaintRect()));
}
}
filterData->sourceGraphicBuffer.clear();
}
FloatRect RenderSVGResourceFilter::resourceBoundingBox(const RenderObject* object)
{
if (SVGFilterElement* element = toSVGFilterElement(this->element()))
return SVGLengthContext::resolveRectangle<SVGFilterElement>(element, element->filterUnits()->currentValue()->enumValue(), object->objectBoundingBox());
return FloatRect();
}
void RenderSVGResourceFilter::primitiveAttributeChanged(RenderObject* object, const QualifiedName& attribute)
{
if (object->document().settings()->deferredFiltersEnabled()) {
markAllClientsForInvalidation(RepaintInvalidation);
markAllClientLayersForInvalidation();
return;
}
FilterMap::iterator it = m_filter.begin();
FilterMap::iterator end = m_filter.end();
SVGFilterPrimitiveStandardAttributes* primitve = static_cast<SVGFilterPrimitiveStandardAttributes*>(object->node());
for (; it != end; ++it) {
FilterData* filterData = it->value.get();
if (filterData->state != FilterData::Built)
continue;
SVGFilterBuilder* builder = filterData->builder.get();
FilterEffect* effect = builder->effectByRenderer(object);
if (!effect)
continue;
if (!primitve->setFilterEffectAttribute(effect, attribute))
return;
builder->clearResultsRecursive(effect);
markClientForInvalidation(it->key, RepaintInvalidation);
}
markAllClientLayersForInvalidation();
}
FloatRect RenderSVGResourceFilter::drawingRegion(RenderObject* object) const
{
FilterData* filterData = m_filter.get(object);
return filterData ? filterData->drawingRegion : FloatRect();
}
}