| 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 "include/effects/SkImageFilters.h" |
| 9 |
|
| 10 |
#include "include/core/SkFlattenable.h" |
| 11 |
#include "include/core/SkImageFilter.h" |
| 12 |
#include "include/core/SkRect.h" |
| 13 |
#include "include/core/SkRefCnt.h" |
| 14 |
#include "include/core/SkScalar.h" |
| 15 |
#include "include/core/SkSize.h" |
| 16 |
#include "include/core/SkTileMode.h" |
| 17 |
#include "include/core/SkTypes.h" |
| 18 |
#include "include/private/base/SkFloatingPoint.h" |
| 19 |
#include "src/core/SkBlurEngine.h" |
| 20 |
#include "src/core/SkImageFilterTypes.h" |
| 21 |
#include "src/core/SkImageFilter_Base.h" |
| 22 |
#include "src/core/SkReadBuffer.h" |
| 23 |
#include "src/core/SkWriteBuffer.h" |
| 24 |
|
| 25 |
#include <algorithm> |
| 26 |
#include <optional> |
| 27 |
#include <utility> |
| 28 |
|
| 29 |
namespace { |
| 30 |
|
| 31 |
class SkBlurImageFilter final : public SkImageFilter_Base { |
| 32 |
public: |
| 33 |
SkBlurImageFilter(SkSize sigma, sk_sp<SkImageFilter> input) |
| 34 |
: SkImageFilter_Base(&input, 1) |
| 35 |
, fSigma{sigma} {} |
| 36 |
|
| 37 |
SkBlurImageFilter(SkSize sigma, SkTileMode legacyTileMode, sk_sp<SkImageFilter> input) |
| 38 |
: SkImageFilter_Base(&input, 1) |
| 39 |
, fSigma(sigma) |
| 40 |
, fLegacyTileMode(legacyTileMode) {} |
| 41 |
|
| 42 |
SkRect computeFastBounds(const SkRect&) const override; |
| 43 |
|
| 44 |
protected: |
| 45 |
void flatten(SkWriteBuffer&) const override; |
| 46 |
|
| 47 |
private: |
| 48 |
friend void ::SkRegisterBlurImageFilterFlattenable(); |
| 49 |
SK_FLATTENABLE_HOOKS(SkBlurImageFilter) |
| 50 |
|
| 51 |
skif::FilterResult onFilterImage(const skif::Context& context) const override; |
| 52 |
|
| 53 |
skif::LayerSpace<SkIRect> onGetInputLayerBounds( |
| 54 |
const skif::Mapping& mapping, |
| 55 |
const skif::LayerSpace<SkIRect>& desiredOutput, |
| 56 |
std::optional<skif::LayerSpace<SkIRect>> contentBounds) const override; |
| 57 |
|
| 58 |
std::optional<skif::LayerSpace<SkIRect>> onGetOutputLayerBounds( |
| 59 |
const skif::Mapping& mapping, |
| 60 |
std::optional<skif::LayerSpace<SkIRect>> contentBounds) const override; |
| 61 |
|
| 62 |
skif::LayerSpace<SkSize> mapSigma(const skif::Mapping& mapping) const; |
| 63 |
|
| 64 |
skif::LayerSpace<SkIRect> kernelBounds(const skif::Mapping& mapping, |
| 65 |
skif::LayerSpace<SkIRect> bounds) const { |
| 66 |
skif::LayerSpace<SkSize> sigma = this->mapSigma(mapping); |
| 67 |
bounds.outset(skif::LayerSpace<SkSize>({3 * sigma.width(), 3 * sigma.height()}).ceil()); |
| 68 |
return bounds; |
| 69 |
} |
| 70 |
|
| 71 |
skif::ParameterSpace<SkSize> fSigma; |
| 72 |
// kDecal means no legacy tiling, it will be handled by SkCropImageFilter instead. Legacy |
| 73 |
// tiling occurs when there's no provided crop rect, and should be deleted once clients create |
| 74 |
// their filters with defined tiling geometry. |
| 75 |
SkTileMode fLegacyTileMode = SkTileMode::kDecal; |
| 76 |
}; |
| 77 |
|
| 78 |
} // end namespace |
| 79 |
|
| 80 |
sk_sp<SkImageFilter> SkImageFilters::Blur( |
| 81 |
SkScalar sigmaX, SkScalar sigmaY, SkTileMode tileMode, sk_sp<SkImageFilter> input, |
| 82 |
const CropRect& cropRect) { |
| 83 |
if (!SkIsFinite(sigmaX, sigmaY) || sigmaX < 0.f || sigmaY < 0.f) { |
| 84 |
// Non-finite or negative sigmas are error conditions. We allow 0 sigma for X and/or Y |
| 85 |
// for 1D blurs; onFilterImage() will detect when no visible blurring would occur based on |
| 86 |
// the Context mapping. |
| 87 |
return nullptr; |
| 88 |
} |
| 89 |
|
| 90 |
// Temporarily allow tiling with no crop rect |
| 91 |
if (tileMode != SkTileMode::kDecal && !cropRect) { |
| 92 |
return sk_make_sp<SkBlurImageFilter>(SkSize{sigmaX, sigmaY}, tileMode, std::move(input)); |
| 93 |
} |
| 94 |
|
| 95 |
// The 'tileMode' behavior is not well-defined if there is no crop. We only apply it if |
| 96 |
// there is a provided 'cropRect'. |
| 97 |
sk_sp<SkImageFilter> filter = std::move(input); |
| 98 |
if (tileMode != SkTileMode::kDecal && cropRect) { |
| 99 |
// Historically the input image was restricted to the cropRect when tiling was not |
| 100 |
// kDecal, so that the kernel evaluated the tiled edge conditions, while a kDecal crop |
| 101 |
// only affected the output. |
| 102 |
filter = SkImageFilters::Crop(*cropRect, tileMode, std::move(filter)); |
| 103 |
} |
| 104 |
|
| 105 |
filter = sk_make_sp<SkBlurImageFilter>(SkSize{sigmaX, sigmaY}, std::move(filter)); |
| 106 |
if (cropRect) { |
| 107 |
// But regardless of the tileMode, the output is always decal cropped |
| 108 |
filter = SkImageFilters::Crop(*cropRect, SkTileMode::kDecal, std::move(filter)); |
| 109 |
} |
| 110 |
return filter; |
| 111 |
} |
| 112 |
|
| 113 |
void SkRegisterBlurImageFilterFlattenable() { |
| 114 |
SK_REGISTER_FLATTENABLE(SkBlurImageFilter); |
| 115 |
SkFlattenable::Register("SkBlurImageFilterImpl", SkBlurImageFilter::CreateProc); |
| 116 |
} |
| 117 |
|
| 118 |
sk_sp<SkFlattenable> SkBlurImageFilter::CreateProc(SkReadBuffer& buffer) { |
| 119 |
SK_IMAGEFILTER_UNFLATTEN_COMMON(common, 1); |
| 120 |
SkScalar sigmaX = buffer.readScalar(); |
| 121 |
SkScalar sigmaY = buffer.readScalar(); |
| 122 |
SkTileMode tileMode = buffer.read32LE(SkTileMode::kLastTileMode); |
| 123 |
|
| 124 |
// NOTE: For new SKPs, 'tileMode' holds the "legacy" tile mode; any originally specified tile |
| 125 |
// mode with valid tiling geometry is handled in the SkCropImageFilters that wrap the blur. |
| 126 |
// In a new SKP, when 'tileMode' is not kDecal, common.cropRect() will be null and the blur |
| 127 |
// will automatically emulate the legacy tiling. |
| 128 |
// |
| 129 |
// In old SKPs, the 'tileMode' and common.cropRect() may not be null. ::Blur() automatically |
| 130 |
// detects when this is a legacy or valid tiling and constructs the DAG appropriately. |
| 131 |
return SkImageFilters::Blur( |
| 132 |
sigmaX, sigmaY, tileMode, common.getInput(0), common.cropRect()); |
| 133 |
} |
| 134 |
|
| 135 |
void SkBlurImageFilter::flatten(SkWriteBuffer& buffer) const { |
| 136 |
this->SkImageFilter_Base::flatten(buffer); |
| 137 |
|
| 138 |
buffer.writeScalar(SkSize(fSigma).fWidth); |
| 139 |
buffer.writeScalar(SkSize(fSigma).fHeight); |
| 140 |
buffer.writeInt(static_cast<int>(fLegacyTileMode)); |
| 141 |
} |
| 142 |
|
| 143 |
/////////////////////////////////////////////////////////////////////////////// |
| 144 |
|
| 145 |
namespace { |
| 146 |
|
| 147 |
// This rather arbitrary-looking value results in a maximum box blur kernel size of 1000 pixels on |
| 148 |
// the raster path, which matches the WebKit and Firefox implementations. Since the GPU path does |
| 149 |
// not compute a box blur, putting the limit on sigma ensures consistent behaviour between the GPU |
| 150 |
// and raster paths. |
| 151 |
static constexpr SkScalar kMaxSigma = 532.f; |
| 152 |
|
| 153 |
} // namespace |
| 154 |
|
| 155 |
skif::FilterResult SkBlurImageFilter::onFilterImage(const skif::Context& ctx) const { |
| 156 |
skif::Context inputCtx = ctx.withNewDesiredOutput( |
| 157 |
this->kernelBounds(ctx.mapping(), ctx.desiredOutput())); |
| 158 |
|
| 159 |
skif::FilterResult childOutput = this->getChildOutput(0, inputCtx); |
| 160 |
skif::LayerSpace<SkSize> sigma = this->mapSigma(ctx.mapping()); |
| 161 |
if (sigma.width() == 0.f && sigma.height() == 0.f) { |
| 162 |
// No actual blur, so just return the input unmodified |
| 163 |
return childOutput; |
| 164 |
} |
| 165 |
|
| 166 |
SkASSERT(sigma.width() >= 0.f && sigma.width() <= kMaxSigma && |
| 167 |
sigma.height() >= 0.f && sigma.height() <= kMaxSigma); |
| 168 |
|
| 169 |
// By default, FilterResult::blur() will calculate a more optimal output automatically, so |
| 170 |
// convey the original output to it. |
| 171 |
skif::LayerSpace<SkIRect> maxOutput = ctx.desiredOutput(); |
| 172 |
if (fLegacyTileMode != SkTileMode::kDecal) { |
| 173 |
// Legacy tiling output is also dependent on the original child output bounds ignoring |
| 174 |
// the tile mode's effect. |
| 175 |
maxOutput = this->kernelBounds(ctx.mapping(), childOutput.layerBounds()); |
| 176 |
if (!maxOutput.intersect(ctx.desiredOutput())) { |
| 177 |
return {}; |
| 178 |
} |
| 179 |
} |
| 180 |
if (fLegacyTileMode != SkTileMode::kDecal) { |
| 181 |
// Legacy tiling applied to the input image when there was no explicit crop rect. Use the |
| 182 |
// child's output image's layer bounds as the crop rectangle to adjust the edge tile mode |
| 183 |
// without restricting the image. |
| 184 |
childOutput = childOutput.applyCrop(inputCtx, |
| 185 |
childOutput.layerBounds(), |
| 186 |
fLegacyTileMode); |
| 187 |
} |
| 188 |
|
| 189 |
// For non-legacy tiling, 'maxOutput' is equal to the desired output. For decal's it matches |
| 190 |
// what Builder::blur() calculates internally. For legacy tiling, however, it's dependent on |
| 191 |
// the original child output's bounds ignoring the tile mode's effect. |
| 192 |
skif::Context croppedOutput = ctx.withNewDesiredOutput(maxOutput); |
| 193 |
skif::FilterResult::Builder builder{croppedOutput}; |
| 194 |
builder.add(childOutput); |
| 195 |
return builder.blur(sigma); |
| 196 |
} |
| 197 |
|
| 198 |
skif::LayerSpace<SkSize> SkBlurImageFilter::mapSigma(const skif::Mapping& mapping) const { |
| 199 |
skif::LayerSpace<SkSize> sigma = mapping.paramToLayer(fSigma); |
| 200 |
// Clamp to the maximum sigma |
| 201 |
sigma = skif::LayerSpace<SkSize>({std::min(sigma.width(), kMaxSigma), |
| 202 |
std::min(sigma.height(), kMaxSigma)}); |
| 203 |
|
| 204 |
// Disable bluring on axes that are not finite, or that are small enough that the blur is |
| 205 |
// effectively an identity. |
| 206 |
if (!SkIsFinite(sigma.width()) || SkBlurEngine::IsEffectivelyIdentity(sigma.width())) { |
| 207 |
sigma = skif::LayerSpace<SkSize>({0.f, sigma.height()}); |
| 208 |
} |
| 209 |
|
| 210 |
if (!SkIsFinite(sigma.height()) || SkBlurEngine::IsEffectivelyIdentity(sigma.height())) { |
| 211 |
sigma = skif::LayerSpace<SkSize>({sigma.width(), 0.f}); |
| 212 |
} |
| 213 |
|
| 214 |
return sigma; |
| 215 |
} |
| 216 |
|
| 217 |
skif::LayerSpace<SkIRect> SkBlurImageFilter::onGetInputLayerBounds( |
| 218 |
const skif::Mapping& mapping, |
| 219 |
const skif::LayerSpace<SkIRect>& desiredOutput, |
| 220 |
std::optional<skif::LayerSpace<SkIRect>> contentBounds) const { |
| 221 |
skif::LayerSpace<SkIRect> requiredInput = |
| 222 |
this->kernelBounds(mapping, desiredOutput); |
| 223 |
return this->getChildInputLayerBounds(0, mapping, requiredInput, contentBounds); |
| 224 |
} |
| 225 |
|
| 226 |
std::optional<skif::LayerSpace<SkIRect>> SkBlurImageFilter::onGetOutputLayerBounds( |
| 227 |
const skif::Mapping& mapping, |
| 228 |
std::optional<skif::LayerSpace<SkIRect>> contentBounds) const { |
| 229 |
auto childOutput = this->getChildOutputLayerBounds(0, mapping, contentBounds); |
| 230 |
if (childOutput) { |
| 231 |
return this->kernelBounds(mapping, *childOutput); |
| 232 |
} else { |
| 233 |
return skif::LayerSpace<SkIRect>::Unbounded(); |
| 234 |
} |
| 235 |
} |
| 236 |
|
| 237 |
SkRect SkBlurImageFilter::computeFastBounds(const SkRect& src) const { |
| 238 |
SkRect bounds = this->getInput(0) ? this->getInput(0)->computeFastBounds(src) : src; |
| 239 |
bounds.outset(SkSize(fSigma).width() * 3, SkSize(fSigma).height() * 3); |
| 240 |
return bounds; |
| 241 |
} |