Home | History | Annotate | Download | only in core
      1 /*
      2  * Copyright 2011 The Android Open Source Project
      3  *
      4  * Use of this source code is governed by a BSD-style license that can be
      5  * found in the LICENSE file.
      6  */
      7 
      8 #include "SkBlurImageFilter.h"
      9 
     10 #include "SkAutoPixmapStorage.h"
     11 #include "SkColorPriv.h"
     12 #include "SkColorSpaceXformer.h"
     13 #include "SkGpuBlurUtils.h"
     14 #include "SkOpts.h"
     15 #include "SkReadBuffer.h"
     16 #include "SkSpecialImage.h"
     17 #include "SkWriteBuffer.h"
     18 
     19 #if SK_SUPPORT_GPU
     20 #include "GrContext.h"
     21 #include "GrTextureProxy.h"
     22 #include "SkGr.h"
     23 #endif
     24 
     25 class SkBlurImageFilterImpl : public SkImageFilter {
     26 public:
     27     SkBlurImageFilterImpl(SkScalar sigmaX,
     28                       SkScalar sigmaY,
     29                       sk_sp<SkImageFilter> input,
     30                       const CropRect* cropRect,
     31                       SkBlurImageFilter::TileMode tileMode);
     32 
     33     SkRect computeFastBounds(const SkRect&) const override;
     34 
     35     SK_TO_STRING_OVERRIDE()
     36     SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkBlurImageFilterImpl)
     37 
     38 protected:
     39     void flatten(SkWriteBuffer&) const override;
     40     sk_sp<SkSpecialImage> onFilterImage(SkSpecialImage* source, const Context&,
     41                                         SkIPoint* offset) const override;
     42     sk_sp<SkImageFilter> onMakeColorSpace(SkColorSpaceXformer*) const override;
     43     SkIRect onFilterNodeBounds(const SkIRect& src, const SkMatrix&, MapDirection) const override;
     44 
     45 private:
     46     SkSize   fSigma;
     47     SkBlurImageFilter::TileMode fTileMode;
     48     typedef SkImageFilter INHERITED;
     49 
     50     friend class SkImageFilter;
     51 };
     52 
     53 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkImageFilter)
     54     SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkBlurImageFilterImpl)
     55 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END
     56 
     57 ///////////////////////////////////////////////////////////////////////////////
     58 
     59 sk_sp<SkImageFilter> SkBlurImageFilter::Make(SkScalar sigmaX, SkScalar sigmaY,
     60                                              sk_sp<SkImageFilter> input,
     61                                              const SkImageFilter::CropRect* cropRect,
     62                                              TileMode tileMode) {
     63     if (0 == sigmaX && 0 == sigmaY && !cropRect) {
     64         return input;
     65     }
     66     return sk_sp<SkImageFilter>(
     67           new SkBlurImageFilterImpl(sigmaX, sigmaY, input, cropRect, tileMode));
     68 }
     69 
     70 // This rather arbitrary-looking value results in a maximum box blur kernel size
     71 // of 1000 pixels on the raster path, which matches the WebKit and Firefox
     72 // implementations. Since the GPU path does not compute a box blur, putting
     73 // the limit on sigma ensures consistent behaviour between the GPU and
     74 // raster paths.
     75 #define MAX_SIGMA SkIntToScalar(532)
     76 
     77 static SkVector map_sigma(const SkSize& localSigma, const SkMatrix& ctm) {
     78     SkVector sigma = SkVector::Make(localSigma.width(), localSigma.height());
     79     ctm.mapVectors(&sigma, 1);
     80     sigma.fX = SkMinScalar(SkScalarAbs(sigma.fX), MAX_SIGMA);
     81     sigma.fY = SkMinScalar(SkScalarAbs(sigma.fY), MAX_SIGMA);
     82     return sigma;
     83 }
     84 
     85 SkBlurImageFilterImpl::SkBlurImageFilterImpl(SkScalar sigmaX,
     86                                              SkScalar sigmaY,
     87                                              sk_sp<SkImageFilter> input,
     88                                              const CropRect* cropRect,
     89                                              SkBlurImageFilter::TileMode tileMode)
     90         : INHERITED(&input, 1, cropRect), fSigma{sigmaX, sigmaY}, fTileMode(tileMode) {}
     91 
     92 sk_sp<SkFlattenable> SkBlurImageFilterImpl::CreateProc(SkReadBuffer& buffer) {
     93     SK_IMAGEFILTER_UNFLATTEN_COMMON(common, 1);
     94     SkScalar sigmaX = buffer.readScalar();
     95     SkScalar sigmaY = buffer.readScalar();
     96     SkBlurImageFilter::TileMode tileMode;
     97     if (buffer.isVersionLT(SkReadBuffer::kTileModeInBlurImageFilter_Version)) {
     98         tileMode = SkBlurImageFilter::kClampToBlack_TileMode;
     99     } else {
    100         tileMode = static_cast<SkBlurImageFilter::TileMode>(buffer.readInt());
    101     }
    102 
    103     return SkBlurImageFilter::Make(
    104           sigmaX, sigmaY, common.getInput(0), &common.cropRect(), tileMode);
    105 }
    106 
    107 void SkBlurImageFilterImpl::flatten(SkWriteBuffer& buffer) const {
    108     this->INHERITED::flatten(buffer);
    109     buffer.writeScalar(fSigma.fWidth);
    110     buffer.writeScalar(fSigma.fHeight);
    111     buffer.writeInt(static_cast<int>(fTileMode));
    112 }
    113 
    114 #if SK_SUPPORT_GPU
    115 static GrTextureDomain::Mode to_texture_domain_mode(SkBlurImageFilter::TileMode tileMode) {
    116     switch (tileMode) {
    117       case SkBlurImageFilter::TileMode::kClamp_TileMode:
    118         return GrTextureDomain::kClamp_Mode;
    119       case SkBlurImageFilter::TileMode::kClampToBlack_TileMode:
    120         return GrTextureDomain::kDecal_Mode;
    121       case SkBlurImageFilter::TileMode::kRepeat_TileMode:
    122         return GrTextureDomain::kRepeat_Mode;
    123       default:
    124         SkFAIL("Unsupported tile mode.");
    125         return GrTextureDomain::kDecal_Mode;
    126     }
    127 }
    128 #endif
    129 
    130 static void get_box3_params(SkScalar s, int *kernelSize, int* kernelSize3, int *lowOffset,
    131                             int *highOffset) {
    132     float pi = SkScalarToFloat(SK_ScalarPI);
    133     int d = static_cast<int>(floorf(SkScalarToFloat(s) * 3.0f * sqrtf(2.0f * pi) / 4.0f + 0.5f));
    134     *kernelSize = d;
    135     if (d % 2 == 1) {
    136         *lowOffset = *highOffset = (d - 1) / 2;
    137         *kernelSize3 = d;
    138     } else {
    139         *highOffset = d / 2;
    140         *lowOffset = *highOffset - 1;
    141         *kernelSize3 = d + 1;
    142     }
    143 }
    144 
    145 sk_sp<SkSpecialImage> SkBlurImageFilterImpl::onFilterImage(SkSpecialImage* source,
    146                                                            const Context& ctx,
    147                                                            SkIPoint* offset) const {
    148     SkIPoint inputOffset = SkIPoint::Make(0, 0);
    149 
    150     sk_sp<SkSpecialImage> input(this->filterInput(0, source, ctx, &inputOffset));
    151     if (!input) {
    152         return nullptr;
    153     }
    154 
    155     SkIRect inputBounds = SkIRect::MakeXYWH(inputOffset.fX, inputOffset.fY,
    156                                             input->width(), input->height());
    157 
    158     SkIRect dstBounds;
    159     if (!this->applyCropRect(this->mapContext(ctx), inputBounds, &dstBounds)) {
    160         return nullptr;
    161     }
    162     if (!inputBounds.intersect(dstBounds)) {
    163         return nullptr;
    164     }
    165 
    166     const SkVector sigma = map_sigma(fSigma, ctx.ctm());
    167 
    168 #if SK_SUPPORT_GPU
    169     if (source->isTextureBacked()) {
    170         GrContext* context = source->getContext();
    171 
    172         // Ensure the input is in the destination's gamut. This saves us from having to do the
    173         // xform during the filter itself.
    174         input = ImageToColorSpace(input.get(), ctx.outputProperties());
    175 
    176         sk_sp<GrTextureProxy> inputTexture(input->asTextureProxyRef(context));
    177         if (!inputTexture) {
    178             return nullptr;
    179         }
    180 
    181         if (0 == sigma.x() && 0 == sigma.y()) {
    182             offset->fX = inputBounds.x();
    183             offset->fY = inputBounds.y();
    184             return input->makeSubset(inputBounds.makeOffset(-inputOffset.x(),
    185                                                             -inputOffset.y()));
    186         }
    187 
    188         offset->fX = dstBounds.fLeft;
    189         offset->fY = dstBounds.fTop;
    190         inputBounds.offset(-inputOffset);
    191         dstBounds.offset(-inputOffset);
    192         // Typically, we would create the RTC with the output's color space (from ctx), but we
    193         // always blur in the PixelConfig of the *input*. Those might not be compatible (if they
    194         // have different transfer functions). We've already guaranteed that those color spaces
    195         // have the same gamut, so in this case, we do everything in the input's color space.
    196         sk_sp<GrRenderTargetContext> renderTargetContext(SkGpuBlurUtils::GaussianBlur(
    197                                                                 context,
    198                                                                 std::move(inputTexture),
    199                                                                 sk_ref_sp(input->getColorSpace()),
    200                                                                 dstBounds,
    201                                                                 inputBounds,
    202                                                                 sigma.x(),
    203                                                                 sigma.y(),
    204                                                                 to_texture_domain_mode(fTileMode)));
    205         if (!renderTargetContext) {
    206             return nullptr;
    207         }
    208 
    209         return SkSpecialImage::MakeDeferredFromGpu(context,
    210                                                    SkIRect::MakeWH(dstBounds.width(),
    211                                                                    dstBounds.height()),
    212                                                    kNeedNewImageUniqueID_SpecialImage,
    213                                                    renderTargetContext->asTextureProxyRef(),
    214                                                    renderTargetContext->refColorSpace(),
    215                                                    &source->props());
    216     }
    217 #endif
    218 
    219     // TODO: Implement CPU backend for different fTileMode.
    220     int kernelSizeX, kernelSizeX3, lowOffsetX, highOffsetX;
    221     int kernelSizeY, kernelSizeY3, lowOffsetY, highOffsetY;
    222     get_box3_params(sigma.x(), &kernelSizeX, &kernelSizeX3, &lowOffsetX, &highOffsetX);
    223     get_box3_params(sigma.y(), &kernelSizeY, &kernelSizeY3, &lowOffsetY, &highOffsetY);
    224 
    225     if (kernelSizeX < 0 || kernelSizeY < 0) {
    226         return nullptr;
    227     }
    228 
    229     if (kernelSizeX == 0 && kernelSizeY == 0) {
    230         offset->fX = inputBounds.x();
    231         offset->fY = inputBounds.y();
    232         return input->makeSubset(inputBounds.makeOffset(-inputOffset.x(),
    233                                                         -inputOffset.y()));
    234     }
    235 
    236     SkBitmap inputBM;
    237 
    238     if (!input->getROPixels(&inputBM)) {
    239         return nullptr;
    240     }
    241 
    242     if (inputBM.colorType() != kN32_SkColorType) {
    243         return nullptr;
    244     }
    245 
    246     SkImageInfo info = SkImageInfo::Make(dstBounds.width(), dstBounds.height(),
    247                                          inputBM.colorType(), inputBM.alphaType());
    248 
    249     SkBitmap tmp, dst;
    250     if (!tmp.tryAllocPixels(info) || !dst.tryAllocPixels(info)) {
    251         return nullptr;
    252     }
    253 
    254     offset->fX = dstBounds.fLeft;
    255     offset->fY = dstBounds.fTop;
    256     SkPMColor* t = tmp.getAddr32(0, 0);
    257     SkPMColor* d = dst.getAddr32(0, 0);
    258     int w = dstBounds.width(), h = dstBounds.height();
    259     const SkPMColor* s = inputBM.getAddr32(inputBounds.x() - inputOffset.x(),
    260                                            inputBounds.y() - inputOffset.y());
    261     inputBounds.offset(-dstBounds.x(), -dstBounds.y());
    262     dstBounds.offset(-dstBounds.x(), -dstBounds.y());
    263     SkIRect inputBoundsT = SkIRect::MakeLTRB(inputBounds.top(), inputBounds.left(),
    264                                              inputBounds.bottom(), inputBounds.right());
    265     SkIRect dstBoundsT = SkIRect::MakeWH(dstBounds.height(), dstBounds.width());
    266     int sw = int(inputBM.rowBytes() >> 2);
    267 
    268     /**
    269      *
    270      * In order to make memory accesses cache-friendly, we reorder the passes to
    271      * use contiguous memory reads wherever possible.
    272      *
    273      * For example, the 6 passes of the X-and-Y blur case are rewritten as
    274      * follows. Instead of 3 passes in X and 3 passes in Y, we perform
    275      * 2 passes in X, 1 pass in X transposed to Y on write, 2 passes in X,
    276      * then 1 pass in X transposed to Y on write.
    277      *
    278      * +----+       +----+       +----+        +---+       +---+       +---+        +----+
    279      * + AB + ----> | AB | ----> | AB | -----> | A | ----> | A | ----> | A | -----> | AB |
    280      * +----+ blurX +----+ blurX +----+ blurXY | B | blurX | B | blurX | B | blurXY +----+
    281      *                                         +---+       +---+       +---+
    282      *
    283      * In this way, two of the y-blurs become x-blurs applied to transposed
    284      * images, and all memory reads are contiguous.
    285      */
    286     if (kernelSizeX > 0 && kernelSizeY > 0) {
    287         SkOpts::box_blur_xx(s, sw,  inputBounds,  t, kernelSizeX,  lowOffsetX,  highOffsetX, w, h);
    288         SkOpts::box_blur_xx(t,  w,  dstBounds,    d, kernelSizeX,  highOffsetX, lowOffsetX,  w, h);
    289         SkOpts::box_blur_xy(d,  w,  dstBounds,    t, kernelSizeX3, highOffsetX, highOffsetX, w, h);
    290         SkOpts::box_blur_xx(t,  h,  dstBoundsT,   d, kernelSizeY,  lowOffsetY,  highOffsetY, h, w);
    291         SkOpts::box_blur_xx(d,  h,  dstBoundsT,   t, kernelSizeY,  highOffsetY, lowOffsetY,  h, w);
    292         SkOpts::box_blur_xy(t,  h,  dstBoundsT,   d, kernelSizeY3, highOffsetY, highOffsetY, h, w);
    293     } else if (kernelSizeX > 0) {
    294         SkOpts::box_blur_xx(s, sw,  inputBounds,  d, kernelSizeX,  lowOffsetX,  highOffsetX, w, h);
    295         SkOpts::box_blur_xx(d,  w,  dstBounds,    t, kernelSizeX,  highOffsetX, lowOffsetX,  w, h);
    296         SkOpts::box_blur_xx(t,  w,  dstBounds,    d, kernelSizeX3, highOffsetX, highOffsetX, w, h);
    297     } else if (kernelSizeY > 0) {
    298         SkOpts::box_blur_yx(s, sw,  inputBoundsT, d, kernelSizeY,  lowOffsetY,  highOffsetY, h, w);
    299         SkOpts::box_blur_xx(d,  h,  dstBoundsT,   t, kernelSizeY,  highOffsetY, lowOffsetY,  h, w);
    300         SkOpts::box_blur_xy(t,  h,  dstBoundsT,   d, kernelSizeY3, highOffsetY, highOffsetY, h, w);
    301     }
    302 
    303     return SkSpecialImage::MakeFromRaster(SkIRect::MakeWH(dstBounds.width(),
    304                                                           dstBounds.height()),
    305                                           dst, &source->props());
    306 }
    307 
    308 sk_sp<SkImageFilter> SkBlurImageFilterImpl::onMakeColorSpace(SkColorSpaceXformer* xformer)
    309 const {
    310     SkASSERT(1 == this->countInputs());
    311 
    312     auto input = xformer->apply(this->getInput(0));
    313     if (this->getInput(0) != input.get()) {
    314         return SkBlurImageFilter::Make(fSigma.width(), fSigma.height(), std::move(input),
    315                                        this->getCropRectIfSet(), fTileMode);
    316     }
    317     return this->refMe();
    318 }
    319 
    320 SkRect SkBlurImageFilterImpl::computeFastBounds(const SkRect& src) const {
    321     SkRect bounds = this->getInput(0) ? this->getInput(0)->computeFastBounds(src) : src;
    322     bounds.outset(fSigma.width() * 3, fSigma.height() * 3);
    323     return bounds;
    324 }
    325 
    326 SkIRect SkBlurImageFilterImpl::onFilterNodeBounds(const SkIRect& src, const SkMatrix& ctm,
    327                                               MapDirection) const {
    328     SkVector sigma = map_sigma(fSigma, ctm);
    329     return src.makeOutset(SkScalarCeilToInt(sigma.x() * 3), SkScalarCeilToInt(sigma.y() * 3));
    330 }
    331 
    332 #ifndef SK_IGNORE_TO_STRING
    333 void SkBlurImageFilterImpl::toString(SkString* str) const {
    334     str->appendf("SkBlurImageFilterImpl: (");
    335     str->appendf("sigma: (%f, %f) tileMode: %d input (", fSigma.fWidth, fSigma.fHeight,
    336                  static_cast<int>(fTileMode));
    337 
    338     if (this->getInput(0)) {
    339         this->getInput(0)->toString(str);
    340     }
    341 
    342     str->append("))");
    343 }
    344 #endif
    345