RenderSVGImage.cpp [plain text]
#include "config.h"
#if ENABLE(SVG)
#include "RenderSVGImage.h"
#include "Attr.h"
#include "FloatConversion.h"
#include "FloatQuad.h"
#include "GraphicsContext.h"
#include "LayoutRepainter.h"
#include "PointerEventsHitRules.h"
#include "RenderImageResource.h"
#include "RenderLayer.h"
#include "RenderSVGResourceContainer.h"
#include "RenderSVGResourceFilter.h"
#include "SVGImageElement.h"
#include "SVGLength.h"
#include "SVGPreserveAspectRatio.h"
#include "SVGRenderingContext.h"
#include "SVGResources.h"
#include "SVGResourcesCache.h"
#include <wtf/StackStats.h>
namespace WebCore {
RenderSVGImage::RenderSVGImage(SVGImageElement* impl)
: RenderSVGModelObject(impl)
, m_needsBoundariesUpdate(true)
, m_needsTransformUpdate(true)
, m_imageResource(RenderImageResource::create())
{
m_imageResource->initialize(this);
}
RenderSVGImage::~RenderSVGImage()
{
m_imageResource->shutdown();
}
bool RenderSVGImage::updateImageViewport()
{
SVGImageElement* image = static_cast<SVGImageElement*>(node());
FloatRect oldBoundaries = m_objectBoundingBox;
SVGLengthContext lengthContext(image);
m_objectBoundingBox = FloatRect(image->x().value(lengthContext), image->y().value(lengthContext), image->width().value(lengthContext), image->height().value(lengthContext));
if (oldBoundaries == m_objectBoundingBox)
return false;
m_imageResource->setContainerSizeForRenderer(enclosingIntRect(m_objectBoundingBox).size());
m_needsBoundariesUpdate = true;
return true;
}
void RenderSVGImage::layout()
{
StackStats::LayoutCheckPoint layoutCheckPoint;
ASSERT(needsLayout());
LayoutRepainter repainter(*this, SVGRenderSupport::checkForSVGRepaintDuringLayout(this) && selfNeedsLayout());
updateImageViewport();
bool transformOrBoundariesUpdate = m_needsTransformUpdate || m_needsBoundariesUpdate;
if (m_needsTransformUpdate) {
m_localTransform = static_cast<SVGImageElement*>(node())->animatedLocalTransform();
m_needsTransformUpdate = false;
}
if (m_needsBoundariesUpdate) {
m_repaintBoundingBoxExcludingShadow = m_objectBoundingBox;
SVGRenderSupport::intersectRepaintRectWithResources(this, m_repaintBoundingBoxExcludingShadow);
m_repaintBoundingBox = m_repaintBoundingBoxExcludingShadow;
SVGRenderSupport::intersectRepaintRectWithShadows(this, m_repaintBoundingBox);
m_needsBoundariesUpdate = false;
}
if (everHadLayout() && selfNeedsLayout())
SVGResourcesCache::clientLayoutChanged(this);
if (transformOrBoundariesUpdate)
RenderSVGModelObject::setNeedsBoundariesUpdate();
repainter.repaintAfterLayout();
setNeedsLayout(false);
}
void RenderSVGImage::paint(PaintInfo& paintInfo, const LayoutPoint&)
{
if (paintInfo.context->paintingDisabled() || style()->visibility() == HIDDEN || !m_imageResource->hasImage())
return;
FloatRect boundingBox = repaintRectInLocalCoordinates();
if (!SVGRenderSupport::paintInfoIntersectsRepaintRect(boundingBox, m_localTransform, paintInfo))
return;
PaintInfo childPaintInfo(paintInfo);
bool drawsOutline = style()->outlineWidth() && (childPaintInfo.phase == PaintPhaseOutline || childPaintInfo.phase == PaintPhaseSelfOutline);
if (drawsOutline || childPaintInfo.phase == PaintPhaseForeground) {
GraphicsContextStateSaver stateSaver(*childPaintInfo.context);
childPaintInfo.applyTransform(m_localTransform);
if (childPaintInfo.phase == PaintPhaseForeground) {
SVGRenderingContext renderingContext(this, childPaintInfo);
if (renderingContext.isRenderingPrepared()) {
if (style()->svgStyle()->bufferedRendering() == BR_STATIC && renderingContext.bufferForeground(m_bufferedForeground))
return;
paintForeground(childPaintInfo);
}
}
if (drawsOutline)
paintOutline(childPaintInfo.context, IntRect(boundingBox));
}
}
void RenderSVGImage::paintForeground(PaintInfo& paintInfo)
{
RefPtr<Image> image = m_imageResource->image();
FloatRect destRect = m_objectBoundingBox;
FloatRect srcRect(0, 0, image->width(), image->height());
SVGImageElement* imageElement = static_cast<SVGImageElement*>(node());
imageElement->preserveAspectRatio().transformRect(destRect, srcRect);
paintInfo.context->drawImage(image.get(), ColorSpaceDeviceRGB, destRect, srcRect);
}
void RenderSVGImage::invalidateBufferedForeground()
{
m_bufferedForeground.clear();
}
bool RenderSVGImage::nodeAtFloatPoint(const HitTestRequest& request, HitTestResult& result, const FloatPoint& pointInParent, HitTestAction hitTestAction)
{
if (hitTestAction != HitTestForeground)
return false;
PointerEventsHitRules hitRules(PointerEventsHitRules::SVG_IMAGE_HITTESTING, request, style()->pointerEvents());
bool isVisible = (style()->visibility() == VISIBLE);
if (isVisible || !hitRules.requireVisible) {
FloatPoint localPoint = localToParentTransform().inverse().mapPoint(pointInParent);
if (!SVGRenderSupport::pointInClippingArea(this, localPoint))
return false;
if (hitRules.canHitFill) {
if (m_objectBoundingBox.contains(localPoint)) {
updateHitTestResult(result, roundedLayoutPoint(localPoint));
return true;
}
}
}
return false;
}
void RenderSVGImage::imageChanged(WrappedImagePtr, const IntRect*)
{
if (SVGResources* resources = SVGResourcesCache::cachedResourcesForRenderObject(this))
resources->removeClientFromCache(this);
RenderSVGResource::markForLayoutAndParentResourceInvalidation(this, false);
m_objectBoundingBox = FloatRect();
updateImageViewport();
invalidateBufferedForeground();
repaint();
}
void RenderSVGImage::addFocusRingRects(Vector<IntRect>& rects, const LayoutPoint&)
{
IntRect contentRect = enclosingIntRect(repaintRectInLocalCoordinates());
if (!contentRect.isEmpty())
rects.append(contentRect);
}
}
#endif // ENABLE(SVG)