[Reland of r13154, since the Housekeeping bot seems to have reverted it in r13155...
authorsenorblanco@chromium.org <senorblanco@chromium.org@2bbb7eff-a529-9590-31e7-b0007b416f81>
Fri, 24 Jan 2014 15:43:50 +0000 (15:43 +0000)
committersenorblanco@chromium.org <senorblanco@chromium.org@2bbb7eff-a529-9590-31e7-b0007b416f81>
Fri, 24 Jan 2014 15:43:50 +0000 (15:43 +0000)
Refactor SkMorphologyImageFilter, CPU and GPU paths. This required making opts/ dependent on effects/, so that we could use the SkMorphologyProc type in SkMorphologyImageFilter.h.

Correctness and performance covered by existing tests; no change in functionality.

R=bsalomon@google.com, djsollen@google.com, reed@google.com

Committed: https://code.google.com/p/skia/source/detail?r=13154

BUG=skia:

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

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

gyp/opts.gyp
include/effects/SkMorphologyImageFilter.h
src/effects/SkMorphologyImageFilter.cpp
src/opts/SkMorphology_opts.h
src/opts/SkMorphology_opts_none.cpp
src/opts/opts_check_SSE2.cpp
src/opts/opts_check_arm.cpp

index bd072cb..9c63766 100644 (file)
@@ -25,6 +25,7 @@
       'standalone_static_library': 1,
       'dependencies': [
         'core.gyp:*',
+        'effects.gyp:*'
       ],
       'include_dirs': [
         '../src/core',
       'standalone_static_library': 1,
       'dependencies': [
         'core.gyp:*',
+        'effects.gyp:*'
       ],
       'include_dirs': [
         '../src/core',
       'standalone_static_library': 1,
       'dependencies': [
         'core.gyp:*',
+        'effects.gyp:*'
       ],
       'include_dirs': [
         '../src/core',
index 880fc2e..3a55199 100644 (file)
@@ -9,6 +9,7 @@
 #ifndef SkMorphologyImageFilter_DEFINED
 #define SkMorphologyImageFilter_DEFINED
 
+#include "SkColor.h"
 #include "SkImageFilter.h"
 #include "SkSize.h"
 
@@ -16,11 +17,27 @@ class SK_API SkMorphologyImageFilter : public SkImageFilter {
 public:
     SkMorphologyImageFilter(int radiusX, int radiusY, SkImageFilter* input, const CropRect* cropRect);
 
+    /**
+     * All morphology procs have the same signature: src is the source buffer, dst the
+     * destination buffer, radius is the morphology radius, width and height are the bounds
+     * of the destination buffer (in pixels), and srcStride and dstStride are the
+     * number of pixels per row in each buffer. All buffers are 8888.
+     */
+
+    typedef void (*Proc)(const SkPMColor* src, SkPMColor* dst, int radius,
+                         int width, int height, int srcStride, int dstStride);
+
 protected:
+    bool filterImageGeneric(Proc procX, Proc procY,
+                            Proxy*, const SkBitmap& src, const SkMatrix&,
+                            SkBitmap* result, SkIPoint* offset);
     SkMorphologyImageFilter(SkFlattenableReadBuffer& buffer);
     virtual void flatten(SkFlattenableWriteBuffer&) const SK_OVERRIDE;
 #if SK_SUPPORT_GPU
     virtual bool canFilterImageGPU() const SK_OVERRIDE { return true; }
+    bool filterImageGPUGeneric(bool dilate, Proxy* proxy, const SkBitmap& src,
+                               const SkMatrix& ctm, SkBitmap* result,
+                               SkIPoint* offset);
 #endif
 
     SkISize    radius() const { return fRadius; }
index 09d9f5d..af45c0d 100644 (file)
@@ -84,28 +84,6 @@ static void erode(const SkPMColor* src, SkPMColor* dst,
     }
 }
 
-static void erodeX(const SkBitmap& src, SkBitmap* dst, int radiusX, const SkIRect& bounds)
-{
-    SkMorphologyProc erodeXProc = SkMorphologyGetPlatformProc(kErodeX_SkMorphologyProcType);
-    if (!erodeXProc) {
-        erodeXProc = erode<kX>;
-    }
-    erodeXProc(src.getAddr32(bounds.left(), bounds.top()), dst->getAddr32(0, 0),
-               radiusX, bounds.width(), bounds.height(),
-               src.rowBytesAsPixels(), dst->rowBytesAsPixels());
-}
-
-static void erodeY(const SkBitmap& src, SkBitmap* dst, int radiusY, const SkIRect& bounds)
-{
-    SkMorphologyProc erodeYProc = SkMorphologyGetPlatformProc(kErodeY_SkMorphologyProcType);
-    if (!erodeYProc) {
-        erodeYProc = erode<kY>;
-    }
-    erodeYProc(src.getAddr32(bounds.left(), bounds.top()), dst->getAddr32(0, 0),
-               radiusY, bounds.height(), bounds.width(),
-               src.rowBytesAsPixels(), dst->rowBytesAsPixels());
-}
-
 template<MorphDirection direction>
 static void dilate(const SkPMColor* src, SkPMColor* dst,
                    int radius, int width, int height,
@@ -144,31 +122,27 @@ static void dilate(const SkPMColor* src, SkPMColor* dst,
     }
 }
 
-static void dilateX(const SkBitmap& src, SkBitmap* dst, int radiusX, const SkIRect& bounds)
+static void callProcX(SkMorphologyImageFilter::Proc procX, const SkBitmap& src, SkBitmap* dst, int radiusX, const SkIRect& bounds)
 {
-    SkMorphologyProc dilateXProc = SkMorphologyGetPlatformProc(kDilateX_SkMorphologyProcType);
-    if (!dilateXProc) {
-        dilateXProc = dilate<kX>;
-    }
-    dilateXProc(src.getAddr32(bounds.left(), bounds.top()), dst->getAddr32(0, 0),
-                radiusX, bounds.width(), bounds.height(),
-                src.rowBytesAsPixels(), dst->rowBytesAsPixels());
+    procX(src.getAddr32(bounds.left(), bounds.top()), dst->getAddr32(0, 0),
+          radiusX, bounds.width(), bounds.height(),
+          src.rowBytesAsPixels(), dst->rowBytesAsPixels());
 }
 
-static void dilateY(const SkBitmap& src, SkBitmap* dst, int radiusY, const SkIRect& bounds)
+static void callProcY(SkMorphologyImageFilter::Proc procY, const SkBitmap& src, SkBitmap* dst, int radiusY, const SkIRect& bounds)
 {
-    SkMorphologyProc dilateYProc = SkMorphologyGetPlatformProc(kDilateY_SkMorphologyProcType);
-    if (!dilateYProc) {
-        dilateYProc = dilate<kY>;
-    }
-    dilateYProc(src.getAddr32(bounds.left(), bounds.top()), dst->getAddr32(0, 0),
-                radiusY, bounds.height(), bounds.width(),
-                src.rowBytesAsPixels(), dst->rowBytesAsPixels());
+    procY(src.getAddr32(bounds.left(), bounds.top()), dst->getAddr32(0, 0),
+          radiusY, bounds.height(), bounds.width(),
+          src.rowBytesAsPixels(), dst->rowBytesAsPixels());
 }
 
-bool SkErodeImageFilter::onFilterImage(Proxy* proxy,
-                                       const SkBitmap& source, const SkMatrix& ctm,
-                                       SkBitmap* dst, SkIPoint* offset) {
+bool SkMorphologyImageFilter::filterImageGeneric(SkMorphologyImageFilter::Proc procX,
+                                                 SkMorphologyImageFilter::Proc procY,
+                                                 Proxy* proxy,
+                                                 const SkBitmap& source,
+                                                 const SkMatrix& ctm,
+                                                 SkBitmap* dst,
+                                                 SkIPoint* offset) {
     SkBitmap src = source;
     SkIPoint srcOffset = SkIPoint::Make(0, 0);
     if (getInput(0) && !getInput(0)->filterImage(proxy, source, ctm, &src, &srcOffset)) {
@@ -224,87 +198,45 @@ bool SkErodeImageFilter::onFilterImage(Proxy* proxy,
     }
 
     if (width > 0 && height > 0) {
-        erodeX(src, &temp, width, srcBounds);
+        callProcX(procX, src, &temp, width, srcBounds);
         SkIRect tmpBounds = SkIRect::MakeWH(srcBounds.width(), srcBounds.height());
-        erodeY(temp, dst, height, tmpBounds);
+        callProcY(procY, temp, dst, height, tmpBounds);
     } else if (width > 0) {
-        erodeX(src, dst, width, srcBounds);
+        callProcX(procX, src, dst, width, srcBounds);
     } else if (height > 0) {
-        erodeY(src, dst, height, srcBounds);
+        callProcY(procY, src, dst, height, srcBounds);
     }
     offset->fX = bounds.left();
     offset->fY = bounds.top();
     return true;
 }
 
-bool SkDilateImageFilter::onFilterImage(Proxy* proxy,
-                                        const SkBitmap& source, const SkMatrix& ctm,
-                                        SkBitmap* dst, SkIPoint* offset) {
-    SkBitmap src = source;
-    SkIPoint srcOffset = SkIPoint::Make(0, 0);
-    if (getInput(0) && !getInput(0)->filterImage(proxy, source, ctm, &src, &srcOffset)) {
-        return false;
-    }
-    if (src.config() != SkBitmap::kARGB_8888_Config) {
-        return false;
-    }
-
-    SkIRect bounds;
-    src.getBounds(&bounds);
-    bounds.offset(srcOffset);
-    if (!this->applyCropRect(&bounds, ctm)) {
-        return false;
-    }
-
-    SkAutoLockPixels alp(src);
-    if (!src.getPixels()) {
-        return false;
-    }
-
-    dst->setConfig(src.config(), bounds.width(), bounds.height());
-    dst->allocPixels();
-    if (!dst->getPixels()) {
-        return false;
-    }
-
-    SkVector radius = SkVector::Make(SkIntToScalar(this->radius().width()),
-                                     SkIntToScalar(this->radius().height()));
-    ctm.mapVectors(&radius, 1);
-    int width = SkScalarFloorToInt(radius.fX);
-    int height = SkScalarFloorToInt(radius.fY);
-
-    if (width < 0 || height < 0) {
-        return false;
+bool SkErodeImageFilter::onFilterImage(Proxy* proxy,
+                                       const SkBitmap& source, const SkMatrix& ctm,
+                                       SkBitmap* dst, SkIPoint* offset) {
+    Proc erodeXProc = SkMorphologyGetPlatformProc(kErodeX_SkMorphologyProcType);
+    if (!erodeXProc) {
+        erodeXProc = erode<kX>;
     }
-
-    SkIRect srcBounds = bounds;
-    srcBounds.offset(-srcOffset);
-
-    if (width == 0 && height == 0) {
-        src.extractSubset(dst, srcBounds);
-        offset->fX = bounds.left();
-        offset->fY = bounds.top();
-        return true;
+    Proc erodeYProc = SkMorphologyGetPlatformProc(kErodeY_SkMorphologyProcType);
+    if (!erodeYProc) {
+        erodeYProc = erode<kY>;
     }
+    return this->filterImageGeneric(erodeXProc, erodeYProc, proxy, source, ctm, dst, offset);
+}
 
-    SkBitmap temp;
-    temp.setConfig(dst->config(), dst->width(), dst->height());
-    if (!temp.allocPixels()) {
-        return false;
+bool SkDilateImageFilter::onFilterImage(Proxy* proxy,
+                                        const SkBitmap& source, const SkMatrix& ctm,
+                                        SkBitmap* dst, SkIPoint* offset) {
+    Proc dilateXProc = SkMorphologyGetPlatformProc(kDilateX_SkMorphologyProcType);
+    if (!dilateXProc) {
+        dilateXProc = dilate<kX>;
     }
-
-    if (width > 0 && height > 0) {
-        dilateX(src, &temp, width, srcBounds);
-        SkIRect tmpBounds = SkIRect::MakeWH(srcBounds.width(), srcBounds.height());
-        dilateY(temp, dst, height, tmpBounds);
-    } else if (width > 0) {
-        dilateX(src, dst, width, srcBounds);
-    } else if (height > 0) {
-        dilateY(src, dst, height, srcBounds);
+    Proc dilateYProc = SkMorphologyGetPlatformProc(kDilateY_SkMorphologyProcType);
+    if (!dilateYProc) {
+        dilateYProc = dilate<kY>;
     }
-    offset->fX = bounds.left();
-    offset->fY = bounds.top();
-    return true;
+    return this->filterImageGeneric(dilateXProc, dilateYProc, proxy, source, ctm, dst, offset);
 }
 
 #if SK_SUPPORT_GPU
@@ -579,8 +511,12 @@ bool apply_morphology(const SkBitmap& input,
 
 };
 
-bool SkDilateImageFilter::filterImageGPU(Proxy* proxy, const SkBitmap& src, const SkMatrix& ctm,
-                                         SkBitmap* result, SkIPoint* offset) {
+bool SkMorphologyImageFilter::filterImageGPUGeneric(bool dilate,
+                                                    Proxy* proxy,
+                                                    const SkBitmap& src,
+                                                    const SkMatrix& ctm,
+                                                    SkBitmap* result,
+                                                    SkIPoint* offset) {
     SkBitmap input;
     SkIPoint srcOffset = SkIPoint::Make(0, 0);
     if (!SkImageFilterUtils::GetInputResultGPU(getInput(0), proxy, src, ctm, &input, &srcOffset)) {
@@ -611,7 +547,8 @@ bool SkDilateImageFilter::filterImageGPU(Proxy* proxy, const SkBitmap& src, cons
         return true;
     }
 
-    if (!apply_morphology(input, srcBounds, GrMorphologyEffect::kDilate_MorphologyType,
+    GrMorphologyEffect::MorphologyType type = dilate ? GrMorphologyEffect::kDilate_MorphologyType : GrMorphologyEffect::kErode_MorphologyType;
+    if (!apply_morphology(input, srcBounds, type,
                           SkISize::Make(width, height), result)) {
         return false;
     }
@@ -620,46 +557,14 @@ bool SkDilateImageFilter::filterImageGPU(Proxy* proxy, const SkBitmap& src, cons
     return true;
 }
 
+bool SkDilateImageFilter::filterImageGPU(Proxy* proxy, const SkBitmap& src, const SkMatrix& ctm,
+                                         SkBitmap* result, SkIPoint* offset) {
+    return this->filterImageGPUGeneric(true, proxy, src, ctm, result, offset);
+}
+
 bool SkErodeImageFilter::filterImageGPU(Proxy* proxy, const SkBitmap& src, const SkMatrix& ctm,
                                         SkBitmap* result, SkIPoint* offset) {
-    SkBitmap input;
-    SkIPoint srcOffset = SkIPoint::Make(0, 0);
-    if (!SkImageFilterUtils::GetInputResultGPU(getInput(0), proxy, src, ctm, &input, &srcOffset)) {
-        return false;
-    }
-    SkIRect bounds;
-    input.getBounds(&bounds);
-    bounds.offset(srcOffset);
-    if (!this->applyCropRect(&bounds, ctm)) {
-        return false;
-    }
-    SkVector radius = SkVector::Make(SkIntToScalar(this->radius().width()),
-                                     SkIntToScalar(this->radius().height()));
-    ctm.mapVectors(&radius, 1);
-    int width = SkScalarFloorToInt(radius.fX);
-    int height = SkScalarFloorToInt(radius.fY);
-
-    if (width < 0 || height < 0) {
-        return false;
-    }
-
-    SkIRect srcBounds = bounds;
-    srcBounds.offset(-srcOffset);
-
-    if (width == 0 && height == 0) {
-        input.extractSubset(result, srcBounds);
-        offset->fX = bounds.left();
-        offset->fY = bounds.top();
-        return true;
-    }
-
-    if (!apply_morphology(input, srcBounds, GrMorphologyEffect::kErode_MorphologyType,
-        SkISize::Make(width, height), result)) {
-        return false;
-    }
-    offset->fX = bounds.left();
-    offset->fY = bounds.top();
-    return true;
+    return this->filterImageGPUGeneric(false, proxy, src, ctm, result, offset);
 }
 
 #endif
index e3ad853..7ea7c54 100644 (file)
@@ -5,17 +5,10 @@
  * found in the LICENSE file.
  */
 
-#include <SkColor.h>
+#ifndef SkMorphology_opts_DEFINED
+#define SkMorphology_opts_DEFINED
 
-/**
- * All morphology procs have the same signature: src is the source buffer, dst the
- * destination buffer, radius is the morphology radius, width and height are the bounds
- * of the destination buffer (in pixels), and srcStride and dstStride are the
- * number of pixels per row in each buffer. All buffers are 8888.
- */
-
-typedef void (*SkMorphologyProc)(const SkPMColor* src, SkPMColor* dst, int radius,
-                                 int width, int height, int srcStride, int dstStride);
+#include <SkMorphologyImageFilter.h>
 
 enum SkMorphologyProcType {
     kDilateX_SkMorphologyProcType,
@@ -24,4 +17,6 @@ enum SkMorphologyProcType {
     kErodeY_SkMorphologyProcType
 };
 
-SkMorphologyProc SkMorphologyGetPlatformProc(SkMorphologyProcType type);
+SkMorphologyImageFilter::Proc SkMorphologyGetPlatformProc(SkMorphologyProcType type);
+
+#endif
index 66d58ba..ade261f 100644 (file)
@@ -7,6 +7,6 @@
 
 #include "SkMorphology_opts.h"
 
-SkMorphologyProc SkMorphologyGetPlatformProc(SkMorphologyProcType) {
+SkMorphologyImageFilter::Proc SkMorphologyGetPlatformProc(SkMorphologyProcType) {
     return NULL;
 }
index aaf6b2e..d0d88fe 100644 (file)
@@ -251,7 +251,7 @@ SkMemset32Proc SkMemset32GetPlatformProc() {
     }
 }
 
-SkMorphologyProc SkMorphologyGetPlatformProc(SkMorphologyProcType type) {
+SkMorphologyImageFilter::Proc SkMorphologyGetPlatformProc(SkMorphologyProcType type) {
     if (!cachedHasSSE2()) {
         return NULL;
     }
index 3a322aa..9e8b472 100644 (file)
@@ -69,7 +69,7 @@ SkBlitRow::ColorRectProc PlatformColorRectProcFactory() {
     return NULL;
 }
 
-SkMorphologyProc SkMorphologyGetPlatformProc(SkMorphologyProcType type) {
+SkMorphologyImageFilter::Proc SkMorphologyGetPlatformProc(SkMorphologyProcType type) {
 #if SK_ARM_NEON_IS_NONE
     return NULL;
 #else