Implement a computeFastBounds() traversal for SkImageFilter.
authorsenorblanco@chromium.org <senorblanco@chromium.org@2bbb7eff-a529-9590-31e7-b0007b416f81>
Mon, 27 Jan 2014 21:03:17 +0000 (21:03 +0000)
committersenorblanco@chromium.org <senorblanco@chromium.org@2bbb7eff-a529-9590-31e7-b0007b416f81>
Mon, 27 Jan 2014 21:03:17 +0000 (21:03 +0000)
This allows for correct culling of primitives which have image filters applied.

R=reed@google.com
BUG=skia:

Review URL: https://codereview.chromium.org/137423005

git-svn-id: http://skia.googlecode.com/svn/trunk@13207 2bbb7eff-a529-9590-31e7-b0007b416f81

16 files changed:
include/core/SkImageFilter.h
include/core/SkPaint.h
include/effects/SkBitmapSource.h
include/effects/SkBlurImageFilter.h
include/effects/SkDisplacementMapEffect.h
include/effects/SkDropShadowImageFilter.h
include/effects/SkMorphologyImageFilter.h
include/effects/SkOffsetImageFilter.h
src/core/SkImageFilter.cpp
src/core/SkPaint.cpp
src/effects/SkBitmapSource.cpp
src/effects/SkBlurImageFilter.cpp
src/effects/SkDisplacementMapEffect.cpp
src/effects/SkDropShadowImageFilter.cpp
src/effects/SkMorphologyImageFilter.cpp
src/effects/SkOffsetImageFilter.cpp

index 398af90ca0d97f2349ae3476d9481db8bcfcf0a0..25d3bac8020b5b3a82224c5ee9424fe6c01201d8 100644 (file)
@@ -143,6 +143,9 @@ public:
      */
     bool cropRectIsSet() const { return fCropRect.flags() != 0x0; }
 
+    // Default impl returns union of all input bounds.
+    virtual void computeFastBounds(const SkRect&, SkRect*) const;
+
     SK_DEFINE_FLATTENABLE_TYPE(SkImageFilter)
 
 protected:
index 7dc3a4e03ccefb56e802b26fe4e38ee1d6a3a9e7..ab93eac1023617ae11cecca2fa8bc325c1a93d5e 100644 (file)
@@ -943,6 +943,7 @@ public:
             uintptr_t effects = reinterpret_cast<uintptr_t>(this->getLooper());
             effects |= reinterpret_cast<uintptr_t>(this->getMaskFilter());
             effects |= reinterpret_cast<uintptr_t>(this->getPathEffect());
+            effects |= reinterpret_cast<uintptr_t>(this->getImageFilter());
             if (!effects) {
                 return orig;
             }
index 699186e9f1eb6c90d5b29d251a5392f56282768f..ec779eafe85625d091e0dceed819d9c2fa32a7ff 100644 (file)
@@ -15,6 +15,7 @@ class SK_API SkBitmapSource : public SkImageFilter {
 public:
     explicit SkBitmapSource(const SkBitmap& bitmap);
     SkBitmapSource(const SkBitmap& bitmap, const SkRect& srcRect, const SkRect& dstRect);
+    virtual void computeFastBounds(const SkRect& src, SkRect* dst) const SK_OVERRIDE;
 
     SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkBitmapSource)
 
index f08725490bf4d35ae99d93075736ab9baebabba9..d0d2446a7faad67abd5d8488bb10a86073b83644 100644 (file)
@@ -17,6 +17,7 @@ public:
                       SkScalar sigmaY,
                       SkImageFilter* input = NULL,
                       const CropRect* cropRect = NULL);
+    virtual void computeFastBounds(const SkRect&, SkRect*) const SK_OVERRIDE;
 
     SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkBlurImageFilter)
 
index d789ca24f21e3e5cbb92d31733fa3755b9b15e38..d82a40ab9fe2eeb39e150035a5c7a77924997bf6 100644 (file)
@@ -37,6 +37,8 @@ public:
                                const SkMatrix& ctm,
                                SkBitmap* dst,
                                SkIPoint* offset) SK_OVERRIDE;
