OSDN Git Service

Merge WebKit at r73109: Initial merge by git.
[android-x86/external-webkit.git] / WebCore / rendering / RenderSVGResourceGradient.cpp
index 73b2ab6..b76e7f8 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * Copyright (C) 2006 Nikolas Zimmermann <zimmermann@kde.org>
- *               2008 Eric Seidel <eric@webkit.org>
- *               2008 Dirk Schulze <krit@webkit.org>
+ * Copyright (C) 2008 Eric Seidel <eric@webkit.org>
+ * Copyright (C) 2008 Dirk Schulze <krit@webkit.org>
  * Copyright (C) Research In Motion Limited 2010. All rights reserved.
  *
  * This library is free software; you can redistribute it and/or
@@ -28,6 +28,8 @@
 
 #include "GradientAttributes.h"
 #include "GraphicsContext.h"
+#include "RenderSVGText.h"
+#include "SVGImageBufferTools.h"
 #include "SVGRenderSupport.h"
 #include <wtf/UnusedParam.h>
 
@@ -35,6 +37,7 @@ namespace WebCore {
 
 RenderSVGResourceGradient::RenderSVGResourceGradient(SVGGradientElement* node)
     : RenderSVGResourceContainer(node)
+    , m_shouldCollectGradientAttributes(true)
 #if PLATFORM(CG)
     , m_savedContext(0)
 #endif
@@ -50,89 +53,88 @@ RenderSVGResourceGradient::~RenderSVGResourceGradient()
     m_gradient.clear();
 }
 
-void RenderSVGResourceGradient::invalidateClients()
+void RenderSVGResourceGradient::removeAllClientsFromCache(bool markForInvalidation)
 {
     if (!m_gradient.isEmpty()) {
         deleteAllValues(m_gradient);
         m_gradient.clear();
     }
 
-    markAllClientsForInvalidation(RepaintInvalidation);
+    m_shouldCollectGradientAttributes = true;
+    markAllClientsForInvalidation(markForInvalidation ? RepaintInvalidation : ParentOnlyInvalidation);
 }
 
-void RenderSVGResourceGradient::invalidateClient(RenderObject* client)
+void RenderSVGResourceGradient::removeClientFromCache(RenderObject* client, bool markForInvalidation)
 {
     ASSERT(client);
-    ASSERT(client->selfNeedsLayout());
 
     if (m_gradient.contains(client))
         delete m_gradient.take(client);
 
-    markClientForInvalidation(client, RepaintInvalidation);
+    markClientForInvalidation(client, markForInvalidation ? RepaintInvalidation : ParentOnlyInvalidation);
 }
 
 #if PLATFORM(CG)
