| 1 |
/* |
| 2 |
* Copyright 2006 The Android Open Source Project |
| 3 |
* |
| 4 |
* Use of this source code is governed by a BSD-style license that can be |
| 5 |
* found in the LICENSE file. |
| 6 |
*/ |
| 7 |
|
| 8 |
#include "src/core/SkBlurMaskFilterImpl.h" |
| 9 |
|
| 10 |
#include "include/core/SkBlendMode.h" |
| 11 |
#include "include/core/SkBlurTypes.h" |
| 12 |
#include "include/core/SkFlattenable.h" |
| 13 |
#include "include/core/SkImageFilter.h" |
| 14 |
#include "include/core/SkImageInfo.h" |
| 15 |
#include "include/core/SkM44.h" |
| 16 |
#include "include/core/SkMaskFilter.h" |
| 17 |
#include "include/core/SkMatrix.h" |
| 18 |
#include "include/core/SkPaint.h" |
| 19 |
#include "include/core/SkPath.h" |
| 20 |
#include "include/core/SkPathBuilder.h" |
| 21 |
#include "include/core/SkPathTypes.h" |
| 22 |
#include "include/core/SkPixmap.h" |
| 23 |
#include "include/core/SkPoint.h" |
| 24 |
#include "include/core/SkRRect.h" |
| 25 |
#include "include/core/SkRect.h" |
| 26 |
#include "include/core/SkRefCnt.h" |
| 27 |
#include "include/core/SkScalar.h" |
| 28 |
#include "include/effects/SkImageFilters.h" |
| 29 |
#include "include/private/base/SkAlign.h" |
| 30 |
#include "include/private/base/SkAssert.h" |
| 31 |
#include "include/private/base/SkFloatingPoint.h" |
| 32 |
#include "include/private/base/SkTemplates.h" |
| 33 |
#include "src/base/SkTLazy.h" |
| 34 |
#include "src/core/SkBlitter_A8.h" |
| 35 |
#include "src/core/SkBlurMask.h" |
| 36 |
#include "src/core/SkCachedData.h" |
| 37 |
#include "src/core/SkDraw.h" |
| 38 |
#include "src/core/SkMask.h" |
| 39 |
#include "src/core/SkMaskCache.h" |
| 40 |
#include "src/core/SkMaskFilterBase.h" |
| 41 |
#include "src/core/SkRasterClip.h" |
| 42 |
#include "src/core/SkReadBuffer.h" |
| 43 |
#include "src/core/SkResourceCache.h" |
| 44 |
#include "src/core/SkWriteBuffer.h" |
| 45 |
|
| 46 |
#include <algorithm> |
| 47 |
#include <cstdint> |
| 48 |
#include <cstring> |
| 49 |
#include <utility> |
| 50 |
|
| 51 |
SkBlurMaskFilterImpl::SkBlurMaskFilterImpl(SkScalar sigma, SkBlurStyle style, bool respectCTM) |
| 52 |
: fSigma(sigma) |
| 53 |
, fBlurStyle(style) |
| 54 |
, fRespectCTM(respectCTM) { |
| 55 |
SkASSERT(fSigma > 0); |
| 56 |
SkASSERT((unsigned)style <= kLastEnum_SkBlurStyle); |
| 57 |
} |
| 58 |
|
| 59 |
SkMask::Format SkBlurMaskFilterImpl::getFormat() const { |
| 60 |
return SkMask::kA8_Format; |
| 61 |
} |
| 62 |
|
| 63 |
bool SkBlurMaskFilterImpl::asABlur(BlurRec* rec) const { |
| 64 |
if (this->ignoreXform()) { |
| 65 |
return false; |
| 66 |
} |
| 67 |
|
| 68 |
if (rec) { |
| 69 |
rec->fSigma = fSigma; |
| 70 |
rec->fStyle = fBlurStyle; |
| 71 |
} |
| 72 |
return true; |
| 73 |
} |
| 74 |
|
| 75 |
std::pair<sk_sp<SkImageFilter>, bool> SkBlurMaskFilterImpl::asImageFilter(const SkMatrix& ctm, |
| 76 |
const SkPaint&) const { |
| 77 |
// Mask filters apply a uniform blur in either local or device space. Depending on the scale |
| 78 |
// factors of the `ctm`, the actual blur radii can end up non-uniform. |
| 79 |
SkV2 sigma = {fSigma, fSigma}; |
| 80 |
if (this->ignoreXform()) { |
| 81 |
// This is analogous to computeXformedSigma(), but it might be more correct to wrap the |
| 82 |
// blur image filter in a local matrix with ctm^-1, or to control the skif::Mapping when |
| 83 |
// the mask filter layer is restored. It calculates new blur radii such that transforming |
| 84 |
// these to the layer space of the image filter will match the original device-space fSigma. |
| 85 |
// This can be inaccurate when 'ctm' has skew or perspective. A full fix requires layers |
| 86 |
// having flexible operating coordinate spaces (e.g. parent or root canvas). |
| 87 |
const float xScaleFactor = fSigma / ctm.mapVector(fSigma, 0.f).length(); |
| 88 |
const float yScaleFactor = fSigma / ctm.mapVector(0.f, fSigma).length(); |
| 89 |
sigma = {fSigma * xScaleFactor, fSigma * yScaleFactor}; |
| 90 |
} |
| 91 |
|
| 92 |
// The null input image filter will be bound to the original coverage mask. |
| 93 |
sk_sp<SkImageFilter> filter = SkImageFilters::Blur(sigma.x, sigma.y, nullptr); |
| 94 |
// Combine the original coverage mask (src) and the blurred coverage mask (dst) |
| 95 |
switch(fBlurStyle) { |
| 96 |
case kInner_SkBlurStyle: // dst = dst * src |
| 97 |
// = 0 * src + src * dst |
| 98 |
return {SkImageFilters::Blend(SkBlendMode::kDstIn, std::move(filter), nullptr), false}; |
| 99 |
case kSolid_SkBlurStyle: // dst = src + dst - src * dst |
| 100 |
// = 1 * src + (1 - src) * dst |
| 101 |
return {SkImageFilters::Blend(SkBlendMode::kSrcOver, std::move(filter), nullptr), false}; |
| 102 |
case kOuter_SkBlurStyle: // dst = dst * (1 - src) |
| 103 |
// = 0 * src + (1 - src) * dst |
| 104 |
return {SkImageFilters::Blend(SkBlendMode::kDstOut, std::move(filter), nullptr), false}; |
| 105 |
case kNormal_SkBlurStyle: |
| 106 |
return {filter, false}; |
| 107 |
} |
| 108 |
SkUNREACHABLE; |
| 109 |
} |
| 110 |
|
| 111 |
SkScalar SkBlurMaskFilterImpl::computeXformedSigma(const SkMatrix& ctm) const { |
| 112 |
constexpr SkScalar kMaxBlurSigma = SkIntToScalar(128); |
| 113 |
SkScalar xformedSigma = this->ignoreXform() ? fSigma : ctm.mapRadius(fSigma); |
| 114 |
return std::min(xformedSigma, kMaxBlurSigma); |
| 115 |
} |
| 116 |
|
| 117 |
bool SkBlurMaskFilterImpl::filterMask(SkMaskBuilder* dst, const SkMask& src, |
| 118 |
const SkMatrix& matrix, |
| 119 |
SkIPoint* margin) const { |
| 120 |
SkScalar sigma = this->computeXformedSigma(matrix); |
| 121 |
return SkBlurMask::BoxBlur(dst, src, sigma, fBlurStyle, margin); |
| 122 |
} |
| 123 |
|
| 124 |
bool SkBlurMaskFilterImpl::filterRectMask(SkMaskBuilder* dst, const SkRect& r, |
| 125 |
const SkMatrix& matrix, |
| 126 |
SkIPoint* margin, |
| 127 |
SkMaskBuilder::CreateMode createMode) const { |
| 128 |
SkScalar sigma = computeXformedSigma(matrix); |
| 129 |
|
| 130 |
return SkBlurMask::BlurRect(sigma, dst, r, fBlurStyle, margin, createMode); |
| 131 |
} |
| 132 |
|
| 133 |
static bool prepare_to_draw_into_mask(const SkRect& bounds, SkMaskBuilder* mask) { |
| 134 |
SkASSERT(mask != nullptr); |
| 135 |
|
| 136 |
mask->bounds() = bounds.roundOut(); |
| 137 |
mask->rowBytes() = SkAlign4(mask->fBounds.width()); |
| 138 |
mask->format() = SkMask::kA8_Format; |
| 139 |
const size_t size = mask->computeImageSize(); |
| 140 |
if (size == 0) { |
| 141 |
return false; |
| 142 |
} |
| 143 |
mask->image() = SkMaskBuilder::AllocImage(size, SkMaskBuilder::kZeroInit_Alloc); |
| 144 |
if (nullptr == mask->fImage) { |
| 145 |
return false; |
| 146 |
} |
| 147 |
return true; |
| 148 |
} |
| 149 |
|
| 150 |
template <typename Proc> bool draw_into_mask(SkMaskBuilder* mask, const SkRect& bounds, Proc proc) { |
| 151 |
if (!prepare_to_draw_into_mask(bounds, mask)) { |
| 152 |
return false; |
| 153 |
} |
| 154 |
|
| 155 |
const int dx = mask->fBounds.fLeft; |
| 156 |
const int dy = mask->fBounds.fTop; |
| 157 |
SkRasterClip rclip(mask->fBounds); |
| 158 |
rclip.setRect(mask->fBounds.makeOffset(-dx, -dy)); |
| 159 |
|
| 160 |
SkASSERT(mask->fFormat == SkMask::kA8_Format); |
| 161 |
auto info = SkImageInfo::MakeA8(mask->fBounds.width(), mask->fBounds.height()); |
| 162 |
auto pm = SkPixmap(info, mask->fImage, mask->fRowBytes); |
| 163 |
|
| 164 |
SkMatrix ctm = SkMatrix::Translate(-SkIntToScalar(dx), -SkIntToScalar(dy)); |
| 165 |
|
| 166 |
skcpu::Draw draw; |
| 167 |
draw.fBlitterChooser = SkA8Blitter_Choose; |
| 168 |
draw.fCTM = &ctm; |
| 169 |
draw.fDst = pm; |
| 170 |
draw.fRC = &rclip; |
| 171 |
|
| 172 |
SkPaint paint; |
| 173 |
paint.setAntiAlias(true); |
| 174 |
|
| 175 |
proc(draw, paint); |
| 176 |
return true; |
| 177 |
} |
| 178 |
|
| 179 |
static bool draw_rects_into_mask(SkSpan<const SkRect> rects, SkMaskBuilder* mask) { |
| 180 |
SkASSERT(rects.size() == 1 || rects.size() == 2); |
| 181 |
return draw_into_mask(mask, rects[0], [&](skcpu::Draw& draw, const SkPaint& paint) { |
| 182 |
if (rects.size() == 1) { |
| 183 |
draw.drawRect(rects[0], paint); |
| 184 |
} else { |
| 185 |
// todo: do I need a fast way to do this? |
| 186 |
SkPath path = SkPathBuilder().addRect(rects[0]) |
| 187 |
.addRect(rects[1]) |
| 188 |
.setFillType(SkPathFillType::kEvenOdd) |
| 189 |
.detach(); |
| 190 |
draw.drawPath(path, paint, nullptr); |
| 191 |
} |
| 192 |
}); |
| 193 |
} |
| 194 |
|
| 195 |
static bool draw_rrect_into_mask(const SkRRect& rrect, SkMaskBuilder* mask) { |
| 196 |
return draw_into_mask(mask, rrect.rect(), [&](skcpu::Draw& draw, const SkPaint& paint) { |
| 197 |
draw.drawRRect(rrect, paint); |
| 198 |
}); |
| 199 |
} |
| 200 |
|
| 201 |
static bool rect_exceeds(const SkRect& r, SkScalar v) { |
| 202 |
return r.fLeft < -v || r.fTop < -v || r.fRight > v || r.fBottom > v || |
| 203 |
r.width() > v || r.height() > v; |
| 204 |
} |
| 205 |
|
| 206 |
static SkCachedData* copy_mask_to_cacheddata(SkMaskBuilder* mask, SkResourceCache* cache) { |
| 207 |
const size_t size = mask->computeTotalImageSize(); |
| 208 |
SkCachedData* data; |
| 209 |
if (cache) { |
| 210 |
data = cache->newCachedData(size); |
| 211 |
} else { |
| 212 |
data = SkResourceCache::NewCachedData(size); |
| 213 |
} |
| 214 |
if (data) { |
| 215 |
memcpy(data->writable_data(), mask->fImage, size); |
| 216 |
SkMaskBuilder::FreeImage(mask->image()); |
| 217 |
mask->image() = (uint8_t*)data->writable_data(); |
| 218 |
} |
| 219 |
return data; |
| 220 |
} |
| 221 |
|
| 222 |
static SkCachedData* find_cached_rrect(SkTLazy<SkMask>* mask, |
| 223 |
SkScalar sigma, |
| 224 |
SkBlurStyle style, |
| 225 |
const SkRRect& rrect, |
| 226 |
SkResourceCache* cache) { |
| 227 |
return SkMaskCache::FindAndRef(sigma, style, rrect, mask, cache); |
| 228 |
} |
| 229 |
|
| 230 |
static SkCachedData* add_cached_rrect(SkMaskBuilder* mask, |
| 231 |
SkScalar sigma, |
| 232 |
SkBlurStyle style, |
| 233 |
const SkRRect& rrect, |
| 234 |
SkResourceCache* cache) { |
| 235 |
SkCachedData* cached = copy_mask_to_cacheddata(mask, cache); |
| 236 |
if (cached) { |
| 237 |
SkMaskCache::Add(sigma, style, rrect, *mask, cached, cache); |
| 238 |
} |
| 239 |
return cached; |
| 240 |
} |
| 241 |
|
| 242 |
static SkCachedData* find_cached_rects(SkTLazy<SkMask>* mask, |
| 243 |
SkScalar sigma, |
| 244 |
SkBlurStyle style, |
| 245 |
SkSpan<const SkRect> rects, |
| 246 |
SkResourceCache* cache) { |
| 247 |
return SkMaskCache::FindAndRef(sigma, style, rects, mask, cache); |
| 248 |
} |
| 249 |
|
| 250 |
static SkCachedData* add_cached_rects(SkMaskBuilder* mask, |
| 251 |
SkScalar sigma, |
| 252 |
SkBlurStyle style, |
| 253 |
SkSpan<const SkRect> rects, |
| 254 |
SkResourceCache* cache) { |
| 255 |
SkCachedData* cached = copy_mask_to_cacheddata(mask, cache); |
| 256 |
if (cached) { |
| 257 |
SkMaskCache::Add(sigma, style, rects, *mask, cached, cache); |
| 258 |
} |
| 259 |
return cached; |
| 260 |
} |
| 261 |
|
| 262 |
std::optional<SkMaskFilterBase::NinePatch> SkBlurMaskFilterImpl::filterRRectToNine( |
| 263 |
const SkRRect& rrect, |
| 264 |
const SkMatrix& matrix, |
| 265 |
const SkIRect& clipBounds, |
| 266 |
SkResourceCache* cache) const { |
| 267 |
switch (rrect.getType()) { |
| 268 |
case SkRRect::kEmpty_Type: |
| 269 |
// Nothing to draw. |
| 270 |
return std::nullopt; |
| 271 |
|
| 272 |
case SkRRect::kRect_Type: |
| 273 |
// We should have caught this earlier. |
| 274 |
SkDEBUGFAIL("Should use a different special case"); |
| 275 |
return std::nullopt; |
| 276 |
case SkRRect::kOval_Type: |
| 277 |
// The nine patch special case does not handle ovals, and we |
| 278 |
// already have code for rectangles. |
| 279 |
return std::nullopt; |
| 280 |
|
| 281 |
// These three can take advantage of this fast path. |
| 282 |
case SkRRect::kSimple_Type: |
| 283 |
case SkRRect::kNinePatch_Type: |
| 284 |
case SkRRect::kComplex_Type: |
| 285 |
break; |
| 286 |
} |
| 287 |
|
| 288 |
// TODO: report correct metrics for innerstyle, where we do not grow the |
| 289 |
// total bounds, but we do need an inset the size of our blur-radius |
| 290 |
if (kInner_SkBlurStyle == fBlurStyle) { |
| 291 |
return std::nullopt; |
| 292 |
} |
| 293 |
|
| 294 |
// TODO: take clipBounds into account to limit our coordinates up front |
| 295 |
// for now, just skip too-large src rects (to take the old code path). |
| 296 |
if (rect_exceeds(rrect.rect(), SkIntToScalar(32767))) { |
| 297 |
return std::nullopt; |
| 298 |
} |
| 299 |
|
| 300 |
// To better understand the following code, consider the concrete example |
| 301 |
// where the passed in rrect is a 16x12 rounded rectangle with (rX,rY) |
| 302 |
// being (2.5, 2.0) and the sigma is 0.5 (which was turned into matrix). |
| 303 |
// The full non-blurred rrect would look something like this (intensity |
| 304 |
// ranges from 0-F and each letter is one pixel in the bitmap). |
| 305 |
// 4BFFFFFFFFFFFFB4 |
| 306 |
// CFFFFFFFFFFFFFFC |
| 307 |
// FFFFFFFFFFFFFFFF |
| 308 |
// FFFFFFFFFFFFFFFF |
| 309 |
// FFFFFFFFFFFFFFFF |
| 310 |
// FFFFFFFFFFFFFFFF |
| 311 |
// FFFFFFFFFFFFFFFF |
| 312 |
// FFFFFFFFFFFFFFFF |
| 313 |
// FFFFFFFFFFFFFFFF |
| 314 |
// FFFFFFFFFFFFFFFF |
| 315 |
// CFFFFFFFFFFFFFFC |
| 316 |
// 4BFFFFFFFFFFFFB4 |
| 317 |
|
| 318 |
// We first figure out how much we need to expand the mask (the margin) to account |
| 319 |
// for the blurred area. In the example, margin will be 1x1 |
| 320 |
SkIVector margin; |
| 321 |
SkMaskBuilder srcM(nullptr, rrect.rect().roundOut(), 0, SkMask::kA8_Format), dstM; |
| 322 |
if (!this->filterMask(&dstM, srcM, matrix, &margin)) { |
| 323 |
return std::nullopt; |
| 324 |
} |
| 325 |
|
| 326 |
// Most of the pixels in the center of the bitmap are the same as their neighbors, so |
| 327 |
// blurring is a waste of compute. If we made a smaller nine-patch rrect, blurred |
| 328 |
// that, we could then expand the result later, saving cycles. As a bonus, we can cache |
| 329 |
// that smaller rectangle and re-use it for other rrects with the same radii/sigma |
| 330 |
// combinations. |
| 331 |
|
| 332 |
// To figure out the appropriate width and height of the nine patch rrect, we use the |
| 333 |
// larger radius per side as well as the margin, to account for inner blur. In the example, |
| 334 |
// the minimum nine patch is 9 x 7 |
| 335 |
// |
| 336 |
// width: ceil(2.5) + 1 (margin) + 1 (stretch) + 1 (margin) + ceil(2.5) = 9 |
| 337 |
// height: ceil(2.0) + 1 (margin) + 1 (stretch) + 1 (margin) + ceil(2.0) = 7 |
| 338 |
// |
| 339 |
// 4BFF F FFB4 |
| 340 |
// CFFF F FFFC |
| 341 |
// FFFF F FFFF |
| 342 |
// |
| 343 |
// FFFF F FFFF |
| 344 |
// |
| 345 |
// FFFF F FFFF |
| 346 |
// CFFF F FFFC |
| 347 |
// 4BFF F FFB4 |
| 348 |
const SkVector& UL = rrect.radii(SkRRect::kUpperLeft_Corner); |
| 349 |
const SkVector& UR = rrect.radii(SkRRect::kUpperRight_Corner); |
| 350 |
const SkVector& LR = rrect.radii(SkRRect::kLowerRight_Corner); |
| 351 |
const SkVector& LL = rrect.radii(SkRRect::kLowerLeft_Corner); |
| 352 |
|
| 353 |
// If there's a fractional radii, round up so that our final rrect is an integer width |
| 354 |
// to allow for symmetrical blurring across the x and y axes. |
| 355 |
const int32_t leftUnstretched = SkScalarCeilToInt(std::max(UL.fX, LL.fX)) + margin.fX; |
| 356 |
const int32_t rightUnstretched = SkScalarCeilToInt(std::max(UR.fX, LR.fX)) + margin.fX; |
| 357 |
|
| 358 |
// Extra space in the middle to ensure an unchanging piece for stretching. |
| 359 |
const int32_t stretchSize = 1; |
| 360 |
|
| 361 |
const int32_t totalSmallWidth = leftUnstretched + rightUnstretched + stretchSize; |
| 362 |
if (totalSmallWidth >= rrect.rect().width()) { |
| 363 |
// There is no valid piece to stretch. |
| 364 |
return std::nullopt; |
| 365 |
} |
| 366 |
|
| 367 |
const int32_t topUnstretched = SkScalarCeilToInt(std::max(UL.fY, UR.fY)) + margin.fY; |
| 368 |
const int32_t botUnstretched = SkScalarCeilToInt(std::max(LL.fY, LR.fY)) + margin.fY; |
| 369 |
|
| 370 |
const int32_t totalSmallHeight = topUnstretched + botUnstretched + stretchSize; |
| 371 |
if (totalSmallHeight >= rrect.rect().height()) { |
| 372 |
// There is no valid piece to stretch. |
| 373 |
return std::nullopt; |
| 374 |
} |
| 375 |
|
| 376 |
// Now make that scaled down nine patch rrect. |
| 377 |
SkRect smallR = SkRect::MakeWH(totalSmallWidth, totalSmallHeight); |
| 378 |
SkRRect smallRR; |
| 379 |
smallRR.setRectRadii(smallR, rrect.radii().data()); |
| 380 |
|
| 381 |
const float sigma = this->computeXformedSigma(matrix); |
| 382 |
// If we've already blurred this small rrect, pull it out of the cache and we are done |
| 383 |
SkTLazy<SkMask> cachedMask; |
| 384 |
SkCachedData* cached = find_cached_rrect(&cachedMask, sigma, fBlurStyle, smallRR, cache); |
| 385 |
if (!cached) { |
| 386 |
// Blit the small rrect into a buffer (9x7) |
| 387 |
// 4BFFFFFB4 |
| 388 |
// CFFFFFFFC |
| 389 |
// FFFFFFFFF |
| 390 |
// FFFFFFFFF |
| 391 |
// FFFFFFFFF |
| 392 |
// CFFFFFFFC |
| 393 |
// 4BFFFFFB4 |
| 394 |
if (!draw_rrect_into_mask(smallRR, &srcM)) { |
| 395 |
return std::nullopt; |
| 396 |
} |
| 397 |
SkAutoMaskFreeImage amf(srcM.image()); // delete small rrect's pixels when done |
| 398 |
|
| 399 |
// Blur the small rrect. This will expand the mask on all sides by margin to account for |
| 400 |
// outer blur (11x9) |
| 401 |
// 00111111100 |
| 402 |
// 04ADEEEDA40 |
| 403 |
// 1BFFFFFFFB1 |
| 404 |
// 1EFFFFFFFE1 |
| 405 |
// 1EFFFFFFFE1 |
| 406 |
// 1EFFFFFFFE1 |
| 407 |
// 1BFFFFFFFB1 |
| 408 |
// 04ADEEEDA40 |
| 409 |
// 00111111100 |
| 410 |
SkMaskBuilder filterM; |
| 411 |
if (!this->filterMask(&filterM, srcM, matrix, nullptr)) { |
| 412 |
return std::nullopt; |
| 413 |
} |
| 414 |
SkASSERT(filterM.fBounds.width() == (srcM.fBounds.width() + 2*margin.fX)); |
| 415 |
SkASSERT(filterM.fBounds.height() == (srcM.fBounds.height() + 2*margin.fY)); |
| 416 |
|
| 417 |
cached = add_cached_rrect(&filterM, sigma, fBlurStyle, smallRR, cache); |
| 418 |
cachedMask.init(filterM); |
| 419 |
} |
| 420 |
|
| 421 |
// The bounds of the blurred mask are at -margin, so we need to offset it back to 0,0 |
| 422 |
SkIRect bounds = cachedMask->fBounds; |
| 423 |
bounds.offsetTo(0, 0); |
| 424 |
// The ninepatch could be asymmetrical (e.g. if the left rX are wider than the right), so |
| 425 |
// we must tell the caller where the center stretchy bit is in both directions. We added |
| 426 |
// margin once before to unstretched to account for inner blur, but now we add to account |
| 427 |
// for the outer blur. In the example, this will be (5, 4) [note that it's 0-indexed] |
| 428 |
SkIPoint center = SkIPoint{margin.fX + leftUnstretched, |
| 429 |
margin.fY + topUnstretched}; |
| 430 |
return std::optional<SkMaskFilterBase::NinePatch>( |
| 431 |
std::in_place, |
| 432 |
SkMask{cachedMask->fImage, bounds, cachedMask->fRowBytes, cachedMask->fFormat}, |
| 433 |
dstM.fBounds, |
| 434 |
center, |
| 435 |
cached); // transfer ownership to patch |
| 436 |
} |
| 437 |
|
| 438 |
SkMaskFilterBase::FilterReturn SkBlurMaskFilterImpl::filterRectsToNine( |
| 439 |
SkSpan<const SkRect> rects, |
| 440 |
const SkMatrix& matrix, |
| 441 |
const SkIRect& clipBounds, |
| 442 |
std::optional<NinePatch>* patch, |
| 443 |
SkResourceCache* cache) const { |
| 444 |
SkASSERT(patch != nullptr); |
| 445 |
SkASSERT(rects.size() == 1 || rects.size() == 2); |
| 446 |
|
| 447 |
// TODO: report correct metrics for innerstyle, where we do not grow the |
| 448 |
// total bounds, but we do need an inset the size of our blur-radius |
| 449 |
if (kInner_SkBlurStyle == fBlurStyle || kOuter_SkBlurStyle == fBlurStyle) { |
| 450 |
return FilterReturn::kUnimplemented; |
| 451 |
} |
| 452 |
|
| 453 |
// TODO: take clipBounds into account to limit our coordinates up front |
| 454 |
// for now, just skip too-large src rects (to take the old code path). |
| 455 |
if (rect_exceeds(rects[0], SkIntToScalar(32767))) { |
| 456 |
return FilterReturn::kUnimplemented; |
| 457 |
} |
| 458 |
|
| 459 |
SkIPoint margin; |
| 460 |
SkMaskBuilder srcM(nullptr, rects[0].roundOut(), 0, SkMask::kA8_Format), dstM; |
| 461 |
|
| 462 |
bool filterResult = false; |
| 463 |
if (rects.size() == 1) { |
| 464 |
// special case for fast rect blur |
| 465 |
// don't actually do the blur the first time, just compute the correct size |
| 466 |
filterResult = this->filterRectMask(&dstM, rects[0], matrix, &margin, |
| 467 |
SkMaskBuilder::kJustComputeBounds_CreateMode); |
| 468 |
} else { |
| 469 |
filterResult = this->filterMask(&dstM, srcM, matrix, &margin); |
| 470 |
} |
| 471 |
|
| 472 |
if (!filterResult) { |
| 473 |
return FilterReturn::kFalse; |
| 474 |
} |
| 475 |
|
| 476 |
/* |
| 477 |
* smallR is the smallest version of 'rect' that will still guarantee that |
| 478 |
* we get the same blur results on all edges, plus 1 center row/col that is |
| 479 |
* representative of the extendible/stretchable edges of the ninepatch. |
| 480 |
* Since our actual edge may be fractional we inset 1 more to be sure we |
| 481 |
* don't miss any interior blur. |
| 482 |
* x is an added pixel of blur, and { and } are the (fractional) edge |
| 483 |
* pixels from the original rect. |
| 484 |
* |
| 485 |
* x x { x x .... x x } x x |
| 486 |
* |
| 487 |
* Thus, in this case, we inset by a total of 5 (on each side) beginning |
| 488 |
* with our outer-rect (dstM.fBounds) |
| 489 |
*/ |
| 490 |
SkRect smallR[2]; |
| 491 |
int rectCount; |
| 492 |
SkIPoint center; |
| 493 |
|
| 494 |
// +2 is from +1 for each edge (to account for possible fractional edges |
| 495 |
int smallW = dstM.fBounds.width() - srcM.fBounds.width() + 2; |
| 496 |
int smallH = dstM.fBounds.height() - srcM.fBounds.height() + 2; |
| 497 |
SkIRect innerIR; |
| 498 |
|
| 499 |
if (rects.size() == 1) { |
| 500 |
rectCount = 1; |
| 501 |
innerIR = srcM.fBounds; |
| 502 |
center.set(smallW, smallH); |
| 503 |
} else { |
| 504 |
rectCount = 2; |
| 505 |
rects[1].roundIn(&innerIR); |
| 506 |
center.set(smallW + (innerIR.left() - srcM.fBounds.left()), |
| 507 |
smallH + (innerIR.top() - srcM.fBounds.top())); |
| 508 |
} |
| 509 |
|
| 510 |
// +1 so we get a clean, stretchable, center row/col |
| 511 |
smallW += 1; |
| 512 |
smallH += 1; |
| 513 |
|
| 514 |
// we want the inset amounts to be integral, so we don't change any |
| 515 |
// fractional phase on the fRight or fBottom of our smallR. |
| 516 |
const SkScalar dx = SkIntToScalar(innerIR.width() - smallW); |
| 517 |
const SkScalar dy = SkIntToScalar(innerIR.height() - smallH); |
| 518 |
if (dx < 0 || dy < 0) { |
| 519 |
// we're too small, relative to our blur, to break into nine-patch, |
| 520 |
// so we ask to have our normal filterMask() be called. |
| 521 |
return FilterReturn::kUnimplemented; |
| 522 |
} |
| 523 |
|
| 524 |
smallR[0].setLTRB(rects[0].left(), rects[0].top(), |
| 525 |
rects[0].right() - dx, rects[0].bottom() - dy); |
| 526 |
if (smallR[0].width() < 2 || smallR[0].height() < 2) { |
| 527 |
return FilterReturn::kUnimplemented; |
| 528 |
} |
| 529 |
if (rectCount == 2) { |
| 530 |
smallR[1].setLTRB(rects[1].left(), rects[1].top(), |
| 531 |
rects[1].right() - dx, rects[1].bottom() - dy); |
| 532 |
SkASSERT(!smallR[1].isEmpty()); |
| 533 |
} |
| 534 |
|
| 535 |
const SkScalar sigma = this->computeXformedSigma(matrix); |
| 536 |
SkTLazy<SkMask> cachedMask; |
| 537 |
SkSpan<const SkRect> smallRects = SkSpan(smallR, rectCount); |
| 538 |
SkCachedData* cached = find_cached_rects(&cachedMask, sigma, fBlurStyle, smallRects, cache); |
| 539 |
if (!cached) { |
| 540 |
SkMaskBuilder filterM; |
| 541 |
if (rectCount == 2) { |
| 542 |
if (!draw_rects_into_mask(smallRects, &srcM)) { |
| 543 |
return FilterReturn::kFalse; |
| 544 |
} |
| 545 |
|
| 546 |
SkAutoMaskFreeImage amf(srcM.image()); |
| 547 |
|
| 548 |
if (!this->filterMask(&filterM, srcM, matrix, nullptr)) { |
| 549 |
return FilterReturn::kFalse; |
| 550 |
} |
| 551 |
} else { |
| 552 |
if (!this->filterRectMask(&filterM, smallR[0], matrix, nullptr, |
| 553 |
SkMaskBuilder::kComputeBoundsAndRenderImage_CreateMode)) { |
| 554 |
return FilterReturn::kFalse; |
| 555 |
} |
| 556 |
} |
| 557 |
cached = add_cached_rects(&filterM, sigma, fBlurStyle, smallRects, cache); |
| 558 |
cachedMask.init(filterM); |
| 559 |
} |
| 560 |
SkIRect bounds = cachedMask->fBounds; |
| 561 |
bounds.offsetTo(0, 0); |
| 562 |
patch->emplace(SkMask{cachedMask->fImage, bounds, cachedMask->fRowBytes, cachedMask->fFormat}, |
| 563 |
dstM.fBounds, |
| 564 |
center, |
| 565 |
cached); // transfer ownership to patch |
| 566 |
return FilterReturn::kTrue; |
| 567 |
} |
| 568 |
|
| 569 |
void SkBlurMaskFilterImpl::computeFastBounds(const SkRect& src, |
| 570 |
SkRect* dst) const { |
| 571 |
// TODO: if we're doing kInner blur, should we return a different outset? |
| 572 |
// i.e. pad == 0 ? |
| 573 |
|
| 574 |
SkScalar pad = 3.0f * fSigma; |
| 575 |
|
| 576 |
dst->setLTRB(src.fLeft - pad, src.fTop - pad, |
| 577 |
src.fRight + pad, src.fBottom + pad); |
| 578 |
} |
| 579 |
|
| 580 |
sk_sp<SkFlattenable> SkBlurMaskFilterImpl::CreateProc(SkReadBuffer& buffer) { |
| 581 |
const SkScalar sigma = buffer.readScalar(); |
| 582 |
SkBlurStyle style = buffer.read32LE(kLastEnum_SkBlurStyle); |
| 583 |
|
| 584 |
uint32_t flags = buffer.read32LE(0x3); // historically we only recorded 2 bits |
| 585 |
bool respectCTM = !(flags & 1); // historically we stored ignoreCTM in low bit |
| 586 |
|
| 587 |
return SkMaskFilter::MakeBlur((SkBlurStyle)style, sigma, respectCTM); |
| 588 |
} |
| 589 |
|
| 590 |
void SkBlurMaskFilterImpl::flatten(SkWriteBuffer& buffer) const { |
| 591 |
buffer.writeScalar(fSigma); |
| 592 |
buffer.writeUInt(fBlurStyle); |
| 593 |
buffer.writeUInt(!fRespectCTM); // historically we recorded ignoreCTM |
| 594 |
} |
| 595 |
|
| 596 |
void sk_register_blur_maskfilter_createproc() { SK_REGISTER_FLATTENABLE(SkBlurMaskFilterImpl); } |
| 597 |
|
| 598 |
sk_sp<SkMaskFilter> SkMaskFilter::MakeBlur(SkBlurStyle style, SkScalar sigma, bool respectCTM) { |
| 599 |
if (SkIsFinite(sigma) && sigma > 0) { |
| 600 |
return sk_sp<SkMaskFilter>(new SkBlurMaskFilterImpl(sigma, style, respectCTM)); |
| 601 |
} |
| 602 |
return nullptr; |
| 603 |
} |