This source file includes following definitions.
- m_preserveAspectRatio
- m_preserveAspectRatio
- createWithImage
- createWithIRIReference
- getRendererRepaintRect
- determineAbsolutePaintRect
- referencedRenderer
- applySoftware
- externalRepresentation
- createImageFilterForRenderer
- createImageFilter
#include "config.h"
#include "core/svg/graphics/filters/SVGFEImage.h"
#include "SkBitmapSource.h"
#include "SkPictureImageFilter.h"
#include "core/rendering/RenderObject.h"
#include "core/rendering/svg/SVGRenderingContext.h"
#include "core/svg/SVGElement.h"
#include "core/svg/SVGURIReference.h"
#include "platform/graphics/DisplayList.h"
#include "platform/graphics/GraphicsContext.h"
#include "platform/graphics/filters/Filter.h"
#include "platform/graphics/filters/SkiaImageFilterBuilder.h"
#include "platform/text/TextStream.h"
#include "platform/transforms/AffineTransform.h"
namespace WebCore {
FEImage::FEImage(Filter* filter, PassRefPtr<Image> image, PassRefPtr<SVGPreserveAspectRatio> preserveAspectRatio)
: FilterEffect(filter)
, m_image(image)
, m_document(0)
, m_preserveAspectRatio(preserveAspectRatio)
{
}
FEImage::FEImage(Filter* filter, Document& document, const String& href, PassRefPtr<SVGPreserveAspectRatio> preserveAspectRatio)
: FilterEffect(filter)
, m_document(&document)
, m_href(href)
, m_preserveAspectRatio(preserveAspectRatio)
{
}
PassRefPtr<FEImage> FEImage::createWithImage(Filter* filter, PassRefPtr<Image> image, PassRefPtr<SVGPreserveAspectRatio> preserveAspectRatio)
{
return adoptRef(new FEImage(filter, image, preserveAspectRatio));
}
PassRefPtr<FEImage> FEImage::createWithIRIReference(Filter* filter, Document& document, const String& href, PassRefPtr<SVGPreserveAspectRatio> preserveAspectRatio)
{
return adoptRef(new FEImage(filter, document, href, preserveAspectRatio));
}
static FloatRect getRendererRepaintRect(RenderObject* renderer)
{
return renderer->localToParentTransform().mapRect(
renderer->repaintRectInLocalCoordinates());
}
FloatRect FEImage::determineAbsolutePaintRect(const FloatRect& originalRequestedRect)
{
RenderObject* renderer = referencedRenderer();
if (!m_image && !renderer)
return FloatRect();
FloatRect requestedRect = originalRequestedRect;
if (clipsToBounds())
requestedRect.intersect(maxEffectRect());
FloatRect destRect = filter()->mapLocalRectToAbsoluteRect(filterPrimitiveSubregion());
FloatRect srcRect;
if (renderer) {
srcRect = getRendererRepaintRect(renderer);
SVGElement* contextNode = toSVGElement(renderer->node());
if (contextNode->hasRelativeLengths()) {
SVGLengthContext lengthContext(contextNode);
FloatSize viewportSize;
if (lengthContext.determineViewport(viewportSize)) {
srcRect = makeMapBetweenRects(FloatRect(FloatPoint(), viewportSize), destRect).mapRect(srcRect);
}
} else {
srcRect = filter()->mapLocalRectToAbsoluteRect(srcRect);
srcRect.move(destRect.x(), destRect.y());
}
destRect.intersect(srcRect);
} else {
srcRect = FloatRect(FloatPoint(), m_image->size());
m_preserveAspectRatio->transformRect(destRect, srcRect);
}
destRect.intersect(requestedRect);
addAbsolutePaintRect(destRect);
return destRect;
}
RenderObject* FEImage::referencedRenderer() const
{
if (!m_document)
return 0;
Element* hrefElement = SVGURIReference::targetElementFromIRIString(m_href, *m_document);
if (!hrefElement || !hrefElement->isSVGElement())
return 0;
return hrefElement->renderer();
}
void FEImage::applySoftware()
{
RenderObject* renderer = referencedRenderer();
if (!m_image && !renderer)
return;
ImageBuffer* resultImage = createImageBufferResult();
if (!resultImage)
return;
IntPoint paintLocation = absolutePaintRect().location();
resultImage->context()->translate(-paintLocation.x(), -paintLocation.y());
setResultColorSpace(ColorSpaceDeviceRGB);
FloatRect destRect = filter()->mapLocalRectToAbsoluteRect(filterPrimitiveSubregion());
FloatRect srcRect;
if (!renderer) {
srcRect = FloatRect(FloatPoint(), m_image->size());
m_preserveAspectRatio->transformRect(destRect, srcRect);
resultImage->context()->drawImage(m_image.get(), destRect, srcRect);
return;
}
SVGElement* contextNode = toSVGElement(renderer->node());
if (contextNode->hasRelativeLengths()) {
SVGLengthContext lengthContext(contextNode);
FloatSize viewportSize;
if (lengthContext.determineViewport(viewportSize))
resultImage->context()->concatCTM(makeMapBetweenRects(FloatRect(FloatPoint(), viewportSize), destRect));
} else {
resultImage->context()->translate(destRect.x(), destRect.y());
resultImage->context()->concatCTM(filter()->absoluteTransform());
}
AffineTransform contentTransformation;
SVGRenderingContext::renderSubtree(resultImage->context(), renderer, contentTransformation);
}
TextStream& FEImage::externalRepresentation(TextStream& ts, int indent) const
{
IntSize imageSize;
if (m_image)
imageSize = m_image->size();
else if (RenderObject* renderer = referencedRenderer())
imageSize = enclosingIntRect(getRendererRepaintRect(renderer)).size();
writeIndent(ts, indent);
ts << "[feImage";
FilterEffect::externalRepresentation(ts);
ts << " image-size=\"" << imageSize.width() << "x" << imageSize.height() << "\"]\n";
return ts;
}
PassRefPtr<SkImageFilter> FEImage::createImageFilterForRenderer(RenderObject* renderer, SkiaImageFilterBuilder* builder)
{
FloatRect dstRect = filterPrimitiveSubregion();
AffineTransform transform;
SVGElement* contextNode = toSVGElement(renderer->node());
if (contextNode->hasRelativeLengths()) {
SVGLengthContext lengthContext(contextNode);
FloatSize viewportSize;
if (lengthContext.determineViewport(viewportSize))
transform = makeMapBetweenRects(FloatRect(FloatPoint(), viewportSize), dstRect);
} else {
transform.translate(dstRect.x(), dstRect.y());
}
GraphicsContext* context = builder->context();
if (!context)
return adoptRef(new SkBitmapSource(SkBitmap()));
AffineTransform contentTransformation;
context->save();
context->beginRecording(FloatRect(FloatPoint(), dstRect.size()));
context->concatCTM(transform);
SVGRenderingContext::renderSubtree(context, renderer, contentTransformation);
RefPtr<DisplayList> displayList = context->endRecording();
context->restore();
RefPtr<SkImageFilter> result = adoptRef(new SkPictureImageFilter(displayList->picture(), dstRect));
return result.release();
}
PassRefPtr<SkImageFilter> FEImage::createImageFilter(SkiaImageFilterBuilder* builder)
{
RenderObject* renderer = referencedRenderer();
if (!m_image && !renderer)
return adoptRef(new SkBitmapSource(SkBitmap()));
setOperatingColorSpace(ColorSpaceDeviceRGB);
if (renderer)
return createImageFilterForRenderer(renderer, builder);
FloatRect srcRect = FloatRect(FloatPoint(), m_image->size());
FloatRect dstRect = filterPrimitiveSubregion();
if (dstRect.isEmpty())
dstRect = srcRect;
m_preserveAspectRatio->transformRect(dstRect, srcRect);
if (!m_image->nativeImageForCurrentFrame())
return adoptRef(new SkBitmapSource(SkBitmap()));
RefPtr<SkImageFilter> result = adoptRef(new SkBitmapSource(m_image->nativeImageForCurrentFrame()->bitmap(), srcRect, dstRect));
return result.release();
}
}