+    virtual void computeFastBounds(const SkRect& src, SkRect* dst) const SK_OVERRIDE;
+
 #if SK_SUPPORT_GPU
     virtual bool canFilterImageGPU() const SK_OVERRIDE { return true; }
     virtual bool filterImageGPU(Proxy* proxy, const SkBitmap& src, const SkMatrix& ctm,
@@ -54,6 +56,8 @@ private:
     typedef SkImageFilter INHERITED;
     SkImageFilter* getDisplacementInput() { return getInput(0); }
     SkImageFilter* getColorInput() { return getInput(1); }
+    const SkImageFilter* getDisplacementInput() const { return getInput(0); }
+    const SkImageFilter* getColorInput() const { return getInput(1); }
 };
 
 #endif
index 5a58a0a06a4c6bd867639f2349620eb55f03e5ef..0c66272dd2d2170c0873a543f6074ddd160f1e32 100644 (file)
@@ -13,6 +13,7 @@ class SK_API SkDropShadowImageFilter : public SkImageFilter {
 public:
     SkDropShadowImageFilter(SkScalar dx, SkScalar dy, SkScalar sigma, SkColor, SkImageFilter* input = NULL);
     SkDropShadowImageFilter(SkScalar dx, SkScalar dy, SkScalar sigmaX, SkScalar sigmaY, SkColor, SkImageFilter* input = NULL, const CropRect* cropRect = NULL);
+    virtual void computeFastBounds(const SkRect&, SkRect*) const SK_OVERRIDE;
     SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkDropShadowImageFilter)
 
 protected:
