| 1 | /* |
| 2 | * Copyright 2013 Google Inc. |
| 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/SkAlphaThresholdFilter.h" |
| 9 | |
| 10 | #include "include/core/SkBitmap.h" |
| 11 | #include "include/core/SkRegion.h" |
| 12 | #include "src/core/SkImageFilter_Base.h" |
| 13 | #include "src/core/SkReadBuffer.h" |
| 14 | #include "src/core/SkSpecialImage.h" |
| 15 | #include "src/core/SkWriteBuffer.h" |
| 16 | |
| 17 | #if SK_SUPPORT_GPU |
| 18 | #include "include/gpu/GrContext.h" |
| 19 | #include "include/private/GrRecordingContext.h" |
| 20 | #include "src/gpu/GrCaps.h" |
| 21 | #include "src/gpu/GrColorSpaceXform.h" |
| 22 | #include "src/gpu/GrFixedClip.h" |
| 23 | #include "src/gpu/GrRecordingContextPriv.h" |
| 24 | #include "src/gpu/GrRenderTargetContext.h" |
| 25 | #include "src/gpu/GrTextureProxy.h" |
| 26 | #include "src/gpu/effects/GrTextureEffect.h" |
| 27 | #include "src/gpu/effects/generated/GrAlphaThresholdFragmentProcessor.h" |
| 28 | #endif |
| 29 | |
| 30 | namespace { |
| 31 | |
| 32 | class SkAlphaThresholdFilterImpl final : public SkImageFilter_Base { |
| 33 | public: |
| 34 | SkAlphaThresholdFilterImpl(const SkRegion& region, SkScalar innerThreshold, |
| 35 | SkScalar outerThreshold, sk_sp<SkImageFilter> input, |
| 36 | const CropRect* cropRect = nullptr) |
| 37 | : INHERITED(&input, 1, cropRect) |
| 38 | , fRegion(region) |
| 39 | , fInnerThreshold(innerThreshold) |
| 40 | , fOuterThreshold(outerThreshold) {} |
| 41 | |
| 42 | protected: |
| 43 | void flatten(SkWriteBuffer&) const override; |
| 44 | |
| 45 | sk_sp<SkSpecialImage> onFilterImage(const Context&, SkIPoint* offset) const override; |
| 46 | |
| 47 | #if SK_SUPPORT_GPU |
| 48 | GrSurfaceProxyView createMaskTexture(GrRecordingContext*, |
| 49 | const SkMatrix&, |
| 50 | const SkIRect& bounds) const; |
| 51 | #endif |
| 52 | |
| 53 | private: |
| 54 | friend void SkAlphaThresholdFilter::RegisterFlattenables(); |
| 55 | SK_FLATTENABLE_HOOKS(SkAlphaThresholdFilterImpl) |
| 56 | |
| 57 | SkRegion fRegion; |
| 58 | SkScalar fInnerThreshold; |
| 59 | SkScalar fOuterThreshold; |
| 60 | |
| 61 | typedef SkImageFilter_Base INHERITED; |
| 62 | }; |
| 63 | |
| 64 | }; // end namespace |
| 65 | |
| 66 | sk_sp<SkImageFilter> SkAlphaThresholdFilter::Make(const SkRegion& region, SkScalar innerThreshold, |
| 67 | SkScalar outerThreshold, |
| 68 | sk_sp<SkImageFilter> input, |
| 69 | const SkImageFilter::CropRect* cropRect) { |
| 70 | innerThreshold = SkTPin(innerThreshold, 0.f, 1.f); |
| 71 | outerThreshold = SkTPin(outerThreshold, 0.f, 1.f); |
| 72 | if (!SkScalarIsFinite(innerThreshold) || !SkScalarIsFinite(outerThreshold)) { |
| 73 | return nullptr; |
| 74 | } |
| 75 | return sk_sp<SkImageFilter>(new SkAlphaThresholdFilterImpl( |
| 76 | region, innerThreshold, outerThreshold, std::move(input), cropRect)); |
| 77 | } |
| 78 | |
| 79 | void SkAlphaThresholdFilter::RegisterFlattenables() { |
| 80 | SK_REGISTER_FLATTENABLE(SkAlphaThresholdFilterImpl); |
| 81 | } |
| 82 | |
| 83 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 84 | |
| 85 | sk_sp<SkFlattenable> SkAlphaThresholdFilterImpl::CreateProc(SkReadBuffer& buffer) { |
| 86 | SK_IMAGEFILTER_UNFLATTEN_COMMON(common, 1); |
| 87 | SkScalar inner = buffer.readScalar(); |
| 88 | SkScalar outer = buffer.readScalar(); |
| 89 | SkRegion rgn; |
| 90 | buffer.readRegion(&rgn); |
| 91 | return SkAlphaThresholdFilter::Make(rgn, inner, outer, common.getInput(0), |
| 92 | &common.cropRect()); |
| 93 | } |
| 94 | |
| 95 | void SkAlphaThresholdFilterImpl::flatten(SkWriteBuffer& buffer) const { |
| 96 | this->INHERITED::flatten(buffer); |
| 97 | buffer.writeScalar(fInnerThreshold); |
| 98 | buffer.writeScalar(fOuterThreshold); |
| 99 | buffer.writeRegion(fRegion); |
| 100 | } |
| 101 | |
| 102 | #if SK_SUPPORT_GPU |
| 103 | GrSurfaceProxyView SkAlphaThresholdFilterImpl::createMaskTexture(GrRecordingContext* context, |
| 104 | const SkMatrix& inMatrix, |
| 105 | const SkIRect& bounds) const { |
| 106 | auto rtContext = GrRenderTargetContext::MakeWithFallback( |
| 107 | context, GrColorType::kAlpha_8, nullptr, SkBackingFit::kApprox, bounds.size()); |
| 108 | if (!rtContext) { |
| 109 | return {}; |
| 110 | } |
| 111 | |
| 112 | SkRegion::Iterator iter(fRegion); |
| 113 | rtContext->clear(nullptr, SK_PMColor4fTRANSPARENT, |
| 114 | GrRenderTargetContext::CanClearFullscreen::kYes); |
| 115 | |
| 116 | GrFixedClip clip(SkIRect::MakeWH(bounds.width(), bounds.height())); |
| 117 | while (!iter.done()) { |
| 118 | GrPaint paint; |
| 119 | paint.setPorterDuffXPFactory(SkBlendMode::kSrc); |
| 120 | |
| 121 | SkRect rect = SkRect::Make(iter.rect()); |
| 122 | |
| 123 | rtContext->drawRect(clip, std::move(paint), GrAA::kNo, inMatrix, rect); |
| 124 | |
| 125 | iter.next(); |
| 126 | } |
| 127 | |
| 128 | return rtContext->readSurfaceView(); |
| 129 | } |
| 130 | #endif |
| 131 | |
| 132 | sk_sp<SkSpecialImage> SkAlphaThresholdFilterImpl::onFilterImage(const Context& ctx, |
| 133 | SkIPoint* offset) const { |
| 134 | SkIPoint inputOffset = SkIPoint::Make(0, 0); |
| 135 | sk_sp<SkSpecialImage> input(this->filterInput(0, ctx, &inputOffset)); |
| 136 | if (!input) { |
| 137 | return nullptr; |
| 138 | } |
| 139 | |
| 140 | const SkIRect inputBounds = SkIRect::MakeXYWH(inputOffset.x(), inputOffset.y(), |
| 141 | input->width(), input->height()); |
| 142 | |
| 143 | SkIRect bounds; |
| 144 | if (!this->applyCropRect(ctx, inputBounds, &bounds)) { |
| 145 | return nullptr; |
| 146 | } |
| 147 | |
| 148 | #if SK_SUPPORT_GPU |
| 149 | if (ctx.gpuBacked()) { |
| 150 | auto context = ctx.getContext(); |
| 151 | |
| 152 | GrSurfaceProxyView inputView = (input->view(context)); |
| 153 | SkASSERT(inputView.asTextureProxy()); |
| 154 | const GrProtected isProtected = inputView.proxy()->isProtected(); |
| 155 | |
| 156 | offset->fX = bounds.left(); |
| 157 | offset->fY = bounds.top(); |
| 158 | |
| 159 | bounds.offset(-inputOffset); |
| 160 | |
| 161 | SkMatrix matrix(ctx.ctm()); |
| 162 | matrix.postTranslate(SkIntToScalar(-offset->fX), SkIntToScalar(-offset->fY)); |
| 163 | |
| 164 | GrSurfaceProxyView maskView = this->createMaskTexture(context, matrix, bounds); |
| 165 | if (!maskView) { |
| 166 | return nullptr; |
| 167 | } |
| 168 | |
| 169 | auto textureFP = GrTextureEffect::Make( |
| 170 | std::move(inputView), input->alphaType(), |
| 171 | SkMatrix::MakeTrans(input->subset().x(), input->subset().y())); |
| 172 | textureFP = GrColorSpaceXformEffect::Make(std::move(textureFP), input->getColorSpace(), |
| 173 | input->alphaType(), ctx.colorSpace()); |
| 174 | if (!textureFP) { |
| 175 | return nullptr; |
| 176 | } |
| 177 | |
| 178 | auto thresholdFP = GrAlphaThresholdFragmentProcessor::Make( |
| 179 | std::move(maskView), fInnerThreshold, fOuterThreshold, bounds); |
| 180 | if (!thresholdFP) { |
| 181 | return nullptr; |
| 182 | } |
| 183 | |
| 184 | std::unique_ptr<GrFragmentProcessor> fpSeries[] = { std::move(textureFP), |
| 185 | std::move(thresholdFP) }; |
| 186 | auto fp = GrFragmentProcessor::RunInSeries(fpSeries, 2); |
| 187 | |
| 188 | return DrawWithFP(context, std::move(fp), bounds, ctx.colorType(), ctx.colorSpace(), |
| 189 | isProtected); |
| 190 | } |
| 191 | #endif |
| 192 | |
| 193 | SkBitmap inputBM; |
| 194 | |
| 195 | if (!input->getROPixels(&inputBM)) { |
| 196 | return nullptr; |
| 197 | } |
| 198 | |
| 199 | if (inputBM.colorType() != kN32_SkColorType) { |
| 200 | return nullptr; |
| 201 | } |
| 202 | |
| 203 | if (!inputBM.getPixels() || inputBM.width() <= 0 || inputBM.height() <= 0) { |
| 204 | return nullptr; |
| 205 | } |
| 206 | |
| 207 | |
| 208 | SkMatrix localInverse; |
| 209 | if (!ctx.ctm().invert(&localInverse)) { |
| 210 | return nullptr; |
| 211 | } |
| 212 | |
| 213 | SkImageInfo info = SkImageInfo::MakeN32(bounds.width(), bounds.height(), |
| 214 | kPremul_SkAlphaType); |
| 215 | |
| 216 | SkBitmap dst; |
| 217 | if (!dst.tryAllocPixels(info)) { |
| 218 | return nullptr; |
| 219 | } |
| 220 | |
| 221 | U8CPU innerThreshold = (U8CPU)(fInnerThreshold * 0xFF); |
| 222 | U8CPU outerThreshold = (U8CPU)(fOuterThreshold * 0xFF); |
| 223 | SkColor* dptr = dst.getAddr32(0, 0); |
| 224 | int dstWidth = dst.width(), dstHeight = dst.height(); |
| 225 | SkIPoint srcOffset = { bounds.fLeft - inputOffset.fX, bounds.fTop - inputOffset.fY }; |
| 226 | for (int y = 0; y < dstHeight; ++y) { |
| 227 | const SkColor* sptr = inputBM.getAddr32(srcOffset.fX, srcOffset.fY+y); |
| 228 | |
| 229 | for (int x = 0; x < dstWidth; ++x) { |
| 230 | const SkColor& source = sptr[x]; |
| 231 | SkColor outputColor(source); |
| 232 | SkPoint position; |
| 233 | localInverse.mapXY((SkScalar)x + bounds.fLeft, (SkScalar)y + bounds.fTop, &position); |
| 234 | if (fRegion.contains((int32_t)position.x(), (int32_t)position.y())) { |
| 235 | if (SkColorGetA(source) < innerThreshold) { |
| 236 | U8CPU alpha = SkColorGetA(source); |
| 237 | if (alpha == 0) { |
| 238 | alpha = 1; |
| 239 | } |
| 240 | float scale = (float)innerThreshold / alpha; |
| 241 | outputColor = SkColorSetARGB(innerThreshold, |
| 242 | (U8CPU)(SkColorGetR(source) * scale), |
| 243 | (U8CPU)(SkColorGetG(source) * scale), |
| 244 | (U8CPU)(SkColorGetB(source) * scale)); |
| 245 | } |
| 246 | } else { |
| 247 | if (SkColorGetA(source) > outerThreshold) { |
| 248 | float scale = (float)outerThreshold / SkColorGetA(source); |
| 249 | outputColor = SkColorSetARGB(outerThreshold, |
| 250 | (U8CPU)(SkColorGetR(source) * scale), |
| 251 | (U8CPU)(SkColorGetG(source) * scale), |
| 252 | (U8CPU)(SkColorGetB(source) * scale)); |
| 253 | } |
| 254 | } |
| 255 | dptr[y * dstWidth + x] = outputColor; |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | offset->fX = bounds.left(); |
| 260 | offset->fY = bounds.top(); |
| 261 | return SkSpecialImage::MakeFromRaster(SkIRect::MakeWH(bounds.width(), bounds.height()), |
| 262 | dst); |
| 263 | } |
| 264 | |