| 1 |
/* |
| 2 |
* Copyright 2024 Google LLC |
| 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/SkBlurEngine.h" |
| 9 |
|
| 10 |
#include "include/core/SkAlphaType.h" |
| 11 |
#include "include/core/SkBitmap.h" |
| 12 |
#include "include/core/SkBlendMode.h" |
| 13 |
#include "include/core/SkClipOp.h" |
| 14 |
#include "include/core/SkColor.h" |
| 15 |
#include "include/core/SkColorSpace.h" // IWYU pragma: keep |
| 16 |
#include "include/core/SkColorType.h" |
| 17 |
#include "include/core/SkImageInfo.h" |
| 18 |
#include "include/core/SkM44.h" |
| 19 |
#include "include/core/SkMatrix.h" |
| 20 |
#include "include/core/SkPaint.h" |
| 21 |
#include "include/core/SkPoint.h" |
| 22 |
#include "include/core/SkRect.h" |
| 23 |
#include "include/core/SkSamplingOptions.h" |
| 24 |
#include "include/core/SkScalar.h" |
| 25 |
#include "include/core/SkSurfaceProps.h" |
| 26 |
#include "include/core/SkTileMode.h" |
| 27 |
#include "include/effects/SkRuntimeEffect.h" |
| 28 |
#include "include/private/base/SkAssert.h" |
| 29 |
#include "include/private/base/SkFeatures.h" |
| 30 |
#include "include/private/base/SkMalloc.h" |
| 31 |
#include "include/private/base/SkMath.h" |
| 32 |
#include "include/private/base/SkTo.h" |
| 33 |
#include "src/base/SkArenaAlloc.h" |
| 34 |
#include "src/base/SkVx.h" |
| 35 |
#include "src/core/SkBitmapDevice.h" |
| 36 |
#include "src/core/SkDevice.h" |
| 37 |
#include "src/core/SkKnownRuntimeEffects.h" |
| 38 |
#include "src/core/SkSpecialImage.h" |
| 39 |
|
| 40 |
#include <algorithm> |
| 41 |
#include <array> |
| 42 |
#include <cmath> |
| 43 |
#include <cstdint> |
| 44 |
#include <cstring> |
| 45 |
#include <utility> |
| 46 |
|
| 47 |
|
| 48 |
#if SK_CPU_SSE_LEVEL >= SK_CPU_SSE_LEVEL_SSE1 |
| 49 |
#include <xmmintrin.h> |
| 50 |
#define SK_PREFETCH(ptr) _mm_prefetch(reinterpret_cast<const char*>(ptr), _MM_HINT_T0) |
| 51 |
#elif defined(__GNUC__) |
| 52 |
#define SK_PREFETCH(ptr) __builtin_prefetch(ptr) |
| 53 |
#else |
| 54 |
#define SK_PREFETCH(ptr) |
| 55 |
#endif |
| 56 |
|
| 57 |
// RasterBlurEngine |
| 58 |
// ---------------------------------------------------------------------------- |
| 59 |
|
| 60 |
namespace { |
| 61 |
|
| 62 |
class Pass { |
| 63 |
public: |
| 64 |
explicit Pass(int border) : fBorder(border) {} |
| 65 |
virtual ~Pass() = default; |
| 66 |
|
| 67 |
// T is type of the pixel format for the color type. |
| 68 |
template <typename T> |
| 69 |
void blur(int srcLeft, int srcRight, int dstRight, |
| 70 |
const T* src, int srcStride, |
| 71 |
T* dst, int dstStride) { |
| 72 |
this->startBlur(); |
| 73 |
|
| 74 |
auto srcStart = srcLeft - fBorder, |
| 75 |
srcEnd = srcRight - fBorder, |
| 76 |
dstEnd = dstRight, |
| 77 |
srcIdx = srcStart, |
| 78 |
dstIdx = 0; |
| 79 |
|
| 80 |
const T* srcCursor = src; |
| 81 |
T* dstCursor = dst; |
| 82 |
|
| 83 |
if (dstIdx < srcIdx) { |
| 84 |
// The destination pixels are not effected by the src pixels, |
| 85 |
// change to zero as per the spec. |
| 86 |
// https://drafts.fxtf.org/filter-effects/#FilterPrimitivesOverviewIntro |
| 87 |
int commonEnd = std::min(srcIdx, dstEnd); |
| 88 |
while (dstIdx < commonEnd) { |
| 89 |
*dstCursor = 0; |
| 90 |
dstCursor += dstStride; |
| 91 |
SK_PREFETCH(dstCursor); |
| 92 |
dstIdx++; |
| 93 |
} |
| 94 |
} else if (srcIdx < dstIdx) { |
| 95 |
// The edge of the source is before the edge of the destination. Calculate the sums for |
| 96 |
// the pixels before the start of the destination. |
| 97 |
if (int commonEnd = std::min(dstIdx, srcEnd); srcIdx < commonEnd) { |
| 98 |
// Preload the blur with values from src before dst is entered. |
| 99 |
int n = commonEnd - srcIdx; |
| 100 |
this->blurSegment(n, srcCursor, srcStride, nullptr, 0); |
| 101 |
srcIdx += n; |
| 102 |
srcCursor += n * srcStride; |
| 103 |
} |
| 104 |
if (srcIdx < dstIdx) { |
| 105 |
// The weird case where src is out of pixels before dst is even started. |
| 106 |
int n = dstIdx - srcIdx; |
| 107 |
this->blurSegment(n, nullptr, 0, nullptr, 0); |
| 108 |
srcIdx += n; |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
if (int commonEnd = std::min(dstEnd, srcEnd); dstIdx < commonEnd) { |
| 113 |
// Both srcIdx and dstIdx are in sync now, and can run in a 1:1 fashion. This is the |
| 114 |
// normal mode of operation. |
| 115 |
SkASSERT(srcIdx == dstIdx); |
| 116 |
|
| 117 |
int n = commonEnd - dstIdx; |
| 118 |
this->blurSegment(n, srcCursor, srcStride, dstCursor, dstStride); |
| 119 |
srcCursor += n * srcStride; |
| 120 |
dstCursor += n * dstStride; |
| 121 |
dstIdx += n; |
| 122 |
srcIdx += n; |
| 123 |
} |
| 124 |
|
| 125 |
// Drain the remaining blur values into dst assuming 0's for the leading edge. |
| 126 |
if (dstIdx < dstEnd) { |
| 127 |
int n = dstEnd - dstIdx; |
| 128 |
this->blurSegment(n, nullptr, 0, dstCursor, dstStride); |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
protected: |
| 133 |
virtual void startBlur() = 0; |
| 134 |
virtual void blurSegment( |
| 135 |
int n, const void* src, int srcStride, void* dst, int dstStride) = 0; |
| 136 |
|
| 137 |
private: |
| 138 |
const int fBorder; |
| 139 |
}; |
| 140 |
|
| 141 |
class PassMaker { |
| 142 |
public: |
| 143 |
explicit PassMaker(int window, float sigma) : fWindow{window}, |
| 144 |
fSigma{sigma} {} |
| 145 |
virtual ~PassMaker() = default; |
| 146 |
virtual Pass* makePass(void* buffer, SkArenaAlloc* alloc) const = 0; |
| 147 |
virtual size_t bufferSizeBytes() const = 0; |
| 148 |
int window() const {return fWindow;} |
| 149 |
float sigma() const {return fSigma;} |
| 150 |
|
| 151 |
private: |
| 152 |
const int fWindow; |
| 153 |
const float fSigma; |
| 154 |
}; |
| 155 |
|
| 156 |
// T is type of the pixel format for the color type. |
| 157 |
// This should only be used for 8bit color channels. |
| 158 |
template <typename T> |
| 159 |
static sk_sp<SkSpecialImage> eval_blur_passes(PassMaker* makerX, PassMaker* makerY, |
| 160 |
SkBitmap src, const SkIRect& originalSrcBounds, |
| 161 |
const SkIRect& originalDstBounds, |
| 162 |
SkArenaAlloc* alloc) { |
| 163 |
static constexpr int N = sizeof(T) / sizeof(uint8_t); |
| 164 |
static_assert(N*sizeof(uint8_t) == sizeof(T), "N must be the the size of T in bytes."); |
| 165 |
|
| 166 |
SkIRect srcBounds = originalSrcBounds; |
| 167 |
SkIRect dstBounds = originalDstBounds; |
| 168 |
if (makerX->window() > 1) { |
| 169 |
// Inflate the dst by the window required for the Y pass so that the X pass can prepare |
| 170 |
// it. The Y pass will be offset to only write to the original rows in dstBounds, but |
| 171 |
// its window will access these extra rows calculated by the X pass. The SpecialImage |
| 172 |
// factory will then subset the bitmap so it appears to match 'originalDstBounds' |
| 173 |
// tightly. We make one slightly larger image to hold this extra data instead of two |
| 174 |
// separate images sized exactly to each pass because the CPU blur can write in place. |
| 175 |
dstBounds.outset(0, SkBlurEngine::SigmaToRadius(makerY->sigma())); |
| 176 |
} |
| 177 |
|
| 178 |
SkBitmap dst; |
| 179 |
const SkIPoint dstOrigin = dstBounds.topLeft(); |
| 180 |
if (!dst.tryAllocPixels(src.info().makeWH(dstBounds.width(), dstBounds.height()))) { |
| 181 |
return nullptr; |
| 182 |
} |
| 183 |
dst.eraseColor(SK_ColorTRANSPARENT); |
| 184 |
|
| 185 |
auto buffer = alloc->makeBytesAlignedTo(std::max(makerX->bufferSizeBytes(), |
| 186 |
makerY->bufferSizeBytes()), |
| 187 |
alignof(skvx::Vec<N, uint32_t>)); |
| 188 |
|
| 189 |
// Basic Plan: The three cases to handle |
| 190 |
// * Horizontal and Vertical - blur horizontally while copying values from the source to |
| 191 |
// the destination. Then, do an in-place vertical blur. |
| 192 |
// * Horizontal only - blur horizontally copying values from the source to the destination. |
| 193 |
// * Vertical only - blur vertically copying values from the source to the destination. |
| 194 |
|
| 195 |
// Initialize these assuming the Y-only case |
| 196 |
int loopStart = std::max(srcBounds.left(), dstBounds.left()); |
| 197 |
int loopEnd = std::min(srcBounds.right(), dstBounds.right()); |
| 198 |
int dstYOffset = 0; |
| 199 |
|
| 200 |
if (makerX->window() > 1) { |
| 201 |
// First an X-only blur from src into dst, including the extra rows that will become |
| 202 |
// input for the second Y pass, which will then be performed in place. |
| 203 |
loopStart = std::max(srcBounds.top(), dstBounds.top()); |
| 204 |
loopEnd = std::min(srcBounds.bottom(), dstBounds.bottom()); |
| 205 |
|
| 206 |
auto srcAddr = reinterpret_cast<T*>(src.getAddr(0, loopStart - srcBounds.top())); |
| 207 |
auto dstAddr = reinterpret_cast<T*>(dst.getAddr(0, loopStart - dstBounds.top())); |
| 208 |
|
| 209 |
// Iterate over each row to calculate 1D blur along X. |
| 210 |
Pass* pass = makerX->makePass(buffer, alloc); |
| 211 |
for (int y = loopStart; y < loopEnd; ++y) { |
| 212 |
pass->blur<T>(srcBounds.left() - dstBounds.left(), |
| 213 |
srcBounds.right() - dstBounds.left(), |
| 214 |
dstBounds.width(), |
| 215 |
srcAddr, 1, |
| 216 |
dstAddr, 1); |
| 217 |
srcAddr += src.rowBytesAsPixels(); |
| 218 |
dstAddr += dst.rowBytesAsPixels(); |
| 219 |
} |
| 220 |
|
| 221 |
// Set up the Y pass to blur from the full dst into the non-outset portion of dst |
| 222 |
src = dst; |
| 223 |
loopStart = originalDstBounds.left(); |
| 224 |
loopEnd = originalDstBounds.right(); |
| 225 |
// The new 'dst' is equal to dst.extractSubset(originalDstBounds.offset(-dstOrigin)), |
| 226 |
// but by construction only the Y offset has an interesting value so this is a little |
| 227 |
// more efficient. |
| 228 |
dstYOffset = originalDstBounds.top() - dstBounds.top(); |
| 229 |
|
| 230 |
srcBounds = dstBounds; |
| 231 |
dstBounds = originalDstBounds; |
| 232 |
} |
| 233 |
|
| 234 |
// Iterate over each column to calculate 1D blur along Y. This is either blurring from src |
| 235 |
// into dst for a 1D blur; or it's blurring from dst into dst for the second pass of a 2D |
| 236 |
// blur. |
| 237 |
if (makerY->window() > 1) { |
| 238 |
auto srcAddr = reinterpret_cast<T*>(src.getAddr(loopStart - srcBounds.left(), 0)); |
| 239 |
auto dstAddr = reinterpret_cast<T*>(dst.getAddr(loopStart - dstBounds.left(), dstYOffset)); |
| 240 |
|
| 241 |
Pass* pass = makerY->makePass(buffer, alloc); |
| 242 |
for (int x = loopStart; x < loopEnd; ++x) { |
| 243 |
pass->blur<T>(srcBounds.top() - dstBounds.top(), |
| 244 |
srcBounds.bottom() - dstBounds.top(), |
| 245 |
dstBounds.height(), |
| 246 |
srcAddr, src.rowBytesAsPixels(), |
| 247 |
dstAddr, dst.rowBytesAsPixels()); |
| 248 |
srcAddr += 1; |
| 249 |
dstAddr += 1; |
| 250 |
} |
| 251 |
} |
| 252 |
|
| 253 |
#if defined(SK_AVOID_SLOW_RASTER_PIPELINE_BLURS) |
| 254 |
// When avoiding the shader-based algorithm, handle the box identity case. |
| 255 |
if (makerX->window() == 1 && makerY->window() == 1) { |
| 256 |
dst.writePixels(src.pixmap(), |
| 257 |
srcBounds.left() - dstBounds.left(), |
| 258 |
srcBounds.top() - dstBounds.top()); |
| 259 |
} |
| 260 |
#endif //SK_AVOID_SLOW_RASTER_PIPELINE_BLURS |
| 261 |
|
| 262 |
dstBounds = originalDstBounds.makeOffset(-dstOrigin); // Make relative to dst's pixels |
| 263 |
return SkSpecialImages::MakeFromRaster(dstBounds, dst, SkSurfaceProps{}); |
| 264 |
} |
| 265 |
|
| 266 |
// Implement a scanline processor for a true 1D Gaussian kernel. |
| 267 |
// T is type of the pixel format for the color type. |
| 268 |
// This should only be used for 8bit color channels. |
| 269 |
template <typename T> |
| 270 |
class GaussianPass final : public Pass { |
| 271 |
public: |
| 272 |
static constexpr int N = sizeof(T) / sizeof(uint8_t); |
| 273 |
static_assert(N*sizeof(uint8_t) == sizeof(T), "N must be the the size of T in bytes."); |
| 274 |
|
| 275 |
static constexpr float kMaxSigma = 2.f; |
| 276 |
|
| 277 |
static PassMaker* MakeMaker(float sigma, SkArenaAlloc* alloc) { |
| 278 |
if (sigma >= kMaxSigma) { return nullptr; } |
| 279 |
|
| 280 |
class Maker : public PassMaker { |
| 281 |
public: |
| 282 |
explicit Maker(float sigma) |
| 283 |
: PassMaker{2 * SkBlurEngine::SigmaToRadius(sigma) + 1, sigma} {} |
| 284 |
Pass* makePass(void* buffer, SkArenaAlloc* alloc) const override { |
| 285 |
return GaussianPass::Make(this->sigma(), buffer, alloc); |
| 286 |
} |
| 287 |
size_t bufferSizeBytes() const override { |
| 288 |
// Data is skvx::Vec<N, float>[window] + float[window] |
| 289 |
return this->window() * (sizeof(skvx::Vec<N, float>) + sizeof(float)); |
| 290 |
|
| 291 |
} |
| 292 |
}; |
| 293 |
|
| 294 |
return alloc->make<Maker>(sigma); |
| 295 |
} |
| 296 |
|
| 297 |
static GaussianPass* Make(float sigma, void* buffers, SkArenaAlloc* alloc) { |
| 298 |
int radius = SkBlurEngine::SigmaToRadius(sigma); |
| 299 |
size_t kernelWidth = 2*radius + 1; |
| 300 |
|
| 301 |
skvx::Vec<N, float>* srcBuffer = static_cast<skvx::Vec<N, float>*>(buffers); |
| 302 |
|
| 303 |
float* kernelValues = reinterpret_cast<float*>(srcBuffer + kernelWidth); |
| 304 |
SkShaderBlurAlgorithm::Compute1DBlurKernel(sigma, radius, {kernelValues, kernelWidth}); |
| 305 |
|
| 306 |
return alloc->make<GaussianPass>(radius, kernelValues, srcBuffer); |
| 307 |
} |
| 308 |
|
| 309 |
|
| 310 |
GaussianPass(int radius, float* kernel, skvx::Vec<N, float>* srcBuffer) |
| 311 |
: Pass(radius), |
| 312 |
fWindow(2 * radius + 1), |
| 313 |
fKernel(kernel), |
| 314 |
fSrcBuffer(srcBuffer), |
| 315 |
fSrcBufferBase(0) {} |
| 316 |
|
| 317 |
private: |
| 318 |
void startBlur() override { |
| 319 |
// Zero out the source buffer to ensure a clean state. |
| 320 |
sk_bzero(fSrcBuffer, fWindow * sizeof(skvx::Vec<N, float>)); |
| 321 |
// Reset the circular buffer's starting position. |
| 322 |
fSrcBufferBase = 0; |
| 323 |
} |
| 324 |
|
| 325 |
void blurSegment(int n, const void* src, int srcStride, void* dst, int dstStride) override { |
| 326 |
const T* srcPtr = reinterpret_cast<const T*>(src); |
| 327 |
T* dstPtr = reinterpret_cast<T*>(dst); |
| 328 |
|
| 329 |
// Load the state from the last run. |
| 330 |
int base = fSrcBufferBase; |
| 331 |
|
| 332 |
auto convolve = [this](int srcBase) { |
| 333 |
skvx::Vec<N, float> sum = 0.f; |
| 334 |
for (int i = 0; i < fWindow; ++i) { |
| 335 |
int s = (i + srcBase) % fWindow; |
| 336 |
sum += fSrcBuffer[s] * fKernel[i]; |
| 337 |
} |
| 338 |
return skvx::cast<uint8_t>(skvx::pin(sum * 255.f + 0.5f, |
| 339 |
skvx::Vec<N, float>(0.f), |
| 340 |
skvx::Vec<N, float>(255.f))); |
| 341 |
}; |
| 342 |
|
| 343 |
while (n-- > 0) { |
| 344 |
skvx::Vec<N, float> leadingEdge = srcPtr |
| 345 |
? skvx::cast<float>(skvx::Vec<N, uint8_t>::Load(srcPtr)) * (1 / 255.0f) |
| 346 |
: skvx::Vec<N, float>(0.f); |
| 347 |
|
| 348 |
// Load the new leading edge into the circular buffer. |
| 349 |
fSrcBuffer[(base + fWindow - 1) % fWindow] = leadingEdge; |
| 350 |
|
| 351 |
// Perform the convolution and store the result. |
| 352 |
if (dstPtr) { |
| 353 |
convolve(base).store(dstPtr); |
| 354 |
dstPtr += dstStride; |
| 355 |
} |
| 356 |
|
| 357 |
// Advance the source pointer (if it exists) and the circular buffer base. |
| 358 |
if (srcPtr) { |
| 359 |
srcPtr += srcStride; |
| 360 |
} |
| 361 |
base = (base + 1) % fWindow; |
| 362 |
} |
| 363 |
|
| 364 |
fSrcBufferBase = base; |
| 365 |
} |
| 366 |
|
| 367 |
const int fWindow; |
| 368 |
float* fKernel; |
| 369 |
skvx::Vec<N, float>* fSrcBuffer; |
| 370 |
int fSrcBufferBase; |
| 371 |
}; |
| 372 |
|
| 373 |
|
| 374 |
// Implement a scanline processor that uses a three-box filter to approximate a Gaussian blur. |
| 375 |
// The ThreeBoxApproxPass is limit to processing sigmas < 135. |
| 376 |
class ThreeBoxApproxPass final : public Pass { |
| 377 |
public: |
| 378 |
// NB 136 is the largest sigma that will not cause a buffer full of 255 mask values to overflow |
| 379 |
// using the Gauss filter. It also limits the size of buffers used hold intermediate values. |
| 380 |
// Explanation of maximums: |
| 381 |
// sum0 = window * 255 |
| 382 |
// sum1 = window * sum0 -> window * window * 255 |
| 383 |
// sum2 = window * sum1 -> window * window * window * 255 -> window^3 * 255 |
| 384 |
// |
| 385 |
// The value window^3 * 255 must fit in a uint32_t. So, |
| 386 |
// window^3 < 2^32. window = 255. |
| 387 |
// |
| 388 |
// window = floor(sigma * 3 * sqrt(2 * kPi) / 4 + 0.5) |
| 389 |
// For window <= 255, the largest value for sigma is 136. |
| 390 |
static PassMaker* MakeMaker(float sigma, SkArenaAlloc* alloc) { |
| 391 |
SkASSERT(0 <= sigma); |
| 392 |
int window = SkBlurEngine::BoxBlurWindow(sigma); |
| 393 |
if (255 <= window) { |
| 394 |
return nullptr; |
| 395 |
} |
| 396 |
|
| 397 |
class Maker : public PassMaker { |
| 398 |
public: |
| 399 |
explicit Maker(int window, float sigma) : PassMaker{window, sigma} {} |
| 400 |
Pass* makePass(void* buffer, SkArenaAlloc* alloc) const override { |
| 401 |
return ThreeBoxApproxPass::Make(this->window(), buffer, alloc); |
| 402 |
} |
| 403 |
|
| 404 |
size_t bufferSizeBytes() const override { |
| 405 |
int window = this->window(); |
| 406 |
size_t onePassSize = window - 1; |
| 407 |
// If the window is odd, then there is an obvious middle element. For even sizes |
| 408 |
// 2 passes are shifted, and the last pass has an extra element. Like this: |
| 409 |
// S |
| 410 |
// aaaAaa |
| 411 |
// bbBbbb |
| 412 |
// cccCccc |
| 413 |
// D |
| 414 |
size_t bufferCount = (window & 1) == 1 ? 3 * onePassSize : 3 * onePassSize + 1; |
| 415 |
return bufferCount * sizeof(skvx::Vec<4, uint32_t>); |
| 416 |
} |
| 417 |
}; |
| 418 |
|
| 419 |
return alloc->make<Maker>(window, sigma); |
| 420 |
} |
| 421 |
|
| 422 |
static ThreeBoxApproxPass* Make(int window, void* buffers, SkArenaAlloc* alloc) { |
| 423 |
// We don't need to store the trailing edge pixel in the buffer; |
| 424 |
int passSize = window - 1; |
| 425 |
skvx::Vec<4, uint32_t>* buffer0 = static_cast<skvx::Vec<4, uint32_t>*>(buffers); |
| 426 |
skvx::Vec<4, uint32_t>* buffer1 = buffer0 + passSize; |
| 427 |
skvx::Vec<4, uint32_t>* buffer2 = buffer1 + passSize; |
| 428 |
// If the window is odd just one buffer is needed, but if it's even, then there is one |
| 429 |
// more element on that pass. |
| 430 |
skvx::Vec<4, uint32_t>* buffersEnd = buffer2 + ((window & 1) ? passSize : passSize + 1); |
| 431 |
|
| 432 |
// Calculating the border is tricky. The border is the distance in pixels between the first |
| 433 |
// dst pixel and the first src pixel (or the last src pixel and the last dst pixel). |
| 434 |
// I will go through the odd case which is simpler, and then through the even case. Given a |
| 435 |
// stack of filters seven wide for the odd case of three passes. |
| 436 |
// |
| 437 |
// S |
| 438 |
// aaaAaaa |
| 439 |
// bbbBbbb |
| 440 |
// cccCccc |
| 441 |
// D |
| 442 |
// |
| 443 |
// The furthest changed pixel is when the filters are in the following configuration. |
| 444 |
// |
| 445 |
// S |
| 446 |
// aaaAaaa |
| 447 |
// bbbBbbb |
| 448 |
// cccCccc |
| 449 |
// D |
| 450 |
// |
| 451 |
// The A pixel is calculated using the value S, the B uses A, and the C uses B, and |
| 452 |
// finally D is C. So, with a window size of seven the border is nine. In the odd case, the |
| 453 |
// border is 3*((window - 1)/2). |
| 454 |
// |
| 455 |
// For even cases the filter stack is more complicated. The spec specifies two passes |
| 456 |
// of even filters and a final pass of odd filters. A stack for a width of six looks like |
| 457 |
// this. |
| 458 |
// |
| 459 |
// S |
| 460 |
// aaaAaa |
| 461 |
// bbBbbb |
| 462 |
// cccCccc |
| 463 |
// D |
| 464 |
// |
| 465 |
// The furthest pixel looks like this. |
| 466 |
// |
| 467 |
// S |
| 468 |
// aaaAaa |
| 469 |
// bbBbbb |
| 470 |
// cccCccc |
| 471 |
// D |
| 472 |
// |
| 473 |
// For a window of six, the border value is eight. In the even case the border is 3 * |
| 474 |
// (window/2) - 1. |
| 475 |
int border = (window & 1) == 1 ? 3 * ((window - 1) / 2) : 3 * (window / 2) - 1; |
| 476 |
|
| 477 |
// If the window is odd then the divisor is just window ^ 3 otherwise, |
| 478 |
// it is window * window * (window + 1) = window ^ 3 + window ^ 2; |
| 479 |
int window2 = window * window; |
| 480 |
int window3 = window2 * window; |
| 481 |
int divisor = (window & 1) == 1 ? window3 : window3 + window2; |
| 482 |
return alloc->make<ThreeBoxApproxPass>(buffer0, buffer1, buffer2, |
| 483 |
buffersEnd, border, divisor); |
| 484 |
} |
| 485 |
|
| 486 |
ThreeBoxApproxPass(skvx::Vec<4, uint32_t>* buffer0, |
| 487 |
skvx::Vec<4, uint32_t>* buffer1, |
| 488 |
skvx::Vec<4, uint32_t>* buffer2, |
| 489 |
skvx::Vec<4, uint32_t>* buffersEnd, |
| 490 |
int border, |
| 491 |
int divisor) |
| 492 |
: Pass{border} |
| 493 |
, fBuffer0{buffer0} |
| 494 |
, fBuffer1{buffer1} |
| 495 |
, fBuffer2{buffer2} |
| 496 |
, fBuffersEnd{buffersEnd} |
| 497 |
, fDivider(divisor) {} |
| 498 |
|
| 499 |
private: |
| 500 |
void startBlur() override { |
| 501 |
skvx::Vec<4, uint32_t> zero = {0u, 0u, 0u, 0u}; |
| 502 |
zero.store(fSum0); |
| 503 |
zero.store(fSum1); |
| 504 |
auto half = fDivider.half(); |
| 505 |
skvx::Vec<4, uint32_t>{half, half, half, half}.store(fSum2); |
| 506 |
sk_bzero(fBuffer0, (fBuffersEnd - fBuffer0) * sizeof(skvx::Vec<4, uint32_t>)); |
| 507 |
|
| 508 |
fBuffer0Cursor = fBuffer0; |
| 509 |
fBuffer1Cursor = fBuffer1; |
| 510 |
fBuffer2Cursor = fBuffer2; |
| 511 |
} |
| 512 |
|
| 513 |
// GaussPass implements the common three pass box filter approximation of Gaussian blur, |
| 514 |
// but combines all three passes into a single pass. This approach is facilitated by three |
| 515 |
// circular buffers the width of the window which track values for trailing edges of each of |
| 516 |
// the three passes. This allows the algorithm to use more precision in the calculation |
| 517 |
// because the values are not rounded each pass. And this implementation also avoids a trap |
| 518 |
// that's easy to fall into resulting in blending in too many zeroes near the edge. |
| 519 |
// |
| 520 |
// In general, a window sum has the form: |
| 521 |
// sum_n+1 = sum_n + leading_edge - trailing_edge. |
| 522 |
// If instead we do the subtraction at the end of the previous iteration, we can just |
| 523 |
// calculate the sums instead of having to do the subtractions too. |
| 524 |
// |
| 525 |
// In previous iteration: |
| 526 |
// sum_n+1 = sum_n - trailing_edge. |
| 527 |
// |
| 528 |
// In this iteration: |
| 529 |
// sum_n+1 = sum_n + leading_edge. |
| 530 |
// |
| 531 |
// Now we can stack all three sums and do them at once. Sum0 gets its leading edge from the |
| 532 |
// actual data. Sum1's leading edge is just Sum0, and Sum2's leading edge is Sum1. So, doing the |
| 533 |
// three passes at the same time has the form: |
| 534 |
// |
| 535 |
// sum0_n+1 = sum0_n + leading edge |
| 536 |
// sum1_n+1 = sum1_n + sum0_n+1 |
| 537 |
// sum2_n+1 = sum2_n + sum1_n+1 |
| 538 |
// |
| 539 |
// sum2_n+1 / window^3 is the new value of the destination pixel. |
| 540 |
// |
| 541 |
// Reduce the sums by the trailing edges which were stored in the circular buffers for the |
| 542 |
// next go around. This is the case for odd sized windows, even windows the the third |
| 543 |
// circular buffer is one larger then the first two circular buffers. |
| 544 |
// |
| 545 |
// sum2_n+2 = sum2_n+1 - buffer2[i]; |
| 546 |
// buffer2[i] = sum1; |
| 547 |
// sum1_n+2 = sum1_n+1 - buffer1[i]; |
| 548 |
// buffer1[i] = sum0; |
| 549 |
// sum0_n+2 = sum0_n+1 - buffer0[i]; |
| 550 |
// buffer0[i] = leading edge |
| 551 |
void blurSegment( |
| 552 |
int n, const void* src, int srcStride, void* dst, int dstStride) override { |
| 553 |
const uint32_t* src32 = reinterpret_cast<const uint32_t*>(src); |
| 554 |
uint32_t* dst32 = reinterpret_cast<uint32_t*>(dst); |
| 555 |
#if SK_CPU_LSX_LEVEL >= SK_CPU_LSX_LEVEL_LSX |
| 556 |
skvx::Vec<4, uint32_t>* buffer0Cursor = fBuffer0Cursor; |
| 557 |
skvx::Vec<4, uint32_t>* buffer1Cursor = fBuffer1Cursor; |
| 558 |
skvx::Vec<4, uint32_t>* buffer2Cursor = fBuffer2Cursor; |
| 559 |
v4u32 sum0 = __lsx_vld(fSum0, 0); // same as skvx::Vec<4, uint32_t>::Load(fSum0); |
| 560 |
v4u32 sum1 = __lsx_vld(fSum1, 0); |
| 561 |
v4u32 sum2 = __lsx_vld(fSum2, 0); |
| 562 |
|
| 563 |
auto processValue = [&](v4u32& vLeadingEdge){ |
| 564 |
sum0 += vLeadingEdge; |
| 565 |
sum1 += sum0; |
| 566 |
sum2 += sum1; |
| 567 |
|
| 568 |
v4u32 divisorFactor = __lsx_vreplgr2vr_w(fDivider.divisorFactor()); |
| 569 |
v4u32 blurred = __lsx_vmuh_w(divisorFactor, sum2); |
| 570 |
|
| 571 |
v4u32 buffer2Value = __lsx_vld(buffer2Cursor, 0); //Not fBuffer0Cursor, out of bounds. |
| 572 |
sum2 -= buffer2Value; |
| 573 |
__lsx_vst(sum1, (void *)buffer2Cursor, 0); |
| 574 |
buffer2Cursor = (buffer2Cursor + 1) < fBuffersEnd ? buffer2Cursor + 1 : fBuffer2; |
| 575 |
v4u32 buffer1Value = __lsx_vld(buffer1Cursor, 0); |
| 576 |
sum1 -= buffer1Value; |
| 577 |
__lsx_vst(sum0, (void *)buffer1Cursor, 0); |
| 578 |
buffer1Cursor = (buffer1Cursor + 1) < fBuffer2 ? buffer1Cursor + 1 : fBuffer1; |
| 579 |
v4u32 buffer0Value = __lsx_vld(buffer0Cursor, 0); |
| 580 |
sum0 -= buffer0Value; |
| 581 |
__lsx_vst(vLeadingEdge, (void *)buffer0Cursor, 0); |
| 582 |
buffer0Cursor = (buffer0Cursor + 1) < fBuffer1 ? buffer0Cursor + 1 : fBuffer0; |
| 583 |
|
| 584 |
v16u8 shuf = {0x0,0x4,0x8,0xc,0x0}; |
| 585 |
v16u8 ret = __lsx_vshuf_b(blurred, blurred, shuf); |
| 586 |
return ret; |
| 587 |
}; |
| 588 |
|
| 589 |
v4u32 zero = __lsx_vldi(0x0); |
| 590 |
if (!src32 && !dst32) { |
| 591 |
while (n --> 0) { |
| 592 |
(void)processValue(zero); |
| 593 |
} |
| 594 |
} else if (src32 && !dst32) { |
| 595 |
while (n --> 0) { |
| 596 |
v4u32 edge = __lsx_vinsgr2vr_w(zero, *src32, 0); |
| 597 |
edge = __lsx_vilvl_b(zero, edge); |
| 598 |
edge = __lsx_vilvl_h(zero, edge); |
| 599 |
(void)processValue(edge); |
| 600 |
src32 += srcStride; |
| 601 |
} |
| 602 |
} else if (!src32 && dst32) { |
| 603 |
while (n --> 0) { |
| 604 |
v4u32 ret = processValue(zero); |
| 605 |
__lsx_vstelm_w(ret, dst32, 0, 0); // 3rd is offset, 4th is idx. |
| 606 |
dst32 += dstStride; |
| 607 |
} |
| 608 |
} else if (src32 && dst32) { |
| 609 |
while (n --> 0) { |
| 610 |
v4u32 edge = __lsx_vinsgr2vr_w(zero, *src32, 0); |
| 611 |
edge = __lsx_vilvl_b(zero, edge); |
| 612 |
edge = __lsx_vilvl_h(zero, edge); |
| 613 |
v4u32 ret = processValue(edge); |
| 614 |
__lsx_vstelm_w(ret, dst32, 0, 0); |
| 615 |
src32 += srcStride; |
| 616 |
dst32 += dstStride; |
| 617 |
} |
| 618 |
} |
| 619 |
|
| 620 |
// Store the state |
| 621 |
fBuffer0Cursor = buffer0Cursor; |
| 622 |
fBuffer1Cursor = buffer1Cursor; |
| 623 |
fBuffer2Cursor = buffer2Cursor; |
| 624 |
|
| 625 |
__lsx_vst(sum0, fSum0, 0); |
| 626 |
__lsx_vst(sum1, fSum1, 0); |
| 627 |
__lsx_vst(sum2, fSum2, 0); |
| 628 |
#else |
| 629 |
skvx::Vec<4, uint32_t>* buffer0Cursor = fBuffer0Cursor; |
| 630 |
skvx::Vec<4, uint32_t>* buffer1Cursor = fBuffer1Cursor; |
| 631 |
skvx::Vec<4, uint32_t>* buffer2Cursor = fBuffer2Cursor; |
| 632 |
skvx::Vec<4, uint32_t> sum0 = skvx::Vec<4, uint32_t>::Load(fSum0); |
| 633 |
skvx::Vec<4, uint32_t> sum1 = skvx::Vec<4, uint32_t>::Load(fSum1); |
| 634 |
skvx::Vec<4, uint32_t> sum2 = skvx::Vec<4, uint32_t>::Load(fSum2); |
| 635 |
|
| 636 |
// Given an expanded input pixel, move the window ahead using the leadingEdge value. |
| 637 |
auto processValue = [&](const skvx::Vec<4, uint32_t>& leadingEdge) { |
| 638 |
sum0 += leadingEdge; |
| 639 |
sum1 += sum0; |
| 640 |
sum2 += sum1; |
| 641 |
|
| 642 |
skvx::Vec<4, uint32_t> blurred = fDivider.divide(sum2); |
| 643 |
|
| 644 |
sum2 -= *buffer2Cursor; |
| 645 |
*buffer2Cursor = sum1; |
| 646 |
buffer2Cursor = (buffer2Cursor + 1) < fBuffersEnd ? buffer2Cursor + 1 : fBuffer2; |
| 647 |
sum1 -= *buffer1Cursor; |
| 648 |
*buffer1Cursor = sum0; |
| 649 |
buffer1Cursor = (buffer1Cursor + 1) < fBuffer2 ? buffer1Cursor + 1 : fBuffer1; |
| 650 |
sum0 -= *buffer0Cursor; |
| 651 |
*buffer0Cursor = leadingEdge; |
| 652 |
buffer0Cursor = (buffer0Cursor + 1) < fBuffer1 ? buffer0Cursor + 1 : fBuffer0; |
| 653 |
|
| 654 |
return skvx::cast<uint8_t>(blurred); |
| 655 |
}; |
| 656 |
|
| 657 |
auto loadEdge = [&](const uint32_t* srcCursor) { |
| 658 |
return skvx::cast<uint32_t>(skvx::Vec<4, uint8_t>::Load(srcCursor)); |
| 659 |
}; |
| 660 |
|
| 661 |
if (!src32 && !dst32) { |
| 662 |
while (n --> 0) { |
| 663 |
(void)processValue(0); |
| 664 |
} |
| 665 |
} else if (src32 && !dst32) { |
| 666 |
while (n --> 0) { |
| 667 |
(void)processValue(loadEdge(src32)); |
| 668 |
src32 += srcStride; |
| 669 |
} |
| 670 |
} else if (!src32 && dst32) { |
| 671 |
while (n --> 0) { |
| 672 |
processValue(0u).store(dst32); |
| 673 |
dst32 += dstStride; |
| 674 |
} |
| 675 |
} else if (src32 && dst32) { |
| 676 |
while (n --> 0) { |
| 677 |
processValue(loadEdge(src32)).store(dst32); |
| 678 |
src32 += srcStride; |
| 679 |
dst32 += dstStride; |
| 680 |
} |
| 681 |
} |
| 682 |
|
| 683 |
// Store the state |
| 684 |
fBuffer0Cursor = buffer0Cursor; |
| 685 |
fBuffer1Cursor = buffer1Cursor; |
| 686 |
fBuffer2Cursor = buffer2Cursor; |
| 687 |
|
| 688 |
sum0.store(fSum0); |
| 689 |
sum1.store(fSum1); |
| 690 |
sum2.store(fSum2); |
| 691 |
#endif |
| 692 |
} |
| 693 |
|
| 694 |
skvx::Vec<4, uint32_t>* const fBuffer0; |
| 695 |
skvx::Vec<4, uint32_t>* const fBuffer1; |
| 696 |
skvx::Vec<4, uint32_t>* const fBuffer2; |
| 697 |
skvx::Vec<4, uint32_t>* const fBuffersEnd; |
| 698 |
const skvx::ScaledDividerU32 fDivider; |
| 699 |
|
| 700 |
// blur state |
| 701 |
char fSum0[sizeof(skvx::Vec<4, uint32_t>)]; |
| 702 |
char fSum1[sizeof(skvx::Vec<4, uint32_t>)]; |
| 703 |
char fSum2[sizeof(skvx::Vec<4, uint32_t>)]; |
| 704 |
skvx::Vec<4, uint32_t>* fBuffer0Cursor; |
| 705 |
skvx::Vec<4, uint32_t>* fBuffer1Cursor; |
| 706 |
skvx::Vec<4, uint32_t>* fBuffer2Cursor; |
| 707 |
}; |
| 708 |
|
| 709 |
// Implement a scanline processor that uses a two-box filter to approximate a Tent filter. |
| 710 |
// The TentPass is limit to processing sigmas < 2183. |
| 711 |
class TentPass final : public Pass { |
| 712 |
public: |
| 713 |
// NB 2183 is the largest sigma that will not cause a buffer full of 255 mask values to overflow |
| 714 |
// using the Tent filter. It also limits the size of buffers used hold intermediate values. |
| 715 |
// Explanation of maximums: |
| 716 |
// sum0 = window * 255 |
| 717 |
// sum1 = window * sum0 -> window * window * 255 |
| 718 |
// |
| 719 |
// The value window^2 * 255 must fit in a uint32_t. So, |
| 720 |
// window^2 < 2^32. window = 4104. |
| 721 |
// |
| 722 |
// window = floor(sigma * 3 * sqrt(2 * kPi) / 4 + 0.5) |
| 723 |
// For window <= 4104, the largest value for sigma is 2183. |
| 724 |
static PassMaker* MakeMaker(float sigma, SkArenaAlloc* alloc) { |
| 725 |
SkASSERT(0 <= sigma); |
| 726 |
int gaussianWindow = SkBlurEngine::BoxBlurWindow(sigma); |
| 727 |
// This is a naive method of using the window size for the Gaussian blur to calculate the |
| 728 |
// window size for the Tent blur. This seems to work well in practice. |
| 729 |
// |
| 730 |
// We can use a single pixel to generate the effective blur area given a window size. For |
| 731 |
// the Gaussian blur this is 3 * window size. For the Tent filter this is 2 * window size. |
| 732 |
int tentWindow = 3 * gaussianWindow / 2; |
| 733 |
if (tentWindow >= 4104) { |
| 734 |
return nullptr; |
| 735 |
} |
| 736 |
|
| 737 |
class Maker : public PassMaker { |
| 738 |
public: |
| 739 |
explicit Maker(int window, float sigma) : PassMaker{window, sigma} {} |
| 740 |
Pass* makePass(void* buffer, SkArenaAlloc* alloc) const override { |
| 741 |
return TentPass::Make(this->window(), buffer, alloc); |
| 742 |
} |
| 743 |
|
| 744 |
size_t bufferSizeBytes() const override { |
| 745 |
size_t onePassSize = this->window() - 1; |
| 746 |
// If the window is odd, then there is an obvious middle element. For even sizes 2 |
| 747 |
// passes are shifted, and the last pass has an extra element. Like this: |
| 748 |
// S |
| 749 |
// aaaAaa |
| 750 |
// bbBbbb |
| 751 |
// D |
| 752 |
size_t bufferCount = 2 * onePassSize; |
| 753 |
return bufferCount * sizeof(skvx::Vec<4, uint32_t>); |
| 754 |
} |
| 755 |
}; |
| 756 |
|
| 757 |
return alloc->make<Maker>(tentWindow, sigma); |
| 758 |
} |
| 759 |
|
| 760 |
static TentPass* Make(int window, void* buffers, SkArenaAlloc* alloc) { |
| 761 |
if (window > 4104) { |
| 762 |
return nullptr; |
| 763 |
} |
| 764 |
|
| 765 |
// We don't need to store the trailing edge pixel in the buffer; |
| 766 |
int passSize = window - 1; |
| 767 |
skvx::Vec<4, uint32_t>* buffer0 = static_cast<skvx::Vec<4, uint32_t>*>(buffers); |
| 768 |
skvx::Vec<4, uint32_t>* buffer1 = buffer0 + passSize; |
| 769 |
skvx::Vec<4, uint32_t>* buffersEnd = buffer1 + passSize; |
| 770 |
|
| 771 |
// Calculating the border is tricky. The border is the distance in pixels between the first |
| 772 |
// dst pixel and the first src pixel (or the last src pixel and the last dst pixel). |
| 773 |
// I will go through the odd case which is simpler, and then through the even case. Given a |
| 774 |
// stack of filters seven wide for the odd case of three passes. |
| 775 |
// |
| 776 |
// S |
| 777 |
// aaaAaaa |
| 778 |
// bbbBbbb |
| 779 |
// D |
| 780 |
// |
| 781 |
// The furthest changed pixel is when the filters are in the following configuration. |
| 782 |
// |
| 783 |
// S |
| 784 |
// aaaAaaa |
| 785 |
// bbbBbbb |
| 786 |
// D |
| 787 |
// |
| 788 |
// The A pixel is calculated using the value S, the B uses A, and the D uses B. |
| 789 |
// So, with a window size of seven the border is nine. In the odd case, the border is |
| 790 |
// window - 1. |
| 791 |
// |
| 792 |
// For even cases the filter stack is more complicated. It uses two passes |
| 793 |
// of even filters offset from each other. A stack for a width of six looks like |
| 794 |
// this. |
| 795 |
// |
| 796 |
// S |
| 797 |
// aaaAaa |
| 798 |
// bbBbbb |
| 799 |
// D |
| 800 |
// |
| 801 |
// The furthest pixel looks like this. |
| 802 |
// |
| 803 |
// S |
| 804 |
// aaaAaa |
| 805 |
// bbBbbb |
| 806 |
// D |
| 807 |
// |
| 808 |
// For a window of six, the border value is 5. In the even case the border is |
| 809 |
// window - 1. |
| 810 |
int border = window - 1; |
| 811 |
|
| 812 |
int divisor = window * window; |
| 813 |
return alloc->make<TentPass>(buffer0, buffer1, buffersEnd, border, divisor); |
| 814 |
} |
| 815 |
|
| 816 |
TentPass(skvx::Vec<4, uint32_t>* buffer0, |
| 817 |
skvx::Vec<4, uint32_t>* buffer1, |
| 818 |
skvx::Vec<4, uint32_t>* buffersEnd, |
| 819 |
int border, |
| 820 |
int divisor) |
| 821 |
: Pass{border} |
| 822 |
, fBuffer0{buffer0} |
| 823 |
, fBuffer1{buffer1} |
| 824 |
, fBuffersEnd{buffersEnd} |
| 825 |
, fDivider(divisor) {} |
| 826 |
|
| 827 |
private: |
| 828 |
void startBlur() override { |
| 829 |
skvx::Vec<4, uint32_t>{0u, 0u, 0u, 0u}.store(fSum0); |
| 830 |
auto half = fDivider.half(); |
| 831 |
skvx::Vec<4, uint32_t>{half, half, half, half}.store(fSum1); |
| 832 |
sk_bzero(fBuffer0, (fBuffersEnd - fBuffer0) * sizeof(skvx::Vec<4, uint32_t>)); |
| 833 |
|
| 834 |
fBuffer0Cursor = fBuffer0; |
| 835 |
fBuffer1Cursor = fBuffer1; |
| 836 |
} |
| 837 |
|
| 838 |
// TentPass implements the common two pass box filter approximation of Tent filter, |
| 839 |
// but combines all both passes into a single pass. This approach is facilitated by two |
| 840 |
// circular buffers the width of the window which track values for trailing edges of each of |
| 841 |
// both passes. This allows the algorithm to use more precision in the calculation |
| 842 |
// because the values are not rounded each pass. And this implementation also avoids a trap |
| 843 |
// that's easy to fall into resulting in blending in too many zeroes near the edge. |
| 844 |
// |
| 845 |
// In general, a window sum has the form: |
| 846 |
// sum_n+1 = sum_n + leading_edge - trailing_edge. |
| 847 |
// If instead we do the subtraction at the end of the previous iteration, we can just |
| 848 |
// calculate the sums instead of having to do the subtractions too. |
| 849 |
// |
| 850 |
// In previous iteration: |
| 851 |
// sum_n+1 = sum_n - trailing_edge. |
| 852 |
// |
| 853 |
// In this iteration: |
| 854 |
// sum_n+1 = sum_n + leading_edge. |
| 855 |
// |
| 856 |
// Now we can stack all three sums and do them at once. Sum0 gets its leading edge from the |
| 857 |
// actual data. Sum1's leading edge is just Sum0, and Sum2's leading edge is Sum1. So, doing the |
| 858 |
// three passes at the same time has the form: |
| 859 |
// |
| 860 |
// sum0_n+1 = sum0_n + leading edge |
| 861 |
// sum1_n+1 = sum1_n + sum0_n+1 |
| 862 |
// |
| 863 |
// sum1_n+1 / window^2 is the new value of the destination pixel. |
| 864 |
// |
| 865 |
// Reduce the sums by the trailing edges which were stored in the circular buffers for the |
| 866 |
// next go around. |
| 867 |
// |
| 868 |
// sum1_n+2 = sum1_n+1 - buffer1[i]; |
| 869 |
// buffer1[i] = sum0; |
| 870 |
// sum0_n+2 = sum0_n+1 - buffer0[i]; |
| 871 |
// buffer0[i] = leading edge |
| 872 |
void blurSegment( |
| 873 |
int n, const void* src, int srcStride, void* dst, int dstStride) override { |
| 874 |
const uint32_t* src32 = reinterpret_cast<const uint32_t*>(src); |
| 875 |
uint32_t* dst32 = reinterpret_cast<uint32_t*>(dst); |
| 876 |
skvx::Vec<4, uint32_t>* buffer0Cursor = fBuffer0Cursor; |
| 877 |
skvx::Vec<4, uint32_t>* buffer1Cursor = fBuffer1Cursor; |
| 878 |
skvx::Vec<4, uint32_t> sum0 = skvx::Vec<4, uint32_t>::Load(fSum0); |
| 879 |
skvx::Vec<4, uint32_t> sum1 = skvx::Vec<4, uint32_t>::Load(fSum1); |
| 880 |
|
| 881 |
// Given an expanded input pixel, move the window ahead using the leadingEdge value. |
| 882 |
auto processValue = [&](const skvx::Vec<4, uint32_t>& leadingEdge) { |
| 883 |
sum0 += leadingEdge; |
| 884 |
sum1 += sum0; |
| 885 |
|
| 886 |
skvx::Vec<4, uint32_t> blurred = fDivider.divide(sum1); |
| 887 |
|
| 888 |
sum1 -= *buffer1Cursor; |
| 889 |
*buffer1Cursor = sum0; |
| 890 |
buffer1Cursor = (buffer1Cursor + 1) < fBuffersEnd ? buffer1Cursor + 1 : fBuffer1; |
| 891 |
sum0 -= *buffer0Cursor; |
| 892 |
*buffer0Cursor = leadingEdge; |
| 893 |
buffer0Cursor = (buffer0Cursor + 1) < fBuffer1 ? buffer0Cursor + 1 : fBuffer0; |
| 894 |
|
| 895 |
return skvx::cast<uint8_t>(blurred); |
| 896 |
}; |
| 897 |
|
| 898 |
auto loadEdge = [&](const uint32_t* srcCursor) { |
| 899 |
return skvx::cast<uint32_t>(skvx::Vec<4, uint8_t>::Load(srcCursor)); |
| 900 |
}; |
| 901 |
|
| 902 |
if (!src32 && !dst32) { |
| 903 |
while (n --> 0) { |
| 904 |
(void)processValue(0); |
| 905 |
} |
| 906 |
} else if (src32 && !dst32) { |
| 907 |
while (n --> 0) { |
| 908 |
(void)processValue(loadEdge(src32)); |
| 909 |
src32 += srcStride; |
| 910 |
} |
| 911 |
} else if (!src32 && dst32) { |
| 912 |
while (n --> 0) { |
| 913 |
processValue(0u).store(dst32); |
| 914 |
dst32 += dstStride; |
| 915 |
} |
| 916 |
} else if (src32 && dst32) { |
| 917 |
while (n --> 0) { |
| 918 |
processValue(loadEdge(src32)).store(dst32); |
| 919 |
src32 += srcStride; |
| 920 |
dst32 += dstStride; |
| 921 |
} |
| 922 |
} |
| 923 |
|
| 924 |
// Store the state |
| 925 |
fBuffer0Cursor = buffer0Cursor; |
| 926 |
fBuffer1Cursor = buffer1Cursor; |
| 927 |
sum0.store(fSum0); |
| 928 |
sum1.store(fSum1); |
| 929 |
} |
| 930 |
|
| 931 |
skvx::Vec<4, uint32_t>* const fBuffer0; |
| 932 |
skvx::Vec<4, uint32_t>* const fBuffer1; |
| 933 |
skvx::Vec<4, uint32_t>* const fBuffersEnd; |
| 934 |
const skvx::ScaledDividerU32 fDivider; |
| 935 |
|
| 936 |
// blur state |
| 937 |
char fSum0[sizeof(skvx::Vec<4, uint32_t>)]; |
| 938 |
char fSum1[sizeof(skvx::Vec<4, uint32_t>)]; |
| 939 |
skvx::Vec<4, uint32_t>* fBuffer0Cursor; |
| 940 |
skvx::Vec<4, uint32_t>* fBuffer1Cursor; |
| 941 |
}; |
| 942 |
|
| 943 |
class A8Pass final : public Pass { |
| 944 |
public: |
| 945 |
static PassMaker* MakeMaker(float sigma, SkArenaAlloc* alloc) { |
| 946 |
SkASSERT(0 <= sigma); |
| 947 |
int possibleWindow = static_cast<int>(floor(sigma * 3 * sqrt(2 * SK_DoublePI) / 4 + 0.5)); |
| 948 |
int window = std::max(1, possibleWindow); |
| 949 |
|
| 950 |
class Maker : public PassMaker { |
| 951 |
public: |
| 952 |
explicit Maker(int window, float sigma) : PassMaker{window, sigma} {} |
| 953 |
Pass* makePass(void* buffer, SkArenaAlloc* alloc) const override { |
| 954 |
return A8Pass::Make(this->window(), buffer, alloc); |
| 955 |
} |
| 956 |
|
| 957 |
size_t bufferSizeBytes() const override { |
| 958 |
int window = this->window(); |
| 959 |
size_t pass0Size = window - 1; |
| 960 |
size_t pass1Size = window - 1; |
| 961 |
size_t pass2Size = (window & 1) == 1 ? window - 1 : window; |
| 962 |
return (pass0Size + pass1Size + pass2Size) * sizeof(uint32_t); |
| 963 |
} |
| 964 |
}; |
| 965 |
|
| 966 |
return alloc->make<Maker>(window, sigma); |
| 967 |
} |
| 968 |
|
| 969 |
static A8Pass* Make(int window, void* buffers, SkArenaAlloc* alloc) { |
| 970 |
size_t pass0Size = window - 1; |
| 971 |
size_t pass1Size = window - 1; |
| 972 |
size_t pass2Size = (window & 1) == 1 ? window - 1 : window; |
| 973 |
uint32_t* buffer0, *buffer0End, *buffer1, *buffer1End, *buffer2, *buffer2End; |
| 974 |
buffer0 = static_cast<uint32_t*>(buffers); |
| 975 |
buffer0End = buffer1 = buffer0 + pass0Size; |
| 976 |
buffer1End = buffer2 = buffer1 + pass1Size; |
| 977 |
buffer2End = buffer2 + pass2Size; |
| 978 |
|
| 979 |
// Calculating the border is tricky. The border is the distance in pixels between the first |
| 980 |
// dst pixel and the first src pixel (or the last src pixel and the last dst pixel). |
| 981 |
// I will go through the odd case which is simpler, and then through the even case. Given a |
| 982 |
// stack of filters seven wide for the odd case of three passes. |
| 983 |
// |
| 984 |
// S |
| 985 |
// aaaAaaa |
| 986 |
// bbbBbbb |
| 987 |
// cccCccc |
| 988 |
// D |
| 989 |
// |
| 990 |
// The furthest changed pixel is when the filters are in the following configuration. |
| 991 |
// |
| 992 |
// S |
| 993 |
// aaaAaaa |
| 994 |
// bbbBbbb |
| 995 |
// cccCccc |
| 996 |
// D |
| 997 |
// |
| 998 |
// The A pixel is calculated using the value S, the B uses A, and the C uses B, and |
| 999 |
// finally D is C. So, with a window size of seven the border is nine. In the odd case, the |
| 1000 |
// border is 3*((window - 1)/2). |
| 1001 |
// |
| 1002 |
// For even cases the filter stack is more complicated. The spec specifies two passes |
| 1003 |
// of even filters and a final pass of odd filters. A stack for a width of six looks like |
| 1004 |
// this. |
| 1005 |
// |
| 1006 |
// S |
| 1007 |
// aaaAaa |
| 1008 |
// bbBbbb |
| 1009 |
// cccCccc |
| 1010 |
// D |
| 1011 |
// |
| 1012 |
// The furthest pixel looks like this. |
| 1013 |
// |
| 1014 |
// S |
| 1015 |
// aaaAaa |
| 1016 |
// bbBbbb |
| 1017 |
// cccCccc |
| 1018 |
// D |
| 1019 |
// |
| 1020 |
// For a window of six, the border value is eight. In the even case the border is 3 * |
| 1021 |
// (window/2) - 1. |
| 1022 |
int border = (window & 1) == 1 ? 3 * ((window - 1) / 2) : 3 * (window / 2) - 1; |
| 1023 |
|
| 1024 |
// If the window is odd then the divisor is just window ^ 3 otherwise, |
| 1025 |
// it is window * window * (window + 1) = window ^ 2 + window ^ 3; |
| 1026 |
auto window2 = window * window; |
| 1027 |
auto window3 = window2 * window; |
| 1028 |
auto divisor = (window & 1) == 1 ? window3 : window3 + window2; |
| 1029 |
|
| 1030 |
uint64_t weight = static_cast<uint64_t>(round(1.0 / divisor * (1ull << 32))); |
| 1031 |
|
| 1032 |
return alloc->make<A8Pass>(weight, buffer0, buffer0End, buffer1, buffer1End, |
| 1033 |
buffer2, buffer2End, border); |
| 1034 |
} |
| 1035 |
|
| 1036 |
A8Pass(uint64_t weight, |
| 1037 |
uint32_t* buffer0, uint32_t* buffer0End, |
| 1038 |
uint32_t* buffer1, uint32_t* buffer1End, |
| 1039 |
uint32_t* buffer2, uint32_t* buffer2End, |
| 1040 |
int border) |
| 1041 |
: Pass{border} |
| 1042 |
, fWeight(weight) |
| 1043 |
, fBuffer0{buffer0} |
| 1044 |
, fBuffer0End{buffer0End} |
| 1045 |
, fBuffer1{buffer1} |
| 1046 |
, fBuffer1End{buffer1End} |
| 1047 |
, fBuffer2{buffer2} |
| 1048 |
, fBuffer2End{buffer2End} {} |
| 1049 |
|
| 1050 |
private: |
| 1051 |
void startBlur() override { |
| 1052 |
fSum0 = 0; |
| 1053 |
fSum1 = 0; |
| 1054 |
fSum2 = 0; |
| 1055 |
|
| 1056 |
sk_bzero(fBuffer0, (fBuffer2End - fBuffer0) * sizeof(*fBuffer0)); |
| 1057 |
|
| 1058 |
fBuffer0Cursor = fBuffer0; |
| 1059 |
fBuffer1Cursor = fBuffer1; |
| 1060 |
fBuffer2Cursor = fBuffer2; |
| 1061 |
} |
| 1062 |
|
| 1063 |
void blurSegment( |
| 1064 |
int n, const void* src, int srcStride, void* dst, int dstStride) override { |
| 1065 |
const uint8_t* src8 = reinterpret_cast<const uint8_t*>(src); |
| 1066 |
uint8_t* dst8 = reinterpret_cast<uint8_t*>(dst); |
| 1067 |
// If n is zero or negative, there's nothing to do. |
| 1068 |
if (n <= 0) { |
| 1069 |
return; |
| 1070 |
} |
| 1071 |
|
| 1072 |
auto buffer0Cursor = fBuffer0Cursor; |
| 1073 |
auto buffer1Cursor = fBuffer1Cursor; |
| 1074 |
auto buffer2Cursor = fBuffer2Cursor; |
| 1075 |
uint32_t sum0 = fSum0; |
| 1076 |
uint32_t sum1 = fSum1; |
| 1077 |
uint32_t sum2 = fSum2; |
| 1078 |
|
| 1079 |
auto processValue = [&](const uint32_t leadingEdge) { |
| 1080 |
sum0 += leadingEdge; sum1 += sum0; sum2 += sum1; |
| 1081 |
|
| 1082 |
const uint8_t blurred = this->finalScale(sum2); |
| 1083 |
|
| 1084 |
sum2 -= *buffer2Cursor; *buffer2Cursor = sum1; |
| 1085 |
buffer2Cursor = (buffer2Cursor + 1) < fBuffer2End ? buffer2Cursor + 1 : fBuffer2; |
| 1086 |
sum1 -= *buffer1Cursor; *buffer1Cursor = sum0; |
| 1087 |
buffer1Cursor = (buffer1Cursor + 1) < fBuffer1End ? buffer1Cursor + 1 : fBuffer1; |
| 1088 |
sum0 -= *buffer0Cursor; *buffer0Cursor = leadingEdge; |
| 1089 |
buffer0Cursor = (buffer0Cursor + 1) < fBuffer0End ? buffer0Cursor + 1 : fBuffer0; |
| 1090 |
|
| 1091 |
return blurred; |
| 1092 |
}; |
| 1093 |
|
| 1094 |
if (!src8 && !dst8) { |
| 1095 |
while (n --> 0) { |
| 1096 |
(void)processValue(0); |
| 1097 |
} |
| 1098 |
} else if (src8 && !dst8) { |
| 1099 |
while (n --> 0) { |
| 1100 |
(void)processValue(*src8); |
| 1101 |
src8 += srcStride; |
| 1102 |
} |
| 1103 |
} else if (!src8 && dst8) { |
| 1104 |
while (n --> 0) { |
| 1105 |
*dst8 = processValue(0); |
| 1106 |
dst8 += dstStride; |
| 1107 |
} |
| 1108 |
} else if (src8 && dst8) { |
| 1109 |
while (n --> 0) { |
| 1110 |
*dst8 = processValue(*src8); |
| 1111 |
src8 += srcStride; |
| 1112 |
dst8 += dstStride; |
| 1113 |
} |
| 1114 |
} |
| 1115 |
|
| 1116 |
// Store the updated state back into member variables for the next call. |
| 1117 |
fBuffer0Cursor = buffer0Cursor; |
| 1118 |
fBuffer1Cursor = buffer1Cursor; |
| 1119 |
fBuffer2Cursor = buffer2Cursor; |
| 1120 |
fSum0 = sum0; |
| 1121 |
fSum1 = sum1; |
| 1122 |
fSum2 = sum2; |
| 1123 |
} |
| 1124 |
|
| 1125 |
inline static constexpr uint64_t kHalf = static_cast<uint64_t>(1) << 31; |
| 1126 |
|
| 1127 |
uint8_t finalScale(uint32_t sum) const { |
| 1128 |
return SkTo<uint8_t>((fWeight * sum + kHalf) >> 32); |
| 1129 |
} |
| 1130 |
|
| 1131 |
// While input data is only A8 (only needing uint8_t to store), we need to store |
| 1132 |
// single-channel 32-bit data for the accumulation calculations. |
| 1133 |
uint64_t fWeight; |
| 1134 |
uint32_t* fBuffer0; |
| 1135 |
uint32_t* fBuffer0End; |
| 1136 |
uint32_t* fBuffer1; |
| 1137 |
uint32_t* fBuffer1End; |
| 1138 |
uint32_t* fBuffer2; |
| 1139 |
uint32_t* fBuffer2End; |
| 1140 |
|
| 1141 |
uint32_t* fBuffer0Cursor; |
| 1142 |
uint32_t* fBuffer1Cursor; |
| 1143 |
uint32_t* fBuffer2Cursor; |
| 1144 |
uint32_t fSum0; |
| 1145 |
uint32_t fSum1; |
| 1146 |
uint32_t fSum2; |
| 1147 |
}; |
| 1148 |
|
| 1149 |
class RasterA8BlurAlgorithm : public SkBlurEngine::Algorithm { |
| 1150 |
public: |
| 1151 |
// See analysis in description of GaussPass for the max supported sigma. |
| 1152 |
float maxSigma() const override { |
| 1153 |
static constexpr float kMaxSigma = 135.f; |
| 1154 |
SkASSERT(SkBlurEngine::BoxBlurWindow(kMaxSigma) <= 255); |
| 1155 |
return kMaxSigma; |
| 1156 |
} |
| 1157 |
|
| 1158 |
// TODO: Implement CPU backend for different fTileMode. This is still worth doing inline with |
| 1159 |
// the blur; at the moment the tiling is applied via the CropImageFilter and carried as metadata |
| 1160 |
// on the FilterResult. This is forcefully applied in FilterResult::Builder::blur() when |
| 1161 |
// supportsOnlyDecalTiling() returns true. |
| 1162 |
bool supportsOnlyDecalTiling() const override { return true; } |
| 1163 |
|
| 1164 |
sk_sp<SkSpecialImage> blur(SkSize sigma, |
| 1165 |
sk_sp<SkSpecialImage> input, |
| 1166 |
const SkIRect& originalSrcBounds, |
| 1167 |
SkTileMode tileMode, |
| 1168 |
const SkIRect& originalDstBounds) const override { |
| 1169 |
SkASSERT(tileMode == SkTileMode::kDecal); |
| 1170 |
SkASSERT(SkIRect::MakeSize(input->dimensions()).contains(originalSrcBounds)); |
| 1171 |
|
| 1172 |
SkBitmap src; |
| 1173 |
if (!SkSpecialImages::AsBitmap(input.get(), &src)) { |
| 1174 |
return nullptr; // Should only have been called by CPU-backed images |
| 1175 |
} |
| 1176 |
// The blur engine should not have picked this algorithm for a non-8-bit color type. |
| 1177 |
SkASSERT(src.colorType() == kAlpha_8_SkColorType); |
| 1178 |
|
| 1179 |
// 1024 is a place holder guess until more analysis can be done. |
| 1180 |
SkSTArenaAlloc<1024> alloc; |
| 1181 |
auto makeMaker = [&](float sigma) -> PassMaker* { |
| 1182 |
SkASSERT(0 <= sigma && sigma <= 135); // should be guaranteed after map_sigma |
| 1183 |
if (PassMaker* maker = GaussianPass<uint8_t>::MakeMaker(sigma, &alloc)) { |
| 1184 |
return maker; |
| 1185 |
} |
| 1186 |
if (PassMaker* maker = A8Pass::MakeMaker(sigma, &alloc)) { |
| 1187 |
return maker; |
| 1188 |
} |
| 1189 |
SK_ABORT("Sigma is out of range."); |
| 1190 |
}; |
| 1191 |
|
| 1192 |
PassMaker* makerX = makeMaker(sigma.width()); |
| 1193 |
PassMaker* makerY = makeMaker(sigma.height()); |
| 1194 |
|
| 1195 |
return eval_blur_passes<uint8_t>(makerX, makerY, src, originalSrcBounds, |
| 1196 |
originalDstBounds, &alloc); |
| 1197 |
} |
| 1198 |
}; |
| 1199 |
|
| 1200 |
class Raster8888BlurAlgorithm : public SkBlurEngine::Algorithm { |
| 1201 |
public: |
| 1202 |
// See analysis in description of TentPass for the max supported sigma. |
| 1203 |
float maxSigma() const override { |
| 1204 |
// TentPass supports a sigma up to 2183, and was added so that the CPU blur algorithm's |
| 1205 |
// blur radius was as large as that supported by the GPU. GaussPass only supports up to 136. |
| 1206 |
// However, there is a very apparent pop in blur weight when switching from successive box |
| 1207 |
// blurs to the tent filter. The TentPass is preserved for legacy blurs, which do not use |
| 1208 |
// FilterResult::rescale(). However, using kMaxSigma = 135 with the raster SkBlurEngine |
| 1209 |
// ensures that the non-legacy raster blurs will always use the GaussPass implementation. |
| 1210 |
// This is about 6-7x faster on large blurs to rescale a few times to a lower resolution |
| 1211 |
// than it is to evaluate the much larger original window. |
| 1212 |
static constexpr float kMaxSigma = 135.f; |
| 1213 |
SkASSERT(SkBlurEngine::BoxBlurWindow(kMaxSigma) <= 255); // see GaussPass::MakeMaker(). |
| 1214 |
return kMaxSigma; |
| 1215 |
} |
| 1216 |
|
| 1217 |
// TODO: Implement CPU backend for different fTileMode. This is still worth doing inline with |
| 1218 |
// the blur; at the moment the tiling is applied via the CropImageFilter and carried as metadata |
| 1219 |
// on the FilterResult. This is forcefully applied in FilterResult::Builder::blur() when |
| 1220 |
// supportsOnlyDecalTiling() returns true. |
| 1221 |
bool supportsOnlyDecalTiling() const override { return true; } |
| 1222 |
|
| 1223 |
sk_sp<SkSpecialImage> blur(SkSize sigma, |
| 1224 |
sk_sp<SkSpecialImage> input, |
| 1225 |
const SkIRect& originalSrcBounds, |
| 1226 |
SkTileMode tileMode, |
| 1227 |
const SkIRect& originalDstBounds) const override { |
| 1228 |
// TODO: Enable this assert when the TentPass is no longer used for legacy blurs |
| 1229 |
// (which supports blur sigmas larger than what's reported in maxSigma()). |
| 1230 |
// SkASSERT(sigma.width() <= this->maxSigma() && sigma.height() <= this->maxSigma()); |
| 1231 |
SkASSERT(tileMode == SkTileMode::kDecal); |
| 1232 |
|
| 1233 |
SkASSERT(SkIRect::MakeSize(input->dimensions()).contains(originalSrcBounds)); |
| 1234 |
|
| 1235 |
SkBitmap src; |
| 1236 |
if (!SkSpecialImages::AsBitmap(input.get(), &src)) { |
| 1237 |
return nullptr; // Should only have been called by CPU-backed images |
| 1238 |
} |
| 1239 |
// The blur engine should not have picked this algorithm for a non-32-bit color type |
| 1240 |
SkASSERT(src.colorType() == kRGBA_8888_SkColorType || |
| 1241 |
src.colorType() == kBGRA_8888_SkColorType); |
| 1242 |
|
| 1243 |
SkSTArenaAlloc<1024> alloc; |
| 1244 |
auto makeMaker = [&](float sigma) -> PassMaker* { |
| 1245 |
SkASSERT(0 <= sigma && sigma <= 2183); // should be guaranteed after map_sigma |
| 1246 |
#ifndef SK_AVOID_SLOW_RASTER_PIPELINE_BLURS |
| 1247 |
if (PassMaker* maker = GaussianPass<uint32_t>::MakeMaker(sigma, &alloc)) { |
| 1248 |
return maker; |
| 1249 |
} |
| 1250 |
#endif //SK_AVOID_SLOW_RASTER_PIPELINE_BLURS |
| 1251 |
if (PassMaker* maker = ThreeBoxApproxPass::MakeMaker(sigma, &alloc)) { |
| 1252 |
return maker; |
| 1253 |
} |
| 1254 |
if (PassMaker* maker = TentPass::MakeMaker(sigma, &alloc)) { |
| 1255 |
return maker; |
| 1256 |
} |
| 1257 |
SK_ABORT("Sigma is out of range."); |
| 1258 |
}; |
| 1259 |
|
| 1260 |
PassMaker* makerX = makeMaker(sigma.width()); |
| 1261 |
PassMaker* makerY = makeMaker(sigma.height()); |
| 1262 |
|
| 1263 |
return eval_blur_passes<uint32_t>(makerX, makerY, src, originalSrcBounds, |
| 1264 |
originalDstBounds, &alloc); |
| 1265 |
} |
| 1266 |
|
| 1267 |
}; |
| 1268 |
|
| 1269 |
class RasterShaderBlurAlgorithm : public SkShaderBlurAlgorithm { |
| 1270 |
public: |
| 1271 |
sk_sp<SkDevice> makeDevice(const SkImageInfo& imageInfo) const override { |
| 1272 |
// This Device will only be used to draw blurs, so use default SkSurfaceProps. The pixel |
| 1273 |
// geometry and font configuration do not matter. This is not a GPU surface, so DMSAA and |
| 1274 |
// the kAlwaysDither surface property are also irrelevant. |
| 1275 |
return SkBitmapDevice::Create(imageInfo, SkSurfaceProps{}); |
| 1276 |
} |
| 1277 |
}; |
| 1278 |
|
| 1279 |
class RasterBlurEngine : public SkBlurEngine { |
| 1280 |
public: |
| 1281 |
const Algorithm* findAlgorithm(SkSize sigma, SkColorType colorType) const override { |
| 1282 |
// The box blur doesn't actually care about channel order as long as it's 4 8-bit channels. |
| 1283 |
const bool rgba8Blur = colorType == kRGBA_8888_SkColorType || |
| 1284 |
colorType == kBGRA_8888_SkColorType; |
| 1285 |
const bool a8Blur = colorType == kAlpha_8_SkColorType; |
| 1286 |
|
| 1287 |
// For small sigmas, a8 and rgba blurs will use a gaussian blur, otherwise using |
| 1288 |
// box blur approximation. |
| 1289 |
if (a8Blur) { |
| 1290 |
return &fA8BlurAlgorithm; |
| 1291 |
} else if (rgba8Blur) { |
| 1292 |
return &fRGBA8BlurAlgorithm; |
| 1293 |
} else { |
| 1294 |
return &fShaderBlurAlgorithm; |
| 1295 |
} |
| 1296 |
} |
| 1297 |
|
| 1298 |
private: |
| 1299 |
// For non-A8 or non-8888, use the shader algorithm |
| 1300 |
RasterShaderBlurAlgorithm fShaderBlurAlgorithm; |
| 1301 |
// For large blurs with RGBA8 or BGRA8, use consecutive box blurs, |
| 1302 |
// For small 8888 blurs, use gaussian blur |
| 1303 |
Raster8888BlurAlgorithm fRGBA8BlurAlgorithm; |
| 1304 |
// For any large blurs with A8, use consecutive box blurs, |
| 1305 |
// For small a8 blurs use gaussian blur |
| 1306 |
RasterA8BlurAlgorithm fA8BlurAlgorithm; |
| 1307 |
}; |
| 1308 |
|
| 1309 |
} // anonymous namespace |
| 1310 |
|
| 1311 |
const SkBlurEngine* SkBlurEngine::GetRasterBlurEngine() { |
| 1312 |
static const RasterBlurEngine kInstance; |
| 1313 |
return &kInstance; |
| 1314 |
} |
| 1315 |
|
| 1316 |
// SkShaderBlurAlgorithm |
| 1317 |
// ---------------------------------------------------------------------------- |
| 1318 |
|
| 1319 |
void SkShaderBlurAlgorithm::Compute2DBlurKernel(SkSize sigma, |
| 1320 |
SkISize radius, |
| 1321 |
SkSpan<float> kernel) { |
| 1322 |
// Callers likely had to calculate the radius prior to filling out the kernel value, which is |
| 1323 |
// why it's provided; but make sure it's consistent with expectations. |
| 1324 |
SkASSERT(SkBlurEngine::SigmaToRadius(sigma.width()) == radius.width() && |
| 1325 |
SkBlurEngine::SigmaToRadius(sigma.height()) == radius.height()); |
| 1326 |
|
| 1327 |
// Callers are responsible for downscaling large sigmas to values that can be processed by the |
| 1328 |
// effects, so ensure the radius won't overflow 'kernel' |
| 1329 |
const int width = KernelWidth(radius.width()); |
| 1330 |
const int height = KernelWidth(radius.height()); |
| 1331 |
const size_t kernelSize = SkTo<size_t>(sk_64_mul(width, height)); |
| 1332 |
SkASSERT(kernelSize <= kernel.size()); |
| 1333 |
|
| 1334 |
// And the definition of an identity blur should be sufficient that 2sigma^2 isn't near zero |
| 1335 |
// when there's a non-trivial radius. |
| 1336 |
const float twoSigmaSqrdX = 2.0f * sigma.width() * sigma.width(); |
| 1337 |
const float twoSigmaSqrdY = 2.0f * sigma.height() * sigma.height(); |
| 1338 |
SkASSERT((radius.width() == 0 || !SkScalarNearlyZero(twoSigmaSqrdX)) && |
| 1339 |
(radius.height() == 0 || !SkScalarNearlyZero(twoSigmaSqrdY))); |
| 1340 |
|
| 1341 |
// Setting the denominator to 1 when the radius is 0 automatically converts the remaining math |
| 1342 |
// to the 1D Gaussian distribution. When both radii are 0, it correctly computes a weight of 1.0 |
| 1343 |
const float sigmaXDenom = radius.width() > 0 ? 1.0f / twoSigmaSqrdX : 1.f; |
| 1344 |
const float sigmaYDenom = radius.height() > 0 ? 1.0f / twoSigmaSqrdY : 1.f; |
| 1345 |
|
| 1346 |
float sum = 0.0f; |
| 1347 |
for (int x = 0; x < width; x++) { |
| 1348 |
float xTerm = static_cast<float>(x - radius.width()); |
| 1349 |
xTerm = xTerm * xTerm * sigmaXDenom; |
| 1350 |
for (int y = 0; y < height; y++) { |
| 1351 |
float yTerm = static_cast<float>(y - radius.height()); |
| 1352 |
float xyTerm = std::exp(-(xTerm + yTerm * yTerm * sigmaYDenom)); |
| 1353 |
// Note that the constant term (1/(sqrt(2*pi*sigma^2)) of the Gaussian |
| 1354 |
// is dropped here, since we renormalize the kernel below. |
| 1355 |
kernel[y * width + x] = xyTerm; |
| 1356 |
sum += xyTerm; |
| 1357 |
} |
| 1358 |
} |
| 1359 |
// Normalize the kernel |
| 1360 |
float scale = 1.0f / sum; |
| 1361 |
for (size_t i = 0; i < kernelSize; ++i) { |
| 1362 |
kernel[i] *= scale; |
| 1363 |
} |
| 1364 |
// Zero remainder of the array |
| 1365 |
memset(kernel.data() + kernelSize, 0, sizeof(float)*(kernel.size() - kernelSize)); |
| 1366 |
} |
| 1367 |
|
| 1368 |
void SkShaderBlurAlgorithm::Compute2DBlurKernel(SkSize sigma, |
| 1369 |
SkISize radii, |
| 1370 |
std::array<SkV4, kMaxSamples/4>& kernel) { |
| 1371 |
static_assert(sizeof(kernel) == sizeof(std::array<float, kMaxSamples>)); |
| 1372 |
static_assert(alignof(float) == alignof(SkV4)); |
| 1373 |
float* data = kernel[0].ptr(); |
| 1374 |
Compute2DBlurKernel(sigma, radii, SkSpan<float>(data, kMaxSamples)); |
| 1375 |
} |
| 1376 |
|
| 1377 |
void SkShaderBlurAlgorithm::Compute2DBlurOffsets(SkISize radius, |
| 1378 |
std::array<SkV4, kMaxSamples/2>& offsets) { |
| 1379 |
const int kernelArea = KernelWidth(radius.width()) * KernelWidth(radius.height()); |
| 1380 |
SkASSERT(kernelArea <= kMaxSamples); |
| 1381 |
|
| 1382 |
SkSpan<float> offsetView{offsets[0].ptr(), kMaxSamples*2}; |
| 1383 |
|
| 1384 |
int i = 0; |
| 1385 |
for (int y = -radius.height(); y <= radius.height(); ++y) { |
| 1386 |
for (int x = -radius.width(); x <= radius.width(); ++x) { |
| 1387 |
offsetView[2*i] = x; |
| 1388 |
offsetView[2*i+1] = y; |
| 1389 |
++i; |
| 1390 |
} |
| 1391 |
} |
| 1392 |
SkASSERT(i == kernelArea); |
| 1393 |
const int lastValidOffset = 2*(kernelArea - 1); |
| 1394 |
for (; i < kMaxSamples; ++i) { |
| 1395 |
offsetView[2*i] = offsetView[lastValidOffset]; |
| 1396 |
offsetView[2*i+1] = offsetView[lastValidOffset+1]; |
| 1397 |
} |
| 1398 |
} |
| 1399 |
|
| 1400 |
void SkShaderBlurAlgorithm::Compute1DBlurLinearKernel( |
| 1401 |
float sigma, |
| 1402 |
int radius, |
| 1403 |
std::array<SkV4, kMaxSamples/2>& offsetsAndKernel) { |
| 1404 |
SkASSERT(sigma <= kMaxLinearSigma); |
| 1405 |
SkASSERT(radius == SkBlurEngine::SigmaToRadius(sigma)); |
| 1406 |
SkASSERT(LinearKernelWidth(radius) <= kMaxSamples); |
| 1407 |
|
| 1408 |
// Given 2 adjacent gaussian points, they are blended as: Wi * Ci + Wj * Cj. |
| 1409 |
// The GPU will mix Ci and Cj as Ci * (1 - x) + Cj * x during sampling. |
| 1410 |
// Compute W', x such that W' * (Ci * (1 - x) + Cj * x) = Wi * Ci + Wj * Cj. |
| 1411 |
// Solving W' * x = Wj, W' * (1 - x) = Wi: |
| 1412 |
// W' = Wi + Wj |
| 1413 |
// x = Wj / (Wi + Wj) |
| 1414 |
auto get_new_weight = [](float* new_w, float* offset, float wi, float wj) { |
| 1415 |
*new_w = wi + wj; |
| 1416 |
*offset = wj / (wi + wj); |
| 1417 |
}; |
| 1418 |
|
| 1419 |
// Create a temporary standard kernel. The maximum blur radius that can be passed to this |
| 1420 |
// function is (kMaxBlurSamples-1), so make an array large enough to hold the full kernel width. |
| 1421 |
static constexpr int kMaxKernelWidth = KernelWidth(kMaxSamples - 1); |
| 1422 |
SkASSERT(KernelWidth(radius) <= kMaxKernelWidth); |
| 1423 |
std::array<float, kMaxKernelWidth> fullKernel; |
| 1424 |
Compute1DBlurKernel(sigma, radius, SkSpan<float>{fullKernel.data(), (size_t)KernelWidth(radius)}); |
| 1425 |
|
| 1426 |
std::array<float, kMaxSamples> kernel; |
| 1427 |
std::array<float, kMaxSamples> offsets; |
| 1428 |
// Note that halfsize isn't just size / 2, but radius + 1. This is the size of the output array. |
| 1429 |
int halfSize = LinearKernelWidth(radius); |
| 1430 |
int halfRadius = halfSize / 2; |
| 1431 |
int lowIndex = halfRadius - 1; |
| 1432 |
|
| 1433 |
// Compute1DGaussianKernel produces a full 2N + 1 kernel. Since the kernel can be mirrored, |
| 1434 |
// compute only the upper half and mirror to the lower half. |
| 1435 |
|
| 1436 |
int index = radius; |
| 1437 |
if (radius & 1) { |
| 1438 |
// If N is odd, then use two samples. |
| 1439 |
// The centre texel gets sampled twice, so halve its influence for each sample. |
| 1440 |
// We essentially sample like this: |
| 1441 |
// Texel edges |
| 1442 |
// v v v v |
| 1443 |
// | | | | |
| 1444 |
// \-----^---/ Lower sample |
| 1445 |
// \---^-----/ Upper sample |
| 1446 |
get_new_weight(&kernel[halfRadius], |
| 1447 |
&offsets[halfRadius], |
| 1448 |
fullKernel[index] * 0.5f, |
| 1449 |
fullKernel[index + 1]); |
| 1450 |
kernel[lowIndex] = kernel[halfRadius]; |
| 1451 |
offsets[lowIndex] = -offsets[halfRadius]; |
| 1452 |
index++; |
| 1453 |
lowIndex--; |
| 1454 |
} else { |
| 1455 |
// If N is even, then there are an even number of texels on either side of the centre texel. |
| 1456 |
// Sample the centre texel directly. |
| 1457 |
kernel[halfRadius] = fullKernel[index]; |
| 1458 |
offsets[halfRadius] = 0.0f; |
| 1459 |
} |
| 1460 |
index++; |
| 1461 |
|
| 1462 |
// Every other pair gets one sample. |
| 1463 |
for (int i = halfRadius + 1; i < halfSize; index += 2, i++, lowIndex--) { |
| 1464 |
get_new_weight(&kernel[i], &offsets[i], fullKernel[index], fullKernel[index + 1]); |
| 1465 |
offsets[i] += static_cast<float>(index - radius); |
| 1466 |
|
| 1467 |
// Mirror to lower half. |
| 1468 |
kernel[lowIndex] = kernel[i]; |
| 1469 |
offsets[lowIndex] = -offsets[i]; |
| 1470 |
} |
| 1471 |
|
| 1472 |
// Zero out remaining values in the kernel |
| 1473 |
memset(kernel.data() + halfSize, 0, sizeof(float)*(kMaxSamples - halfSize)); |
| 1474 |
// But copy the last valid offset into the remaining offsets, to increase the chance that |
| 1475 |
// over-iteration in a fragment shader will have a cache hit. |
| 1476 |
for (int i = halfSize; i < kMaxSamples; ++i) { |
| 1477 |
offsets[i] = offsets[halfSize - 1]; |
| 1478 |
} |
| 1479 |
|
| 1480 |
// Interleave into the output array to match the 1D SkSL effect |
| 1481 |
for (int i = 0; i < kMaxSamples / 2; ++i) { |
| 1482 |
offsetsAndKernel[i] = SkV4{offsets[2*i], kernel[2*i], offsets[2*i+1], kernel[2*i+1]}; |
| 1483 |
} |
| 1484 |
} |
| 1485 |
|
| 1486 |
static SkKnownRuntimeEffects::StableKey to_stablekey(int kernelWidth, uint32_t baseKey) { |
| 1487 |
SkASSERT(kernelWidth >= 2 && kernelWidth <= SkShaderBlurAlgorithm::kMaxSamples); |
| 1488 |
switch(kernelWidth) { |
| 1489 |
// Batch on multiples of 4 (skipping width=1, since that can't happen) |
| 1490 |
case 2: [[fallthrough]]; |
| 1491 |
case 3: [[fallthrough]]; |
| 1492 |
case 4: return static_cast<SkKnownRuntimeEffects::StableKey>(baseKey); |
| 1493 |
case 5: [[fallthrough]]; |
| 1494 |
case 6: [[fallthrough]]; |
| 1495 |
case 7: [[fallthrough]]; |
| 1496 |
case 8: return static_cast<SkKnownRuntimeEffects::StableKey>(baseKey+1); |
| 1497 |
case 9: [[fallthrough]]; |
| 1498 |
case 10: [[fallthrough]]; |
| 1499 |
case 11: [[fallthrough]]; |
| 1500 |
case 12: return static_cast<SkKnownRuntimeEffects::StableKey>(baseKey+2); |
| 1501 |
case 13: [[fallthrough]]; |
| 1502 |
case 14: [[fallthrough]]; |
| 1503 |
case 15: [[fallthrough]]; |
| 1504 |
case 16: return static_cast<SkKnownRuntimeEffects::StableKey>(baseKey+3); |
| 1505 |
case 17: [[fallthrough]]; |
| 1506 |
case 18: [[fallthrough]]; |
| 1507 |
case 19: [[fallthrough]]; |
| 1508 |
// With larger kernels, batch on multiples of eight so up to 7 wasted samples. |
| 1509 |
case 20: return static_cast<SkKnownRuntimeEffects::StableKey>(baseKey+4); |
| 1510 |
case 21: [[fallthrough]]; |
| 1511 |
case 22: [[fallthrough]]; |
| 1512 |
case 23: [[fallthrough]]; |
| 1513 |
case 24: [[fallthrough]]; |
| 1514 |
case 25: [[fallthrough]]; |
| 1515 |
case 26: [[fallthrough]]; |
| 1516 |
case 27: [[fallthrough]]; |
| 1517 |
case 28: return static_cast<SkKnownRuntimeEffects::StableKey>(baseKey+5); |
| 1518 |
default: |
| 1519 |
SkUNREACHABLE; |
| 1520 |
} |
| 1521 |
} |
| 1522 |
|
| 1523 |
const SkRuntimeEffect* SkShaderBlurAlgorithm::GetLinearBlur1DEffect(int radius) { |
| 1524 |
return GetKnownRuntimeEffect( |
| 1525 |
to_stablekey(LinearKernelWidth(radius), |
| 1526 |
static_cast<uint32_t>(SkKnownRuntimeEffects::StableKey::k1DBlurBase))); |
| 1527 |
} |
| 1528 |
|
| 1529 |
const SkRuntimeEffect* SkShaderBlurAlgorithm::GetBlur2DEffect(const SkISize& radii) { |
| 1530 |
int kernelArea = KernelWidth(radii.width()) * KernelWidth(radii.height()); |
| 1531 |
return GetKnownRuntimeEffect( |
| 1532 |
to_stablekey(kernelArea, |
| 1533 |
static_cast<uint32_t>(SkKnownRuntimeEffects::StableKey::k2DBlurBase))); |
| 1534 |
} |
| 1535 |
|
| 1536 |
sk_sp<SkSpecialImage> SkShaderBlurAlgorithm::renderBlur(SkRuntimeShaderBuilder* blurEffectBuilder, |
| 1537 |
SkFilterMode filter, |
| 1538 |
SkISize radii, |
| 1539 |
sk_sp<SkSpecialImage> input, |
| 1540 |
const SkIRect& srcRect, |
| 1541 |
SkTileMode tileMode, |
| 1542 |
const SkIRect& dstRect) const { |
| 1543 |
SkImageInfo outII = SkImageInfo::Make({dstRect.width(), dstRect.height()}, |
| 1544 |
input->colorType(), |
| 1545 |
kPremul_SkAlphaType, |
| 1546 |
input->colorInfo().refColorSpace()); |
| 1547 |
sk_sp<SkDevice> device = this->makeDevice(outII); |
| 1548 |
if (!device) { |
| 1549 |
return nullptr; |
| 1550 |
} |
| 1551 |
|
| 1552 |
SkIRect subset = SkIRect::MakeSize(dstRect.size()); |
| 1553 |
device->clipRect(SkRect::Make(subset), SkClipOp::kIntersect, /*aa=*/false); |
| 1554 |
device->setLocalToDevice(SkM44::Translate(-dstRect.left(), -dstRect.top())); |
| 1555 |
|
| 1556 |
// renderBlur() will either mix multiple fast and strict draws to cover dstRect, or will issue |
| 1557 |
// a single strict draw. While the SkShader object changes (really just strict mode), the rest |
| 1558 |
// of the SkPaint remains the same. |
| 1559 |
SkPaint paint; |
| 1560 |
paint.setBlendMode(SkBlendMode::kSrc); |
| 1561 |
|
| 1562 |
SkIRect safeSrcRect = srcRect.makeInset(radii.width(), radii.height()); |
| 1563 |
SkIRect fastDstRect = dstRect; |
| 1564 |
|
| 1565 |
// Only consider the safeSrcRect for shader-based tiling if the original srcRect is different |
| 1566 |
// from the backing store dimensions; when they match the full image we can use HW tiling. |
| 1567 |
if (srcRect != SkIRect::MakeSize(input->backingStoreDimensions())) { |
| 1568 |
if (fastDstRect.intersect(safeSrcRect)) { |
| 1569 |
// If the area of the non-clamping shader is small, it's better to just issue a single |
| 1570 |
// draw that performs shader tiling over the whole dst. |
| 1571 |
if (fastDstRect != dstRect && fastDstRect.width() * fastDstRect.height() < 128 * 128) { |
| 1572 |
fastDstRect.setEmpty(); |
| 1573 |
} |
| 1574 |
} else { |
| 1575 |
fastDstRect.setEmpty(); |
| 1576 |
} |
| 1577 |
} |
| 1578 |
|
| 1579 |
if (!fastDstRect.isEmpty()) { |
| 1580 |
// Fill as much as possible without adding shader tiling logic to each blur sample, |
| 1581 |
// switching to clamp tiling if we aren't in this block due to HW tiling. |
| 1582 |
SkIRect untiledSrcRect = srcRect.makeInset(1, 1); |
| 1583 |
SkTileMode fastTileMode = untiledSrcRect.contains(fastDstRect) ? SkTileMode::kClamp |
| 1584 |
: tileMode; |
| 1585 |
blurEffectBuilder->child("child") = input->asShader( |
| 1586 |
fastTileMode, filter, SkMatrix::I(), /*strict=*/false); |
| 1587 |
paint.setShader(blurEffectBuilder->makeShader()); |
| 1588 |
device->drawRect(SkRect::Make(fastDstRect), paint); |
| 1589 |
} |
| 1590 |
|
| 1591 |
// Switch to a strict shader if there are remaining pixels to fill |
| 1592 |
if (fastDstRect != dstRect) { |
| 1593 |
blurEffectBuilder->child("child") = input->makeSubset(srcRect)->asShader( |
| 1594 |
tileMode, filter, SkMatrix::Translate(srcRect.left(), srcRect.top())); |
| 1595 |
paint.setShader(blurEffectBuilder->makeShader()); |
| 1596 |
} |
| 1597 |
|
| 1598 |
if (fastDstRect.isEmpty()) { |
| 1599 |
// Fill the entire dst with the strict shader |
| 1600 |
device->drawRect(SkRect::Make(dstRect), paint); |
| 1601 |
} else if (fastDstRect != dstRect) { |
| 1602 |
// There will be up to four additional strict draws to fill in the border. The left and |
| 1603 |
// right sides will span the full height of the dst rect. The top and bottom will span |
| 1604 |
// the just the width of the fast interior. Strict border draws with zero width/height |
| 1605 |
// are skipped. |
| 1606 |
auto drawBorder = [&](const SkIRect& r) { |
| 1607 |
if (!r.isEmpty()) { |
| 1608 |
device->drawRect(SkRect::Make(r), paint); |
| 1609 |
} |
| 1610 |
}; |
| 1611 |
|
| 1612 |
drawBorder({dstRect.left(), dstRect.top(), |
| 1613 |
fastDstRect.left(), dstRect.bottom()}); // Left, spanning full height |
| 1614 |
drawBorder({fastDstRect.right(), dstRect.top(), |
| 1615 |
dstRect.right(), dstRect.bottom()}); // Right, spanning full height |
| 1616 |
drawBorder({fastDstRect.left(), dstRect.top(), |
| 1617 |
fastDstRect.right(), fastDstRect.top()}); // Top, spanning inner width |
| 1618 |
drawBorder({fastDstRect.left(), fastDstRect.bottom(), |
| 1619 |
fastDstRect.right(), dstRect.bottom()}); // Bottom, spanning inner width |
| 1620 |
} |
| 1621 |
|
| 1622 |
return device->snapSpecial(subset); |
| 1623 |
} |
| 1624 |
|
| 1625 |
sk_sp<SkSpecialImage> SkShaderBlurAlgorithm::evalBlur2D(SkSize sigma, |
| 1626 |
SkISize radii, |
| 1627 |
sk_sp<SkSpecialImage> input, |
| 1628 |
const SkIRect& srcRect, |
| 1629 |
SkTileMode tileMode, |
| 1630 |
const SkIRect& dstRect) const { |
| 1631 |
std::array<SkV4, kMaxSamples/4> kernel; |
| 1632 |
std::array<SkV4, kMaxSamples/2> offsets; |
| 1633 |
Compute2DBlurKernel(sigma, radii, kernel); |
| 1634 |
Compute2DBlurOffsets(radii, offsets); |
| 1635 |
|
| 1636 |
SkRuntimeShaderBuilder builder{sk_ref_sp(GetBlur2DEffect(radii))}; |
| 1637 |
builder.uniform("kernel") = kernel; |
| 1638 |
builder.uniform("offsets") = offsets; |
| 1639 |
// NOTE: renderBlur() will configure the "child" shader as needed. The 2D blur effect only |
| 1640 |
// requires nearest-neighbor filtering. |
| 1641 |
return this->renderBlur(&builder, SkFilterMode::kNearest, radii, |
| 1642 |
std::move(input), srcRect, tileMode, dstRect); |
| 1643 |
} |
| 1644 |
|
| 1645 |
sk_sp<SkSpecialImage> SkShaderBlurAlgorithm::evalBlur1D(float sigma, |
| 1646 |
int radius, |
| 1647 |
SkV2 dir, |
| 1648 |
sk_sp<SkSpecialImage> input, |
| 1649 |
SkIRect srcRect, |
| 1650 |
SkTileMode tileMode, |
| 1651 |
SkIRect dstRect) const { |
| 1652 |
std::array<SkV4, kMaxSamples/2> offsetsAndKernel; |
| 1653 |
Compute1DBlurLinearKernel(sigma, radius, offsetsAndKernel); |
| 1654 |
|
| 1655 |
SkRuntimeShaderBuilder builder{sk_ref_sp(GetLinearBlur1DEffect(radius))}; |
| 1656 |
builder.uniform("offsetsAndKernel") = offsetsAndKernel; |
| 1657 |
builder.uniform("dir") = dir; |
| 1658 |
// NOTE: renderBlur() will configure the "child" shader as needed. The 1D blur effect requires |
| 1659 |
// linear filtering. Reconstruct the appropriate "2D" radii inset value from 'dir'. |
| 1660 |
SkISize radii{dir.x ? radius : 0, dir.y ? radius : 0}; |
| 1661 |
return this->renderBlur(&builder, SkFilterMode::kLinear, radii, |
| 1662 |
std::move(input), srcRect, tileMode, dstRect); |
| 1663 |
} |
| 1664 |
|
| 1665 |
sk_sp<SkSpecialImage> SkShaderBlurAlgorithm::blur(SkSize sigma, |
| 1666 |
sk_sp<SkSpecialImage> src, |
| 1667 |
const SkIRect& srcRect, |
| 1668 |
SkTileMode tileMode, |
| 1669 |
const SkIRect& dstRect) const { |
| 1670 |
SkASSERT(sigma.width() <= kMaxLinearSigma && sigma.height() <= kMaxLinearSigma); |
| 1671 |
|
| 1672 |
int radiusX = SkBlurEngine::SigmaToRadius(sigma.width()); |
| 1673 |
int radiusY = SkBlurEngine::SigmaToRadius(sigma.height()); |
| 1674 |
const int kernelArea = KernelWidth(radiusX) * KernelWidth(radiusY); |
| 1675 |
if (kernelArea <= kMaxSamples && radiusX > 0 && radiusY > 0) { |
| 1676 |
// Use a single-pass 2D kernel if it fits and isn't just 1D already |
| 1677 |
return this->evalBlur2D(sigma, |
| 1678 |
{radiusX, radiusY}, |
| 1679 |
std::move(src), |
| 1680 |
srcRect, |
| 1681 |
tileMode, |
| 1682 |
dstRect); |
| 1683 |
} else { |
| 1684 |
// Use two passes of a 1D kernel (one per axis). |
| 1685 |
SkIRect intermediateSrcRect = srcRect; |
| 1686 |
SkIRect intermediateDstRect = dstRect; |
| 1687 |
if (radiusX > 0) { |
| 1688 |
if (radiusY > 0) { |
| 1689 |
// May need to maintain extra rows above and below 'dstRect' for the follow-up pass. |
| 1690 |
if (tileMode == SkTileMode::kRepeat || tileMode == SkTileMode::kMirror) { |
| 1691 |
// If the srcRect and dstRect are aligned, then we don't need extra rows since |
| 1692 |
// the periodic tiling on srcRect is the same for the intermediate. If they |
| 1693 |
// are not aligned, then outset by the Y radius. |
| 1694 |
const int period = srcRect.height() * (tileMode == SkTileMode::kMirror ? 2 : 1); |
| 1695 |
if (std::abs(dstRect.fTop - srcRect.fTop) % period != 0 || |
| 1696 |
dstRect.height() != srcRect.height()) { |
| 1697 |
intermediateDstRect.outset(0, radiusY); |
| 1698 |
} |
| 1699 |
} else { |
| 1700 |
// For clamp and decal tiling, we outset by the Y radius up to what's available |
| 1701 |
// from the srcRect. Anything beyond that is identical to tiling the |
| 1702 |
// intermediate dst image directly. |
| 1703 |
intermediateDstRect.outset(0, radiusY); |
| 1704 |
intermediateDstRect.fTop = std::max(intermediateDstRect.fTop, srcRect.fTop); |
| 1705 |
intermediateDstRect.fBottom = |
| 1706 |
std::min(intermediateDstRect.fBottom, srcRect.fBottom); |
| 1707 |
if (intermediateDstRect.fTop >= intermediateDstRect.fBottom) { |
| 1708 |
return nullptr; |
| 1709 |
} |
| 1710 |
} |
| 1711 |
} |
| 1712 |
|
| 1713 |
src = this->evalBlur1D(sigma.width(), |
| 1714 |
radiusX, |
| 1715 |
/*dir=*/{1.f, 0.f}, |
| 1716 |
std::move(src), |
| 1717 |
srcRect, |
| 1718 |
tileMode, |
| 1719 |
intermediateDstRect); |
| 1720 |
if (!src) { |
| 1721 |
return nullptr; |
| 1722 |
} |
| 1723 |
intermediateSrcRect = SkIRect::MakeWH(src->width(), src->height()); |
| 1724 |
intermediateDstRect = dstRect.makeOffset(-intermediateDstRect.left(), |
| 1725 |
-intermediateDstRect.top()); |
| 1726 |
} |
| 1727 |
|
| 1728 |
if (radiusY > 0) { |
| 1729 |
src = this->evalBlur1D(sigma.height(), |
| 1730 |
radiusY, |
| 1731 |
/*dir=*/{0.f, 1.f}, |
| 1732 |
std::move(src), |
| 1733 |
intermediateSrcRect, |
| 1734 |
tileMode, |
| 1735 |
intermediateDstRect); |
| 1736 |
} |
| 1737 |
|
| 1738 |
return src; |
| 1739 |
} |
| 1740 |
} |