1cb93a386Sopenharmony_ci/* 2cb93a386Sopenharmony_ci * Copyright 2006 The Android Open Source Project 3cb93a386Sopenharmony_ci * 4cb93a386Sopenharmony_ci * Use of this source code is governed by a BSD-style license that can be 5cb93a386Sopenharmony_ci * found in the LICENSE file. 6cb93a386Sopenharmony_ci */ 7cb93a386Sopenharmony_ci 8cb93a386Sopenharmony_ci#include "include/core/SkBitmap.h" 9cb93a386Sopenharmony_ci#include "include/core/SkMaskFilter.h" 10cb93a386Sopenharmony_ci#include "include/core/SkPathBuilder.h" 11cb93a386Sopenharmony_ci#include "include/core/SkRRect.h" 12cb93a386Sopenharmony_ci#include "include/core/SkStrokeRec.h" 13cb93a386Sopenharmony_ci#include "include/core/SkVertices.h" 14cb93a386Sopenharmony_ci#include "src/core/SkBlurMask.h" 15cb93a386Sopenharmony_ci#include "src/core/SkGpuBlurUtils.h" 16cb93a386Sopenharmony_ci#include "src/core/SkMaskFilterBase.h" 17cb93a386Sopenharmony_ci#include "src/core/SkMathPriv.h" 18cb93a386Sopenharmony_ci#include "src/core/SkMatrixProvider.h" 19cb93a386Sopenharmony_ci#include "src/core/SkRRectPriv.h" 20cb93a386Sopenharmony_ci#include "src/core/SkReadBuffer.h" 21cb93a386Sopenharmony_ci#include "src/core/SkStringUtils.h" 22cb93a386Sopenharmony_ci#include "src/core/SkSDFFilter.h" 23cb93a386Sopenharmony_ci#include "src/core/SkWriteBuffer.h" 24cb93a386Sopenharmony_ci 25cb93a386Sopenharmony_ci#if SK_SUPPORT_GPU 26cb93a386Sopenharmony_ci#include "include/gpu/GrRecordingContext.h" 27cb93a386Sopenharmony_ci#include "src/core/SkRuntimeEffectPriv.h" 28cb93a386Sopenharmony_ci#include "src/gpu/GrFragmentProcessor.h" 29cb93a386Sopenharmony_ci#include "src/gpu/GrRecordingContextPriv.h" 30cb93a386Sopenharmony_ci#include "src/gpu/GrResourceProvider.h" 31cb93a386Sopenharmony_ci#include "src/gpu/GrShaderCaps.h" 32cb93a386Sopenharmony_ci#include "src/gpu/GrStyle.h" 33cb93a386Sopenharmony_ci#include "src/gpu/GrTextureProxy.h" 34cb93a386Sopenharmony_ci#include "src/gpu/GrThreadSafeCache.h" 35cb93a386Sopenharmony_ci#include "src/gpu/SkGr.h" 36cb93a386Sopenharmony_ci#include "src/gpu/effects/GrMatrixEffect.h" 37cb93a386Sopenharmony_ci#include "src/gpu/effects/GrSkSLFP.h" 38cb93a386Sopenharmony_ci#include "src/gpu/effects/GrTextureEffect.h" 39cb93a386Sopenharmony_ci#include "src/gpu/geometry/GrStyledShape.h" 40cb93a386Sopenharmony_ci#include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h" 41cb93a386Sopenharmony_ci#include "src/gpu/glsl/GrGLSLProgramDataManager.h" 42cb93a386Sopenharmony_ci#include "src/gpu/glsl/GrGLSLUniformHandler.h" 43cb93a386Sopenharmony_ci#if SK_GPU_V1 44cb93a386Sopenharmony_ci#include "src/gpu/v1/SurfaceDrawContext_v1.h" 45cb93a386Sopenharmony_ci#endif // SK_GPU_V1 46cb93a386Sopenharmony_ci#endif // SK_SUPPORT_GPU 47cb93a386Sopenharmony_ci 48cb93a386Sopenharmony_ciclass SkBlurMaskFilterImpl : public SkMaskFilterBase { 49cb93a386Sopenharmony_cipublic: 50cb93a386Sopenharmony_ci SkBlurMaskFilterImpl(SkScalar sigma, SkBlurStyle, bool respectCTM); 51cb93a386Sopenharmony_ci 52cb93a386Sopenharmony_ci // overrides from SkMaskFilter 53cb93a386Sopenharmony_ci SkMask::Format getFormat() const override; 54cb93a386Sopenharmony_ci bool filterMask(SkMask* dst, const SkMask& src, const SkMatrix&, 55cb93a386Sopenharmony_ci SkIPoint* margin) const override; 56cb93a386Sopenharmony_ci 57cb93a386Sopenharmony_ci#if SK_SUPPORT_GPU && SK_GPU_V1 58cb93a386Sopenharmony_ci bool canFilterMaskGPU(const GrStyledShape& shape, 59cb93a386Sopenharmony_ci const SkIRect& devSpaceShapeBounds, 60cb93a386Sopenharmony_ci const SkIRect& clipBounds, 61cb93a386Sopenharmony_ci const SkMatrix& ctm, 62cb93a386Sopenharmony_ci SkIRect* maskRect, 63cb93a386Sopenharmony_ci const bool canUseSDFBlur = false) const override; 64cb93a386Sopenharmony_ci bool directFilterMaskGPU(GrRecordingContext*, 65cb93a386Sopenharmony_ci skgpu::v1::SurfaceDrawContext*, 66cb93a386Sopenharmony_ci GrPaint&&, 67cb93a386Sopenharmony_ci const GrClip*, 68cb93a386Sopenharmony_ci const SkMatrix& viewMatrix, 69cb93a386Sopenharmony_ci const GrStyledShape&) const override; 70cb93a386Sopenharmony_ci GrSurfaceProxyView filterMaskGPU(GrRecordingContext*, 71cb93a386Sopenharmony_ci GrSurfaceProxyView srcView, 72cb93a386Sopenharmony_ci GrColorType srcColorType, 73cb93a386Sopenharmony_ci SkAlphaType srcAlphaType, 74cb93a386Sopenharmony_ci const SkMatrix& ctm, 75cb93a386Sopenharmony_ci const SkIRect& maskRect) const override; 76cb93a386Sopenharmony_ci 77cb93a386Sopenharmony_ci float getNoxFormedSigma3() const override; 78cb93a386Sopenharmony_ci 79cb93a386Sopenharmony_ci GrSurfaceProxyView filterMaskGPUNoxFormed(GrRecordingContext*, GrSurfaceProxyView srcView, 80cb93a386Sopenharmony_ci GrColorType srcColorType, SkAlphaType srcAlphaType, const SkMatrix& viewMatrix, const SkIRect& maskRect, 81cb93a386Sopenharmony_ci const SkRRect& srcRRect) const override; 82cb93a386Sopenharmony_ci#endif 83cb93a386Sopenharmony_ci 84cb93a386Sopenharmony_ci void computeFastBounds(const SkRect&, SkRect*) const override; 85cb93a386Sopenharmony_ci bool asABlur(BlurRec*) const override; 86cb93a386Sopenharmony_ci 87cb93a386Sopenharmony_ci 88cb93a386Sopenharmony_ciprotected: 89cb93a386Sopenharmony_ci FilterReturn filterRectsToNine(const SkRect[], int count, const SkMatrix&, 90cb93a386Sopenharmony_ci const SkIRect& clipBounds, 91cb93a386Sopenharmony_ci NinePatch*) const override; 92cb93a386Sopenharmony_ci 93cb93a386Sopenharmony_ci FilterReturn filterRRectToNine(const SkRRect&, const SkMatrix&, 94cb93a386Sopenharmony_ci const SkIRect& clipBounds, 95cb93a386Sopenharmony_ci NinePatch*) const override; 96cb93a386Sopenharmony_ci 97cb93a386Sopenharmony_ci bool filterRectMask(SkMask* dstM, const SkRect& r, const SkMatrix& matrix, 98cb93a386Sopenharmony_ci SkIPoint* margin, SkMask::CreateMode createMode) const; 99cb93a386Sopenharmony_ci bool filterRRectMask(SkMask* dstM, const SkRRect& r, const SkMatrix& matrix, 100cb93a386Sopenharmony_ci SkIPoint* margin, SkMask::CreateMode createMode) const; 101cb93a386Sopenharmony_ci 102cb93a386Sopenharmony_ci bool ignoreXform() const { return !fRespectCTM; } 103cb93a386Sopenharmony_ci 104cb93a386Sopenharmony_ciprivate: 105cb93a386Sopenharmony_ci SK_FLATTENABLE_HOOKS(SkBlurMaskFilterImpl) 106cb93a386Sopenharmony_ci // To avoid unseemly allocation requests (esp. for finite platforms like 107cb93a386Sopenharmony_ci // handset) we limit the radius so something manageable. (as opposed to 108cb93a386Sopenharmony_ci // a request like 10,000) 109cb93a386Sopenharmony_ci static const SkScalar kMAX_BLUR_SIGMA; 110cb93a386Sopenharmony_ci 111cb93a386Sopenharmony_ci SkScalar fSigma; 112cb93a386Sopenharmony_ci SkBlurStyle fBlurStyle; 113cb93a386Sopenharmony_ci bool fRespectCTM; 114cb93a386Sopenharmony_ci 115cb93a386Sopenharmony_ci SkBlurMaskFilterImpl(SkReadBuffer&); 116cb93a386Sopenharmony_ci void flatten(SkWriteBuffer&) const override; 117cb93a386Sopenharmony_ci 118cb93a386Sopenharmony_ci SkScalar computeXformedSigma(const SkMatrix& ctm) const { 119cb93a386Sopenharmony_ci SkScalar xformedSigma = this->ignoreXform() ? fSigma : ctm.mapRadius(fSigma); 120cb93a386Sopenharmony_ci return std::min(xformedSigma, kMAX_BLUR_SIGMA); 121cb93a386Sopenharmony_ci } 122cb93a386Sopenharmony_ci 123cb93a386Sopenharmony_ci friend class SkBlurMaskFilter; 124cb93a386Sopenharmony_ci 125cb93a386Sopenharmony_ci using INHERITED = SkMaskFilter; 126cb93a386Sopenharmony_ci friend void sk_register_blur_maskfilter_createproc(); 127cb93a386Sopenharmony_ci}; 128cb93a386Sopenharmony_ci 129cb93a386Sopenharmony_ciconst SkScalar SkBlurMaskFilterImpl::kMAX_BLUR_SIGMA = SkIntToScalar(128); 130cb93a386Sopenharmony_ci 131cb93a386Sopenharmony_ci/////////////////////////////////////////////////////////////////////////////// 132cb93a386Sopenharmony_ci 133cb93a386Sopenharmony_ciSkBlurMaskFilterImpl::SkBlurMaskFilterImpl(SkScalar sigma, SkBlurStyle style, bool respectCTM) 134cb93a386Sopenharmony_ci : fSigma(sigma) 135cb93a386Sopenharmony_ci , fBlurStyle(style) 136cb93a386Sopenharmony_ci , fRespectCTM(respectCTM) { 137cb93a386Sopenharmony_ci SkASSERT(fSigma > 0); 138cb93a386Sopenharmony_ci SkASSERT((unsigned)style <= kLastEnum_SkBlurStyle); 139cb93a386Sopenharmony_ci} 140cb93a386Sopenharmony_ci 141cb93a386Sopenharmony_ciSkMask::Format SkBlurMaskFilterImpl::getFormat() const { 142cb93a386Sopenharmony_ci return SkMask::kA8_Format; 143cb93a386Sopenharmony_ci} 144cb93a386Sopenharmony_ci 145cb93a386Sopenharmony_cibool SkBlurMaskFilterImpl::asABlur(BlurRec* rec) const { 146cb93a386Sopenharmony_ci if (this->ignoreXform()) { 147cb93a386Sopenharmony_ci return false; 148cb93a386Sopenharmony_ci } 149cb93a386Sopenharmony_ci 150cb93a386Sopenharmony_ci if (rec) { 151cb93a386Sopenharmony_ci rec->fSigma = fSigma; 152cb93a386Sopenharmony_ci rec->fStyle = fBlurStyle; 153cb93a386Sopenharmony_ci } 154cb93a386Sopenharmony_ci return true; 155cb93a386Sopenharmony_ci} 156cb93a386Sopenharmony_ci 157cb93a386Sopenharmony_cibool SkBlurMaskFilterImpl::filterMask(SkMask* dst, const SkMask& src, 158cb93a386Sopenharmony_ci const SkMatrix& matrix, 159cb93a386Sopenharmony_ci SkIPoint* margin) const { 160cb93a386Sopenharmony_ci SkScalar sigma = this->computeXformedSigma(matrix); 161cb93a386Sopenharmony_ci return SkBlurMask::BoxBlur(dst, src, sigma, fBlurStyle, margin); 162cb93a386Sopenharmony_ci} 163cb93a386Sopenharmony_ci 164cb93a386Sopenharmony_cibool SkBlurMaskFilterImpl::filterRectMask(SkMask* dst, const SkRect& r, 165cb93a386Sopenharmony_ci const SkMatrix& matrix, 166cb93a386Sopenharmony_ci SkIPoint* margin, SkMask::CreateMode createMode) const { 167cb93a386Sopenharmony_ci SkScalar sigma = computeXformedSigma(matrix); 168cb93a386Sopenharmony_ci 169cb93a386Sopenharmony_ci return SkBlurMask::BlurRect(sigma, dst, r, fBlurStyle, margin, createMode); 170cb93a386Sopenharmony_ci} 171cb93a386Sopenharmony_ci 172cb93a386Sopenharmony_cibool SkBlurMaskFilterImpl::filterRRectMask(SkMask* dst, const SkRRect& r, 173cb93a386Sopenharmony_ci const SkMatrix& matrix, 174cb93a386Sopenharmony_ci SkIPoint* margin, SkMask::CreateMode createMode) const { 175cb93a386Sopenharmony_ci SkScalar sigma = computeXformedSigma(matrix); 176cb93a386Sopenharmony_ci 177cb93a386Sopenharmony_ci return SkBlurMask::BlurRRect(sigma, dst, r, fBlurStyle, margin, createMode); 178cb93a386Sopenharmony_ci} 179cb93a386Sopenharmony_ci 180cb93a386Sopenharmony_ci#include "include/core/SkCanvas.h" 181cb93a386Sopenharmony_ci 182cb93a386Sopenharmony_cistatic bool prepare_to_draw_into_mask(const SkRect& bounds, SkMask* mask) { 183cb93a386Sopenharmony_ci SkASSERT(mask != nullptr); 184cb93a386Sopenharmony_ci 185cb93a386Sopenharmony_ci mask->fBounds = bounds.roundOut(); 186cb93a386Sopenharmony_ci mask->fRowBytes = SkAlign4(mask->fBounds.width()); 187cb93a386Sopenharmony_ci mask->fFormat = SkMask::kA8_Format; 188cb93a386Sopenharmony_ci const size_t size = mask->computeImageSize(); 189cb93a386Sopenharmony_ci mask->fImage = SkMask::AllocImage(size, SkMask::kZeroInit_Alloc); 190cb93a386Sopenharmony_ci if (nullptr == mask->fImage) { 191cb93a386Sopenharmony_ci return false; 192cb93a386Sopenharmony_ci } 193cb93a386Sopenharmony_ci return true; 194cb93a386Sopenharmony_ci} 195cb93a386Sopenharmony_ci 196cb93a386Sopenharmony_cistatic bool draw_rrect_into_mask(const SkRRect rrect, SkMask* mask) { 197cb93a386Sopenharmony_ci if (!prepare_to_draw_into_mask(rrect.rect(), mask)) { 198cb93a386Sopenharmony_ci return false; 199cb93a386Sopenharmony_ci } 200cb93a386Sopenharmony_ci 201cb93a386Sopenharmony_ci // FIXME: This code duplicates code in draw_rects_into_mask, below. Is there a 202cb93a386Sopenharmony_ci // clean way to share more code? 203cb93a386Sopenharmony_ci SkBitmap bitmap; 204cb93a386Sopenharmony_ci bitmap.installMaskPixels(*mask); 205cb93a386Sopenharmony_ci 206cb93a386Sopenharmony_ci SkCanvas canvas(bitmap); 207cb93a386Sopenharmony_ci canvas.translate(-SkIntToScalar(mask->fBounds.left()), 208cb93a386Sopenharmony_ci -SkIntToScalar(mask->fBounds.top())); 209cb93a386Sopenharmony_ci 210cb93a386Sopenharmony_ci SkPaint paint; 211cb93a386Sopenharmony_ci paint.setAntiAlias(true); 212cb93a386Sopenharmony_ci canvas.drawRRect(rrect, paint); 213cb93a386Sopenharmony_ci return true; 214cb93a386Sopenharmony_ci} 215cb93a386Sopenharmony_ci 216cb93a386Sopenharmony_cistatic bool draw_rects_into_mask(const SkRect rects[], int count, SkMask* mask) { 217cb93a386Sopenharmony_ci if (!prepare_to_draw_into_mask(rects[0], mask)) { 218cb93a386Sopenharmony_ci return false; 219cb93a386Sopenharmony_ci } 220cb93a386Sopenharmony_ci 221cb93a386Sopenharmony_ci SkBitmap bitmap; 222cb93a386Sopenharmony_ci bitmap.installPixels(SkImageInfo::Make(mask->fBounds.width(), 223cb93a386Sopenharmony_ci mask->fBounds.height(), 224cb93a386Sopenharmony_ci kAlpha_8_SkColorType, 225cb93a386Sopenharmony_ci kPremul_SkAlphaType), 226cb93a386Sopenharmony_ci mask->fImage, mask->fRowBytes); 227cb93a386Sopenharmony_ci 228cb93a386Sopenharmony_ci SkCanvas canvas(bitmap); 229cb93a386Sopenharmony_ci canvas.translate(-SkIntToScalar(mask->fBounds.left()), 230cb93a386Sopenharmony_ci -SkIntToScalar(mask->fBounds.top())); 231cb93a386Sopenharmony_ci 232cb93a386Sopenharmony_ci SkPaint paint; 233cb93a386Sopenharmony_ci paint.setAntiAlias(true); 234cb93a386Sopenharmony_ci 235cb93a386Sopenharmony_ci if (1 == count) { 236cb93a386Sopenharmony_ci canvas.drawRect(rects[0], paint); 237cb93a386Sopenharmony_ci } else { 238cb93a386Sopenharmony_ci // todo: do I need a fast way to do this? 239cb93a386Sopenharmony_ci SkPath path = SkPathBuilder().addRect(rects[0]) 240cb93a386Sopenharmony_ci .addRect(rects[1]) 241cb93a386Sopenharmony_ci .setFillType(SkPathFillType::kEvenOdd) 242cb93a386Sopenharmony_ci .detach(); 243cb93a386Sopenharmony_ci canvas.drawPath(path, paint); 244cb93a386Sopenharmony_ci } 245cb93a386Sopenharmony_ci return true; 246cb93a386Sopenharmony_ci} 247cb93a386Sopenharmony_ci 248cb93a386Sopenharmony_cistatic bool rect_exceeds(const SkRect& r, SkScalar v) { 249cb93a386Sopenharmony_ci return r.fLeft < -v || r.fTop < -v || r.fRight > v || r.fBottom > v || 250cb93a386Sopenharmony_ci r.width() > v || r.height() > v; 251cb93a386Sopenharmony_ci} 252cb93a386Sopenharmony_ci 253cb93a386Sopenharmony_ci#include "src/core/SkMaskCache.h" 254cb93a386Sopenharmony_ci 255cb93a386Sopenharmony_cistatic SkCachedData* copy_mask_to_cacheddata(SkMask* mask) { 256cb93a386Sopenharmony_ci const size_t size = mask->computeTotalImageSize(); 257cb93a386Sopenharmony_ci SkCachedData* data = SkResourceCache::NewCachedData(size); 258cb93a386Sopenharmony_ci if (data) { 259cb93a386Sopenharmony_ci memcpy(data->writable_data(), mask->fImage, size); 260cb93a386Sopenharmony_ci SkMask::FreeImage(mask->fImage); 261cb93a386Sopenharmony_ci mask->fImage = (uint8_t*)data->data(); 262cb93a386Sopenharmony_ci } 263cb93a386Sopenharmony_ci return data; 264cb93a386Sopenharmony_ci} 265cb93a386Sopenharmony_ci 266cb93a386Sopenharmony_cistatic SkCachedData* find_cached_rrect(SkMask* mask, SkScalar sigma, SkBlurStyle style, 267cb93a386Sopenharmony_ci const SkRRect& rrect) { 268cb93a386Sopenharmony_ci return SkMaskCache::FindAndRef(sigma, style, rrect, mask); 269cb93a386Sopenharmony_ci} 270cb93a386Sopenharmony_ci 271cb93a386Sopenharmony_cistatic SkCachedData* add_cached_rrect(SkMask* mask, SkScalar sigma, SkBlurStyle style, 272cb93a386Sopenharmony_ci const SkRRect& rrect) { 273cb93a386Sopenharmony_ci SkCachedData* cache = copy_mask_to_cacheddata(mask); 274cb93a386Sopenharmony_ci if (cache) { 275cb93a386Sopenharmony_ci SkMaskCache::Add(sigma, style, rrect, *mask, cache); 276cb93a386Sopenharmony_ci } 277cb93a386Sopenharmony_ci return cache; 278cb93a386Sopenharmony_ci} 279cb93a386Sopenharmony_ci 280cb93a386Sopenharmony_cistatic SkCachedData* find_cached_rects(SkMask* mask, SkScalar sigma, SkBlurStyle style, 281cb93a386Sopenharmony_ci const SkRect rects[], int count) { 282cb93a386Sopenharmony_ci return SkMaskCache::FindAndRef(sigma, style, rects, count, mask); 283cb93a386Sopenharmony_ci} 284cb93a386Sopenharmony_ci 285cb93a386Sopenharmony_cistatic SkCachedData* add_cached_rects(SkMask* mask, SkScalar sigma, SkBlurStyle style, 286cb93a386Sopenharmony_ci const SkRect rects[], int count) { 287cb93a386Sopenharmony_ci SkCachedData* cache = copy_mask_to_cacheddata(mask); 288cb93a386Sopenharmony_ci if (cache) { 289cb93a386Sopenharmony_ci SkMaskCache::Add(sigma, style, rects, count, *mask, cache); 290cb93a386Sopenharmony_ci } 291cb93a386Sopenharmony_ci return cache; 292cb93a386Sopenharmony_ci} 293cb93a386Sopenharmony_ci 294cb93a386Sopenharmony_cistatic const bool c_analyticBlurRRect{true}; 295cb93a386Sopenharmony_ci 296cb93a386Sopenharmony_ciSkMaskFilterBase::FilterReturn 297cb93a386Sopenharmony_ciSkBlurMaskFilterImpl::filterRRectToNine(const SkRRect& rrect, const SkMatrix& matrix, 298cb93a386Sopenharmony_ci const SkIRect& clipBounds, 299cb93a386Sopenharmony_ci NinePatch* patch) const { 300cb93a386Sopenharmony_ci SkASSERT(patch != nullptr); 301cb93a386Sopenharmony_ci switch (rrect.getType()) { 302cb93a386Sopenharmony_ci case SkRRect::kEmpty_Type: 303cb93a386Sopenharmony_ci // Nothing to draw. 304cb93a386Sopenharmony_ci return kFalse_FilterReturn; 305cb93a386Sopenharmony_ci 306cb93a386Sopenharmony_ci case SkRRect::kRect_Type: 307cb93a386Sopenharmony_ci // We should have caught this earlier. 308cb93a386Sopenharmony_ci SkASSERT(false); 309cb93a386Sopenharmony_ci [[fallthrough]]; 310cb93a386Sopenharmony_ci case SkRRect::kOval_Type: 311cb93a386Sopenharmony_ci // The nine patch special case does not handle ovals, and we 312cb93a386Sopenharmony_ci // already have code for rectangles. 313cb93a386Sopenharmony_ci return kUnimplemented_FilterReturn; 314cb93a386Sopenharmony_ci 315cb93a386Sopenharmony_ci // These three can take advantage of this fast path. 316cb93a386Sopenharmony_ci case SkRRect::kSimple_Type: 317cb93a386Sopenharmony_ci case SkRRect::kNinePatch_Type: 318cb93a386Sopenharmony_ci case SkRRect::kComplex_Type: 319cb93a386Sopenharmony_ci break; 320cb93a386Sopenharmony_ci } 321cb93a386Sopenharmony_ci 322cb93a386Sopenharmony_ci // TODO: report correct metrics for innerstyle, where we do not grow the 323cb93a386Sopenharmony_ci // total bounds, but we do need an inset the size of our blur-radius 324cb93a386Sopenharmony_ci if (kInner_SkBlurStyle == fBlurStyle) { 325cb93a386Sopenharmony_ci return kUnimplemented_FilterReturn; 326cb93a386Sopenharmony_ci } 327cb93a386Sopenharmony_ci 328cb93a386Sopenharmony_ci // TODO: take clipBounds into account to limit our coordinates up front 329cb93a386Sopenharmony_ci // for now, just skip too-large src rects (to take the old code path). 330cb93a386Sopenharmony_ci if (rect_exceeds(rrect.rect(), SkIntToScalar(32767))) { 331cb93a386Sopenharmony_ci return kUnimplemented_FilterReturn; 332cb93a386Sopenharmony_ci } 333cb93a386Sopenharmony_ci 334cb93a386Sopenharmony_ci SkIPoint margin; 335cb93a386Sopenharmony_ci SkMask srcM, dstM; 336cb93a386Sopenharmony_ci srcM.fBounds = rrect.rect().roundOut(); 337cb93a386Sopenharmony_ci srcM.fFormat = SkMask::kA8_Format; 338cb93a386Sopenharmony_ci srcM.fRowBytes = 0; 339cb93a386Sopenharmony_ci 340cb93a386Sopenharmony_ci bool filterResult = false; 341cb93a386Sopenharmony_ci if (c_analyticBlurRRect) { 342cb93a386Sopenharmony_ci // special case for fast round rect blur 343cb93a386Sopenharmony_ci // don't actually do the blur the first time, just compute the correct size 344cb93a386Sopenharmony_ci filterResult = this->filterRRectMask(&dstM, rrect, matrix, &margin, 345cb93a386Sopenharmony_ci SkMask::kJustComputeBounds_CreateMode); 346cb93a386Sopenharmony_ci } 347cb93a386Sopenharmony_ci 348cb93a386Sopenharmony_ci if (!filterResult) { 349cb93a386Sopenharmony_ci filterResult = this->filterMask(&dstM, srcM, matrix, &margin); 350cb93a386Sopenharmony_ci } 351cb93a386Sopenharmony_ci 352cb93a386Sopenharmony_ci if (!filterResult) { 353cb93a386Sopenharmony_ci return kFalse_FilterReturn; 354cb93a386Sopenharmony_ci } 355cb93a386Sopenharmony_ci 356cb93a386Sopenharmony_ci // Now figure out the appropriate width and height of the smaller round rectangle 357cb93a386Sopenharmony_ci // to stretch. It will take into account the larger radius per side as well as double 358cb93a386Sopenharmony_ci // the margin, to account for inner and outer blur. 359cb93a386Sopenharmony_ci const SkVector& UL = rrect.radii(SkRRect::kUpperLeft_Corner); 360cb93a386Sopenharmony_ci const SkVector& UR = rrect.radii(SkRRect::kUpperRight_Corner); 361cb93a386Sopenharmony_ci const SkVector& LR = rrect.radii(SkRRect::kLowerRight_Corner); 362cb93a386Sopenharmony_ci const SkVector& LL = rrect.radii(SkRRect::kLowerLeft_Corner); 363cb93a386Sopenharmony_ci 364cb93a386Sopenharmony_ci const SkScalar leftUnstretched = std::max(UL.fX, LL.fX) + SkIntToScalar(2 * margin.fX); 365cb93a386Sopenharmony_ci const SkScalar rightUnstretched = std::max(UR.fX, LR.fX) + SkIntToScalar(2 * margin.fX); 366cb93a386Sopenharmony_ci 367cb93a386Sopenharmony_ci // Extra space in the middle to ensure an unchanging piece for stretching. Use 3 to cover 368cb93a386Sopenharmony_ci // any fractional space on either side plus 1 for the part to stretch. 369cb93a386Sopenharmony_ci const SkScalar stretchSize = SkIntToScalar(3); 370cb93a386Sopenharmony_ci 371cb93a386Sopenharmony_ci const SkScalar totalSmallWidth = leftUnstretched + rightUnstretched + stretchSize; 372cb93a386Sopenharmony_ci if (totalSmallWidth >= rrect.rect().width()) { 373cb93a386Sopenharmony_ci // There is no valid piece to stretch. 374cb93a386Sopenharmony_ci return kUnimplemented_FilterReturn; 375cb93a386Sopenharmony_ci } 376cb93a386Sopenharmony_ci 377cb93a386Sopenharmony_ci const SkScalar topUnstretched = std::max(UL.fY, UR.fY) + SkIntToScalar(2 * margin.fY); 378cb93a386Sopenharmony_ci const SkScalar bottomUnstretched = std::max(LL.fY, LR.fY) + SkIntToScalar(2 * margin.fY); 379cb93a386Sopenharmony_ci 380cb93a386Sopenharmony_ci const SkScalar totalSmallHeight = topUnstretched + bottomUnstretched + stretchSize; 381cb93a386Sopenharmony_ci if (totalSmallHeight >= rrect.rect().height()) { 382cb93a386Sopenharmony_ci // There is no valid piece to stretch. 383cb93a386Sopenharmony_ci return kUnimplemented_FilterReturn; 384cb93a386Sopenharmony_ci } 385cb93a386Sopenharmony_ci 386cb93a386Sopenharmony_ci SkRect smallR = SkRect::MakeWH(totalSmallWidth, totalSmallHeight); 387cb93a386Sopenharmony_ci 388cb93a386Sopenharmony_ci SkRRect smallRR; 389cb93a386Sopenharmony_ci SkVector radii[4]; 390cb93a386Sopenharmony_ci radii[SkRRect::kUpperLeft_Corner] = UL; 391cb93a386Sopenharmony_ci radii[SkRRect::kUpperRight_Corner] = UR; 392cb93a386Sopenharmony_ci radii[SkRRect::kLowerRight_Corner] = LR; 393cb93a386Sopenharmony_ci radii[SkRRect::kLowerLeft_Corner] = LL; 394cb93a386Sopenharmony_ci smallRR.setRectRadii(smallR, radii); 395cb93a386Sopenharmony_ci 396cb93a386Sopenharmony_ci const SkScalar sigma = this->computeXformedSigma(matrix); 397cb93a386Sopenharmony_ci SkCachedData* cache = find_cached_rrect(&patch->fMask, sigma, fBlurStyle, smallRR); 398cb93a386Sopenharmony_ci if (!cache) { 399cb93a386Sopenharmony_ci bool analyticBlurWorked = false; 400cb93a386Sopenharmony_ci if (c_analyticBlurRRect) { 401cb93a386Sopenharmony_ci analyticBlurWorked = 402cb93a386Sopenharmony_ci this->filterRRectMask(&patch->fMask, smallRR, matrix, &margin, 403cb93a386Sopenharmony_ci SkMask::kComputeBoundsAndRenderImage_CreateMode); 404cb93a386Sopenharmony_ci } 405cb93a386Sopenharmony_ci 406cb93a386Sopenharmony_ci if (!analyticBlurWorked) { 407cb93a386Sopenharmony_ci if (!draw_rrect_into_mask(smallRR, &srcM)) { 408cb93a386Sopenharmony_ci return kFalse_FilterReturn; 409cb93a386Sopenharmony_ci } 410cb93a386Sopenharmony_ci 411cb93a386Sopenharmony_ci SkAutoMaskFreeImage amf(srcM.fImage); 412cb93a386Sopenharmony_ci 413cb93a386Sopenharmony_ci if (!this->filterMask(&patch->fMask, srcM, matrix, &margin)) { 414cb93a386Sopenharmony_ci return kFalse_FilterReturn; 415cb93a386Sopenharmony_ci } 416cb93a386Sopenharmony_ci } 417cb93a386Sopenharmony_ci cache = add_cached_rrect(&patch->fMask, sigma, fBlurStyle, smallRR); 418cb93a386Sopenharmony_ci } 419cb93a386Sopenharmony_ci 420cb93a386Sopenharmony_ci patch->fMask.fBounds.offsetTo(0, 0); 421cb93a386Sopenharmony_ci patch->fOuterRect = dstM.fBounds; 422cb93a386Sopenharmony_ci patch->fCenter.fX = SkScalarCeilToInt(leftUnstretched) + 1; 423cb93a386Sopenharmony_ci patch->fCenter.fY = SkScalarCeilToInt(topUnstretched) + 1; 424cb93a386Sopenharmony_ci SkASSERT(nullptr == patch->fCache); 425cb93a386Sopenharmony_ci patch->fCache = cache; // transfer ownership to patch 426cb93a386Sopenharmony_ci return kTrue_FilterReturn; 427cb93a386Sopenharmony_ci} 428cb93a386Sopenharmony_ci 429cb93a386Sopenharmony_ci// Use the faster analytic blur approach for ninepatch rects 430cb93a386Sopenharmony_cistatic const bool c_analyticBlurNinepatch{true}; 431cb93a386Sopenharmony_ci 432cb93a386Sopenharmony_ciSkMaskFilterBase::FilterReturn 433cb93a386Sopenharmony_ciSkBlurMaskFilterImpl::filterRectsToNine(const SkRect rects[], int count, 434cb93a386Sopenharmony_ci const SkMatrix& matrix, 435cb93a386Sopenharmony_ci const SkIRect& clipBounds, 436cb93a386Sopenharmony_ci NinePatch* patch) const { 437cb93a386Sopenharmony_ci if (count < 1 || count > 2) { 438cb93a386Sopenharmony_ci return kUnimplemented_FilterReturn; 439cb93a386Sopenharmony_ci } 440cb93a386Sopenharmony_ci 441cb93a386Sopenharmony_ci // TODO: report correct metrics for innerstyle, where we do not grow the 442cb93a386Sopenharmony_ci // total bounds, but we do need an inset the size of our blur-radius 443cb93a386Sopenharmony_ci if (kInner_SkBlurStyle == fBlurStyle || kOuter_SkBlurStyle == fBlurStyle) { 444cb93a386Sopenharmony_ci return kUnimplemented_FilterReturn; 445cb93a386Sopenharmony_ci } 446cb93a386Sopenharmony_ci 447cb93a386Sopenharmony_ci // TODO: take clipBounds into account to limit our coordinates up front 448cb93a386Sopenharmony_ci // for now, just skip too-large src rects (to take the old code path). 449cb93a386Sopenharmony_ci if (rect_exceeds(rects[0], SkIntToScalar(32767))) { 450cb93a386Sopenharmony_ci return kUnimplemented_FilterReturn; 451cb93a386Sopenharmony_ci } 452cb93a386Sopenharmony_ci 453cb93a386Sopenharmony_ci SkIPoint margin; 454cb93a386Sopenharmony_ci SkMask srcM, dstM; 455cb93a386Sopenharmony_ci srcM.fBounds = rects[0].roundOut(); 456cb93a386Sopenharmony_ci srcM.fFormat = SkMask::kA8_Format; 457cb93a386Sopenharmony_ci srcM.fRowBytes = 0; 458cb93a386Sopenharmony_ci 459cb93a386Sopenharmony_ci bool filterResult = false; 460cb93a386Sopenharmony_ci if (count == 1 && c_analyticBlurNinepatch) { 461cb93a386Sopenharmony_ci // special case for fast rect blur 462cb93a386Sopenharmony_ci // don't actually do the blur the first time, just compute the correct size 463cb93a386Sopenharmony_ci filterResult = this->filterRectMask(&dstM, rects[0], matrix, &margin, 464cb93a386Sopenharmony_ci SkMask::kJustComputeBounds_CreateMode); 465cb93a386Sopenharmony_ci } else { 466cb93a386Sopenharmony_ci filterResult = this->filterMask(&dstM, srcM, matrix, &margin); 467cb93a386Sopenharmony_ci } 468cb93a386Sopenharmony_ci 469cb93a386Sopenharmony_ci if (!filterResult) { 470cb93a386Sopenharmony_ci return kFalse_FilterReturn; 471cb93a386Sopenharmony_ci } 472cb93a386Sopenharmony_ci 473cb93a386Sopenharmony_ci /* 474cb93a386Sopenharmony_ci * smallR is the smallest version of 'rect' that will still guarantee that 475cb93a386Sopenharmony_ci * we get the same blur results on all edges, plus 1 center row/col that is 476cb93a386Sopenharmony_ci * representative of the extendible/stretchable edges of the ninepatch. 477cb93a386Sopenharmony_ci * Since our actual edge may be fractional we inset 1 more to be sure we 478cb93a386Sopenharmony_ci * don't miss any interior blur. 479cb93a386Sopenharmony_ci * x is an added pixel of blur, and { and } are the (fractional) edge 480cb93a386Sopenharmony_ci * pixels from the original rect. 481cb93a386Sopenharmony_ci * 482cb93a386Sopenharmony_ci * x x { x x .... x x } x x 483cb93a386Sopenharmony_ci * 484cb93a386Sopenharmony_ci * Thus, in this case, we inset by a total of 5 (on each side) beginning 485cb93a386Sopenharmony_ci * with our outer-rect (dstM.fBounds) 486cb93a386Sopenharmony_ci */ 487cb93a386Sopenharmony_ci SkRect smallR[2]; 488cb93a386Sopenharmony_ci SkIPoint center; 489cb93a386Sopenharmony_ci 490cb93a386Sopenharmony_ci // +2 is from +1 for each edge (to account for possible fractional edges 491cb93a386Sopenharmony_ci int smallW = dstM.fBounds.width() - srcM.fBounds.width() + 2; 492cb93a386Sopenharmony_ci int smallH = dstM.fBounds.height() - srcM.fBounds.height() + 2; 493cb93a386Sopenharmony_ci SkIRect innerIR; 494cb93a386Sopenharmony_ci 495cb93a386Sopenharmony_ci if (1 == count) { 496cb93a386Sopenharmony_ci innerIR = srcM.fBounds; 497cb93a386Sopenharmony_ci center.set(smallW, smallH); 498cb93a386Sopenharmony_ci } else { 499cb93a386Sopenharmony_ci SkASSERT(2 == count); 500cb93a386Sopenharmony_ci rects[1].roundIn(&innerIR); 501cb93a386Sopenharmony_ci center.set(smallW + (innerIR.left() - srcM.fBounds.left()), 502cb93a386Sopenharmony_ci smallH + (innerIR.top() - srcM.fBounds.top())); 503cb93a386Sopenharmony_ci } 504cb93a386Sopenharmony_ci 505cb93a386Sopenharmony_ci // +1 so we get a clean, stretchable, center row/col 506cb93a386Sopenharmony_ci smallW += 1; 507cb93a386Sopenharmony_ci smallH += 1; 508cb93a386Sopenharmony_ci 509cb93a386Sopenharmony_ci // we want the inset amounts to be integral, so we don't change any 510cb93a386Sopenharmony_ci // fractional phase on the fRight or fBottom of our smallR. 511cb93a386Sopenharmony_ci const SkScalar dx = SkIntToScalar(innerIR.width() - smallW); 512cb93a386Sopenharmony_ci const SkScalar dy = SkIntToScalar(innerIR.height() - smallH); 513cb93a386Sopenharmony_ci if (dx < 0 || dy < 0) { 514cb93a386Sopenharmony_ci // we're too small, relative to our blur, to break into nine-patch, 515cb93a386Sopenharmony_ci // so we ask to have our normal filterMask() be called. 516cb93a386Sopenharmony_ci return kUnimplemented_FilterReturn; 517cb93a386Sopenharmony_ci } 518cb93a386Sopenharmony_ci 519cb93a386Sopenharmony_ci smallR[0].setLTRB(rects[0].left(), rects[0].top(), 520cb93a386Sopenharmony_ci rects[0].right() - dx, rects[0].bottom() - dy); 521cb93a386Sopenharmony_ci if (smallR[0].width() < 2 || smallR[0].height() < 2) { 522cb93a386Sopenharmony_ci return kUnimplemented_FilterReturn; 523cb93a386Sopenharmony_ci } 524cb93a386Sopenharmony_ci if (2 == count) { 525cb93a386Sopenharmony_ci smallR[1].setLTRB(rects[1].left(), rects[1].top(), 526cb93a386Sopenharmony_ci rects[1].right() - dx, rects[1].bottom() - dy); 527cb93a386Sopenharmony_ci SkASSERT(!smallR[1].isEmpty()); 528cb93a386Sopenharmony_ci } 529cb93a386Sopenharmony_ci 530cb93a386Sopenharmony_ci const SkScalar sigma = this->computeXformedSigma(matrix); 531cb93a386Sopenharmony_ci SkCachedData* cache = find_cached_rects(&patch->fMask, sigma, fBlurStyle, smallR, count); 532cb93a386Sopenharmony_ci if (!cache) { 533cb93a386Sopenharmony_ci if (count > 1 || !c_analyticBlurNinepatch) { 534cb93a386Sopenharmony_ci if (!draw_rects_into_mask(smallR, count, &srcM)) { 535cb93a386Sopenharmony_ci return kFalse_FilterReturn; 536cb93a386Sopenharmony_ci } 537cb93a386Sopenharmony_ci 538cb93a386Sopenharmony_ci SkAutoMaskFreeImage amf(srcM.fImage); 539cb93a386Sopenharmony_ci 540cb93a386Sopenharmony_ci if (!this->filterMask(&patch->fMask, srcM, matrix, &margin)) { 541cb93a386Sopenharmony_ci return kFalse_FilterReturn; 542cb93a386Sopenharmony_ci } 543cb93a386Sopenharmony_ci } else { 544cb93a386Sopenharmony_ci if (!this->filterRectMask(&patch->fMask, smallR[0], matrix, &margin, 545cb93a386Sopenharmony_ci SkMask::kComputeBoundsAndRenderImage_CreateMode)) { 546cb93a386Sopenharmony_ci return kFalse_FilterReturn; 547cb93a386Sopenharmony_ci } 548cb93a386Sopenharmony_ci } 549cb93a386Sopenharmony_ci cache = add_cached_rects(&patch->fMask, sigma, fBlurStyle, smallR, count); 550cb93a386Sopenharmony_ci } 551cb93a386Sopenharmony_ci patch->fMask.fBounds.offsetTo(0, 0); 552cb93a386Sopenharmony_ci patch->fOuterRect = dstM.fBounds; 553cb93a386Sopenharmony_ci patch->fCenter = center; 554cb93a386Sopenharmony_ci SkASSERT(nullptr == patch->fCache); 555cb93a386Sopenharmony_ci patch->fCache = cache; // transfer ownership to patch 556cb93a386Sopenharmony_ci return kTrue_FilterReturn; 557cb93a386Sopenharmony_ci} 558cb93a386Sopenharmony_ci 559cb93a386Sopenharmony_civoid SkBlurMaskFilterImpl::computeFastBounds(const SkRect& src, 560cb93a386Sopenharmony_ci SkRect* dst) const { 561cb93a386Sopenharmony_ci // TODO: if we're doing kInner blur, should we return a different outset? 562cb93a386Sopenharmony_ci // i.e. pad == 0 ? 563cb93a386Sopenharmony_ci 564cb93a386Sopenharmony_ci SkScalar pad = 3.0f * fSigma; 565cb93a386Sopenharmony_ci 566cb93a386Sopenharmony_ci dst->setLTRB(src.fLeft - pad, src.fTop - pad, 567cb93a386Sopenharmony_ci src.fRight + pad, src.fBottom + pad); 568cb93a386Sopenharmony_ci} 569cb93a386Sopenharmony_ci 570cb93a386Sopenharmony_cisk_sp<SkFlattenable> SkBlurMaskFilterImpl::CreateProc(SkReadBuffer& buffer) { 571cb93a386Sopenharmony_ci const SkScalar sigma = buffer.readScalar(); 572cb93a386Sopenharmony_ci SkBlurStyle style = buffer.read32LE(kLastEnum_SkBlurStyle); 573cb93a386Sopenharmony_ci 574cb93a386Sopenharmony_ci uint32_t flags = buffer.read32LE(0x3); // historically we only recorded 2 bits 575cb93a386Sopenharmony_ci bool respectCTM = !(flags & 1); // historically we stored ignoreCTM in low bit 576cb93a386Sopenharmony_ci 577cb93a386Sopenharmony_ci return SkMaskFilter::MakeBlur((SkBlurStyle)style, sigma, respectCTM); 578cb93a386Sopenharmony_ci} 579cb93a386Sopenharmony_ci 580cb93a386Sopenharmony_civoid SkBlurMaskFilterImpl::flatten(SkWriteBuffer& buffer) const { 581cb93a386Sopenharmony_ci buffer.writeScalar(fSigma); 582cb93a386Sopenharmony_ci buffer.writeUInt(fBlurStyle); 583cb93a386Sopenharmony_ci buffer.writeUInt(!fRespectCTM); // historically we recorded ignoreCTM 584cb93a386Sopenharmony_ci} 585cb93a386Sopenharmony_ci 586cb93a386Sopenharmony_ci 587cb93a386Sopenharmony_ci#if SK_SUPPORT_GPU && SK_GPU_V1 588cb93a386Sopenharmony_ci 589cb93a386Sopenharmony_ci/////////////////////////////////////////////////////////////////////////////// 590cb93a386Sopenharmony_ci// Circle Blur 591cb93a386Sopenharmony_ci/////////////////////////////////////////////////////////////////////////////// 592cb93a386Sopenharmony_ci 593cb93a386Sopenharmony_ci// Computes an unnormalized half kernel (right side). Returns the summation of all the half 594cb93a386Sopenharmony_ci// kernel values. 595cb93a386Sopenharmony_cistatic float make_unnormalized_half_kernel(float* halfKernel, int halfKernelSize, float sigma) { 596cb93a386Sopenharmony_ci const float invSigma = 1.f / sigma; 597cb93a386Sopenharmony_ci const float b = -0.5f * invSigma * invSigma; 598cb93a386Sopenharmony_ci float tot = 0.0f; 599cb93a386Sopenharmony_ci // Compute half kernel values at half pixel steps out from the center. 600cb93a386Sopenharmony_ci float t = 0.5f; 601cb93a386Sopenharmony_ci for (int i = 0; i < halfKernelSize; ++i) { 602cb93a386Sopenharmony_ci float value = expf(t * t * b); 603cb93a386Sopenharmony_ci tot += value; 604cb93a386Sopenharmony_ci halfKernel[i] = value; 605cb93a386Sopenharmony_ci t += 1.f; 606cb93a386Sopenharmony_ci } 607cb93a386Sopenharmony_ci return tot; 608cb93a386Sopenharmony_ci} 609cb93a386Sopenharmony_ci 610cb93a386Sopenharmony_ci// Create a Gaussian half-kernel (right side) and a summed area table given a sigma and number 611cb93a386Sopenharmony_ci// of discrete steps. The half kernel is normalized to sum to 0.5. 612cb93a386Sopenharmony_cistatic void make_half_kernel_and_summed_table(float* halfKernel, 613cb93a386Sopenharmony_ci float* summedHalfKernel, 614cb93a386Sopenharmony_ci int halfKernelSize, 615cb93a386Sopenharmony_ci float sigma) { 616cb93a386Sopenharmony_ci // The half kernel should sum to 0.5 not 1.0. 617cb93a386Sopenharmony_ci const float tot = 2.f * make_unnormalized_half_kernel(halfKernel, halfKernelSize, sigma); 618cb93a386Sopenharmony_ci float sum = 0.f; 619cb93a386Sopenharmony_ci for (int i = 0; i < halfKernelSize; ++i) { 620cb93a386Sopenharmony_ci halfKernel[i] /= tot; 621cb93a386Sopenharmony_ci sum += halfKernel[i]; 622cb93a386Sopenharmony_ci summedHalfKernel[i] = sum; 623cb93a386Sopenharmony_ci } 624cb93a386Sopenharmony_ci} 625cb93a386Sopenharmony_ci 626cb93a386Sopenharmony_ci// Applies the 1D half kernel vertically at points along the x axis to a circle centered at the 627cb93a386Sopenharmony_ci// origin with radius circleR. 628cb93a386Sopenharmony_civoid apply_kernel_in_y(float* results, 629cb93a386Sopenharmony_ci int numSteps, 630cb93a386Sopenharmony_ci float firstX, 631cb93a386Sopenharmony_ci float circleR, 632cb93a386Sopenharmony_ci int halfKernelSize, 633cb93a386Sopenharmony_ci const float* summedHalfKernelTable) { 634cb93a386Sopenharmony_ci float x = firstX; 635cb93a386Sopenharmony_ci for (int i = 0; i < numSteps; ++i, x += 1.f) { 636cb93a386Sopenharmony_ci if (x < -circleR || x > circleR) { 637cb93a386Sopenharmony_ci results[i] = 0; 638cb93a386Sopenharmony_ci continue; 639cb93a386Sopenharmony_ci } 640cb93a386Sopenharmony_ci float y = sqrtf(circleR * circleR - x * x); 641cb93a386Sopenharmony_ci // In the column at x we exit the circle at +y and -y 642cb93a386Sopenharmony_ci // The summed table entry j is actually reflects an offset of j + 0.5. 643cb93a386Sopenharmony_ci y -= 0.5f; 644cb93a386Sopenharmony_ci int yInt = SkScalarFloorToInt(y); 645cb93a386Sopenharmony_ci SkASSERT(yInt >= -1); 646cb93a386Sopenharmony_ci if (y < 0) { 647cb93a386Sopenharmony_ci results[i] = (y + 0.5f) * summedHalfKernelTable[0]; 648cb93a386Sopenharmony_ci } else if (yInt >= halfKernelSize - 1) { 649cb93a386Sopenharmony_ci results[i] = 0.5f; 650cb93a386Sopenharmony_ci } else { 651cb93a386Sopenharmony_ci float yFrac = y - yInt; 652cb93a386Sopenharmony_ci results[i] = (1.f - yFrac) * summedHalfKernelTable[yInt] + 653cb93a386Sopenharmony_ci yFrac * summedHalfKernelTable[yInt + 1]; 654cb93a386Sopenharmony_ci } 655cb93a386Sopenharmony_ci } 656cb93a386Sopenharmony_ci} 657cb93a386Sopenharmony_ci 658cb93a386Sopenharmony_ci// Apply a Gaussian at point (evalX, 0) to a circle centered at the origin with radius circleR. 659cb93a386Sopenharmony_ci// This relies on having a half kernel computed for the Gaussian and a table of applications of 660cb93a386Sopenharmony_ci// the half kernel in y to columns at (evalX - halfKernel, evalX - halfKernel + 1, ..., evalX + 661cb93a386Sopenharmony_ci// halfKernel) passed in as yKernelEvaluations. 662cb93a386Sopenharmony_cistatic uint8_t eval_at(float evalX, 663cb93a386Sopenharmony_ci float circleR, 664cb93a386Sopenharmony_ci const float* halfKernel, 665cb93a386Sopenharmony_ci int halfKernelSize, 666cb93a386Sopenharmony_ci const float* yKernelEvaluations) { 667cb93a386Sopenharmony_ci float acc = 0; 668cb93a386Sopenharmony_ci 669cb93a386Sopenharmony_ci float x = evalX - halfKernelSize; 670cb93a386Sopenharmony_ci for (int i = 0; i < halfKernelSize; ++i, x += 1.f) { 671cb93a386Sopenharmony_ci if (x < -circleR || x > circleR) { 672cb93a386Sopenharmony_ci continue; 673cb93a386Sopenharmony_ci } 674cb93a386Sopenharmony_ci float verticalEval = yKernelEvaluations[i]; 675cb93a386Sopenharmony_ci acc += verticalEval * halfKernel[halfKernelSize - i - 1]; 676cb93a386Sopenharmony_ci } 677cb93a386Sopenharmony_ci for (int i = 0; i < halfKernelSize; ++i, x += 1.f) { 678cb93a386Sopenharmony_ci if (x < -circleR || x > circleR) { 679cb93a386Sopenharmony_ci continue; 680cb93a386Sopenharmony_ci } 681cb93a386Sopenharmony_ci float verticalEval = yKernelEvaluations[i + halfKernelSize]; 682cb93a386Sopenharmony_ci acc += verticalEval * halfKernel[i]; 683cb93a386Sopenharmony_ci } 684cb93a386Sopenharmony_ci // Since we applied a half kernel in y we multiply acc by 2 (the circle is symmetric about 685cb93a386Sopenharmony_ci // the x axis). 686cb93a386Sopenharmony_ci return SkUnitScalarClampToByte(2.f * acc); 687cb93a386Sopenharmony_ci} 688cb93a386Sopenharmony_ci 689cb93a386Sopenharmony_ci// This function creates a profile of a blurred circle. It does this by computing a kernel for 690cb93a386Sopenharmony_ci// half the Gaussian and a matching summed area table. The summed area table is used to compute 691cb93a386Sopenharmony_ci// an array of vertical applications of the half kernel to the circle along the x axis. The 692cb93a386Sopenharmony_ci// table of y evaluations has 2 * k + n entries where k is the size of the half kernel and n is 693cb93a386Sopenharmony_ci// the size of the profile being computed. Then for each of the n profile entries we walk out k 694cb93a386Sopenharmony_ci// steps in each horizontal direction multiplying the corresponding y evaluation by the half 695cb93a386Sopenharmony_ci// kernel entry and sum these values to compute the profile entry. 696cb93a386Sopenharmony_cistatic void create_circle_profile(uint8_t* weights, 697cb93a386Sopenharmony_ci float sigma, 698cb93a386Sopenharmony_ci float circleR, 699cb93a386Sopenharmony_ci int profileTextureWidth) { 700cb93a386Sopenharmony_ci const int numSteps = profileTextureWidth; 701cb93a386Sopenharmony_ci 702cb93a386Sopenharmony_ci // The full kernel is 6 sigmas wide. 703cb93a386Sopenharmony_ci int halfKernelSize = SkScalarCeilToInt(6.0f * sigma); 704cb93a386Sopenharmony_ci // round up to next multiple of 2 and then divide by 2 705cb93a386Sopenharmony_ci halfKernelSize = ((halfKernelSize + 1) & ~1) >> 1; 706cb93a386Sopenharmony_ci 707cb93a386Sopenharmony_ci // Number of x steps at which to apply kernel in y to cover all the profile samples in x. 708cb93a386Sopenharmony_ci int numYSteps = numSteps + 2 * halfKernelSize; 709cb93a386Sopenharmony_ci 710cb93a386Sopenharmony_ci SkAutoTArray<float> bulkAlloc(halfKernelSize + halfKernelSize + numYSteps); 711cb93a386Sopenharmony_ci float* halfKernel = bulkAlloc.get(); 712cb93a386Sopenharmony_ci float* summedKernel = bulkAlloc.get() + halfKernelSize; 713cb93a386Sopenharmony_ci float* yEvals = bulkAlloc.get() + 2 * halfKernelSize; 714cb93a386Sopenharmony_ci make_half_kernel_and_summed_table(halfKernel, summedKernel, halfKernelSize, sigma); 715cb93a386Sopenharmony_ci 716cb93a386Sopenharmony_ci float firstX = -halfKernelSize + 0.5f; 717cb93a386Sopenharmony_ci apply_kernel_in_y(yEvals, numYSteps, firstX, circleR, halfKernelSize, summedKernel); 718cb93a386Sopenharmony_ci 719cb93a386Sopenharmony_ci for (int i = 0; i < numSteps - 1; ++i) { 720cb93a386Sopenharmony_ci float evalX = i + 0.5f; 721cb93a386Sopenharmony_ci weights[i] = eval_at(evalX, circleR, halfKernel, halfKernelSize, yEvals + i); 722cb93a386Sopenharmony_ci } 723cb93a386Sopenharmony_ci // Ensure the tail of the Gaussian goes to zero. 724cb93a386Sopenharmony_ci weights[numSteps - 1] = 0; 725cb93a386Sopenharmony_ci} 726cb93a386Sopenharmony_ci 727cb93a386Sopenharmony_cistatic void create_half_plane_profile(uint8_t* profile, int profileWidth) { 728cb93a386Sopenharmony_ci SkASSERT(!(profileWidth & 0x1)); 729cb93a386Sopenharmony_ci // The full kernel is 6 sigmas wide. 730cb93a386Sopenharmony_ci float sigma = profileWidth / 6.f; 731cb93a386Sopenharmony_ci int halfKernelSize = profileWidth / 2; 732cb93a386Sopenharmony_ci 733cb93a386Sopenharmony_ci SkAutoTArray<float> halfKernel(halfKernelSize); 734cb93a386Sopenharmony_ci 735cb93a386Sopenharmony_ci // The half kernel should sum to 0.5. 736cb93a386Sopenharmony_ci const float tot = 2.f * make_unnormalized_half_kernel(halfKernel.get(), halfKernelSize, sigma); 737cb93a386Sopenharmony_ci float sum = 0.f; 738cb93a386Sopenharmony_ci // Populate the profile from the right edge to the middle. 739cb93a386Sopenharmony_ci for (int i = 0; i < halfKernelSize; ++i) { 740cb93a386Sopenharmony_ci halfKernel[halfKernelSize - i - 1] /= tot; 741cb93a386Sopenharmony_ci sum += halfKernel[halfKernelSize - i - 1]; 742cb93a386Sopenharmony_ci profile[profileWidth - i - 1] = SkUnitScalarClampToByte(sum); 743cb93a386Sopenharmony_ci } 744cb93a386Sopenharmony_ci // Populate the profile from the middle to the left edge (by flipping the half kernel and 745cb93a386Sopenharmony_ci // continuing the summation). 746cb93a386Sopenharmony_ci for (int i = 0; i < halfKernelSize; ++i) { 747cb93a386Sopenharmony_ci sum += halfKernel[i]; 748cb93a386Sopenharmony_ci profile[halfKernelSize - i - 1] = SkUnitScalarClampToByte(sum); 749cb93a386Sopenharmony_ci } 750cb93a386Sopenharmony_ci // Ensure tail goes to 0. 751cb93a386Sopenharmony_ci profile[profileWidth - 1] = 0; 752cb93a386Sopenharmony_ci} 753cb93a386Sopenharmony_ci 754cb93a386Sopenharmony_cistatic std::unique_ptr<GrFragmentProcessor> create_profile_effect(GrRecordingContext* rContext, 755cb93a386Sopenharmony_ci const SkRect& circle, 756cb93a386Sopenharmony_ci float sigma, 757cb93a386Sopenharmony_ci float* solidRadius, 758cb93a386Sopenharmony_ci float* textureRadius) { 759cb93a386Sopenharmony_ci float circleR = circle.width() / 2.0f; 760cb93a386Sopenharmony_ci if (!sk_float_isfinite(circleR) || circleR < SK_ScalarNearlyZero) { 761cb93a386Sopenharmony_ci return nullptr; 762cb93a386Sopenharmony_ci } 763cb93a386Sopenharmony_ci 764cb93a386Sopenharmony_ci auto threadSafeCache = rContext->priv().threadSafeCache(); 765cb93a386Sopenharmony_ci 766cb93a386Sopenharmony_ci // Profile textures are cached by the ratio of sigma to circle radius and by the size of the 767cb93a386Sopenharmony_ci // profile texture (binned by powers of 2). 768cb93a386Sopenharmony_ci SkScalar sigmaToCircleRRatio = sigma / circleR; 769cb93a386Sopenharmony_ci // When sigma is really small this becomes a equivalent to convolving a Gaussian with a 770cb93a386Sopenharmony_ci // half-plane. Similarly, in the extreme high ratio cases circle becomes a point WRT to the 771cb93a386Sopenharmony_ci // Guassian and the profile texture is a just a Gaussian evaluation. However, we haven't yet 772cb93a386Sopenharmony_ci // implemented this latter optimization. 773cb93a386Sopenharmony_ci sigmaToCircleRRatio = std::min(sigmaToCircleRRatio, 8.f); 774cb93a386Sopenharmony_ci SkFixed sigmaToCircleRRatioFixed; 775cb93a386Sopenharmony_ci static const SkScalar kHalfPlaneThreshold = 0.1f; 776cb93a386Sopenharmony_ci bool useHalfPlaneApprox = false; 777cb93a386Sopenharmony_ci if (sigmaToCircleRRatio <= kHalfPlaneThreshold) { 778cb93a386Sopenharmony_ci useHalfPlaneApprox = true; 779cb93a386Sopenharmony_ci sigmaToCircleRRatioFixed = 0; 780cb93a386Sopenharmony_ci *solidRadius = circleR - 3 * sigma; 781cb93a386Sopenharmony_ci *textureRadius = 6 * sigma; 782cb93a386Sopenharmony_ci } else { 783cb93a386Sopenharmony_ci // Convert to fixed point for the key. 784cb93a386Sopenharmony_ci sigmaToCircleRRatioFixed = SkScalarToFixed(sigmaToCircleRRatio); 785cb93a386Sopenharmony_ci // We shave off some bits to reduce the number of unique entries. We could probably 786cb93a386Sopenharmony_ci // shave off more than we do. 787cb93a386Sopenharmony_ci sigmaToCircleRRatioFixed &= ~0xff; 788cb93a386Sopenharmony_ci sigmaToCircleRRatio = SkFixedToScalar(sigmaToCircleRRatioFixed); 789cb93a386Sopenharmony_ci sigma = circleR * sigmaToCircleRRatio; 790cb93a386Sopenharmony_ci *solidRadius = 0; 791cb93a386Sopenharmony_ci *textureRadius = circleR + 3 * sigma; 792cb93a386Sopenharmony_ci } 793cb93a386Sopenharmony_ci 794cb93a386Sopenharmony_ci static constexpr int kProfileTextureWidth = 512; 795cb93a386Sopenharmony_ci // This would be kProfileTextureWidth/textureRadius if it weren't for the fact that we do 796cb93a386Sopenharmony_ci // the calculation of the profile coord in a coord space that has already been scaled by 797cb93a386Sopenharmony_ci // 1 / textureRadius. This is done to avoid overflow in length(). 798cb93a386Sopenharmony_ci SkMatrix texM = SkMatrix::Scale(kProfileTextureWidth, 1.f); 799cb93a386Sopenharmony_ci 800cb93a386Sopenharmony_ci static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain(); 801cb93a386Sopenharmony_ci GrUniqueKey key; 802cb93a386Sopenharmony_ci GrUniqueKey::Builder builder(&key, kDomain, 1, "1-D Circular Blur"); 803cb93a386Sopenharmony_ci builder[0] = sigmaToCircleRRatioFixed; 804cb93a386Sopenharmony_ci builder.finish(); 805cb93a386Sopenharmony_ci 806cb93a386Sopenharmony_ci GrSurfaceProxyView profileView = threadSafeCache->find(key); 807cb93a386Sopenharmony_ci if (profileView) { 808cb93a386Sopenharmony_ci SkASSERT(profileView.asTextureProxy()); 809cb93a386Sopenharmony_ci SkASSERT(profileView.origin() == kTopLeft_GrSurfaceOrigin); 810cb93a386Sopenharmony_ci return GrTextureEffect::Make(std::move(profileView), kPremul_SkAlphaType, texM); 811cb93a386Sopenharmony_ci } 812cb93a386Sopenharmony_ci 813cb93a386Sopenharmony_ci SkBitmap bm; 814cb93a386Sopenharmony_ci if (!bm.tryAllocPixels(SkImageInfo::MakeA8(kProfileTextureWidth, 1))) { 815cb93a386Sopenharmony_ci return nullptr; 816cb93a386Sopenharmony_ci } 817cb93a386Sopenharmony_ci 818cb93a386Sopenharmony_ci if (useHalfPlaneApprox) { 819cb93a386Sopenharmony_ci create_half_plane_profile(bm.getAddr8(0, 0), kProfileTextureWidth); 820cb93a386Sopenharmony_ci } else { 821cb93a386Sopenharmony_ci // Rescale params to the size of the texture we're creating. 822cb93a386Sopenharmony_ci SkScalar scale = kProfileTextureWidth / *textureRadius; 823cb93a386Sopenharmony_ci create_circle_profile( 824cb93a386Sopenharmony_ci bm.getAddr8(0, 0), sigma * scale, circleR * scale, kProfileTextureWidth); 825cb93a386Sopenharmony_ci } 826cb93a386Sopenharmony_ci bm.setImmutable(); 827cb93a386Sopenharmony_ci 828cb93a386Sopenharmony_ci profileView = std::get<0>(GrMakeUncachedBitmapProxyView(rContext, bm)); 829cb93a386Sopenharmony_ci if (!profileView) { 830cb93a386Sopenharmony_ci return nullptr; 831cb93a386Sopenharmony_ci } 832cb93a386Sopenharmony_ci 833cb93a386Sopenharmony_ci profileView = threadSafeCache->add(key, profileView); 834cb93a386Sopenharmony_ci return GrTextureEffect::Make(std::move(profileView), kPremul_SkAlphaType, texM); 835cb93a386Sopenharmony_ci} 836cb93a386Sopenharmony_ci 837cb93a386Sopenharmony_cistatic std::unique_ptr<GrFragmentProcessor> make_circle_blur(GrRecordingContext* context, 838cb93a386Sopenharmony_ci const SkRect& circle, 839cb93a386Sopenharmony_ci float sigma) { 840cb93a386Sopenharmony_ci if (SkGpuBlurUtils::IsEffectivelyZeroSigma(sigma)) { 841cb93a386Sopenharmony_ci return nullptr; 842cb93a386Sopenharmony_ci } 843cb93a386Sopenharmony_ci 844cb93a386Sopenharmony_ci float solidRadius; 845cb93a386Sopenharmony_ci float textureRadius; 846cb93a386Sopenharmony_ci std::unique_ptr<GrFragmentProcessor> profile = 847cb93a386Sopenharmony_ci create_profile_effect(context, circle, sigma, &solidRadius, &textureRadius); 848cb93a386Sopenharmony_ci if (!profile) { 849cb93a386Sopenharmony_ci return nullptr; 850cb93a386Sopenharmony_ci } 851cb93a386Sopenharmony_ci 852cb93a386Sopenharmony_ci static auto effect = SkMakeRuntimeEffect(SkRuntimeEffect::MakeForShader, R"( 853cb93a386Sopenharmony_ci uniform shader blurProfile; 854cb93a386Sopenharmony_ci uniform float4 circleData; 855cb93a386Sopenharmony_ci 856cb93a386Sopenharmony_ci half4 main(float2 xy, half4 inColor) { 857cb93a386Sopenharmony_ci // We just want to compute "(length(vec) - circleData.z + 0.5) * circleData.w" but need 858cb93a386Sopenharmony_ci // to rearrange to avoid passing large values to length() that would overflow. 859cb93a386Sopenharmony_ci half4 halfCircleData = circleData; 860cb93a386Sopenharmony_ci half2 vec = (sk_FragCoord.xy - halfCircleData.xy) * circleData.w; 861cb93a386Sopenharmony_ci half dist = length(vec) + (0.5 - halfCircleData.z) * halfCircleData.w; 862cb93a386Sopenharmony_ci return inColor * blurProfile.eval(half2(dist, 0.5)).a; 863cb93a386Sopenharmony_ci } 864cb93a386Sopenharmony_ci )"); 865cb93a386Sopenharmony_ci 866cb93a386Sopenharmony_ci SkV4 circleData = {circle.centerX(), circle.centerY(), solidRadius, 1.f / textureRadius}; 867cb93a386Sopenharmony_ci return GrSkSLFP::Make(effect, "CircleBlur", /*inputFP=*/nullptr, 868cb93a386Sopenharmony_ci GrSkSLFP::OptFlags::kCompatibleWithCoverageAsAlpha, 869cb93a386Sopenharmony_ci "blurProfile", GrSkSLFP::IgnoreOptFlags(std::move(profile)), 870cb93a386Sopenharmony_ci "circleData", circleData); 871cb93a386Sopenharmony_ci} 872cb93a386Sopenharmony_ci 873cb93a386Sopenharmony_ci/////////////////////////////////////////////////////////////////////////////// 874cb93a386Sopenharmony_ci// Rect Blur 875cb93a386Sopenharmony_ci/////////////////////////////////////////////////////////////////////////////// 876cb93a386Sopenharmony_ci 877cb93a386Sopenharmony_cistatic std::unique_ptr<GrFragmentProcessor> make_rect_integral_fp(GrRecordingContext* rContext, 878cb93a386Sopenharmony_ci float sixSigma) { 879cb93a386Sopenharmony_ci SkASSERT(!SkGpuBlurUtils::IsEffectivelyZeroSigma(sixSigma / 6.f)); 880cb93a386Sopenharmony_ci auto threadSafeCache = rContext->priv().threadSafeCache(); 881cb93a386Sopenharmony_ci 882cb93a386Sopenharmony_ci int width = SkGpuBlurUtils::CreateIntegralTable(sixSigma, nullptr); 883cb93a386Sopenharmony_ci 884cb93a386Sopenharmony_ci static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain(); 885cb93a386Sopenharmony_ci GrUniqueKey key; 886cb93a386Sopenharmony_ci GrUniqueKey::Builder builder(&key, kDomain, 1, "Rect Blur Mask"); 887cb93a386Sopenharmony_ci builder[0] = width; 888cb93a386Sopenharmony_ci builder.finish(); 889cb93a386Sopenharmony_ci 890cb93a386Sopenharmony_ci SkMatrix m = SkMatrix::Scale(width / sixSigma, 1.f); 891cb93a386Sopenharmony_ci 892cb93a386Sopenharmony_ci GrSurfaceProxyView view = threadSafeCache->find(key); 893cb93a386Sopenharmony_ci 894cb93a386Sopenharmony_ci if (view) { 895cb93a386Sopenharmony_ci SkASSERT(view.origin() == kTopLeft_GrSurfaceOrigin); 896cb93a386Sopenharmony_ci return GrTextureEffect::Make( 897cb93a386Sopenharmony_ci std::move(view), kPremul_SkAlphaType, m, GrSamplerState::Filter::kLinear); 898cb93a386Sopenharmony_ci } 899cb93a386Sopenharmony_ci 900cb93a386Sopenharmony_ci SkBitmap bitmap; 901cb93a386Sopenharmony_ci if (!SkGpuBlurUtils::CreateIntegralTable(sixSigma, &bitmap)) { 902cb93a386Sopenharmony_ci return {}; 903cb93a386Sopenharmony_ci } 904cb93a386Sopenharmony_ci 905cb93a386Sopenharmony_ci view = std::get<0>(GrMakeUncachedBitmapProxyView(rContext, bitmap)); 906cb93a386Sopenharmony_ci if (!view) { 907cb93a386Sopenharmony_ci return {}; 908cb93a386Sopenharmony_ci } 909cb93a386Sopenharmony_ci 910cb93a386Sopenharmony_ci view = threadSafeCache->add(key, view); 911cb93a386Sopenharmony_ci 912cb93a386Sopenharmony_ci SkASSERT(view.origin() == kTopLeft_GrSurfaceOrigin); 913cb93a386Sopenharmony_ci return GrTextureEffect::Make( 914cb93a386Sopenharmony_ci std::move(view), kPremul_SkAlphaType, m, GrSamplerState::Filter::kLinear); 915cb93a386Sopenharmony_ci} 916cb93a386Sopenharmony_ci 917cb93a386Sopenharmony_cistatic std::unique_ptr<GrFragmentProcessor> make_rect_blur(GrRecordingContext* context, 918cb93a386Sopenharmony_ci const GrShaderCaps& caps, 919cb93a386Sopenharmony_ci const SkRect& srcRect, 920cb93a386Sopenharmony_ci const SkMatrix& viewMatrix, 921cb93a386Sopenharmony_ci float transformedSigma) { 922cb93a386Sopenharmony_ci SkASSERT(viewMatrix.preservesRightAngles()); 923cb93a386Sopenharmony_ci SkASSERT(srcRect.isSorted()); 924cb93a386Sopenharmony_ci 925cb93a386Sopenharmony_ci if (SkGpuBlurUtils::IsEffectivelyZeroSigma(transformedSigma)) { 926cb93a386Sopenharmony_ci // No need to blur the rect 927cb93a386Sopenharmony_ci return nullptr; 928cb93a386Sopenharmony_ci } 929cb93a386Sopenharmony_ci 930cb93a386Sopenharmony_ci SkMatrix invM; 931cb93a386Sopenharmony_ci SkRect rect; 932cb93a386Sopenharmony_ci if (viewMatrix.rectStaysRect()) { 933cb93a386Sopenharmony_ci invM = SkMatrix::I(); 934cb93a386Sopenharmony_ci // We can do everything in device space when the src rect projects to a rect in device space 935cb93a386Sopenharmony_ci SkAssertResult(viewMatrix.mapRect(&rect, srcRect)); 936cb93a386Sopenharmony_ci } else { 937cb93a386Sopenharmony_ci // The view matrix may scale, perhaps anisotropically. But we want to apply our device space 938cb93a386Sopenharmony_ci // "transformedSigma" to the delta of frag coord from the rect edges. Factor out the scaling 939cb93a386Sopenharmony_ci // to define a space that is purely rotation/translation from device space (and scale from 940cb93a386Sopenharmony_ci // src space) We'll meet in the middle: pre-scale the src rect to be in this space and then 941cb93a386Sopenharmony_ci // apply the inverse of the rotation/translation portion to the frag coord. 942cb93a386Sopenharmony_ci SkMatrix m; 943cb93a386Sopenharmony_ci SkSize scale; 944cb93a386Sopenharmony_ci if (!viewMatrix.decomposeScale(&scale, &m)) { 945cb93a386Sopenharmony_ci return nullptr; 946cb93a386Sopenharmony_ci } 947cb93a386Sopenharmony_ci if (!m.invert(&invM)) { 948cb93a386Sopenharmony_ci return nullptr; 949cb93a386Sopenharmony_ci } 950cb93a386Sopenharmony_ci rect = {srcRect.left() * scale.width(), 951cb93a386Sopenharmony_ci srcRect.top() * scale.height(), 952cb93a386Sopenharmony_ci srcRect.right() * scale.width(), 953cb93a386Sopenharmony_ci srcRect.bottom() * scale.height()}; 954cb93a386Sopenharmony_ci } 955cb93a386Sopenharmony_ci 956cb93a386Sopenharmony_ci if (!caps.floatIs32Bits()) { 957cb93a386Sopenharmony_ci // We promote the math that gets us into the Gaussian space to full float when the rect 958cb93a386Sopenharmony_ci // coords are large. If we don't have full float then fail. We could probably clip the rect 959cb93a386Sopenharmony_ci // to an outset device bounds instead. 960cb93a386Sopenharmony_ci if (SkScalarAbs(rect.fLeft) > 16000.f || SkScalarAbs(rect.fTop) > 16000.f || 961cb93a386Sopenharmony_ci SkScalarAbs(rect.fRight) > 16000.f || SkScalarAbs(rect.fBottom) > 16000.f) { 962cb93a386Sopenharmony_ci return nullptr; 963cb93a386Sopenharmony_ci } 964cb93a386Sopenharmony_ci } 965cb93a386Sopenharmony_ci 966cb93a386Sopenharmony_ci const float sixSigma = 6 * transformedSigma; 967cb93a386Sopenharmony_ci std::unique_ptr<GrFragmentProcessor> integral = make_rect_integral_fp(context, sixSigma); 968cb93a386Sopenharmony_ci if (!integral) { 969cb93a386Sopenharmony_ci return nullptr; 970cb93a386Sopenharmony_ci } 971cb93a386Sopenharmony_ci 972cb93a386Sopenharmony_ci // In the fast variant we think of the midpoint of the integral texture as aligning with the 973cb93a386Sopenharmony_ci // closest rect edge both in x and y. To simplify texture coord calculation we inset the rect so 974cb93a386Sopenharmony_ci // that the edge of the inset rect corresponds to t = 0 in the texture. It actually simplifies 975cb93a386Sopenharmony_ci // things a bit in the !isFast case, too. 976cb93a386Sopenharmony_ci float threeSigma = sixSigma / 2; 977cb93a386Sopenharmony_ci SkRect insetRect = {rect.left() + threeSigma, 978cb93a386Sopenharmony_ci rect.top() + threeSigma, 979cb93a386Sopenharmony_ci rect.right() - threeSigma, 980cb93a386Sopenharmony_ci rect.bottom() - threeSigma}; 981cb93a386Sopenharmony_ci 982cb93a386Sopenharmony_ci // In our fast variant we find the nearest horizontal and vertical edges and for each do a 983cb93a386Sopenharmony_ci // lookup in the integral texture for each and multiply them. When the rect is less than 6 sigma 984cb93a386Sopenharmony_ci // wide then things aren't so simple and we have to consider both the left and right edge of the 985cb93a386Sopenharmony_ci // rectangle (and similar in y). 986cb93a386Sopenharmony_ci bool isFast = insetRect.isSorted(); 987cb93a386Sopenharmony_ci 988cb93a386Sopenharmony_ci static auto effect = SkMakeRuntimeEffect(SkRuntimeEffect::MakeForShader, R"( 989cb93a386Sopenharmony_ci // Effect that is a LUT for integral of normal distribution. The value at x:[0,6*sigma] is 990cb93a386Sopenharmony_ci // the integral from -inf to (3*sigma - x). I.e. x is mapped from [0, 6*sigma] to 991cb93a386Sopenharmony_ci // [3*sigma to -3*sigma]. The flip saves a reversal in the shader. 992cb93a386Sopenharmony_ci uniform shader integral; 993cb93a386Sopenharmony_ci 994cb93a386Sopenharmony_ci uniform float4 rect; 995cb93a386Sopenharmony_ci uniform int isFast; // specialized 996cb93a386Sopenharmony_ci 997cb93a386Sopenharmony_ci half4 main(float2 pos, half4 inColor) { 998cb93a386Sopenharmony_ci half xCoverage, yCoverage; 999cb93a386Sopenharmony_ci if (bool(isFast)) { 1000cb93a386Sopenharmony_ci // Get the smaller of the signed distance from the frag coord to the left and right 1001cb93a386Sopenharmony_ci // edges and similar for y. 1002cb93a386Sopenharmony_ci // The integral texture goes "backwards" (from 3*sigma to -3*sigma), So, the below 1003cb93a386Sopenharmony_ci // computations align the left edge of the integral texture with the inset rect's 1004cb93a386Sopenharmony_ci // edge extending outward 6 * sigma from the inset rect. 1005cb93a386Sopenharmony_ci half2 xy = max(half2(rect.LT - pos), half2(pos - rect.RB)); 1006cb93a386Sopenharmony_ci xCoverage = integral.eval(half2(xy.x, 0.5)).a; 1007cb93a386Sopenharmony_ci yCoverage = integral.eval(half2(xy.y, 0.5)).a; 1008cb93a386Sopenharmony_ci } else { 1009cb93a386Sopenharmony_ci // We just consider just the x direction here. In practice we compute x and y 1010cb93a386Sopenharmony_ci // separately and multiply them together. 1011cb93a386Sopenharmony_ci // We define our coord system so that the point at which we're evaluating a kernel 1012cb93a386Sopenharmony_ci // defined by the normal distribution (K) at 0. In this coord system let L be left 1013cb93a386Sopenharmony_ci // edge and R be the right edge of the rectangle. 1014cb93a386Sopenharmony_ci // We can calculate C by integrating K with the half infinite ranges outside the 1015cb93a386Sopenharmony_ci // L to R range and subtracting from 1: 1016cb93a386Sopenharmony_ci // C = 1 - <integral of K from from -inf to L> - <integral of K from R to inf> 1017cb93a386Sopenharmony_ci // K is symmetric about x=0 so: 1018cb93a386Sopenharmony_ci // C = 1 - <integral of K from from -inf to L> - <integral of K from -inf to -R> 1019cb93a386Sopenharmony_ci 1020cb93a386Sopenharmony_ci // The integral texture goes "backwards" (from 3*sigma to -3*sigma) which is 1021cb93a386Sopenharmony_ci // factored in to the below calculations. 1022cb93a386Sopenharmony_ci // Also, our rect uniform was pre-inset by 3 sigma from the actual rect being 1023cb93a386Sopenharmony_ci // blurred, also factored in. 1024cb93a386Sopenharmony_ci half4 rect = half4(half2(rect.LT - pos), half2(pos - rect.RB)); 1025cb93a386Sopenharmony_ci xCoverage = 1 - integral.eval(half2(rect.L, 0.5)).a 1026cb93a386Sopenharmony_ci - integral.eval(half2(rect.R, 0.5)).a; 1027cb93a386Sopenharmony_ci yCoverage = 1 - integral.eval(half2(rect.T, 0.5)).a 1028cb93a386Sopenharmony_ci - integral.eval(half2(rect.B, 0.5)).a; 1029cb93a386Sopenharmony_ci } 1030cb93a386Sopenharmony_ci return inColor * xCoverage * yCoverage; 1031cb93a386Sopenharmony_ci } 1032cb93a386Sopenharmony_ci )"); 1033cb93a386Sopenharmony_ci 1034cb93a386Sopenharmony_ci std::unique_ptr<GrFragmentProcessor> fp = 1035cb93a386Sopenharmony_ci GrSkSLFP::Make(effect, "RectBlur", /*inputFP=*/nullptr, 1036cb93a386Sopenharmony_ci GrSkSLFP::OptFlags::kCompatibleWithCoverageAsAlpha, 1037cb93a386Sopenharmony_ci "integral", GrSkSLFP::IgnoreOptFlags(std::move(integral)), 1038cb93a386Sopenharmony_ci "rect", insetRect, 1039cb93a386Sopenharmony_ci "isFast", GrSkSLFP::Specialize<int>(isFast)); 1040cb93a386Sopenharmony_ci if (!invM.isIdentity()) { 1041cb93a386Sopenharmony_ci fp = GrMatrixEffect::Make(invM, std::move(fp)); 1042cb93a386Sopenharmony_ci } 1043cb93a386Sopenharmony_ci return GrFragmentProcessor::DeviceSpace(std::move(fp)); 1044cb93a386Sopenharmony_ci} 1045cb93a386Sopenharmony_ci 1046cb93a386Sopenharmony_ci/////////////////////////////////////////////////////////////////////////////// 1047cb93a386Sopenharmony_ci// RRect Blur 1048cb93a386Sopenharmony_ci/////////////////////////////////////////////////////////////////////////////// 1049cb93a386Sopenharmony_ci 1050cb93a386Sopenharmony_cistatic constexpr auto kBlurredRRectMaskOrigin = kTopLeft_GrSurfaceOrigin; 1051cb93a386Sopenharmony_ci 1052cb93a386Sopenharmony_cistatic void make_blurred_rrect_key(GrUniqueKey* key, 1053cb93a386Sopenharmony_ci const SkRRect& rrectToDraw, 1054cb93a386Sopenharmony_ci float xformedSigma) { 1055cb93a386Sopenharmony_ci SkASSERT(!SkGpuBlurUtils::IsEffectivelyZeroSigma(xformedSigma)); 1056cb93a386Sopenharmony_ci static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain(); 1057cb93a386Sopenharmony_ci 1058cb93a386Sopenharmony_ci GrUniqueKey::Builder builder(key, kDomain, 9, "RoundRect Blur Mask"); 1059cb93a386Sopenharmony_ci builder[0] = SkScalarCeilToInt(xformedSigma - 1 / 6.0f); 1060cb93a386Sopenharmony_ci 1061cb93a386Sopenharmony_ci int index = 1; 1062cb93a386Sopenharmony_ci // TODO: this is overkill for _simple_ circular rrects 1063cb93a386Sopenharmony_ci for (auto c : {SkRRect::kUpperLeft_Corner, 1064cb93a386Sopenharmony_ci SkRRect::kUpperRight_Corner, 1065cb93a386Sopenharmony_ci SkRRect::kLowerRight_Corner, 1066cb93a386Sopenharmony_ci SkRRect::kLowerLeft_Corner}) { 1067cb93a386Sopenharmony_ci SkASSERT(SkScalarIsInt(rrectToDraw.radii(c).fX) && SkScalarIsInt(rrectToDraw.radii(c).fY)); 1068cb93a386Sopenharmony_ci builder[index++] = SkScalarCeilToInt(rrectToDraw.radii(c).fX); 1069cb93a386Sopenharmony_ci builder[index++] = SkScalarCeilToInt(rrectToDraw.radii(c).fY); 1070cb93a386Sopenharmony_ci } 1071cb93a386Sopenharmony_ci builder.finish(); 1072cb93a386Sopenharmony_ci} 1073cb93a386Sopenharmony_ci 1074cb93a386Sopenharmony_cistatic bool fillin_view_on_gpu(GrDirectContext* dContext, 1075cb93a386Sopenharmony_ci const GrSurfaceProxyView& lazyView, 1076cb93a386Sopenharmony_ci sk_sp<GrThreadSafeCache::Trampoline> trampoline, 1077cb93a386Sopenharmony_ci const SkRRect& rrectToDraw, 1078cb93a386Sopenharmony_ci const SkISize& dimensions, 1079cb93a386Sopenharmony_ci float xformedSigma) { 1080cb93a386Sopenharmony_ci#if SK_GPU_V1 1081cb93a386Sopenharmony_ci SkASSERT(!SkGpuBlurUtils::IsEffectivelyZeroSigma(xformedSigma)); 1082cb93a386Sopenharmony_ci 1083cb93a386Sopenharmony_ci // We cache blur masks. Use default surface props here so we can use the same cached mask 1084cb93a386Sopenharmony_ci // regardless of the final dst surface. 1085cb93a386Sopenharmony_ci SkSurfaceProps defaultSurfaceProps; 1086cb93a386Sopenharmony_ci 1087cb93a386Sopenharmony_ci std::unique_ptr<skgpu::v1::SurfaceDrawContext> sdc = 1088cb93a386Sopenharmony_ci skgpu::v1::SurfaceDrawContext::MakeWithFallback(dContext, 1089cb93a386Sopenharmony_ci GrColorType::kAlpha_8, 1090cb93a386Sopenharmony_ci nullptr, 1091cb93a386Sopenharmony_ci SkBackingFit::kExact, 1092cb93a386Sopenharmony_ci dimensions, 1093cb93a386Sopenharmony_ci defaultSurfaceProps, 1094cb93a386Sopenharmony_ci 1, 1095cb93a386Sopenharmony_ci GrMipmapped::kNo, 1096cb93a386Sopenharmony_ci GrProtected::kNo, 1097cb93a386Sopenharmony_ci kBlurredRRectMaskOrigin); 1098cb93a386Sopenharmony_ci if (!sdc) { 1099cb93a386Sopenharmony_ci return false; 1100cb93a386Sopenharmony_ci } 1101cb93a386Sopenharmony_ci 1102cb93a386Sopenharmony_ci GrPaint paint; 1103cb93a386Sopenharmony_ci 1104cb93a386Sopenharmony_ci sdc->clear(SK_PMColor4fTRANSPARENT); 1105cb93a386Sopenharmony_ci sdc->drawRRect(nullptr, 1106cb93a386Sopenharmony_ci std::move(paint), 1107cb93a386Sopenharmony_ci GrAA::kYes, 1108cb93a386Sopenharmony_ci SkMatrix::I(), 1109cb93a386Sopenharmony_ci rrectToDraw, 1110cb93a386Sopenharmony_ci GrStyle::SimpleFill()); 1111cb93a386Sopenharmony_ci 1112cb93a386Sopenharmony_ci GrSurfaceProxyView srcView = sdc->readSurfaceView(); 1113cb93a386Sopenharmony_ci SkASSERT(srcView.asTextureProxy()); 1114cb93a386Sopenharmony_ci auto rtc2 = SkGpuBlurUtils::GaussianBlur(dContext, 1115cb93a386Sopenharmony_ci std::move(srcView), 1116cb93a386Sopenharmony_ci sdc->colorInfo().colorType(), 1117cb93a386Sopenharmony_ci sdc->colorInfo().alphaType(), 1118cb93a386Sopenharmony_ci nullptr, 1119cb93a386Sopenharmony_ci SkIRect::MakeSize(dimensions), 1120cb93a386Sopenharmony_ci SkIRect::MakeSize(dimensions), 1121cb93a386Sopenharmony_ci xformedSigma, 1122cb93a386Sopenharmony_ci xformedSigma, 1123cb93a386Sopenharmony_ci SkTileMode::kClamp, 1124cb93a386Sopenharmony_ci SkBackingFit::kExact); 1125cb93a386Sopenharmony_ci if (!rtc2 || !rtc2->readSurfaceView()) { 1126cb93a386Sopenharmony_ci return false; 1127cb93a386Sopenharmony_ci } 1128cb93a386Sopenharmony_ci 1129cb93a386Sopenharmony_ci auto view = rtc2->readSurfaceView(); 1130cb93a386Sopenharmony_ci SkASSERT(view.swizzle() == lazyView.swizzle()); 1131cb93a386Sopenharmony_ci SkASSERT(view.origin() == lazyView.origin()); 1132cb93a386Sopenharmony_ci trampoline->fProxy = view.asTextureProxyRef(); 1133cb93a386Sopenharmony_ci 1134cb93a386Sopenharmony_ci return true; 1135cb93a386Sopenharmony_ci#else 1136cb93a386Sopenharmony_ci return false; 1137cb93a386Sopenharmony_ci#endif 1138cb93a386Sopenharmony_ci} 1139cb93a386Sopenharmony_ci 1140cb93a386Sopenharmony_ci// Evaluate the vertical blur at the specified 'y' value given the location of the top of the 1141cb93a386Sopenharmony_ci// rrect. 1142cb93a386Sopenharmony_cistatic uint8_t eval_V(float top, int y, const uint8_t* integral, int integralSize, float sixSigma) { 1143cb93a386Sopenharmony_ci if (top < 0) { 1144cb93a386Sopenharmony_ci return 0; // an empty column 1145cb93a386Sopenharmony_ci } 1146cb93a386Sopenharmony_ci 1147cb93a386Sopenharmony_ci float fT = (top - y - 0.5f) * (integralSize / sixSigma); 1148cb93a386Sopenharmony_ci if (fT < 0) { 1149cb93a386Sopenharmony_ci return 255; 1150cb93a386Sopenharmony_ci } else if (fT >= integralSize - 1) { 1151cb93a386Sopenharmony_ci return 0; 1152cb93a386Sopenharmony_ci } 1153cb93a386Sopenharmony_ci 1154cb93a386Sopenharmony_ci int lower = (int)fT; 1155cb93a386Sopenharmony_ci float frac = fT - lower; 1156cb93a386Sopenharmony_ci 1157cb93a386Sopenharmony_ci SkASSERT(lower + 1 < integralSize); 1158cb93a386Sopenharmony_ci 1159cb93a386Sopenharmony_ci return integral[lower] * (1.0f - frac) + integral[lower + 1] * frac; 1160cb93a386Sopenharmony_ci} 1161cb93a386Sopenharmony_ci 1162cb93a386Sopenharmony_ci// Apply a gaussian 'kernel' horizontally at the specified 'x', 'y' location. 1163cb93a386Sopenharmony_cistatic uint8_t eval_H(int x, 1164cb93a386Sopenharmony_ci int y, 1165cb93a386Sopenharmony_ci const std::vector<float>& topVec, 1166cb93a386Sopenharmony_ci const float* kernel, 1167cb93a386Sopenharmony_ci int kernelSize, 1168cb93a386Sopenharmony_ci const uint8_t* integral, 1169cb93a386Sopenharmony_ci int integralSize, 1170cb93a386Sopenharmony_ci float sixSigma) { 1171cb93a386Sopenharmony_ci SkASSERT(0 <= x && x < (int)topVec.size()); 1172cb93a386Sopenharmony_ci SkASSERT(kernelSize % 2); 1173cb93a386Sopenharmony_ci 1174cb93a386Sopenharmony_ci float accum = 0.0f; 1175cb93a386Sopenharmony_ci 1176cb93a386Sopenharmony_ci int xSampleLoc = x - (kernelSize / 2); 1177cb93a386Sopenharmony_ci for (int i = 0; i < kernelSize; ++i, ++xSampleLoc) { 1178cb93a386Sopenharmony_ci if (xSampleLoc < 0 || xSampleLoc >= (int)topVec.size()) { 1179cb93a386Sopenharmony_ci continue; 1180cb93a386Sopenharmony_ci } 1181cb93a386Sopenharmony_ci 1182cb93a386Sopenharmony_ci accum += kernel[i] * eval_V(topVec[xSampleLoc], y, integral, integralSize, sixSigma); 1183cb93a386Sopenharmony_ci } 1184cb93a386Sopenharmony_ci 1185cb93a386Sopenharmony_ci return accum + 0.5f; 1186cb93a386Sopenharmony_ci} 1187cb93a386Sopenharmony_ci 1188cb93a386Sopenharmony_ci// Create a cpu-side blurred-rrect mask that is close to the version the gpu would've produced. 1189cb93a386Sopenharmony_ci// The match needs to be close bc the cpu- and gpu-generated version must be interchangeable. 1190cb93a386Sopenharmony_cistatic GrSurfaceProxyView create_mask_on_cpu(GrRecordingContext* rContext, 1191cb93a386Sopenharmony_ci const SkRRect& rrectToDraw, 1192cb93a386Sopenharmony_ci const SkISize& dimensions, 1193cb93a386Sopenharmony_ci float xformedSigma) { 1194cb93a386Sopenharmony_ci SkASSERT(!SkGpuBlurUtils::IsEffectivelyZeroSigma(xformedSigma)); 1195cb93a386Sopenharmony_ci int radius = SkGpuBlurUtils::SigmaRadius(xformedSigma); 1196cb93a386Sopenharmony_ci int kernelSize = 2 * radius + 1; 1197cb93a386Sopenharmony_ci 1198cb93a386Sopenharmony_ci SkASSERT(kernelSize % 2); 1199cb93a386Sopenharmony_ci SkASSERT(dimensions.width() % 2); 1200cb93a386Sopenharmony_ci SkASSERT(dimensions.height() % 2); 1201cb93a386Sopenharmony_ci 1202cb93a386Sopenharmony_ci SkVector radii = rrectToDraw.getSimpleRadii(); 1203cb93a386Sopenharmony_ci SkASSERT(SkScalarNearlyEqual(radii.fX, radii.fY)); 1204cb93a386Sopenharmony_ci 1205cb93a386Sopenharmony_ci const int halfWidthPlus1 = (dimensions.width() / 2) + 1; 1206cb93a386Sopenharmony_ci const int halfHeightPlus1 = (dimensions.height() / 2) + 1; 1207cb93a386Sopenharmony_ci 1208cb93a386Sopenharmony_ci std::unique_ptr<float[]> kernel(new float[kernelSize]); 1209cb93a386Sopenharmony_ci 1210cb93a386Sopenharmony_ci SkGpuBlurUtils::Compute1DGaussianKernel(kernel.get(), xformedSigma, radius); 1211cb93a386Sopenharmony_ci 1212cb93a386Sopenharmony_ci SkBitmap integral; 1213cb93a386Sopenharmony_ci if (!SkGpuBlurUtils::CreateIntegralTable(6 * xformedSigma, &integral)) { 1214cb93a386Sopenharmony_ci return {}; 1215cb93a386Sopenharmony_ci } 1216cb93a386Sopenharmony_ci 1217cb93a386Sopenharmony_ci SkBitmap result; 1218cb93a386Sopenharmony_ci if (!result.tryAllocPixels(SkImageInfo::MakeA8(dimensions.width(), dimensions.height()))) { 1219cb93a386Sopenharmony_ci return {}; 1220cb93a386Sopenharmony_ci } 1221cb93a386Sopenharmony_ci 1222cb93a386Sopenharmony_ci std::vector<float> topVec; 1223cb93a386Sopenharmony_ci topVec.reserve(dimensions.width()); 1224cb93a386Sopenharmony_ci for (int x = 0; x < dimensions.width(); ++x) { 1225cb93a386Sopenharmony_ci if (x < rrectToDraw.rect().fLeft || x > rrectToDraw.rect().fRight) { 1226cb93a386Sopenharmony_ci topVec.push_back(-1); 1227cb93a386Sopenharmony_ci } else { 1228cb93a386Sopenharmony_ci if (x + 0.5f < rrectToDraw.rect().fLeft + radii.fX) { // in the circular section 1229cb93a386Sopenharmony_ci float xDist = rrectToDraw.rect().fLeft + radii.fX - x - 0.5f; 1230cb93a386Sopenharmony_ci float h = sqrtf(radii.fX * radii.fX - xDist * xDist); 1231cb93a386Sopenharmony_ci SkASSERT(0 <= h && h < radii.fY); 1232cb93a386Sopenharmony_ci topVec.push_back(rrectToDraw.rect().fTop + radii.fX - h + 3 * xformedSigma); 1233cb93a386Sopenharmony_ci } else { 1234cb93a386Sopenharmony_ci topVec.push_back(rrectToDraw.rect().fTop + 3 * xformedSigma); 1235cb93a386Sopenharmony_ci } 1236cb93a386Sopenharmony_ci } 1237cb93a386Sopenharmony_ci } 1238cb93a386Sopenharmony_ci 1239cb93a386Sopenharmony_ci for (int y = 0; y < halfHeightPlus1; ++y) { 1240cb93a386Sopenharmony_ci uint8_t* scanline = result.getAddr8(0, y); 1241cb93a386Sopenharmony_ci 1242cb93a386Sopenharmony_ci for (int x = 0; x < halfWidthPlus1; ++x) { 1243cb93a386Sopenharmony_ci scanline[x] = eval_H(x, 1244cb93a386Sopenharmony_ci y, 1245cb93a386Sopenharmony_ci topVec, 1246cb93a386Sopenharmony_ci kernel.get(), 1247cb93a386Sopenharmony_ci kernelSize, 1248cb93a386Sopenharmony_ci integral.getAddr8(0, 0), 1249cb93a386Sopenharmony_ci integral.width(), 1250cb93a386Sopenharmony_ci 6 * xformedSigma); 1251cb93a386Sopenharmony_ci scanline[dimensions.width() - x - 1] = scanline[x]; 1252cb93a386Sopenharmony_ci } 1253cb93a386Sopenharmony_ci 1254cb93a386Sopenharmony_ci memcpy(result.getAddr8(0, dimensions.height() - y - 1), scanline, result.rowBytes()); 1255cb93a386Sopenharmony_ci } 1256cb93a386Sopenharmony_ci 1257cb93a386Sopenharmony_ci result.setImmutable(); 1258cb93a386Sopenharmony_ci 1259cb93a386Sopenharmony_ci auto view = std::get<0>(GrMakeUncachedBitmapProxyView(rContext, result)); 1260cb93a386Sopenharmony_ci if (!view) { 1261cb93a386Sopenharmony_ci return {}; 1262cb93a386Sopenharmony_ci } 1263cb93a386Sopenharmony_ci 1264cb93a386Sopenharmony_ci SkASSERT(view.origin() == kBlurredRRectMaskOrigin); 1265cb93a386Sopenharmony_ci return view; 1266cb93a386Sopenharmony_ci} 1267cb93a386Sopenharmony_ci 1268cb93a386Sopenharmony_cistatic std::unique_ptr<GrFragmentProcessor> find_or_create_rrect_blur_mask_fp( 1269cb93a386Sopenharmony_ci GrRecordingContext* rContext, 1270cb93a386Sopenharmony_ci const SkRRect& rrectToDraw, 1271cb93a386Sopenharmony_ci const SkISize& dimensions, 1272cb93a386Sopenharmony_ci float xformedSigma) { 1273cb93a386Sopenharmony_ci SkASSERT(!SkGpuBlurUtils::IsEffectivelyZeroSigma(xformedSigma)); 1274cb93a386Sopenharmony_ci GrUniqueKey key; 1275cb93a386Sopenharmony_ci make_blurred_rrect_key(&key, rrectToDraw, xformedSigma); 1276cb93a386Sopenharmony_ci 1277cb93a386Sopenharmony_ci auto threadSafeCache = rContext->priv().threadSafeCache(); 1278cb93a386Sopenharmony_ci 1279cb93a386Sopenharmony_ci // It seems like we could omit this matrix and modify the shader code to not normalize 1280cb93a386Sopenharmony_ci // the coords used to sample the texture effect. However, the "proxyDims" value in the 1281cb93a386Sopenharmony_ci // shader is not always the actual the proxy dimensions. This is because 'dimensions' here 1282cb93a386Sopenharmony_ci // was computed using integer corner radii as determined in 1283cb93a386Sopenharmony_ci // SkComputeBlurredRRectParams whereas the shader code uses the float radius to compute 1284cb93a386Sopenharmony_ci // 'proxyDims'. Why it draws correctly with these unequal values is a mystery for the ages. 1285cb93a386Sopenharmony_ci auto m = SkMatrix::Scale(dimensions.width(), dimensions.height()); 1286cb93a386Sopenharmony_ci 1287cb93a386Sopenharmony_ci GrSurfaceProxyView view; 1288cb93a386Sopenharmony_ci 1289cb93a386Sopenharmony_ci if (GrDirectContext* dContext = rContext->asDirectContext()) { 1290cb93a386Sopenharmony_ci // The gpu thread gets priority over the recording threads. If the gpu thread is first, 1291cb93a386Sopenharmony_ci // it crams a lazy proxy into the cache and then fills it in later. 1292cb93a386Sopenharmony_ci auto [lazyView, trampoline] = GrThreadSafeCache::CreateLazyView(dContext, 1293cb93a386Sopenharmony_ci GrColorType::kAlpha_8, 1294cb93a386Sopenharmony_ci dimensions, 1295cb93a386Sopenharmony_ci kBlurredRRectMaskOrigin, 1296cb93a386Sopenharmony_ci SkBackingFit::kExact); 1297cb93a386Sopenharmony_ci if (!lazyView) { 1298cb93a386Sopenharmony_ci return nullptr; 1299cb93a386Sopenharmony_ci } 1300cb93a386Sopenharmony_ci 1301cb93a386Sopenharmony_ci view = threadSafeCache->findOrAdd(key, lazyView); 1302cb93a386Sopenharmony_ci if (view != lazyView) { 1303cb93a386Sopenharmony_ci SkASSERT(view.asTextureProxy()); 1304cb93a386Sopenharmony_ci SkASSERT(view.origin() == kBlurredRRectMaskOrigin); 1305cb93a386Sopenharmony_ci return GrTextureEffect::Make(std::move(view), kPremul_SkAlphaType, m); 1306cb93a386Sopenharmony_ci } 1307cb93a386Sopenharmony_ci 1308cb93a386Sopenharmony_ci if (!fillin_view_on_gpu(dContext, 1309cb93a386Sopenharmony_ci lazyView, 1310cb93a386Sopenharmony_ci std::move(trampoline), 1311cb93a386Sopenharmony_ci rrectToDraw, 1312cb93a386Sopenharmony_ci dimensions, 1313cb93a386Sopenharmony_ci xformedSigma)) { 1314cb93a386Sopenharmony_ci // In this case something has gone disastrously wrong so set up to drop the draw 1315cb93a386Sopenharmony_ci // that needed this resource and reduce future pollution of the cache. 1316cb93a386Sopenharmony_ci threadSafeCache->remove(key); 1317cb93a386Sopenharmony_ci return nullptr; 1318cb93a386Sopenharmony_ci } 1319cb93a386Sopenharmony_ci } else { 1320cb93a386Sopenharmony_ci view = threadSafeCache->find(key); 1321cb93a386Sopenharmony_ci if (view) { 1322cb93a386Sopenharmony_ci SkASSERT(view.asTextureProxy()); 1323cb93a386Sopenharmony_ci SkASSERT(view.origin() == kBlurredRRectMaskOrigin); 1324cb93a386Sopenharmony_ci return GrTextureEffect::Make(std::move(view), kPremul_SkAlphaType, m); 1325cb93a386Sopenharmony_ci } 1326cb93a386Sopenharmony_ci 1327cb93a386Sopenharmony_ci view = create_mask_on_cpu(rContext, rrectToDraw, dimensions, xformedSigma); 1328cb93a386Sopenharmony_ci if (!view) { 1329cb93a386Sopenharmony_ci return nullptr; 1330cb93a386Sopenharmony_ci } 1331cb93a386Sopenharmony_ci 1332cb93a386Sopenharmony_ci view = threadSafeCache->add(key, view); 1333cb93a386Sopenharmony_ci } 1334cb93a386Sopenharmony_ci 1335cb93a386Sopenharmony_ci SkASSERT(view.asTextureProxy()); 1336cb93a386Sopenharmony_ci SkASSERT(view.origin() == kBlurredRRectMaskOrigin); 1337cb93a386Sopenharmony_ci return GrTextureEffect::Make(std::move(view), kPremul_SkAlphaType, m); 1338cb93a386Sopenharmony_ci} 1339cb93a386Sopenharmony_ci 1340cb93a386Sopenharmony_cistatic std::unique_ptr<GrFragmentProcessor> make_rrect_blur(GrRecordingContext* context, 1341cb93a386Sopenharmony_ci float sigma, 1342cb93a386Sopenharmony_ci float xformedSigma, 1343cb93a386Sopenharmony_ci const SkRRect& srcRRect, 1344cb93a386Sopenharmony_ci const SkRRect& devRRect) { 1345cb93a386Sopenharmony_ci // Should've been caught up-stream 1346cb93a386Sopenharmony_ci#ifdef SK_DEBUG 1347cb93a386Sopenharmony_ci SkASSERTF(!SkRRectPriv::IsCircle(devRRect), 1348cb93a386Sopenharmony_ci "Unexpected circle. %d\n\t%s\n\t%s", 1349cb93a386Sopenharmony_ci SkRRectPriv::IsCircle(srcRRect), 1350cb93a386Sopenharmony_ci srcRRect.dumpToString(true).c_str(), 1351cb93a386Sopenharmony_ci devRRect.dumpToString(true).c_str()); 1352cb93a386Sopenharmony_ci SkASSERTF(!devRRect.isRect(), 1353cb93a386Sopenharmony_ci "Unexpected rect. %d\n\t%s\n\t%s", 1354cb93a386Sopenharmony_ci srcRRect.isRect(), 1355cb93a386Sopenharmony_ci srcRRect.dumpToString(true).c_str(), 1356cb93a386Sopenharmony_ci devRRect.dumpToString(true).c_str()); 1357cb93a386Sopenharmony_ci#endif 1358cb93a386Sopenharmony_ci 1359cb93a386Sopenharmony_ci // TODO: loosen this up 1360cb93a386Sopenharmony_ci if (!SkRRectPriv::IsSimpleCircular(devRRect)) { 1361cb93a386Sopenharmony_ci return nullptr; 1362cb93a386Sopenharmony_ci } 1363cb93a386Sopenharmony_ci 1364cb93a386Sopenharmony_ci if (SkGpuBlurUtils::IsEffectivelyZeroSigma(xformedSigma)) { 1365cb93a386Sopenharmony_ci return nullptr; 1366cb93a386Sopenharmony_ci } 1367cb93a386Sopenharmony_ci 1368cb93a386Sopenharmony_ci // Make sure we can successfully ninepatch this rrect -- the blur sigma has to be sufficiently 1369cb93a386Sopenharmony_ci // small relative to both the size of the corner radius and the width (and height) of the rrect. 1370cb93a386Sopenharmony_ci SkRRect rrectToDraw; 1371cb93a386Sopenharmony_ci SkISize dimensions; 1372cb93a386Sopenharmony_ci SkScalar ignored[SkGpuBlurUtils::kBlurRRectMaxDivisions]; 1373cb93a386Sopenharmony_ci 1374cb93a386Sopenharmony_ci bool ninePatchable = SkGpuBlurUtils::ComputeBlurredRRectParams(srcRRect, 1375cb93a386Sopenharmony_ci devRRect, 1376cb93a386Sopenharmony_ci sigma, 1377cb93a386Sopenharmony_ci xformedSigma, 1378cb93a386Sopenharmony_ci &rrectToDraw, 1379cb93a386Sopenharmony_ci &dimensions, 1380cb93a386Sopenharmony_ci ignored, 1381cb93a386Sopenharmony_ci ignored, 1382cb93a386Sopenharmony_ci ignored, 1383cb93a386Sopenharmony_ci ignored); 1384cb93a386Sopenharmony_ci if (!ninePatchable) { 1385cb93a386Sopenharmony_ci return nullptr; 1386cb93a386Sopenharmony_ci } 1387cb93a386Sopenharmony_ci 1388cb93a386Sopenharmony_ci std::unique_ptr<GrFragmentProcessor> maskFP = 1389cb93a386Sopenharmony_ci find_or_create_rrect_blur_mask_fp(context, rrectToDraw, dimensions, xformedSigma); 1390cb93a386Sopenharmony_ci if (!maskFP) { 1391cb93a386Sopenharmony_ci return nullptr; 1392cb93a386Sopenharmony_ci } 1393cb93a386Sopenharmony_ci 1394cb93a386Sopenharmony_ci static auto effect = SkMakeRuntimeEffect(SkRuntimeEffect::MakeForShader, R"( 1395cb93a386Sopenharmony_ci uniform shader ninePatchFP; 1396cb93a386Sopenharmony_ci 1397cb93a386Sopenharmony_ci uniform half cornerRadius; 1398cb93a386Sopenharmony_ci uniform float4 proxyRect; 1399cb93a386Sopenharmony_ci uniform half blurRadius; 1400cb93a386Sopenharmony_ci 1401cb93a386Sopenharmony_ci half4 main(float2 xy, half4 inColor) { 1402cb93a386Sopenharmony_ci // Warp the fragment position to the appropriate part of the 9-patch blur texture by 1403cb93a386Sopenharmony_ci // snipping out the middle section of the proxy rect. 1404cb93a386Sopenharmony_ci float2 translatedFragPosFloat = sk_FragCoord.xy - proxyRect.LT; 1405cb93a386Sopenharmony_ci float2 proxyCenter = (proxyRect.RB - proxyRect.LT) * 0.5; 1406cb93a386Sopenharmony_ci half edgeSize = 2.0 * blurRadius + cornerRadius + 0.5; 1407cb93a386Sopenharmony_ci 1408cb93a386Sopenharmony_ci // Position the fragment so that (0, 0) marks the center of the proxy rectangle. 1409cb93a386Sopenharmony_ci // Negative coordinates are on the left/top side and positive numbers are on the 1410cb93a386Sopenharmony_ci // right/bottom. 1411cb93a386Sopenharmony_ci translatedFragPosFloat -= proxyCenter; 1412cb93a386Sopenharmony_ci 1413cb93a386Sopenharmony_ci // Temporarily strip off the fragment's sign. x/y are now strictly increasing as we 1414cb93a386Sopenharmony_ci // move away from the center. 1415cb93a386Sopenharmony_ci half2 fragDirection = half2(sign(translatedFragPosFloat)); 1416cb93a386Sopenharmony_ci translatedFragPosFloat = abs(translatedFragPosFloat); 1417cb93a386Sopenharmony_ci 1418cb93a386Sopenharmony_ci // Our goal is to snip out the "middle section" of the proxy rect (everything but the 1419cb93a386Sopenharmony_ci // edge). We've repositioned our fragment position so that (0, 0) is the centerpoint 1420cb93a386Sopenharmony_ci // and x/y are always positive, so we can subtract here and interpret negative results 1421cb93a386Sopenharmony_ci // as being within the middle section. 1422cb93a386Sopenharmony_ci half2 translatedFragPosHalf = half2(translatedFragPosFloat - (proxyCenter - edgeSize)); 1423cb93a386Sopenharmony_ci 1424cb93a386Sopenharmony_ci // Remove the middle section by clamping to zero. 1425cb93a386Sopenharmony_ci translatedFragPosHalf = max(translatedFragPosHalf, 0); 1426cb93a386Sopenharmony_ci 1427cb93a386Sopenharmony_ci // Reapply the fragment's sign, so that negative coordinates once again mean left/top 1428cb93a386Sopenharmony_ci // side and positive means bottom/right side. 1429cb93a386Sopenharmony_ci translatedFragPosHalf *= fragDirection; 1430cb93a386Sopenharmony_ci 1431cb93a386Sopenharmony_ci // Offset the fragment so that (0, 0) marks the upper-left again, instead of the center 1432cb93a386Sopenharmony_ci // point. 1433cb93a386Sopenharmony_ci translatedFragPosHalf += half2(edgeSize); 1434cb93a386Sopenharmony_ci 1435cb93a386Sopenharmony_ci half2 proxyDims = half2(2.0 * edgeSize); 1436cb93a386Sopenharmony_ci half2 texCoord = translatedFragPosHalf / proxyDims; 1437cb93a386Sopenharmony_ci 1438cb93a386Sopenharmony_ci return inColor * ninePatchFP.eval(texCoord).a; 1439cb93a386Sopenharmony_ci } 1440cb93a386Sopenharmony_ci )"); 1441cb93a386Sopenharmony_ci 1442cb93a386Sopenharmony_ci float cornerRadius = SkRRectPriv::GetSimpleRadii(devRRect).fX; 1443cb93a386Sopenharmony_ci float blurRadius = 3.f * SkScalarCeilToScalar(xformedSigma - 1 / 6.0f); 1444cb93a386Sopenharmony_ci SkRect proxyRect = devRRect.getBounds().makeOutset(blurRadius, blurRadius); 1445cb93a386Sopenharmony_ci 1446cb93a386Sopenharmony_ci return GrSkSLFP::Make(effect, "RRectBlur", /*inputFP=*/nullptr, 1447cb93a386Sopenharmony_ci GrSkSLFP::OptFlags::kCompatibleWithCoverageAsAlpha, 1448cb93a386Sopenharmony_ci "ninePatchFP", GrSkSLFP::IgnoreOptFlags(std::move(maskFP)), 1449cb93a386Sopenharmony_ci "cornerRadius", cornerRadius, 1450cb93a386Sopenharmony_ci "proxyRect", proxyRect, 1451cb93a386Sopenharmony_ci "blurRadius", blurRadius); 1452cb93a386Sopenharmony_ci} 1453cb93a386Sopenharmony_ci 1454cb93a386Sopenharmony_ci/////////////////////////////////////////////////////////////////////////////// 1455cb93a386Sopenharmony_ci 1456cb93a386Sopenharmony_cibool SkBlurMaskFilterImpl::directFilterMaskGPU(GrRecordingContext* context, 1457cb93a386Sopenharmony_ci skgpu::v1::SurfaceDrawContext* sdc, 1458cb93a386Sopenharmony_ci GrPaint&& paint, 1459cb93a386Sopenharmony_ci const GrClip* clip, 1460cb93a386Sopenharmony_ci const SkMatrix& viewMatrix, 1461cb93a386Sopenharmony_ci const GrStyledShape& shape) const { 1462cb93a386Sopenharmony_ci SkASSERT(sdc); 1463cb93a386Sopenharmony_ci 1464cb93a386Sopenharmony_ci if (fBlurStyle != kNormal_SkBlurStyle) { 1465cb93a386Sopenharmony_ci return false; 1466cb93a386Sopenharmony_ci } 1467cb93a386Sopenharmony_ci 1468cb93a386Sopenharmony_ci // TODO: we could handle blurred stroked circles 1469cb93a386Sopenharmony_ci if (!shape.style().isSimpleFill()) { 1470cb93a386Sopenharmony_ci return false; 1471cb93a386Sopenharmony_ci } 1472cb93a386Sopenharmony_ci 1473cb93a386Sopenharmony_ci SkScalar xformedSigma = this->computeXformedSigma(viewMatrix); 1474cb93a386Sopenharmony_ci if (SkGpuBlurUtils::IsEffectivelyZeroSigma(xformedSigma)) { 1475cb93a386Sopenharmony_ci sdc->drawShape(clip, std::move(paint), GrAA::kYes, viewMatrix, GrStyledShape(shape)); 1476cb93a386Sopenharmony_ci return true; 1477cb93a386Sopenharmony_ci } 1478cb93a386Sopenharmony_ci 1479cb93a386Sopenharmony_ci SkRRect srcRRect; 1480cb93a386Sopenharmony_ci bool inverted; 1481cb93a386Sopenharmony_ci if (!shape.asRRect(&srcRRect, nullptr, nullptr, &inverted) || inverted) { 1482cb93a386Sopenharmony_ci return false; 1483cb93a386Sopenharmony_ci } 1484cb93a386Sopenharmony_ci 1485cb93a386Sopenharmony_ci std::unique_ptr<GrFragmentProcessor> fp; 1486cb93a386Sopenharmony_ci 1487cb93a386Sopenharmony_ci SkRRect devRRect; 1488cb93a386Sopenharmony_ci bool devRRectIsValid = srcRRect.transform(viewMatrix, &devRRect); 1489cb93a386Sopenharmony_ci 1490cb93a386Sopenharmony_ci bool devRRectIsCircle = devRRectIsValid && SkRRectPriv::IsCircle(devRRect); 1491cb93a386Sopenharmony_ci 1492cb93a386Sopenharmony_ci bool canBeRect = srcRRect.isRect() && viewMatrix.preservesRightAngles(); 1493cb93a386Sopenharmony_ci bool canBeCircle = (SkRRectPriv::IsCircle(srcRRect) && viewMatrix.isSimilarity()) || 1494cb93a386Sopenharmony_ci devRRectIsCircle; 1495cb93a386Sopenharmony_ci 1496cb93a386Sopenharmony_ci if (canBeRect || canBeCircle) { 1497cb93a386Sopenharmony_ci if (canBeRect) { 1498cb93a386Sopenharmony_ci fp = make_rect_blur(context, *context->priv().caps()->shaderCaps(), 1499cb93a386Sopenharmony_ci srcRRect.rect(), viewMatrix, xformedSigma); 1500cb93a386Sopenharmony_ci } else { 1501cb93a386Sopenharmony_ci SkRect devBounds; 1502cb93a386Sopenharmony_ci if (devRRectIsCircle) { 1503cb93a386Sopenharmony_ci devBounds = devRRect.getBounds(); 1504cb93a386Sopenharmony_ci } else { 1505cb93a386Sopenharmony_ci SkPoint center = {srcRRect.getBounds().centerX(), srcRRect.getBounds().centerY()}; 1506cb93a386Sopenharmony_ci viewMatrix.mapPoints(¢er, 1); 1507cb93a386Sopenharmony_ci SkScalar radius = viewMatrix.mapVector(0, srcRRect.width()/2.f).length(); 1508cb93a386Sopenharmony_ci devBounds = {center.x() - radius, 1509cb93a386Sopenharmony_ci center.y() - radius, 1510cb93a386Sopenharmony_ci center.x() + radius, 1511cb93a386Sopenharmony_ci center.y() + radius}; 1512cb93a386Sopenharmony_ci } 1513cb93a386Sopenharmony_ci fp = make_circle_blur(context, devBounds, xformedSigma); 1514cb93a386Sopenharmony_ci } 1515cb93a386Sopenharmony_ci 1516cb93a386Sopenharmony_ci if (!fp) { 1517cb93a386Sopenharmony_ci return false; 1518cb93a386Sopenharmony_ci } 1519cb93a386Sopenharmony_ci 1520cb93a386Sopenharmony_ci SkRect srcProxyRect = srcRRect.rect(); 1521cb93a386Sopenharmony_ci // Determine how much to outset the src rect to ensure we hit pixels within three sigma. 1522cb93a386Sopenharmony_ci SkScalar outsetX = 3.0f*xformedSigma; 1523cb93a386Sopenharmony_ci SkScalar outsetY = 3.0f*xformedSigma; 1524cb93a386Sopenharmony_ci if (viewMatrix.isScaleTranslate()) { 1525cb93a386Sopenharmony_ci outsetX /= SkScalarAbs(viewMatrix.getScaleX()); 1526cb93a386Sopenharmony_ci outsetY /= SkScalarAbs(viewMatrix.getScaleY()); 1527cb93a386Sopenharmony_ci } else { 1528cb93a386Sopenharmony_ci SkSize scale; 1529cb93a386Sopenharmony_ci if (!viewMatrix.decomposeScale(&scale, nullptr)) { 1530cb93a386Sopenharmony_ci return false; 1531cb93a386Sopenharmony_ci } 1532cb93a386Sopenharmony_ci outsetX /= scale.width(); 1533cb93a386Sopenharmony_ci outsetY /= scale.height(); 1534cb93a386Sopenharmony_ci } 1535cb93a386Sopenharmony_ci srcProxyRect.outset(outsetX, outsetY); 1536cb93a386Sopenharmony_ci 1537cb93a386Sopenharmony_ci paint.setCoverageFragmentProcessor(std::move(fp)); 1538cb93a386Sopenharmony_ci sdc->drawRect(clip, std::move(paint), GrAA::kNo, viewMatrix, srcProxyRect); 1539cb93a386Sopenharmony_ci return true; 1540cb93a386Sopenharmony_ci } 1541cb93a386Sopenharmony_ci if (!viewMatrix.isScaleTranslate()) { 1542cb93a386Sopenharmony_ci return false; 1543cb93a386Sopenharmony_ci } 1544cb93a386Sopenharmony_ci if (!devRRectIsValid || !SkRRectPriv::AllCornersCircular(devRRect)) { 1545cb93a386Sopenharmony_ci return false; 1546cb93a386Sopenharmony_ci } 1547cb93a386Sopenharmony_ci 1548cb93a386Sopenharmony_ci fp = make_rrect_blur(context, fSigma, xformedSigma, srcRRect, devRRect); 1549cb93a386Sopenharmony_ci if (!fp) { 1550cb93a386Sopenharmony_ci return false; 1551cb93a386Sopenharmony_ci } 1552cb93a386Sopenharmony_ci 1553cb93a386Sopenharmony_ci if (!this->ignoreXform()) { 1554cb93a386Sopenharmony_ci SkRect srcProxyRect = srcRRect.rect(); 1555cb93a386Sopenharmony_ci srcProxyRect.outset(3.0f*fSigma, 3.0f*fSigma); 1556cb93a386Sopenharmony_ci paint.setCoverageFragmentProcessor(std::move(fp)); 1557cb93a386Sopenharmony_ci sdc->drawRect(clip, std::move(paint), GrAA::kNo, viewMatrix, srcProxyRect); 1558cb93a386Sopenharmony_ci } else { 1559cb93a386Sopenharmony_ci SkMatrix inverse; 1560cb93a386Sopenharmony_ci if (!viewMatrix.invert(&inverse)) { 1561cb93a386Sopenharmony_ci return false; 1562cb93a386Sopenharmony_ci } 1563cb93a386Sopenharmony_ci 1564cb93a386Sopenharmony_ci SkIRect proxyBounds; 1565cb93a386Sopenharmony_ci float extra=3.f*SkScalarCeilToScalar(xformedSigma-1/6.0f); 1566cb93a386Sopenharmony_ci devRRect.rect().makeOutset(extra, extra).roundOut(&proxyBounds); 1567cb93a386Sopenharmony_ci 1568cb93a386Sopenharmony_ci paint.setCoverageFragmentProcessor(std::move(fp)); 1569cb93a386Sopenharmony_ci sdc->fillPixelsWithLocalMatrix(clip, std::move(paint), proxyBounds, inverse); 1570cb93a386Sopenharmony_ci } 1571cb93a386Sopenharmony_ci 1572cb93a386Sopenharmony_ci return true; 1573cb93a386Sopenharmony_ci} 1574cb93a386Sopenharmony_ci 1575cb93a386Sopenharmony_cibool SkBlurMaskFilterImpl::canFilterMaskGPU(const GrStyledShape& shape, 1576cb93a386Sopenharmony_ci const SkIRect& devSpaceShapeBounds, 1577cb93a386Sopenharmony_ci const SkIRect& clipBounds, 1578cb93a386Sopenharmony_ci const SkMatrix& ctm, 1579cb93a386Sopenharmony_ci SkIRect* maskRect, 1580cb93a386Sopenharmony_ci const bool canUseSDFBlur) const { 1581cb93a386Sopenharmony_ci SkScalar xformedSigma = this->computeXformedSigma(ctm); 1582cb93a386Sopenharmony_ci if (SkGpuBlurUtils::IsEffectivelyZeroSigma(xformedSigma)) { 1583cb93a386Sopenharmony_ci *maskRect = devSpaceShapeBounds; 1584cb93a386Sopenharmony_ci return maskRect->intersect(clipBounds); 1585cb93a386Sopenharmony_ci } 1586cb93a386Sopenharmony_ci 1587cb93a386Sopenharmony_ci if (maskRect) { 1588cb93a386Sopenharmony_ci float sigma3 = 3 * SkScalarToFloat(xformedSigma); 1589cb93a386Sopenharmony_ci 1590cb93a386Sopenharmony_ci // Outset srcRect and clipRect by 3 * sigma, to compute affected blur area. 1591cb93a386Sopenharmony_ci SkIRect clipRect = clipBounds.makeOutset(sigma3, sigma3); 1592cb93a386Sopenharmony_ci SkIRect srcRect = devSpaceShapeBounds.makeOutset(sigma3, sigma3); 1593cb93a386Sopenharmony_ci 1594cb93a386Sopenharmony_ci if (!canUseSDFBlur && !srcRect.intersect(clipRect)) { 1595cb93a386Sopenharmony_ci srcRect.setEmpty(); 1596cb93a386Sopenharmony_ci } 1597cb93a386Sopenharmony_ci SkRRect srcRRect; 1598cb93a386Sopenharmony_ci bool inverted; 1599cb93a386Sopenharmony_ci if (canUseSDFBlur && shape.asRRect(&srcRRect, nullptr, nullptr, &inverted)) { 1600cb93a386Sopenharmony_ci SkScalar sx = ctm.getScaleX(); 1601cb93a386Sopenharmony_ci SkScalar sy = ctm.getScaleY(); 1602cb93a386Sopenharmony_ci float noxFormedSigma3 = this->getNoxFormedSigma3(); 1603cb93a386Sopenharmony_ci int sigmaX = noxFormedSigma3 * sx; 1604cb93a386Sopenharmony_ci int sigmaY = noxFormedSigma3 * sy; 1605cb93a386Sopenharmony_ci srcRect = devSpaceShapeBounds.makeOutset(sigmaX, sigmaY); 1606cb93a386Sopenharmony_ci srcRect = SkIRect::MakeXYWH(srcRect.fLeft, srcRect.fTop, 1607cb93a386Sopenharmony_ci srcRect.width() + srcRRect.rect().fLeft * sx, 1608cb93a386Sopenharmony_ci srcRect.height() + srcRRect.rect().fTop * sy); 1609cb93a386Sopenharmony_ci } 1610cb93a386Sopenharmony_ci *maskRect = srcRect; 1611cb93a386Sopenharmony_ci } 1612cb93a386Sopenharmony_ci 1613cb93a386Sopenharmony_ci // We prefer to blur paths with small blur radii on the CPU. 1614cb93a386Sopenharmony_ci static const SkScalar kMIN_GPU_BLUR_SIZE = SkIntToScalar(64); 1615cb93a386Sopenharmony_ci static const SkScalar kMIN_GPU_BLUR_SIGMA = SkIntToScalar(32); 1616cb93a386Sopenharmony_ci 1617cb93a386Sopenharmony_ci if (devSpaceShapeBounds.width() <= kMIN_GPU_BLUR_SIZE && 1618cb93a386Sopenharmony_ci devSpaceShapeBounds.height() <= kMIN_GPU_BLUR_SIZE && 1619cb93a386Sopenharmony_ci xformedSigma <= kMIN_GPU_BLUR_SIGMA) { 1620cb93a386Sopenharmony_ci return false; 1621cb93a386Sopenharmony_ci } 1622cb93a386Sopenharmony_ci 1623cb93a386Sopenharmony_ci return true; 1624cb93a386Sopenharmony_ci} 1625cb93a386Sopenharmony_ci 1626cb93a386Sopenharmony_ciGrSurfaceProxyView SkBlurMaskFilterImpl::filterMaskGPU(GrRecordingContext* context, 1627cb93a386Sopenharmony_ci GrSurfaceProxyView srcView, 1628cb93a386Sopenharmony_ci GrColorType srcColorType, 1629cb93a386Sopenharmony_ci SkAlphaType srcAlphaType, 1630cb93a386Sopenharmony_ci const SkMatrix& ctm, 1631cb93a386Sopenharmony_ci const SkIRect& maskRect) const { 1632cb93a386Sopenharmony_ci // 'maskRect' isn't snapped to the UL corner but the mask in 'src' is. 1633cb93a386Sopenharmony_ci const SkIRect clipRect = SkIRect::MakeWH(maskRect.width(), maskRect.height()); 1634cb93a386Sopenharmony_ci 1635cb93a386Sopenharmony_ci SkScalar xformedSigma = this->computeXformedSigma(ctm); 1636cb93a386Sopenharmony_ci 1637cb93a386Sopenharmony_ci // If we're doing a normal blur, we can clobber the pathTexture in the 1638cb93a386Sopenharmony_ci // gaussianBlur. Otherwise, we need to save it for later compositing. 1639cb93a386Sopenharmony_ci bool isNormalBlur = (kNormal_SkBlurStyle == fBlurStyle); 1640cb93a386Sopenharmony_ci auto srcBounds = SkIRect::MakeSize(srcView.proxy()->dimensions()); 1641cb93a386Sopenharmony_ci auto surfaceDrawContext = SkGpuBlurUtils::GaussianBlur(context, 1642cb93a386Sopenharmony_ci srcView, 1643cb93a386Sopenharmony_ci srcColorType, 1644cb93a386Sopenharmony_ci srcAlphaType, 1645cb93a386Sopenharmony_ci nullptr, 1646cb93a386Sopenharmony_ci clipRect, 1647cb93a386Sopenharmony_ci srcBounds, 1648cb93a386Sopenharmony_ci xformedSigma, 1649cb93a386Sopenharmony_ci xformedSigma, 1650cb93a386Sopenharmony_ci SkTileMode::kClamp); 1651cb93a386Sopenharmony_ci if (!surfaceDrawContext || !surfaceDrawContext->asTextureProxy()) { 1652cb93a386Sopenharmony_ci return {}; 1653cb93a386Sopenharmony_ci } 1654cb93a386Sopenharmony_ci 1655cb93a386Sopenharmony_ci if (!isNormalBlur) { 1656cb93a386Sopenharmony_ci GrPaint paint; 1657cb93a386Sopenharmony_ci // Blend pathTexture over blurTexture. 1658cb93a386Sopenharmony_ci paint.setCoverageFragmentProcessor(GrTextureEffect::Make(std::move(srcView), srcAlphaType)); 1659cb93a386Sopenharmony_ci if (kInner_SkBlurStyle == fBlurStyle) { 1660cb93a386Sopenharmony_ci // inner: dst = dst * src 1661cb93a386Sopenharmony_ci paint.setCoverageSetOpXPFactory(SkRegion::kIntersect_Op); 1662cb93a386Sopenharmony_ci } else if (kSolid_SkBlurStyle == fBlurStyle) { 1663cb93a386Sopenharmony_ci // solid: dst = src + dst - src * dst 1664cb93a386Sopenharmony_ci // = src + (1 - src) * dst 1665cb93a386Sopenharmony_ci paint.setCoverageSetOpXPFactory(SkRegion::kUnion_Op); 1666cb93a386Sopenharmony_ci } else if (kOuter_SkBlurStyle == fBlurStyle) { 1667cb93a386Sopenharmony_ci // outer: dst = dst * (1 - src) 1668cb93a386Sopenharmony_ci // = 0 * src + (1 - src) * dst 1669cb93a386Sopenharmony_ci paint.setCoverageSetOpXPFactory(SkRegion::kDifference_Op); 1670cb93a386Sopenharmony_ci } else { 1671cb93a386Sopenharmony_ci paint.setCoverageSetOpXPFactory(SkRegion::kReplace_Op); 1672cb93a386Sopenharmony_ci } 1673cb93a386Sopenharmony_ci 1674cb93a386Sopenharmony_ci surfaceDrawContext->fillPixelsWithLocalMatrix(nullptr, std::move(paint), clipRect, 1675cb93a386Sopenharmony_ci SkMatrix::I()); 1676cb93a386Sopenharmony_ci } 1677cb93a386Sopenharmony_ci 1678cb93a386Sopenharmony_ci return surfaceDrawContext->readSurfaceView(); 1679cb93a386Sopenharmony_ci} 1680cb93a386Sopenharmony_ci 1681cb93a386Sopenharmony_cifloat SkBlurMaskFilterImpl::getNoxFormedSigma3() const 1682cb93a386Sopenharmony_ci{ 1683cb93a386Sopenharmony_ci constexpr float kSigma_Factor = 3.f; 1684cb93a386Sopenharmony_ci return kSigma_Factor * fSigma; 1685cb93a386Sopenharmony_ci} 1686cb93a386Sopenharmony_ci 1687cb93a386Sopenharmony_ciGrSurfaceProxyView SkBlurMaskFilterImpl::filterMaskGPUNoxFormed(GrRecordingContext* context, 1688cb93a386Sopenharmony_ci GrSurfaceProxyView srcView, GrColorType srcColorType, SkAlphaType srcAlphaType, const SkMatrix& viewMatrix, 1689cb93a386Sopenharmony_ci const SkIRect& maskRect, const SkRRect& srcRRect) const 1690cb93a386Sopenharmony_ci{ 1691cb93a386Sopenharmony_ci const SkIRect clipRect = SkIRect::MakeWH(maskRect.width(), maskRect.height()); 1692cb93a386Sopenharmony_ci 1693cb93a386Sopenharmony_ci float noxFormedSigma = this->getNoxFormedSigma3(); 1694cb93a386Sopenharmony_ci 1695cb93a386Sopenharmony_ci bool isNormalBlur = (kNormal_SkBlurStyle == fBlurStyle); 1696cb93a386Sopenharmony_ci if (!isNormalBlur) { 1697cb93a386Sopenharmony_ci return {}; 1698cb93a386Sopenharmony_ci } 1699cb93a386Sopenharmony_ci auto srcBounds = SkIRect::MakeSize(srcView.proxy()->dimensions()); 1700cb93a386Sopenharmony_ci auto surfaceDrawContext = SDFBlur::SDFBlur(context, srcView, srcColorType, srcAlphaType, nullptr, 1701cb93a386Sopenharmony_ci clipRect, srcBounds, noxFormedSigma, SkTileMode::kClamp, viewMatrix, srcRRect); 1702cb93a386Sopenharmony_ci if (!surfaceDrawContext || !surfaceDrawContext->asTextureProxy()) { 1703cb93a386Sopenharmony_ci return {}; 1704cb93a386Sopenharmony_ci } 1705cb93a386Sopenharmony_ci 1706cb93a386Sopenharmony_ci return surfaceDrawContext->readSurfaceView(); 1707cb93a386Sopenharmony_ci} 1708cb93a386Sopenharmony_ci 1709cb93a386Sopenharmony_ci#endif // SK_SUPPORT_GPU && SK_GPU_V1 1710cb93a386Sopenharmony_ci 1711cb93a386Sopenharmony_civoid sk_register_blur_maskfilter_createproc() { SK_REGISTER_FLATTENABLE(SkBlurMaskFilterImpl); } 1712cb93a386Sopenharmony_ci 1713cb93a386Sopenharmony_cisk_sp<SkMaskFilter> SkMaskFilter::MakeBlur(SkBlurStyle style, SkScalar sigma, bool respectCTM) { 1714cb93a386Sopenharmony_ci if (SkScalarIsFinite(sigma) && sigma > 0) { 1715cb93a386Sopenharmony_ci return sk_sp<SkMaskFilter>(new SkBlurMaskFilterImpl(sigma, style, respectCTM)); 1716cb93a386Sopenharmony_ci } 1717cb93a386Sopenharmony_ci return nullptr; 1718cb93a386Sopenharmony_ci} 1719