[SVG] Cached filter results are not invalidated on repaint rect change
[WebKit-https.git] / Source / WebCore / rendering / svg / SVGRenderingContext.cpp
index 0f13dcf..e275932 100644 (file)
 #if ENABLE(SVG)
 #include "SVGRenderingContext.h"
 
+#include "BasicShapes.h"
 #include "Frame.h"
 #include "FrameView.h"
 #include "RenderSVGResource.h"
 #include "RenderSVGResourceClipper.h"
 #include "RenderSVGResourceFilter.h"
 #include "RenderSVGResourceMasker.h"
-#include "SVGImageBufferTools.h"
 #include "SVGResources.h"
 #include "SVGResourcesCache.h"
 
+static int kMaxImageBufferSize = 4096;
+
 namespace WebCore {
 
 static inline bool isRenderingMaskImage(RenderObject* object)
@@ -59,6 +61,7 @@ SVGRenderingContext::~SVGRenderingContext()
         ASSERT(m_filter);
         m_filter->postApplyResource(static_cast<RenderSVGShape*>(m_object), m_paintInfo->context, ApplyToDefaultMode, 0, 0);
         m_paintInfo->context = m_savedContext;
+        m_paintInfo->rect = m_savedPaintRect;
     }
 #endif
 
@@ -121,6 +124,12 @@ void SVGRenderingContext::prepareToRenderSVGContent(RenderObject* object, PaintI
         }
     }
 
+    ClipPathOperation* clipPathOperation = style->clipPath();
+    if (clipPathOperation && clipPathOperation->getOperationType() == ClipPathOperation::SHAPE) {
+        ShapeClipPathOperation* clipPath = static_cast<ShapeClipPathOperation*>(clipPathOperation);
+        m_paintInfo->context->clipPath(clipPath->path(object->objectBoundingBox()), clipPath->windRule());
+    }
+
     SVGResources* resources = SVGResourcesCache::cachedResourcesForRenderObject(m_object);
     if (!resources) {
 #if ENABLE(FILTERS)
@@ -138,7 +147,8 @@ void SVGRenderingContext::prepareToRenderSVGContent(RenderObject* object, PaintI
         }
     }
 