index 3a55199939011da151fa96b7834b71bbba9d0d35..d44d0e25359e2ab5634cef1f3d10cccda68c804b 100644 (file)
@@ -16,6 +16,7 @@
 class SK_API SkMorphologyImageFilter : public SkImageFilter {
 public:
     SkMorphologyImageFilter(int radiusX, int radiusY, SkImageFilter* input, const CropRect* cropRect);
+    virtual void computeFastBounds(const SkRect& src, SkRect* dst) const SK_OVERRIDE;
 
     /**
      * All morphology procs have the same signature: src is the source buffer, dst the
index 70590335d2f4ec19ee0058509930d8d28c1b0c20..f156415857222235b87220b8dd88983ba67f52d2 100644 (file)
@@ -17,6 +17,7 @@ class SK_API SkOffsetImageFilter : public SkImageFilter {
 public:
     SkOffsetImageFilter(SkScalar dx, SkScalar dy, SkImageFilter* input = NULL,
                         const CropRect* cropRect = NULL);
+    virtual void computeFastBounds(const SkRect& src, SkRect* dst) const SK_OVERRIDE;
     SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkOffsetImageFilter)
 
 protected:
index d62685a17c4e26322326c24ae2d465bac2584c82..cb1d9fb57129cd1925699197956df5e73879718a 100644 (file)
@@ -111,6 +111,28 @@ bool SkImageFilter::filterBounds(const SkIRect& src, const SkMatrix& ctm,
     return this->onFilterBounds(src, ctm, dst);
 }
 
+void SkImageFilter::computeFastBounds(const SkRect& src, SkRect* dst) const {
+    if (0 == fInputCount) {
+        *dst = src;
+        return;
+    }
+    if (this->getInput(0)) {
+        this->getInput(0)->computeFastBounds(src, dst);
+    } else {
+        *dst = src;
+    }
+    for (int i = 1; i < fInputCount; i++) {
+        SkImageFilter* input = this->getInput(i);
+        if (input) {
+            SkRect bounds;
+            input->computeFastBounds(src, &bounds);
+            dst->join(bounds);
+        } else {
+            dst->join(src);
+        }
+    }
+}
+
 bool SkImageFilter::onFilterImage(Proxy*, const SkBitmap&, const SkMatrix&,
                                   SkBitmap*, SkIPoint*) {
     return false;
index f576015ba0f96c7af1fbea0544447a860e71590b..cc6d7ba1b3d792acc0f00b2f5d04dd224e086b0d 100644 (file)
@@ -2288,6 +2288,10 @@ const SkRect& SkPaint::doComputeFastBounds(const SkRect& origSrc,
         this->getMaskFilter()->computeFastBounds(*storage, storage);
     }
 
+    if (this->getImageFilter()) {
+        this->getImageFilter()->computeFastBounds(*storage, storage);
+    }
+
     return *storage;
 }
 
index daf4fb08b3ec6bdb2185445aedde4f649c082c29..4aab92fc661993aa371746f994804292ed565446 100644 (file)
@@ -78,3 +78,7 @@ bool SkBitmapSource::onFilterImage(Proxy* proxy, const SkBitmap&, const SkMatrix
     offset->fY = dstIRect.fTop;
     return true;
 }
+
+void SkBitmapSource::computeFastBounds(const SkRect&, SkRect* dst) const {
+    *dst = fDstRect;
+}
index 3e60c9bb87ae7c591a500d417bef3aac5199be63..5d9a077d43b9e029d4b368f1651db8421c733ae7 100644 (file)
@@ -224,6 +224,17 @@ bool SkBlurImageFilter::onFilterImage(Proxy* proxy,
     return true;
 }
 
+
+void SkBlurImageFilter::computeFastBounds(const SkRect& src, SkRect* dst) const {
+    if (getInput(0)) {
+        getInput(0)->computeFastBounds(src, dst);
+    } else {
+        *dst = src;
+    }
+
+    dst->outset(SkScalarMul(fSigma.width(), SkIntToScalar(3)),
+                SkScalarMul(fSigma.height(), SkIntToScalar(3)));
+}
 bool SkBlurImageFilter::filterImageGPU(Proxy* proxy, const SkBitmap& src, const SkMatrix& ctm,
                                        SkBitmap* result, SkIPoint* offset) {
 #if SK_SUPPORT_GPU
index 6ad46badb9af994ec36248df5dee687a6994a86c..fc8d3dda7283f2bbf28bfacf41f81bf4ecc621ec 100644 (file)
@@ -246,6 +246,14 @@ bool SkDisplacementMapEffect::onFilterImage(Proxy* proxy,
     return true;
 }
 
+void SkDisplacementMapEffect::computeFastBounds(const SkRect& src, SkRect* dst) const {
+    if (getColorInput()) {
+        getColorInput()->computeFastBounds(src, dst);
+    } else {
+        *dst = src;
+    }
+}
+
 ///////////////////////////////////////////////////////////////////////////////
 
 #if SK_SUPPORT_GPU
index 61093784faf0b2ad915c7048149822bac481882c..87dfa92f9bf3bf28469faa827e4aa1b905afb542 100644 (file)
@@ -95,3 +95,18 @@ bool SkDropShadowImageFilter::onFilterImage(Proxy* proxy, const SkBitmap& source
     offset->fY = bounds.fTop;
     return true;
 }
+
+void SkDropShadowImageFilter::computeFastBounds(const SkRect& src, SkRect* dst) const {
+    if (getInput(0)) {
+        getInput(0)->computeFastBounds(src, dst);
+    } else {
+        *dst = src;
+    }
+
+    SkRect shadowBounds = *dst;
+    shadowBounds.offset(fDx, fDy);
+    shadowBounds.outset(SkScalarMul(fSigmaX, SkIntToScalar(3)),
+                        SkScalarMul(fSigmaY, SkIntToScalar(3)));
+    dst->join(shadowBounds);
+}
+
index af45c0d3a109febeb2e93c7cbba03001df75ce2c..5008ad18145aeb23a5062beae604402f33c8d7f6 100644 (file)
@@ -239,6 +239,15 @@ bool SkDilateImageFilter::onFilterImage(Proxy* proxy,
     return this->filterImageGeneric(dilateXProc, dilateYProc, proxy, source, ctm, dst, offset);
 }
 
+void SkMorphologyImageFilter::computeFastBounds(const SkRect& src, SkRect* dst) const {
+    if (getInput(0)) {
+        getInput(0)->computeFastBounds(src, dst);
+    } else {
+        *dst = src;
+    }
+    dst->outset(SkIntToScalar(fRadius.width()), SkIntToScalar(fRadius.height()));
+}
+
 #if SK_SUPPORT_GPU
 
 ///////////////////////////////////////////////////////////////////////////////
index 61f68f7afedb9f4a205b88cace0ac9de6cf17f13..e69cf411e371ac53e9e84cc1e959fe6796e4c0d2 100644 (file)
@@ -65,6 +65,15 @@ bool SkOffsetImageFilter::onFilterImage(Proxy* proxy, const SkBitmap& source,
     return true;
 }
 
+void SkOffsetImageFilter::computeFastBounds(const SkRect& src, SkRect* dst) const {
+    if (getInput(0)) {
+        getInput(0)->computeFastBounds(src, dst);
+    } else {
+        *dst = src;
+    }
+    dst->offset(fOffset.fX, fOffset.fY);
+}
+
 bool SkOffsetImageFilter::onFilterBounds(const SkIRect& src, const SkMatrix& ctm,
                                          SkIRect* dst) {
     SkVector vec;