-static inline AffineTransform absoluteTransformFromContext(GraphicsContext* context)
-{
-    // Extract current transformation matrix used in the original context. Note that this coordinate
-    // system is flipped compared to SVGs internal coordinate system, done in WebKit level. Fix
-    // this transformation by flipping the y component.
-    return context->getCTM() * AffineTransform().flipY();
-}
-
 static inline bool createMaskAndSwapContextForTextGradient(GraphicsContext*& context,
                                                            GraphicsContext*& savedContext,
                                                            OwnPtr<ImageBuffer>& imageBuffer,
-                                                           const RenderObject* object)
+                                                           RenderObject* object)
 {
-    const RenderObject* textRootBlock = SVGRenderSupport::findTextRootObject(object);
+    RenderObject* textRootBlock = RenderSVGText::locateRenderSVGTextAncestor(object);
+    ASSERT(textRootBlock);
 
-    AffineTransform transform(absoluteTransformFromContext(context));
-    FloatRect maskAbsoluteBoundingBox = transform.mapRect(textRootBlock->repaintRectInLocalCoordinates());
+    AffineTransform absoluteTransform;
+    SVGImageBufferTools::calculateTransformationToOutermostSVGCoordinateSystem(textRootBlock, absoluteTransform);
 
-    IntRect maskImageRect = enclosingIntRect(maskAbsoluteBoundingBox);
-    if (maskImageRect.isEmpty())
+    FloatRect absoluteTargetRect = absoluteTransform.mapRect(textRootBlock->repaintRectInLocalCoordinates());
+    FloatRect clampedAbsoluteTargetRect = SVGImageBufferTools::clampedAbsoluteTargetRectForRenderer(textRootBlock, absoluteTargetRect);
+    if (clampedAbsoluteTargetRect.isEmpty())
         return false;
 
-    // Allocate an image buffer as big as the absolute unclipped size of the object
-    OwnPtr<ImageBuffer> maskImage = ImageBuffer::create(maskImageRect.size());
-    if (!maskImage)
+    OwnPtr<ImageBuffer> maskImage;
+    if (!SVGImageBufferTools::createImageBuffer(absoluteTargetRect, clampedAbsoluteTargetRect, maskImage, ColorSpaceDeviceRGB))
         return false;
 
     GraphicsContext* maskImageContext = maskImage->context();
+    ASSERT(maskImageContext);
 
-    // Transform the mask image coordinate system to absolute screen coordinates
-    maskImageContext->translate(-maskAbsoluteBoundingBox.x(), -maskAbsoluteBoundingBox.y());
-    maskImageContext->concatCTM(transform);
+    maskImageContext->translate(-clampedAbsoluteTargetRect.x(), -clampedAbsoluteTargetRect.y());
+    maskImageContext->concatCTM(absoluteTransform);
 
-    imageBuffer = maskImage.release();
+    ASSERT(maskImage);
     savedContext = context;
     context = maskImageContext;
-
+    imageBuffer = maskImage.release();
     return true;
 }
 
 static inline AffineTransform clipToTextMask(GraphicsContext* context,
                                              OwnPtr<ImageBuffer>& imageBuffer,
-                                             const RenderObject* object,
-                                             GradientData* gradientData)
+                                             FloatRect& targetRect,
+                                             RenderObject* object,
+                                             bool boundingBoxMode,
+                                             const AffineTransform& gradientTransform)
 {
-    const RenderObject* textRootBlock = SVGRenderSupport::findTextRootObject(object);
+    RenderObject* textRootBlock = RenderSVGText::locateRenderSVGTextAncestor(object);
+    ASSERT(textRootBlock);
+
+    targetRect = textRootBlock->repaintRectInLocalCoordinates();
+
+    AffineTransform absoluteTransform;
+    SVGImageBufferTools::calculateTransformationToOutermostSVGCoordinateSystem(textRootBlock, absoluteTransform);
 
-    // The mask image has been created in the device coordinate space, as the image should not be scaled.
-    // So the actual masking process has to be done in the device coordinate space as well.
-    AffineTransform transform(absoluteTransformFromContext(context));
-    context->concatCTM(transform.inverse());
-    context->clipToImageBuffer(transform.mapRect(textRootBlock->repaintRectInLocalCoordinates()), imageBuffer.get());
-    context->concatCTM(transform);
+    FloatRect absoluteTargetRect = absoluteTransform.mapRect(targetRect);
+    FloatRect clampedAbsoluteTargetRect = SVGImageBufferTools::clampedAbsoluteTargetRectForRenderer(textRootBlock, absoluteTargetRect);
+
+    SVGImageBufferTools::clipToImageBuffer(context, absoluteTransform, clampedAbsoluteTargetRect, imageBuffer);
 
     AffineTransform matrix;
-    if (gradientData->boundingBoxMode) {
+    if (boundingBoxMode) {
         FloatRect maskBoundingBox = textRootBlock->objectBoundingBox();
         matrix.translate(maskBoundingBox.x(), maskBoundingBox.y());
         matrix.scaleNonUniform(maskBoundingBox.width(), maskBoundingBox.height());
     }
-    matrix.multLeft(gradientData->transform);
+    matrix.multLeft(gradientTransform);
     return matrix;
 }
 #endif
@@ -146,19 +148,28 @@ bool RenderSVGResourceGradient::applyResource(RenderObject* object, RenderStyle*
 
     // Be sure to synchronize all SVG properties on the gradientElement _before_ processing any further.
     // Otherwhise the call to collectGradientAttributes() in createTileImage(), may cause the SVG DOM property
-    // synchronization to kick in, which causes invalidateClients() to be called, which in turn deletes our
+    // synchronization to kick in, which causes removeAllClientsFromCache() to be called, which in turn deletes our
     // GradientData object! Leaving out the line below will cause svg/dynamic-updates/SVG*GradientElement-svgdom* to crash.
     SVGGradientElement* gradientElement = static_cast<SVGGradientElement*>(node());
     if (!gradientElement)
         return false;
 
-    gradientElement->updateAnimatedSVGAttribute(anyQName());
+    if (m_shouldCollectGradientAttributes) {
+        gradientElement->updateAnimatedSVGAttribute(anyQName());
+        collectGradientAttributes(gradientElement);
+        m_shouldCollectGradientAttributes = false;
+    }
+
+    // Spec: When the geometry of the applicable element has no width or height and objectBoundingBox is specified,
+    // then the given effect (e.g. a gradient or a filter) will be ignored.
+    FloatRect objectBoundingBox = object->objectBoundingBox();
+    if (boundingBoxMode() && objectBoundingBox.isEmpty())
+        return false;
 
     if (!m_gradient.contains(object))
         m_gradient.set(object, new GradientData);
 
     GradientData* gradientData = m_gradient.get(object);
-
     bool isPaintingText = resourceMode & ApplyToTextMode;
 
     // Create gradient object
@@ -169,16 +180,18 @@ bool RenderSVGResourceGradient::applyResource(RenderObject* object, RenderStyle*
         // resource, so don't apply it here. For non-CG platforms, we want the text bounding
         // box applied to the gradient space transform now, so the gradient shader can use it.
 #if PLATFORM(CG)
-        if (gradientData->boundingBoxMode && !isPaintingText) {
+        if (boundingBoxMode() && !objectBoundingBox.isEmpty() && !isPaintingText) {
 #else
-        if (gradientData->boundingBoxMode) {
+        if (boundingBoxMode() && !objectBoundingBox.isEmpty()) {
 #endif
-            FloatRect objectBoundingBox = object->objectBoundingBox();
             gradientData->userspaceTransform.translate(objectBoundingBox.x(), objectBoundingBox.y());
             gradientData->userspaceTransform.scaleNonUniform(objectBoundingBox.width(), objectBoundingBox.height());
         }
 
-        gradientData->userspaceTransform.multLeft(gradientData->transform);
+        AffineTransform gradientTransform;
+        calculateGradientTransform(gradientTransform);
+
+        gradientData->userspaceTransform.multLeft(gradientTransform);
         gradientData->gradient->setGradientSpaceTransform(gradientData->userspaceTransform);
     }
 
@@ -217,7 +230,7 @@ bool RenderSVGResourceGradient::applyResource(RenderObject* object, RenderStyle*
     return true;
 }
 
-void RenderSVGResourceGradient::postApplyResource(RenderObject* object, GraphicsContext*& context, unsigned short resourceMode)
+void RenderSVGResourceGradient::postApplyResource(RenderObject* object, GraphicsContext*& context, unsigned short resourceMode, const Path* path)
 {
     ASSERT(context);
     ASSERT(resourceMode != ApplyToDefaultMode);
@@ -232,22 +245,24 @@ void RenderSVGResourceGradient::postApplyResource(RenderObject* object, Graphics
             context = m_savedContext;
             m_savedContext = 0;
 
-            gradientData->gradient->setGradientSpaceTransform(clipToTextMask(context, m_imageBuffer, object, gradientData));
-            context->setFillGradient(gradientData->gradient);
+            AffineTransform gradientTransform;
+            calculateGradientTransform(gradientTransform);
 
-            const RenderObject* textRootBlock = SVGRenderSupport::findTextRootObject(object);
-            context->fillRect(textRootBlock->repaintRectInLocalCoordinates());
+            FloatRect targetRect;
+            gradientData->gradient->setGradientSpaceTransform(clipToTextMask(context, m_imageBuffer, targetRect, object, boundingBoxMode(), gradientTransform));
+            context->setFillGradient(gradientData->gradient);
 
+            context->fillRect(targetRect);
             m_imageBuffer.clear();
         }
 #else
         UNUSED_PARAM(object);
 #endif
-    } else {
+    } else if (path) {
         if (resourceMode & ApplyToFillMode)
-            context->fillPath();
+            context->fillPath(*path);
         else if (resourceMode & ApplyToStrokeMode)
-            context->strokePath();
+            context->strokePath(*path);
     }
 
     context->restore();