-    if (RenderSVGResourceClipper* clipper = resources->clipper()) {
+    RenderSVGResourceClipper* clipper = resources->clipper();
+    if (!clipPathOperation && clipper) {
         if (!clipper->applyResource(m_object, style, m_paintInfo->context, ApplyToDefaultMode))
             return;
     }
@@ -148,11 +158,18 @@ void SVGRenderingContext::prepareToRenderSVGContent(RenderObject* object, PaintI
         m_filter = resources->filter();
         if (m_filter) {
             m_savedContext = m_paintInfo->context;
+            m_savedPaintRect = m_paintInfo->rect;
             // Return with false here may mean that we don't need to draw the content
             // (because it was either drawn before or empty) but we still need to apply the filter.
             m_renderingFlags |= EndFilterLayer;
             if (!m_filter->applyResource(m_object, style, m_paintInfo->context, ApplyToDefaultMode))
                 return;
+
+            // Since we're caching the resulting bitmap and do not invalidate it on repaint rect
+            // changes, we need to paint the whole filter region. Otherwise, elements not visible
+            // at the time of the initial paint (due to scrolling, window size, etc.) will never
+            // be drawn.
+            m_paintInfo->rect = IntRect(m_filter->drawingRegion(m_object));
         }
     }
 #endif
@@ -160,6 +177,139 @@ void SVGRenderingContext::prepareToRenderSVGContent(RenderObject* object, PaintI
     m_renderingFlags |= RenderingPrepared;
 }
 
+static AffineTransform& currentContentTransformation()
+{
+    DEFINE_STATIC_LOCAL(AffineTransform, s_currentContentTransformation, ());
+    return s_currentContentTransformation;
+}
+
+float SVGRenderingContext::calculateScreenFontSizeScalingFactor(const RenderObject* renderer)
+{
+    ASSERT(renderer);
+
+    AffineTransform ctm;
+    calculateTransformationToOutermostSVGCoordinateSystem(renderer, ctm);
+    return narrowPrecisionToFloat(sqrt((pow(ctm.xScale(), 2) + pow(ctm.yScale(), 2)) / 2));
+}
+
+void SVGRenderingContext::calculateTransformationToOutermostSVGCoordinateSystem(const RenderObject* renderer, AffineTransform& absoluteTransform)
+{
+    const RenderObject* current = renderer;
+    ASSERT(current);
+
+    absoluteTransform = currentContentTransformation();
+    while (current) {
+        absoluteTransform = current->localToParentTransform() * absoluteTransform;
+        if (current->isSVGRoot())
+            break;
+        current = current->parent();
+    }
+}
+
+bool SVGRenderingContext::createImageBuffer(const FloatRect& targetRect, const AffineTransform& absoluteTransform, OwnPtr<ImageBuffer>& imageBuffer, ColorSpace colorSpace, RenderingMode renderingMode)
+{
+    IntRect paintRect = calculateImageBufferRect(targetRect, absoluteTransform);
+    // Don't create empty ImageBuffers.
+    if (paintRect.isEmpty())
+        return false;
+
+    IntSize clampedSize = clampedAbsoluteSize(paintRect.size());
+    OwnPtr<ImageBuffer> image = ImageBuffer::create(clampedSize, 1, colorSpace, renderingMode);
+    if (!image)
+        return false;
+
+    GraphicsContext* imageContext = image->context();
+    ASSERT(imageContext);
+
+    imageContext->scale(FloatSize(static_cast<float>(clampedSize.width()) / paintRect.width(),
+                                  static_cast<float>(clampedSize.height()) / paintRect.height()));
+    imageContext->translate(-paintRect.x(), -paintRect.y());
+    imageContext->concatCTM(absoluteTransform);
+
+    imageBuffer = image.release();
+    return true;
+}
+
+bool SVGRenderingContext::createImageBufferForPattern(const FloatRect& absoluteTargetRect, const FloatRect& clampedAbsoluteTargetRect, OwnPtr<ImageBuffer>& imageBuffer, ColorSpace colorSpace, RenderingMode renderingMode)
+{
+    IntSize imageSize(roundedIntSize(clampedAbsoluteTargetRect.size()));
+    IntSize unclampedImageSize(roundedIntSize(absoluteTargetRect.size()));
+
+    // Don't create empty ImageBuffers.
+    if (imageSize.isEmpty())
+        return false;
+
+    OwnPtr<ImageBuffer> image = ImageBuffer::create(imageSize, 1, colorSpace, renderingMode);
+    if (!image)
+        return false;
+
+    GraphicsContext* imageContext = image->context();
+    ASSERT(imageContext);
+
+    // Compensate rounding effects, as the absolute target rect is using floating-point numbers and the image buffer size is integer.
+    imageContext->scale(FloatSize(unclampedImageSize.width() / absoluteTargetRect.width(), unclampedImageSize.height() / absoluteTargetRect.height()));
+
+    imageBuffer = image.release();
+    return true;
+}
+
+void SVGRenderingContext::renderSubtreeToImageBuffer(ImageBuffer* image, RenderObject* item, const AffineTransform& subtreeContentTransformation)
+{
+    ASSERT(item);
+    ASSERT(image);
+    ASSERT(image->context());
+
+    PaintInfo info(image->context(), PaintInfo::infiniteRect(), PaintPhaseForeground, PaintBehaviorNormal);
+
+    AffineTransform& contentTransformation = currentContentTransformation();
+    AffineTransform savedContentTransformation = contentTransformation;
+    contentTransformation = subtreeContentTransformation * contentTransformation;
+
+    ASSERT(!item->needsLayout());
+    item->paint(info, IntPoint());
+
+    contentTransformation = savedContentTransformation;
+}
+
+void SVGRenderingContext::clipToImageBuffer(GraphicsContext* context, const AffineTransform& absoluteTransform, const FloatRect& targetRect, OwnPtr<ImageBuffer>& imageBuffer, bool safeToClear)
+{
+    ASSERT(context);
+    ASSERT(imageBuffer);
+
+    FloatRect absoluteTargetRect = calculateImageBufferRect(targetRect, absoluteTransform);
+
+    // The mask image has been created in the absolute coordinate space, as the image should not be scaled.
+    // So the actual masking process has to be done in the absolute coordinate space as well.
+    context->concatCTM(absoluteTransform.inverse());
+    context->clipToImageBuffer(imageBuffer.get(), absoluteTargetRect);
+    context->concatCTM(absoluteTransform);
+
+    // When nesting resources, with objectBoundingBox as content unit types, there's no use in caching the
+    // resulting image buffer as the parent resource already caches the result.
+    if (safeToClear && !currentContentTransformation().isIdentity())
+        imageBuffer.clear();
+}
+
+FloatRect SVGRenderingContext::clampedAbsoluteTargetRect(const FloatRect& absoluteTargetRect)
+{
+    const FloatSize maxImageBufferSize(kMaxImageBufferSize, kMaxImageBufferSize);
+    return FloatRect(absoluteTargetRect.location(), absoluteTargetRect.size().shrunkTo(maxImageBufferSize));
+}
+
+IntSize SVGRenderingContext::clampedAbsoluteSize(const IntSize& absoluteSize)
+{
+    const IntSize maxImageBufferSize(kMaxImageBufferSize, kMaxImageBufferSize);
+    return absoluteSize.shrunkTo(maxImageBufferSize);
+}
+
+void SVGRenderingContext::clear2DRotation(AffineTransform& transform)
+{
+    AffineTransform::DecomposedType decomposition;
+    transform.decompose(decomposition);
+    decomposition.angle = 0;
+    transform.recompose(decomposition);
+}
+
 }
 
 #endif