| 1 |
/* |
| 2 |
* Copyright 2015 Google Inc. |
| 3 |
* |
| 4 |
* Use of this source code is governed by a BSD-style license that can be |
| 5 |
* found in the LICENSE file. |
| 6 |
*/ |
| 7 |
|
| 8 |
#include "src/gpu/ganesh/GrBlurUtils.h" |
| 9 |
|
| 10 |
#include "include/core/SkAlphaType.h" |
| 11 |
#include "include/core/SkBitmap.h" |
| 12 |
#include "include/core/SkBlendMode.h" |
| 13 |
#include "include/core/SkBlurTypes.h" |
| 14 |
#include "include/core/SkCanvas.h" |
| 15 |
#include "include/core/SkColorSpace.h" |
| 16 |
#include "include/core/SkData.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/SkPath.h" |
| 22 |
#include "include/core/SkPoint.h" |
| 23 |
#include "include/core/SkRRect.h" |
| 24 |
#include "include/core/SkRect.h" |
| 25 |
#include "include/core/SkRefCnt.h" |
| 26 |
#include "include/core/SkRegion.h" |
| 27 |
#include "include/core/SkSamplingOptions.h" |
| 28 |
#include "include/core/SkScalar.h" |
| 29 |
#include "include/core/SkSize.h" |
| 30 |
#include "include/core/SkSpan.h" |
| 31 |
#include "include/core/SkString.h" |
| 32 |
#include "include/core/SkStrokeRec.h" |
| 33 |
#include "include/core/SkSurface.h" |
| 34 |
#include "include/core/SkSurfaceProps.h" |
| 35 |
#include "include/core/SkTileMode.h" |
| 36 |
#include "include/effects/SkRuntimeEffect.h" |
| 37 |
#include "include/gpu/GpuTypes.h" |
| 38 |
#include "include/gpu/ganesh/GrDirectContext.h" |
| 39 |
#include "include/gpu/ganesh/GrRecordingContext.h" |
| 40 |
#include "include/gpu/ganesh/GrTypes.h" |
| 41 |
#include "include/private/base/SkAssert.h" |
| 42 |
#include "include/private/base/SkFixed.h" |
| 43 |
#include "include/private/base/SkFloatingPoint.h" |
| 44 |
#include "include/private/base/SkMath.h" |
| 45 |
#include "include/private/base/SkTemplates.h" |
| 46 |
#include "include/private/gpu/ganesh/GrTypesPriv.h" |
| 47 |
#include "src/base/SkFloatBits.h" |
| 48 |
#include "src/base/SkSafeMath.h" |
| 49 |
#include "src/core/SkBlurMaskFilterImpl.h" |
| 50 |
#include "src/core/SkColorData.h" |
| 51 |
#include "src/core/SkDraw.h" |
| 52 |
#include "src/core/SkMask.h" |
| 53 |
#include "src/core/SkMaskFilterBase.h" |
| 54 |
#include "src/core/SkRRectPriv.h" |
| 55 |
#include "src/core/SkRuntimeEffectPriv.h" |
| 56 |
#include "src/core/SkTraceEvent.h" |
| 57 |
#include "src/gpu/BlurUtils.h" |
| 58 |
#include "src/gpu/ResourceKey.h" |
| 59 |
#include "src/gpu/SkBackingFit.h" |
| 60 |
#include "src/gpu/Swizzle.h" |
| 61 |
#include "src/gpu/ganesh/GrCaps.h" |
| 62 |
#include "src/gpu/ganesh/GrClip.h" |
| 63 |
#include "src/gpu/ganesh/GrColorInfo.h" |
| 64 |
#include "src/gpu/ganesh/GrColorSpaceXform.h" |
| 65 |
#include "src/gpu/ganesh/GrDirectContextPriv.h" |
| 66 |
#include "src/gpu/ganesh/GrFixedClip.h" |
| 67 |
#include "src/gpu/ganesh/GrFragmentProcessor.h" |
| 68 |
#include "src/gpu/ganesh/GrFragmentProcessors.h" |
| 69 |
#include "src/gpu/ganesh/GrPaint.h" |
| 70 |
#include "src/gpu/ganesh/GrRecordingContextPriv.h" |
| 71 |
#include "src/gpu/ganesh/GrSamplerState.h" |
| 72 |
#include "src/gpu/ganesh/GrShaderCaps.h" |
| 73 |
#include "src/gpu/ganesh/GrStyle.h" |
| 74 |
#include "src/gpu/ganesh/GrSurfaceProxy.h" |
| 75 |
#include "src/gpu/ganesh/GrSurfaceProxyView.h" |
| 76 |
#include "src/gpu/ganesh/GrTextureProxy.h" |
| 77 |
#include "src/gpu/ganesh/GrThreadSafeCache.h" |
| 78 |
#include "src/gpu/ganesh/GrUtil.h" |
| 79 |
#include "src/gpu/ganesh/SkGr.h" |
| 80 |
#include "src/gpu/ganesh/SurfaceContext.h" |
| 81 |
#include "src/gpu/ganesh/SurfaceDrawContext.h" |
| 82 |
#include "src/gpu/ganesh/SurfaceFillContext.h" |
| 83 |
#include "src/gpu/ganesh/effects/GrBlendFragmentProcessor.h" |
| 84 |
#include "src/gpu/ganesh/effects/GrMatrixEffect.h" |
| 85 |
#include "src/gpu/ganesh/effects/GrSkSLFP.h" |
| 86 |
#include "src/gpu/ganesh/effects/GrTextureEffect.h" |
| 87 |
#include "src/gpu/ganesh/geometry/GrStyledShape.h" |
| 88 |
#include "src/gpu/ganesh/image/GrMippedBitmap.h" |
| 89 |
|
| 90 |
#include <algorithm> |
| 91 |
#include <array> |
| 92 |
#include <cstdint> |
| 93 |
#include <initializer_list> |
| 94 |
#include <memory> |
| 95 |
#include <tuple> |
| 96 |
#include <utility> |
| 97 |
|
| 98 |
namespace GrBlurUtils { |
| 99 |
|
| 100 |
static bool clip_bounds_quick_reject(const SkIRect& clipBounds, const SkIRect& rect) { |
| 101 |
return clipBounds.isEmpty() || rect.isEmpty() || !SkIRect::Intersects(clipBounds, rect); |
| 102 |
} |
| 103 |
|
| 104 |
static constexpr auto kMaskOrigin = kTopLeft_GrSurfaceOrigin; |
| 105 |
|
| 106 |
// Draw a mask using the supplied paint. Since the coverage/geometry |
| 107 |
// is already burnt into the mask this boils down to a rect draw. |
| 108 |
// Return true if the mask was successfully drawn. |
| 109 |
static bool draw_mask(skgpu::ganesh::SurfaceDrawContext* sdc, |
| 110 |
const GrClip* clip, |
| 111 |
const SkMatrix& viewMatrix, |
| 112 |
const SkIRect& maskBounds, |
| 113 |
GrPaint&& paint, |
| 114 |
GrSurfaceProxyView mask) { |
| 115 |
auto inverse = viewMatrix.invert(); |
| 116 |
if (!inverse) { |
| 117 |
return false; |
| 118 |
} |
| 119 |
|
| 120 |
mask.concatSwizzle(skgpu::Swizzle("aaaa")); |
| 121 |
|
| 122 |
SkMatrix matrix = SkMatrix::Translate(-SkIntToScalar(maskBounds.fLeft), |
| 123 |
-SkIntToScalar(maskBounds.fTop)); |
| 124 |
matrix.preConcat(viewMatrix); |
| 125 |
paint.setCoverageFragmentProcessor( |
| 126 |
GrTextureEffect::Make(std::move(mask), kUnknown_SkAlphaType, matrix)); |
| 127 |
|
| 128 |
sdc->fillPixelsWithLocalMatrix(clip, std::move(paint), maskBounds, *inverse); |
| 129 |
return true; |
| 130 |
} |
| 131 |
|
| 132 |
static void mask_release_proc(void* addr, void* /*context*/) { |
| 133 |
SkMaskBuilder::FreeImage(addr); |
| 134 |
} |
| 135 |
|
| 136 |
// This stores the mapping from an unclipped, integerized, device-space, shape bounds to |
| 137 |
// the filtered mask's draw rect. |
| 138 |
struct DrawRectData { |
| 139 |
SkIVector fOffset; |
| 140 |
SkISize fSize; |
| 141 |
}; |
| 142 |
|
| 143 |
static sk_sp<SkData> create_data(const SkIRect& drawRect, const SkIRect& origDevBounds) { |
| 144 |
|
| 145 |
DrawRectData drawRectData { {drawRect.fLeft - origDevBounds.fLeft, |
| 146 |
drawRect.fTop - origDevBounds.fTop}, |
| 147 |
drawRect.size() }; |
| 148 |
|
| 149 |
return SkData::MakeWithCopy(&drawRectData, sizeof(drawRectData)); |
| 150 |
} |
| 151 |
|
| 152 |
static SkIRect extract_draw_rect_from_data(SkData* data, const SkIRect& origDevBounds) { |
| 153 |
auto drawRectData = static_cast<const DrawRectData*>(data->data()); |
| 154 |
|
| 155 |
return SkIRect::MakeXYWH(origDevBounds.fLeft + drawRectData->fOffset.fX, |
| 156 |
origDevBounds.fTop + drawRectData->fOffset.fY, |
| 157 |
drawRectData->fSize.fWidth, |
| 158 |
drawRectData->fSize.fHeight); |
| 159 |
} |
| 160 |
|
| 161 |
static GrSurfaceProxyView sw_create_filtered_mask(GrRecordingContext* rContext, |
| 162 |
const SkMatrix& viewMatrix, |
| 163 |
const GrStyledShape& shape, |
| 164 |
const SkMaskFilter* filter, |
| 165 |
const SkIRect& unclippedDevShapeBounds, |
| 166 |
const SkIRect& clipBounds, |
| 167 |
SkIRect* drawRect, |
| 168 |
skgpu::UniqueKey* key) { |
| 169 |
SkASSERT(filter); |
| 170 |
SkASSERT(!shape.style().applies()); |
| 171 |
|
| 172 |
auto threadSafeCache = rContext->priv().threadSafeCache(); |
| 173 |
|
| 174 |
GrSurfaceProxyView filteredMaskView; |
| 175 |
sk_sp<SkData> data; |
| 176 |
|
| 177 |
if (key->isValid()) { |
| 178 |
std::tie(filteredMaskView, data) = threadSafeCache->findWithData(*key); |
| 179 |
} |
| 180 |
|
| 181 |
if (filteredMaskView) { |
| 182 |
SkASSERT(data); |
| 183 |
SkASSERT(kMaskOrigin == filteredMaskView.origin()); |
| 184 |
|
| 185 |
*drawRect = extract_draw_rect_from_data(data.get(), unclippedDevShapeBounds); |
| 186 |
} else { |
| 187 |
SkStrokeRec::InitStyle fillOrHairline = shape.style().isSimpleHairline() |
| 188 |
? SkStrokeRec::kHairline_InitStyle |
| 189 |
: SkStrokeRec::kFill_InitStyle; |
| 190 |
|
| 191 |
// TODO: it seems like we could create an skcpu::Draw here and set its fMatrix field rather |
| 192 |
// than explicitly transforming the path to device space. |
| 193 |
SkPath devPath = shape.asPath().makeTransform(viewMatrix); |
| 194 |
const auto raw = SkPathPriv::Raw(devPath, SkResolveConvexity::kYes); |
| 195 |
if (!raw) { |
| 196 |
return {}; |
| 197 |
} |
| 198 |
|
| 199 |
SkMaskBuilder srcM, dstM; |
| 200 |
if (!skcpu::DrawToMask(*raw, |
| 201 |
clipBounds, |
| 202 |
filter, |
| 203 |
&viewMatrix, |
| 204 |
&srcM, |
| 205 |
SkMaskBuilder::kComputeBoundsAndRenderImage_CreateMode, |
| 206 |
fillOrHairline)) { |
| 207 |
return {}; |
| 208 |
} |
| 209 |
SkAutoMaskFreeImage autoSrc(srcM.image()); |
| 210 |
|
| 211 |
SkASSERT(SkMask::kA8_Format == srcM.fFormat); |
| 212 |
|
| 213 |
if (!as_MFB(filter)->filterMask(&dstM, srcM, viewMatrix, nullptr)) { |
| 214 |
return {}; |
| 215 |
} |
| 216 |
// this will free-up dstM when we're done (allocated in filterMask()) |
| 217 |
SkAutoMaskFreeImage autoDst(dstM.image()); |
| 218 |
|
| 219 |
if (clip_bounds_quick_reject(clipBounds, dstM.fBounds)) { |
| 220 |
return {}; |
| 221 |
} |
| 222 |
|
| 223 |
// we now have a device-aligned 8bit mask in dstM, ready to be drawn using |
| 224 |
// the current clip (and identity matrix) and GrPaint settings |
| 225 |
std::optional<GrMippedBitmap> bm = GrMippedBitmap::Make( |
| 226 |
SkImageInfo::MakeA8(dstM.fBounds.width(), dstM.fBounds.height()), |
| 227 |
autoDst.release(), |
| 228 |
dstM.fRowBytes, |
| 229 |
mask_release_proc, |
| 230 |
nullptr); |
| 231 |
if (!bm) { |
| 232 |
return {}; |
| 233 |
} |
| 234 |
|
| 235 |
std::tie(filteredMaskView, std::ignore) = GrMakeUncachedBitmapProxyView( |
| 236 |
rContext, bm.value(), skgpu::Mipmapped::kNo, SkBackingFit::kApprox); |
| 237 |
if (!filteredMaskView) { |
| 238 |
return {}; |
| 239 |
} |
| 240 |
|
| 241 |
SkASSERT(kMaskOrigin == filteredMaskView.origin()); |
| 242 |
|
| 243 |
*drawRect = dstM.fBounds; |
| 244 |
|
| 245 |
if (key->isValid()) { |
| 246 |
key->setCustomData(create_data(*drawRect, unclippedDevShapeBounds)); |
| 247 |
std::tie(filteredMaskView, data) = threadSafeCache->addWithData(*key, filteredMaskView); |
| 248 |
// If we got a different view back from 'addWithData' it could have a different drawRect |
| 249 |
*drawRect = extract_draw_rect_from_data(data.get(), unclippedDevShapeBounds); |
| 250 |
} |
| 251 |
} |
| 252 |
|
| 253 |
return filteredMaskView; |
| 254 |
} |
| 255 |
|
| 256 |
// Create a mask of 'shape' and return the resulting surfaceDrawContext |
| 257 |
static std::unique_ptr<skgpu::ganesh::SurfaceDrawContext> create_mask_GPU( |
| 258 |
GrRecordingContext* rContext, |
| 259 |
const SkIRect& maskRect, |
| 260 |
const SkMatrix& origViewMatrix, |
| 261 |
const GrStyledShape& shape, |
| 262 |
int sampleCnt) { |
| 263 |
// We cache blur masks. Use default surface props here so we can use the same cached mask |
| 264 |
// regardless of the final dst surface. |
| 265 |
SkSurfaceProps defaultSurfaceProps; |
| 266 |
|
| 267 |
// Use GetApproxSize to implement our own approximate size matching, but demand |
| 268 |
// a "SkBackingFit::kExact" size match on the actual render target. We do this because the |
| 269 |
// filter will reach outside the src bounds, so we need to pre-clear these values to ensure a |
| 270 |
// "decal" sampling effect (i.e., ensure reads outside the src bounds return alpha=0). |
| 271 |
// |
| 272 |
// FIXME: Reads outside the left and top edges will actually clamp to the edge pixel. And in the |
| 273 |
// event that GetApproxSize does not change the size, reads outside the right and/or bottom will |
| 274 |
// do the same. We should offset our filter within the render target and expand the size as |
| 275 |
// needed to guarantee at least 1px of padding on all sides. |
| 276 |
auto approxSize = skgpu::GetApproxSize(maskRect.size()); |
| 277 |
auto sdc = skgpu::ganesh::SurfaceDrawContext::MakeWithFallback(rContext, |
| 278 |
GrColorType::kAlpha_8, |
| 279 |
nullptr, |
| 280 |
SkBackingFit::kExact, |
| 281 |
approxSize, |
| 282 |
defaultSurfaceProps, |
| 283 |
sampleCnt, |
| 284 |
skgpu::Mipmapped::kNo, |
| 285 |
GrProtected::kNo, |
| 286 |
kMaskOrigin); |
| 287 |
if (!sdc) { |
| 288 |
return nullptr; |
| 289 |
} |
| 290 |
|
| 291 |
sdc->clear(SK_PMColor4fTRANSPARENT); |
| 292 |
|
| 293 |
GrPaint maskPaint; |
| 294 |
maskPaint.setCoverageSetOpXPFactory(SkRegion::kReplace_Op); |
| 295 |
|
| 296 |
// setup new clip |
| 297 |
GrFixedClip clip(sdc->dimensions(), SkIRect::MakeWH(maskRect.width(), maskRect.height())); |
| 298 |
|
| 299 |
// Draw the mask into maskTexture with the path's integerized top-left at the origin using |
| 300 |
// maskPaint. |
| 301 |
SkMatrix viewMatrix = origViewMatrix; |
| 302 |
viewMatrix.postTranslate(-SkIntToScalar(maskRect.fLeft), -SkIntToScalar(maskRect.fTop)); |
| 303 |
sdc->drawShape(&clip, std::move(maskPaint), GrAA::kYes, viewMatrix, GrStyledShape(shape)); |
| 304 |
return sdc; |
| 305 |
} |
| 306 |
|
| 307 |
static bool get_unclipped_shape_dev_bounds(const GrStyledShape& shape, const SkMatrix& matrix, |
| 308 |
SkIRect* devBounds) { |
| 309 |
SkRect shapeDevBounds; |
| 310 |
if (shape.inverseFilled()) { |
| 311 |
shapeDevBounds = {SK_ScalarNegativeInfinity, SK_ScalarNegativeInfinity, |
| 312 |
SK_ScalarInfinity, SK_ScalarInfinity}; |
| 313 |
} else { |
| 314 |
SkRect shapeBounds = shape.styledBounds(); |
| 315 |
if (shapeBounds.isEmpty()) { |
| 316 |
return false; |
| 317 |
} |
| 318 |
matrix.mapRect(&shapeDevBounds, shapeBounds); |
| 319 |
} |
| 320 |
// Even though these are "unclipped" bounds we still clip to the int32_t range. |
| 321 |
// This is the largest int32_t that is representable exactly as a float. The next 63 larger ints |
| 322 |
// would round down to this value when cast to a float, but who really cares. |
| 323 |
// INT32_MIN is exactly representable. |
| 324 |
static constexpr int32_t kMaxInt = 2147483520; |
| 325 |
if (!shapeDevBounds.intersect(SkRect::MakeLTRB(INT32_MIN, INT32_MIN, kMaxInt, kMaxInt))) { |
| 326 |
return false; |
| 327 |
} |
| 328 |
// Make sure that the resulting SkIRect can have representable width and height |
| 329 |
if (SkScalarRoundToInt(shapeDevBounds.width()) > kMaxInt || |
| 330 |
SkScalarRoundToInt(shapeDevBounds.height()) > kMaxInt) { |
| 331 |
return false; |
| 332 |
} |
| 333 |
shapeDevBounds.roundOut(devBounds); |
| 334 |
return true; |
| 335 |
} |
| 336 |
|
| 337 |
// Gets the shape bounds, the clip bounds, and the intersection (if any). Returns false if there |
| 338 |
// is no intersection. |
| 339 |
static bool get_shape_and_clip_bounds(skgpu::ganesh::SurfaceDrawContext* sdc, |
| 340 |
const GrClip* clip, |
| 341 |
const GrStyledShape& shape, |
| 342 |
const SkMatrix& matrix, |
| 343 |
SkIRect* unclippedDevShapeBounds, |
| 344 |
SkIRect* devClipBounds) { |
| 345 |
// compute bounds as intersection of rt size, clip, and path |
| 346 |
*devClipBounds = clip ? clip->getConservativeBounds() |
| 347 |
: SkIRect::MakeWH(sdc->width(), sdc->height()); |
| 348 |
|
| 349 |
if (!get_unclipped_shape_dev_bounds(shape, matrix, unclippedDevShapeBounds)) { |
| 350 |
*unclippedDevShapeBounds = SkIRect::MakeEmpty(); |
| 351 |
return false; |
| 352 |
} |
| 353 |
|
| 354 |
return true; |
| 355 |
} |
| 356 |
|
| 357 |
/** |
| 358 |
* If we cannot create a FragmentProcess for a mask filter, we might have special logic for |
| 359 |
* it here. That code path requires constructing a src mask as input. Since that is a potentially |
| 360 |
* expensive operation, this function tests if filter_mask would succeed if the mask |
| 361 |
* were to be created. |
| 362 |
* |
| 363 |
* 'maskRect' returns the device space portion of the mask that the filter needs. The mask |
| 364 |
* passed into 'filter_mask' should have the same extent as 'maskRect' but be |
| 365 |
* translated to the upper-left corner of the mask (i.e., (maskRect.fLeft, maskRect.fTop) |
| 366 |
* appears at (0, 0) in the mask). |
| 367 |
* |
| 368 |
* Logically, how this works is: |
| 369 |
* can_filter_mask is called |
| 370 |
* if (it returns true) |
| 371 |
* the returned mask rect is used for quick rejecting |
| 372 |
* the mask rect is used to generate the mask |
| 373 |
* filter_mask is called to filter the mask |
| 374 |
* |
| 375 |
* TODO: this should work as: |
| 376 |
* if (can_filter_mask(devShape, ...)) // rect, rrect, drrect, path |
| 377 |
* filter_mask(devShape, ...) |
| 378 |
* this would hide the RRect special case and the mask generation |
| 379 |
*/ |
| 380 |
static bool can_filter_mask(const SkMaskFilterBase* maskFilter, |
| 381 |
const GrStyledShape& shape, |
| 382 |
const SkIRect& devSpaceShapeBounds, |
| 383 |
const SkIRect& clipBounds, |
| 384 |
const SkMatrix& ctm, |
| 385 |
SkIRect* maskRect) { |
| 386 |
if (maskFilter->type() != SkMaskFilterBase::Type::kBlur) { |
| 387 |
return false; |
| 388 |
} |
| 389 |
auto bmf = static_cast<const SkBlurMaskFilterImpl*>(maskFilter); |
| 390 |
SkScalar xformedSigma = bmf->computeXformedSigma(ctm); |
| 391 |
if (skgpu::BlurIsEffectivelyIdentity(xformedSigma)) { |
| 392 |
*maskRect = devSpaceShapeBounds; |
| 393 |
return maskRect->intersect(clipBounds); |
| 394 |
} |
| 395 |
|
| 396 |
if (maskRect) { |
| 397 |
float sigma3 = 3 * xformedSigma; |
| 398 |
|
| 399 |
// Outset srcRect and clipRect by 3 * sigma, to compute affected blur area. |
| 400 |
SkIRect clipRect = clipBounds.makeOutset(sigma3, sigma3); |
| 401 |
SkIRect srcRect = devSpaceShapeBounds.makeOutset(sigma3, sigma3); |
| 402 |
|
| 403 |
if (!srcRect.intersect(clipRect)) { |
| 404 |
srcRect.setEmpty(); |
| 405 |
} |
| 406 |
*maskRect = srcRect; |
| 407 |
} |
| 408 |
|
| 409 |
// We prefer to blur paths with small blur radii on the CPU. |
| 410 |
static const SkScalar kMIN_GPU_BLUR_SIZE = SkIntToScalar(64); |
| 411 |
static const SkScalar kMIN_GPU_BLUR_SIGMA = SkIntToScalar(32); |
| 412 |
|
| 413 |
if (devSpaceShapeBounds.width() <= kMIN_GPU_BLUR_SIZE && |
| 414 |
devSpaceShapeBounds.height() <= kMIN_GPU_BLUR_SIZE && |
| 415 |
xformedSigma <= kMIN_GPU_BLUR_SIGMA) { |
| 416 |
return false; |
| 417 |
} |
| 418 |
|
| 419 |
return true; |
| 420 |
} |
| 421 |
|
| 422 |
/////////////////////////////////////////////////////////////////////////////// |
| 423 |
// Circle Blur |
| 424 |
/////////////////////////////////////////////////////////////////////////////// |
| 425 |
|
| 426 |
static std::unique_ptr<GrFragmentProcessor> create_profile_effect(GrRecordingContext* rContext, |
| 427 |
const SkRect& circle, |
| 428 |
float sigma, |
| 429 |
float* solidRadius, |
| 430 |
float* textureRadius) { |
| 431 |
float circleR = circle.width() / 2.0f; |
| 432 |
if (!SkIsFinite(circleR) || circleR < SK_ScalarNearlyZero) { |
| 433 |
return nullptr; |
| 434 |
} |
| 435 |
|
| 436 |
auto threadSafeCache = rContext->priv().threadSafeCache(); |
| 437 |
|
| 438 |
// Profile textures are cached by the ratio of sigma to circle radius and by the size of the |
| 439 |
// profile texture (binned by powers of 2). |
| 440 |
SkScalar sigmaToCircleRRatio = sigma / circleR; |
| 441 |
// When sigma is really small this becomes a equivalent to convolving a Gaussian with a |
| 442 |
// half-plane. Similarly, in the extreme high ratio cases circle becomes a point WRT to the |
| 443 |
// Guassian and the profile texture is a just a Gaussian evaluation. However, we haven't yet |
| 444 |
// implemented this latter optimization. |
| 445 |
sigmaToCircleRRatio = std::min(sigmaToCircleRRatio, 8.f); |
| 446 |
SkFixed sigmaToCircleRRatioFixed; |
| 447 |
static const SkScalar kHalfPlaneThreshold = 0.1f; |
| 448 |
bool useHalfPlaneApprox = false; |
| 449 |
if (sigmaToCircleRRatio <= kHalfPlaneThreshold) { |
| 450 |
useHalfPlaneApprox = true; |
| 451 |
sigmaToCircleRRatioFixed = 0; |
| 452 |
*solidRadius = circleR - 3 * sigma; |
| 453 |
*textureRadius = 6 * sigma; |
| 454 |
} else { |
| 455 |
// Convert to fixed point for the key. |
| 456 |
sigmaToCircleRRatioFixed = SkScalarToFixed(sigmaToCircleRRatio); |
| 457 |
// We shave off some bits to reduce the number of unique entries. We could probably |
| 458 |
// shave off more than we do. |
| 459 |
sigmaToCircleRRatioFixed &= ~0xff; |
| 460 |
sigmaToCircleRRatio = SkFixedToScalar(sigmaToCircleRRatioFixed); |
| 461 |
sigma = circleR * sigmaToCircleRRatio; |
| 462 |
*solidRadius = 0; |
| 463 |
*textureRadius = circleR + 3 * sigma; |
| 464 |
} |
| 465 |
|
| 466 |
static constexpr int kProfileTextureWidth = 512; |
| 467 |
// This would be kProfileTextureWidth/textureRadius if it weren't for the fact that we do |
| 468 |
// the calculation of the profile coord in a coord space that has already been scaled by |
| 469 |
// 1 / textureRadius. This is done to avoid overflow in length(). |
| 470 |
SkMatrix texM = SkMatrix::Scale(kProfileTextureWidth, 1.f); |
| 471 |
|
| 472 |
static const skgpu::UniqueKey::Domain kDomain = skgpu::UniqueKey::GenerateDomain(); |
| 473 |
skgpu::UniqueKey key; |
| 474 |
skgpu::UniqueKey::Builder builder(&key, kDomain, 1, "1-D Circular Blur"); |
| 475 |
builder[0] = sigmaToCircleRRatioFixed; |
| 476 |
builder.finish(); |
| 477 |
|
| 478 |
GrSurfaceProxyView profileView = threadSafeCache->find(key); |
| 479 |
if (profileView) { |
| 480 |
SkASSERT(profileView.asTextureProxy()); |
| 481 |
SkASSERT(profileView.origin() == kTopLeft_GrSurfaceOrigin); |
| 482 |
return GrTextureEffect::Make(std::move(profileView), kPremul_SkAlphaType, texM); |
| 483 |
} |
| 484 |
|
| 485 |
SkBitmap bm; |
| 486 |
if (useHalfPlaneApprox) { |
| 487 |
bm = skgpu::CreateHalfPlaneProfile(kProfileTextureWidth); |
| 488 |
} else { |
| 489 |
// Rescale params to the size of the texture we're creating. |
| 490 |
SkScalar scale = kProfileTextureWidth / *textureRadius; |
| 491 |
bm = skgpu::CreateCircleProfile(sigma * scale, circleR * scale, kProfileTextureWidth); |
| 492 |
} |
| 493 |
|
| 494 |
profileView = std::get<0>(GrMakeUncachedBitmapProxyView(rContext, GrMippedBitmap(bm))); |
| 495 |
if (!profileView) { |
| 496 |
return nullptr; |
| 497 |
} |
| 498 |
|
| 499 |
profileView = threadSafeCache->add(key, profileView); |
| 500 |
return GrTextureEffect::Make(std::move(profileView), kPremul_SkAlphaType, texM); |
| 501 |
} |
| 502 |
|
| 503 |
std::unique_ptr<GrFragmentProcessor> MakeCircleBlur(GrRecordingContext* context, |
| 504 |
const SkRect& circle, |
| 505 |
float sigma) { |
| 506 |
if (skgpu::BlurIsEffectivelyIdentity(sigma)) { |
| 507 |
return nullptr; |
| 508 |
} |
| 509 |
|
| 510 |
float solidRadius; |
| 511 |
float textureRadius; |
| 512 |
std::unique_ptr<GrFragmentProcessor> profile = |
| 513 |
create_profile_effect(context, circle, sigma, &solidRadius, &textureRadius); |
| 514 |
if (!profile) { |
| 515 |
return nullptr; |
| 516 |
} |
| 517 |
|
| 518 |
static const SkRuntimeEffect* effect = SkMakeRuntimeEffect(SkRuntimeEffect::MakeForShader, |
| 519 |
"uniform shader blurProfile;" |
| 520 |
"uniform half4 circleData;" |
| 521 |
|
| 522 |
"half4 main(float2 xy) {" |
| 523 |
// We just want to compute "(length(vec) - circleData.z + 0.5) * circleData.w" but need |
| 524 |
// to rearrange to avoid passing large values to length() that would overflow. |
| 525 |
"half2 vec = half2((sk_FragCoord.xy - circleData.xy) * circleData.w);" |
| 526 |
"half dist = length(vec) + (0.5 - circleData.z) * circleData.w;" |
| 527 |
"return blurProfile.eval(half2(dist, 0.5)).aaaa;" |
| 528 |
"}" |
| 529 |
); |
| 530 |
|
| 531 |
SkV4 circleData = {circle.centerX(), circle.centerY(), solidRadius, 1.f / textureRadius}; |
| 532 |
auto circleBlurFP = GrSkSLFP::Make(effect, "CircleBlur", /*inputFP=*/nullptr, |
| 533 |
GrSkSLFP::OptFlags::kCompatibleWithCoverageAsAlpha, |
| 534 |
"blurProfile", GrSkSLFP::IgnoreOptFlags(std::move(profile)), |
| 535 |
"circleData", circleData); |
| 536 |
// Modulate blur with the input color. |
| 537 |
return GrBlendFragmentProcessor::Make<SkBlendMode::kModulate>(std::move(circleBlurFP), |
| 538 |
/*dst=*/nullptr); |
| 539 |
} |
| 540 |
|
| 541 |
/////////////////////////////////////////////////////////////////////////////// |
| 542 |
// Rect Blur |
| 543 |
/////////////////////////////////////////////////////////////////////////////// |
| 544 |
|
| 545 |
static std::unique_ptr<GrFragmentProcessor> make_rect_integral_fp(GrRecordingContext* rContext, |
| 546 |
float sixSigma) { |
| 547 |
SkASSERT(!skgpu::BlurIsEffectivelyIdentity(sixSigma / 6.f)); |
| 548 |
auto threadSafeCache = rContext->priv().threadSafeCache(); |
| 549 |
|
| 550 |
int width = skgpu::ComputeIntegralTableWidth(sixSigma); |
| 551 |
|
| 552 |
static const skgpu::UniqueKey::Domain kDomain = skgpu::UniqueKey::GenerateDomain(); |
| 553 |
skgpu::UniqueKey key; |
| 554 |
skgpu::UniqueKey::Builder builder(&key, kDomain, 1, "Rect Blur Mask"); |
| 555 |
builder[0] = width; |
| 556 |
builder.finish(); |
| 557 |
|
| 558 |
SkMatrix m = SkMatrix::Scale(width / sixSigma, 1.f); |
| 559 |
|
| 560 |
GrSurfaceProxyView view = threadSafeCache->find(key); |
| 561 |
|
| 562 |
if (view) { |
| 563 |
SkASSERT(view.origin() == kTopLeft_GrSurfaceOrigin); |
| 564 |
return GrTextureEffect::Make( |
| 565 |
std::move(view), kPremul_SkAlphaType, m, GrSamplerState::Filter::kLinear); |
| 566 |
} |
| 567 |
|
| 568 |
SkBitmap bm = skgpu::CreateIntegralTable(width); |
| 569 |
if (bm.empty()) { |
| 570 |
return {}; |
| 571 |
} |
| 572 |
|
| 573 |
view = std::get<0>(GrMakeUncachedBitmapProxyView(rContext, GrMippedBitmap(bm))); |
| 574 |
if (!view) { |
| 575 |
return {}; |
| 576 |
} |
| 577 |
|
| 578 |
view = threadSafeCache->add(key, view); |
| 579 |
|
| 580 |
SkASSERT(view.origin() == kTopLeft_GrSurfaceOrigin); |
| 581 |
return GrTextureEffect::Make( |
| 582 |
std::move(view), kPremul_SkAlphaType, m, GrSamplerState::Filter::kLinear); |
| 583 |
} |
| 584 |
|
| 585 |
std::unique_ptr<GrFragmentProcessor> MakeRectBlur(GrRecordingContext* context, |
| 586 |
const GrShaderCaps& caps, |
| 587 |
const SkRect& srcRect, |
| 588 |
const std::optional<SkRect>& devRect, |
| 589 |
const SkMatrix& viewMatrix, |
| 590 |
float transformedSigma) { |
| 591 |
SkASSERT(srcRect.isSorted()); |
| 592 |
|
| 593 |
if (skgpu::BlurIsEffectivelyIdentity(transformedSigma)) { |
| 594 |
// No need to blur the rect |
| 595 |
return nullptr; |
| 596 |
} |
| 597 |
|
| 598 |
SkMatrix invM; |
| 599 |
SkRect rect; |
| 600 |
if (devRect.has_value()) { |
| 601 |
invM = SkMatrix::I(); |
| 602 |
rect = *devRect; |
| 603 |
} else if (viewMatrix.rectStaysRect()) { |
| 604 |
SkASSERT(viewMatrix.preservesRightAngles()); |
| 605 |
invM = SkMatrix::I(); |
| 606 |
// We can do everything in device space when the src rect projects to a rect in device space |
| 607 |
SkAssertResult(viewMatrix.mapRect(&rect, srcRect)); |
| 608 |
} else { |
| 609 |
// The view matrix may scale, perhaps anisotropically. But we want to apply our device space |
| 610 |
// "transformedSigma" to the delta of frag coord from the rect edges. Factor out the scaling |
| 611 |
// to define a space that is purely rotation/translation from device space (and scale from |
| 612 |
// src space) We'll meet in the middle: pre-scale the src rect to be in this space and then |
| 613 |
// apply the inverse of the rotation/translation portion to the frag coord. |
| 614 |
SkMatrix m; |
| 615 |
SkSize scale; |
| 616 |
if (!viewMatrix.decomposeScale(&scale, &m)) { |
| 617 |
return nullptr; |
| 618 |
} |
| 619 |
if (auto inv = m.invert()) { |
| 620 |
invM = *inv; |
| 621 |
} else { |
| 622 |
return nullptr; |
| 623 |
} |
| 624 |
rect = {srcRect.left() * scale.width(), |
| 625 |
srcRect.top() * scale.height(), |
| 626 |
srcRect.right() * scale.width(), |
| 627 |
srcRect.bottom() * scale.height()}; |
| 628 |
} |
| 629 |
|
| 630 |
if (!caps.fFloatIs32Bits) { |
| 631 |
// We promote the math that gets us into the Gaussian space to full float when the rect |
| 632 |
// coords are large. If we don't have full float then fail. We could probably clip the rect |
| 633 |
// to an outset device bounds instead. |
| 634 |
if (SkScalarAbs(rect.fLeft) > 16000.f || SkScalarAbs(rect.fTop) > 16000.f || |
| 635 |
SkScalarAbs(rect.fRight) > 16000.f || SkScalarAbs(rect.fBottom) > 16000.f) { |
| 636 |
return nullptr; |
| 637 |
} |
| 638 |
} |
| 639 |
|
| 640 |
const float sixSigma = 6 * transformedSigma; |
| 641 |
std::unique_ptr<GrFragmentProcessor> integral = make_rect_integral_fp(context, sixSigma); |
| 642 |
if (!integral) { |
| 643 |
return nullptr; |
| 644 |
} |
| 645 |
|
| 646 |
// In the fast variant we think of the midpoint of the integral texture as aligning with the |
| 647 |
// closest rect edge both in x and y. To simplify texture coord calculation we inset the rect so |
| 648 |
// that the edge of the inset rect corresponds to t = 0 in the texture. It actually simplifies |
| 649 |
// things a bit in the !isFast case, too. |
| 650 |
float threeSigma = sixSigma / 2; |
| 651 |
SkRect insetRect = {rect.left() + threeSigma, |
| 652 |
rect.top() + threeSigma, |
| 653 |
rect.right() - threeSigma, |
| 654 |
rect.bottom() - threeSigma}; |
| 655 |
|
| 656 |
// In our fast variant we find the nearest horizontal and vertical edges and for each do a |
| 657 |
// lookup in the integral texture for each and multiply them. When the rect is less than 6 sigma |
| 658 |
// wide then things aren't so simple and we have to consider both the left and right edge of the |
| 659 |
// rectangle (and similar in y). |
| 660 |
bool isFast = insetRect.isSorted(); |
| 661 |
|
| 662 |
static const SkRuntimeEffect* effect = SkMakeRuntimeEffect(SkRuntimeEffect::MakeForShader, |
| 663 |
// Effect that is a LUT for integral of normal distribution. The value at x:[0,6*sigma] is |
| 664 |
// the integral from -inf to (3*sigma - x). I.e. x is mapped from [0, 6*sigma] to |
| 665 |
// [3*sigma to -3*sigma]. The flip saves a reversal in the shader. |
| 666 |
"uniform shader integral;" |
| 667 |
|
| 668 |
"uniform float4 rect;" |
| 669 |
"uniform int isFast;" // specialized |
| 670 |
|
| 671 |
"half4 main(float2 pos) {" |
| 672 |
"half xCoverage, yCoverage;" |
| 673 |
"if (bool(isFast)) {" |
| 674 |
// Get the smaller of the signed distance from the frag coord to the left and right |
| 675 |
// edges and similar for y. |
| 676 |
// The integral texture goes "backwards" (from 3*sigma to -3*sigma), So, the below |
| 677 |
// computations align the left edge of the integral texture with the inset rect's |
| 678 |
// edge extending outward 6 * sigma from the inset rect. |
| 679 |
"half2 xy = max(half2(rect.LT - pos), half2(pos - rect.RB));" |
| 680 |
"xCoverage = integral.eval(half2(xy.x, 0.5)).a;" |
| 681 |
"yCoverage = integral.eval(half2(xy.y, 0.5)).a;" |
| 682 |
"} else {" |
| 683 |
// We just consider just the x direction here. In practice we compute x and y |
| 684 |
// separately and multiply them together. |
| 685 |
// We define our coord system so that the point at which we're evaluating a kernel |
| 686 |
// defined by the normal distribution (K) at 0. In this coord system let L be left |
| 687 |
// edge and R be the right edge of the rectangle. |
| 688 |
// We can calculate C by integrating K with the half infinite ranges outside the |
| 689 |
// L to R range and subtracting from 1: |
| 690 |
// C = 1 - <integral of K from from -inf to L> - <integral of K from R to inf> |
| 691 |
// K is symmetric about x=0 so: |
| 692 |
// C = 1 - <integral of K from from -inf to L> - <integral of K from -inf to -R> |
| 693 |
|
| 694 |
// The integral texture goes "backwards" (from 3*sigma to -3*sigma) which is |
| 695 |
// factored in to the below calculations. |
| 696 |
// Also, our rect uniform was pre-inset by 3 sigma from the actual rect being |
| 697 |
// blurred, also factored in. |
| 698 |
"half4 rect = half4(half2(rect.LT - pos), half2(pos - rect.RB));" |
| 699 |
"xCoverage = 1 - integral.eval(half2(rect.L, 0.5)).a" |
| 700 |
"- integral.eval(half2(rect.R, 0.5)).a;" |
| 701 |
"yCoverage = 1 - integral.eval(half2(rect.T, 0.5)).a" |
| 702 |
"- integral.eval(half2(rect.B, 0.5)).a;" |
| 703 |
"}" |
| 704 |
"return half4(xCoverage * yCoverage);" |
| 705 |
"}" |
| 706 |
); |
| 707 |
|
| 708 |
std::unique_ptr<GrFragmentProcessor> fp = |
| 709 |
GrSkSLFP::Make(effect, "RectBlur", /*inputFP=*/nullptr, |
| 710 |
GrSkSLFP::OptFlags::kCompatibleWithCoverageAsAlpha, |
| 711 |
"integral", GrSkSLFP::IgnoreOptFlags(std::move(integral)), |
| 712 |
"rect", insetRect, |
| 713 |
"isFast", GrSkSLFP::Specialize<int>(isFast)); |
| 714 |
// Modulate blur with the input color. |
| 715 |
fp = GrBlendFragmentProcessor::Make<SkBlendMode::kModulate>(std::move(fp), |
| 716 |
/*dst=*/nullptr); |
| 717 |
if (!invM.isIdentity()) { |
| 718 |
fp = GrMatrixEffect::Make(invM, std::move(fp)); |
| 719 |
} |
| 720 |
return GrFragmentProcessor::DeviceSpace(std::move(fp)); |
| 721 |
} |
| 722 |
|
| 723 |
/////////////////////////////////////////////////////////////////////////////// |
| 724 |
// RRect Blur |
| 725 |
/////////////////////////////////////////////////////////////////////////////// |
| 726 |
|
| 727 |
static constexpr auto kBlurredRRectMaskOrigin = kTopLeft_GrSurfaceOrigin; |
| 728 |
|
| 729 |
static void make_blurred_rrect_key(skgpu::UniqueKey* key, |
| 730 |
const SkRRect& rrectToDraw, |
| 731 |
float xformedSigma) { |
| 732 |
SkASSERT(!skgpu::BlurIsEffectivelyIdentity(xformedSigma)); |
| 733 |
static const skgpu::UniqueKey::Domain kDomain = skgpu::UniqueKey::GenerateDomain(); |
| 734 |
|
| 735 |
skgpu::UniqueKey::Builder builder(key, kDomain, 9, "RoundRect Blur Mask"); |
| 736 |
builder[0] = SkScalarCeilToInt(xformedSigma - 1 / 6.0f); |
| 737 |
|
| 738 |
int index = 1; |
| 739 |
// TODO: this is overkill for _simple_ circular rrects |
| 740 |
for (auto c : {SkRRect::kUpperLeft_Corner, |
| 741 |
SkRRect::kUpperRight_Corner, |
| 742 |
SkRRect::kLowerRight_Corner, |
| 743 |
SkRRect::kLowerLeft_Corner}) { |
| 744 |
SkASSERT(SkScalarIsInt(rrectToDraw.radii(c).fX) && SkScalarIsInt(rrectToDraw.radii(c).fY)); |
| 745 |
builder[index++] = SkScalarCeilToInt(rrectToDraw.radii(c).fX); |
| 746 |
builder[index++] = SkScalarCeilToInt(rrectToDraw.radii(c).fY); |
| 747 |
} |
| 748 |
builder.finish(); |
| 749 |
} |
| 750 |
|
| 751 |
static bool fillin_view_on_gpu(GrDirectContext* dContext, |
| 752 |
const GrSurfaceProxyView& lazyView, |
| 753 |
GrThreadSafeCache::Trampoline* trampoline, |
| 754 |
const SkRRect& rrectToDraw, |
| 755 |
const SkISize& dimensions, |
| 756 |
float xformedSigma) { |
| 757 |
SkASSERT(!skgpu::BlurIsEffectivelyIdentity(xformedSigma)); |
| 758 |
|
| 759 |
// We cache blur masks. Use default surface props here so we can use the same cached mask |
| 760 |
// regardless of the final dst surface. |
| 761 |
SkSurfaceProps defaultSurfaceProps; |
| 762 |
|
| 763 |
std::unique_ptr<skgpu::ganesh::SurfaceDrawContext> sdc = |
| 764 |
skgpu::ganesh::SurfaceDrawContext::MakeWithFallback(dContext, |
| 765 |
GrColorType::kAlpha_8, |
| 766 |
nullptr, |
| 767 |
SkBackingFit::kExact, |
| 768 |
dimensions, |
| 769 |
defaultSurfaceProps, |
| 770 |
1, |
| 771 |
skgpu::Mipmapped::kNo, |
| 772 |
GrProtected::kNo, |
| 773 |
kBlurredRRectMaskOrigin); |
| 774 |
if (!sdc) { |
| 775 |
return false; |
| 776 |
} |
| 777 |
|
| 778 |
GrPaint paint; |
| 779 |
|
| 780 |
sdc->clear(SK_PMColor4fTRANSPARENT); |
| 781 |
sdc->drawRRect(nullptr, |
| 782 |
std::move(paint), |
| 783 |
GrAA::kYes, |
| 784 |
SkMatrix::I(), |
| 785 |
rrectToDraw, |
| 786 |
GrStyle::SimpleFill()); |
| 787 |
|
| 788 |
GrSurfaceProxyView srcView = sdc->readSurfaceView(); |
| 789 |
SkASSERT(srcView.asTextureProxy()); |
| 790 |
auto rtc2 = GaussianBlur(dContext, |
| 791 |
std::move(srcView), |
| 792 |
sdc->colorInfo().colorType(), |
| 793 |
sdc->colorInfo().alphaType(), |
| 794 |
nullptr, |
| 795 |
SkIRect::MakeSize(dimensions), |
| 796 |
SkIRect::MakeSize(dimensions), |
| 797 |
xformedSigma, |
| 798 |
xformedSigma, |
| 799 |
SkTileMode::kClamp, |
| 800 |
SkBackingFit::kExact); |
| 801 |
if (!rtc2 || !rtc2->readSurfaceView()) { |
| 802 |
return false; |
| 803 |
} |
| 804 |
|
| 805 |
auto view = rtc2->readSurfaceView(); |
| 806 |
SkASSERT(view.swizzle() == lazyView.swizzle()); |
| 807 |
SkASSERT(view.origin() == lazyView.origin()); |
| 808 |
trampoline->fProxy = view.asTextureProxyRef(); |
| 809 |
|
| 810 |
return true; |
| 811 |
} |
| 812 |
|
| 813 |
// Create a cpu-side blurred-rrect mask that is close to the version the gpu would've produced. |
| 814 |
// The match needs to be close bc the cpu- and gpu-generated version must be interchangeable. |
| 815 |
static GrSurfaceProxyView create_mask_on_cpu(GrRecordingContext* rContext, |
| 816 |
const SkRRect& rrectToDraw, |
| 817 |
const SkISize& dimensions, |
| 818 |
float xformedSigma) { |
| 819 |
SkBitmap result = skgpu::CreateRRectBlurMask(rrectToDraw, dimensions, xformedSigma); |
| 820 |
if (result.empty()) { |
| 821 |
return {}; |
| 822 |
} |
| 823 |
|
| 824 |
auto view = std::get<0>(GrMakeUncachedBitmapProxyView(rContext, GrMippedBitmap(result))); |
| 825 |
if (!view) { |
| 826 |
return {}; |
| 827 |
} |
| 828 |
|
| 829 |
SkASSERT(view.origin() == kBlurredRRectMaskOrigin); |
| 830 |
return view; |
| 831 |
} |
| 832 |
|
| 833 |
static std::unique_ptr<GrFragmentProcessor> find_or_create_rrect_blur_mask_fp( |
| 834 |
GrRecordingContext* rContext, |
| 835 |
const SkRRect& rrectToDraw, |
| 836 |
const SkISize& dimensions, |
| 837 |
float xformedSigma) { |
| 838 |
SkASSERT(!skgpu::BlurIsEffectivelyIdentity(xformedSigma)); |
| 839 |
skgpu::UniqueKey key; |
| 840 |
make_blurred_rrect_key(&key, rrectToDraw, xformedSigma); |
| 841 |
|
| 842 |
auto threadSafeCache = rContext->priv().threadSafeCache(); |
| 843 |
|
| 844 |
// It seems like we could omit this matrix and modify the shader code to not normalize |
| 845 |
// the coords used to sample the texture effect. However, the "proxyDims" value in the |
| 846 |
// shader is not always the actual the proxy dimensions. This is because 'dimensions' here |
| 847 |
// was computed using integer corner radii as determined in |
| 848 |
// SkComputeBlurredRRectParams whereas the shader code uses the float radius to compute |
| 849 |
// 'proxyDims'. Why it draws correctly with these unequal values is a mystery for the ages. |
| 850 |
auto m = SkMatrix::Scale(dimensions.width(), dimensions.height()); |
| 851 |
|
| 852 |
GrSurfaceProxyView view; |
| 853 |
|
| 854 |
if (GrDirectContext* dContext = rContext->asDirectContext()) { |
| 855 |
// The gpu thread gets priority over the recording threads. If the gpu thread is first, |
| 856 |
// it crams a lazy proxy into the cache and then fills it in later. |
| 857 |
auto [lazyView, trampoline] = GrThreadSafeCache::CreateLazyView(dContext, |
| 858 |
GrColorType::kAlpha_8, |
| 859 |
dimensions, |
| 860 |
kBlurredRRectMaskOrigin, |
| 861 |
SkBackingFit::kExact); |
| 862 |
if (!lazyView) { |
| 863 |
return nullptr; |
| 864 |
} |
| 865 |
|
| 866 |
view = threadSafeCache->findOrAdd(key, lazyView); |
| 867 |
if (view != lazyView) { |
| 868 |
SkASSERT(view.asTextureProxy()); |
| 869 |
SkASSERT(view.origin() == kBlurredRRectMaskOrigin); |
| 870 |
return GrTextureEffect::Make(std::move(view), kPremul_SkAlphaType, m); |
| 871 |
} |
| 872 |
|
| 873 |
if (!fillin_view_on_gpu(dContext, |
| 874 |
lazyView, |
| 875 |
trampoline.get(), |
| 876 |
rrectToDraw, |
| 877 |
dimensions, |
| 878 |
xformedSigma)) { |
| 879 |
// In this case something has gone disastrously wrong so set up to drop the draw |
| 880 |
// that needed this resource and reduce future pollution of the cache. |
| 881 |
threadSafeCache->remove(key); |
| 882 |
return nullptr; |
| 883 |
} |
| 884 |
} else { |
| 885 |
view = threadSafeCache->find(key); |
| 886 |
if (view) { |
| 887 |
SkASSERT(view.asTextureProxy()); |
| 888 |
SkASSERT(view.origin() == kBlurredRRectMaskOrigin); |
| 889 |
return GrTextureEffect::Make(std::move(view), kPremul_SkAlphaType, m); |
| 890 |
} |
| 891 |
|
| 892 |
view = create_mask_on_cpu(rContext, rrectToDraw, dimensions, xformedSigma); |
| 893 |
if (!view) { |
| 894 |
return nullptr; |
| 895 |
} |
| 896 |
|
| 897 |
view = threadSafeCache->add(key, view); |
| 898 |
} |
| 899 |
|
| 900 |
SkASSERT(view.asTextureProxy()); |
| 901 |
SkASSERT(view.origin() == kBlurredRRectMaskOrigin); |
| 902 |
return GrTextureEffect::Make(std::move(view), kPremul_SkAlphaType, m); |
| 903 |
} |
| 904 |
|
| 905 |
std::unique_ptr<GrFragmentProcessor> MakeRRectBlur(GrRecordingContext* context, |
| 906 |
float sigma, |
| 907 |
float xformedSigma, |
| 908 |
const SkRRect& srcRRect, |
| 909 |
const SkRRect& devRRect) { |
| 910 |
SkASSERTF(!SkRRectPriv::IsCircle(devRRect), |
| 911 |
"Unexpected circle. %d\n\t%s\n\t%s", |
| 912 |
SkRRectPriv::IsCircle(srcRRect), |
| 913 |
srcRRect.dumpToString(true).c_str(), |
| 914 |
devRRect.dumpToString(true).c_str()); |
| 915 |
SkASSERTF(!devRRect.isRect(), |
| 916 |
"Unexpected rect. %d\n\t%s\n\t%s", |
| 917 |
srcRRect.isRect(), |
| 918 |
srcRRect.dumpToString(true).c_str(), |
| 919 |
devRRect.dumpToString(true).c_str()); |
| 920 |
|
| 921 |
// TODO: loosen this up |
| 922 |
if (!SkRRectPriv::IsSimpleCircular(devRRect)) { |
| 923 |
return nullptr; |
| 924 |
} |
| 925 |
|
| 926 |
if (skgpu::BlurIsEffectivelyIdentity(xformedSigma)) { |
| 927 |
return nullptr; |
| 928 |
} |
| 929 |
|
| 930 |
// Make sure we can successfully ninepatch this rrect -- the blur sigma has to be sufficiently |
| 931 |
// small relative to both the size of the corner radius and the width (and height) of the rrect. |
| 932 |
SkRRect rrectToDraw; |
| 933 |
SkISize dimensions; |
| 934 |
SkScalar ignored[kBlurRRectMaxDivisions]; |
| 935 |
|
| 936 |
bool ninePatchable = ComputeBlurredRRectParams(srcRRect, |
| 937 |
devRRect, |
| 938 |
sigma, |
| 939 |
xformedSigma, |
| 940 |
&rrectToDraw, |
| 941 |
&dimensions, |
| 942 |
ignored, |
| 943 |
ignored, |
| 944 |
ignored, |
| 945 |
ignored); |
| 946 |
if (!ninePatchable) { |
| 947 |
return nullptr; |
| 948 |
} |
| 949 |
|
| 950 |
std::unique_ptr<GrFragmentProcessor> maskFP = |
| 951 |
find_or_create_rrect_blur_mask_fp(context, rrectToDraw, dimensions, xformedSigma); |
| 952 |
if (!maskFP) { |
| 953 |
return nullptr; |
| 954 |
} |
| 955 |
|
| 956 |
static const SkRuntimeEffect* effect = SkMakeRuntimeEffect(SkRuntimeEffect::MakeForShader, |
| 957 |
"uniform shader ninePatchFP;" |
| 958 |
|
| 959 |
"uniform half cornerRadius;" |
| 960 |
"uniform float4 proxyRect;" |
| 961 |
"uniform half blurRadius;" |
| 962 |
|
| 963 |
"half4 main(float2 xy) {" |
| 964 |
// Warp the fragment position to the appropriate part of the 9-patch blur texture by |
| 965 |
// snipping out the middle section of the proxy rect. |
| 966 |
"float2 translatedFragPosFloat = sk_FragCoord.xy - proxyRect.LT;" |
| 967 |
"float2 proxyCenter = (proxyRect.RB - proxyRect.LT) * 0.5;" |
| 968 |
"half edgeSize = 2.0 * blurRadius + cornerRadius + 0.5;" |
| 969 |
|
| 970 |
// Position the fragment so that (0, 0) marks the center of the proxy rectangle. |
| 971 |
// Negative coordinates are on the left/top side and positive numbers are on the |
| 972 |
// right/bottom. |
| 973 |
"translatedFragPosFloat -= proxyCenter;" |
| 974 |
|
| 975 |
// Temporarily strip off the fragment's sign. x/y are now strictly increasing as we |
| 976 |
// move away from the center. |
| 977 |
"half2 fragDirection = half2(sign(translatedFragPosFloat));" |
| 978 |
"translatedFragPosFloat = abs(translatedFragPosFloat);" |
| 979 |
|
| 980 |
// Our goal is to snip out the "middle section" of the proxy rect (everything but the |
| 981 |
// edge). We've repositioned our fragment position so that (0, 0) is the centerpoint |
| 982 |
// and x/y are always positive, so we can subtract here and interpret negative results |
| 983 |
// as being within the middle section. |
| 984 |
"half2 translatedFragPosHalf = half2(translatedFragPosFloat - (proxyCenter - edgeSize));" |
| 985 |
|
| 986 |
// Remove the middle section by clamping to zero. |
| 987 |
"translatedFragPosHalf = max(translatedFragPosHalf, 0);" |
| 988 |
|
| 989 |
// Reapply the fragment's sign, so that negative coordinates once again mean left/top |
| 990 |
// side and positive means bottom/right side. |
| 991 |
"translatedFragPosHalf *= fragDirection;" |
| 992 |
|
| 993 |
// Offset the fragment so that (0, 0) marks the upper-left again, instead of the center |
| 994 |
// point. |
| 995 |
"translatedFragPosHalf += half2(edgeSize);" |
| 996 |
|
| 997 |
"half2 proxyDims = half2(2.0 * edgeSize);" |
| 998 |
"half2 texCoord = translatedFragPosHalf / proxyDims;" |
| 999 |
|
| 1000 |
"return ninePatchFP.eval(texCoord).aaaa;" |
| 1001 |
"}" |
| 1002 |
); |
| 1003 |
|
| 1004 |
float cornerRadius = SkRRectPriv::GetSimpleRadii(devRRect).fX; |
| 1005 |
float blurRadius = 3.f * SkScalarCeilToScalar(xformedSigma - 1 / 6.0f); |
| 1006 |
SkRect proxyRect = devRRect.getBounds().makeOutset(blurRadius, blurRadius); |
| 1007 |
|
| 1008 |
auto rrectBlurFP = GrSkSLFP::Make(effect, "RRectBlur", /*inputFP=*/nullptr, |
| 1009 |
GrSkSLFP::OptFlags::kCompatibleWithCoverageAsAlpha, |
| 1010 |
"ninePatchFP", GrSkSLFP::IgnoreOptFlags(std::move(maskFP)), |
| 1011 |
"cornerRadius", cornerRadius, |
| 1012 |
"proxyRect", proxyRect, |
| 1013 |
"blurRadius", blurRadius); |
| 1014 |
// Modulate blur with the input color. |
| 1015 |
return GrBlendFragmentProcessor::Make<SkBlendMode::kModulate>(std::move(rrectBlurFP), |
| 1016 |
/*dst=*/nullptr); |
| 1017 |
} |
| 1018 |
|
| 1019 |
/** |
| 1020 |
* Try to directly render the mask filter into the target. Returns true if drawing was |
| 1021 |
* successful. If false is returned then paint is unmodified. |
| 1022 |
*/ |
| 1023 |
static bool direct_filter_mask(GrRecordingContext* context, |
| 1024 |
const SkMaskFilterBase* maskFilter, |
| 1025 |
skgpu::ganesh::SurfaceDrawContext* sdc, |
| 1026 |
GrPaint&& paint, |
| 1027 |
const GrClip* clip, |
| 1028 |
const SkMatrix& viewMatrix, |
| 1029 |
const GrStyledShape& shape) { |
| 1030 |
SkASSERT(sdc); |
| 1031 |
if (maskFilter->type() != SkMaskFilterBase::Type::kBlur) { |
| 1032 |
return false; |
| 1033 |
} |
| 1034 |
auto bmf = static_cast<const SkBlurMaskFilterImpl*>(maskFilter); |
| 1035 |
|
| 1036 |
if (bmf->blurStyle() != kNormal_SkBlurStyle) { |
| 1037 |
return false; |
| 1038 |
} |
| 1039 |
|
| 1040 |
// TODO: we could handle blurred stroked circles |
| 1041 |
if (!shape.style().isSimpleFill()) { |
| 1042 |
return false; |
| 1043 |
} |
| 1044 |
|
| 1045 |
SkScalar xformedSigma = bmf->computeXformedSigma(viewMatrix); |
| 1046 |
if (skgpu::BlurIsEffectivelyIdentity(xformedSigma)) { |
| 1047 |
sdc->drawShape(clip, std::move(paint), GrAA::kYes, viewMatrix, GrStyledShape(shape)); |
| 1048 |
return true; |
| 1049 |
} |
| 1050 |
|
| 1051 |
SkRRect srcRRect; |
| 1052 |
bool inverted; |
| 1053 |
if (!shape.asRRect(&srcRRect, &inverted) || inverted) { |
| 1054 |
return false; |
| 1055 |
} |
| 1056 |
|
| 1057 |
std::unique_ptr<GrFragmentProcessor> fp; |
| 1058 |
|
| 1059 |
auto devRRect = srcRRect.transform(viewMatrix); |
| 1060 |
|
| 1061 |
bool devRRectIsRect = devRRect.has_value() && (*devRRect).isRect(); |
| 1062 |
bool devRRectIsCircle = devRRect.has_value() && SkRRectPriv::IsCircle(*devRRect); |
| 1063 |
|
| 1064 |
bool canBeRect = (srcRRect.isRect() && viewMatrix.preservesRightAngles()) || devRRectIsRect; |
| 1065 |
bool canBeCircle = (SkRRectPriv::IsCircle(srcRRect) && viewMatrix.isSimilarity()) || |
| 1066 |
devRRectIsCircle; |
| 1067 |
|
| 1068 |
if (canBeRect || canBeCircle) { |
| 1069 |
if (canBeRect) { |
| 1070 |
fp = MakeRectBlur(context, *context->priv().caps()->shaderCaps(), srcRRect.rect(), |
| 1071 |
devRRectIsRect ? std::optional((*devRRect).rect()) : std::nullopt, |
| 1072 |
viewMatrix, xformedSigma); |
| 1073 |
} else { |
| 1074 |
SkRect devBounds; |
| 1075 |
if (devRRectIsCircle) { |
| 1076 |
devBounds = devRRect->getBounds(); |
| 1077 |
} else { |
| 1078 |
SkPoint center = viewMatrix.mapPoint(srcRRect.getBounds().center()); |
| 1079 |
SkScalar radius = viewMatrix.mapVector(0, srcRRect.width()/2.f).length(); |
| 1080 |
devBounds = {center.x() - radius, |
| 1081 |
center.y() - radius, |
| 1082 |
center.x() + radius, |
| 1083 |
center.y() + radius}; |
| 1084 |
} |
| 1085 |
fp = MakeCircleBlur(context, devBounds, xformedSigma); |
| 1086 |
} |
| 1087 |
|
| 1088 |
if (!fp) { |
| 1089 |
return false; |
| 1090 |
} |
| 1091 |
|
| 1092 |
SkRect srcProxyRect = srcRRect.rect(); |
| 1093 |
// Determine how much to outset the src rect to ensure we hit pixels within three sigma. |
| 1094 |
SkScalar outsetX = 3.0f*xformedSigma; |
| 1095 |
SkScalar outsetY = 3.0f*xformedSigma; |
| 1096 |
if (viewMatrix.isScaleTranslate()) { |
| 1097 |
outsetX /= SkScalarAbs(viewMatrix.getScaleX()); |
| 1098 |
outsetY /= SkScalarAbs(viewMatrix.getScaleY()); |
| 1099 |
} else { |
| 1100 |
SkSize scale; |
| 1101 |
if (!viewMatrix.decomposeScale(&scale, nullptr)) { |
| 1102 |
return false; |
| 1103 |
} |
| 1104 |
outsetX /= scale.width(); |
| 1105 |
outsetY /= scale.height(); |
| 1106 |
} |
| 1107 |
srcProxyRect.outset(outsetX, outsetY); |
| 1108 |
|
| 1109 |
paint.setCoverageFragmentProcessor(std::move(fp)); |
| 1110 |
sdc->drawRect(clip, std::move(paint), GrAA::kNo, viewMatrix, srcProxyRect); |
| 1111 |
return true; |
| 1112 |
} |
| 1113 |
if (!viewMatrix.rectStaysRect()) { |
| 1114 |
return false; |
| 1115 |
} |
| 1116 |
|
| 1117 |
if (!devRRect.has_value() || !SkRRectPriv::AllCornersCircular(*devRRect)) { |
| 1118 |
return false; |
| 1119 |
} |
| 1120 |
|
| 1121 |
fp = MakeRRectBlur(context, bmf->sigma(), xformedSigma, srcRRect, *devRRect); |
| 1122 |
if (!fp) { |
| 1123 |
return false; |
| 1124 |
} |
| 1125 |
|
| 1126 |
if (!bmf->ignoreXform()) { |
| 1127 |
SkRect srcProxyRect = srcRRect.rect(); |
| 1128 |
srcProxyRect.outset(3.0f*bmf->sigma(), 3.0f*bmf->sigma()); |
| 1129 |
paint.setCoverageFragmentProcessor(std::move(fp)); |
| 1130 |
sdc->drawRect(clip, std::move(paint), GrAA::kNo, viewMatrix, srcProxyRect); |
| 1131 |
} else { |
| 1132 |
auto inverse = viewMatrix.invert(); |
| 1133 |
if (!inverse) { |
| 1134 |
return false; |
| 1135 |
} |
| 1136 |
|
| 1137 |
float extra=3.f*SkScalarCeilToScalar(xformedSigma-1/6.0f); |
| 1138 |
SkIRect proxyBounds = devRRect->rect().makeOutset(extra, extra).roundOut(); |
| 1139 |
|
| 1140 |
paint.setCoverageFragmentProcessor(std::move(fp)); |
| 1141 |
sdc->fillPixelsWithLocalMatrix(clip, std::move(paint), proxyBounds, *inverse); |
| 1142 |
} |
| 1143 |
|
| 1144 |
return true; |
| 1145 |
} |
| 1146 |
|
| 1147 |
// The key and clip-bounds are computed together because the caching decision can impact the |
| 1148 |
// clip-bound - since we only cache un-clipped masks the clip can be removed entirely. |
| 1149 |
// A 'false' return value indicates that the shape is known to be clipped away. |
| 1150 |
static bool compute_key_and_clip_bounds(skgpu::UniqueKey* maskKey, |
| 1151 |
SkIRect* boundsForClip, |
| 1152 |
const GrCaps* caps, |
| 1153 |
const SkMatrix& viewMatrix, |
| 1154 |
bool inverseFilled, |
| 1155 |
const SkMaskFilterBase* maskFilter, |
| 1156 |
const GrStyledShape& shape, |
| 1157 |
const SkIRect& unclippedDevShapeBounds, |
| 1158 |
const SkIRect& devClipBounds) { |
| 1159 |
SkASSERT(maskFilter); |
| 1160 |
*boundsForClip = devClipBounds; |
| 1161 |
|
| 1162 |
#ifndef SK_DISABLE_MASKFILTERED_MASK_CACHING |
| 1163 |
// To prevent overloading the cache with entries during animations we limit the cache of masks |
| 1164 |
// to cases where the matrix preserves axis alignment. |
| 1165 |
bool useCache = !inverseFilled && viewMatrix.preservesAxisAlignment() && |
| 1166 |
shape.hasUnstyledKey() && as_MFB(maskFilter)->asABlur(nullptr); |
| 1167 |
|
| 1168 |
if (useCache) { |
| 1169 |
SkIRect clippedMaskRect, unClippedMaskRect; |
| 1170 |
can_filter_mask(maskFilter, shape, unclippedDevShapeBounds, devClipBounds, |
| 1171 |
viewMatrix, &clippedMaskRect); |
| 1172 |
if (clippedMaskRect.isEmpty()) { |
| 1173 |
return false; |
| 1174 |
} |
| 1175 |
can_filter_mask(maskFilter, shape, unclippedDevShapeBounds, unclippedDevShapeBounds, |
| 1176 |
viewMatrix, &unClippedMaskRect); |
| 1177 |
|
| 1178 |
// Use the cache only if >50% of the filtered mask is visible. |
| 1179 |
int unclippedWidth = unClippedMaskRect.width(); |
| 1180 |
int unclippedHeight = unClippedMaskRect.height(); |
| 1181 |
int64_t unclippedArea = sk_64_mul(unclippedWidth, unclippedHeight); |
| 1182 |
int64_t clippedArea = sk_64_mul(clippedMaskRect.width(), clippedMaskRect.height()); |
| 1183 |
int maxTextureSize = caps->maxTextureSize(); |
| 1184 |
if (unclippedArea > 2 * clippedArea || unclippedWidth > maxTextureSize || |
| 1185 |
unclippedHeight > maxTextureSize) { |
| 1186 |
useCache = false; |
| 1187 |
} else { |
| 1188 |
// Make the clip not affect the mask |
| 1189 |
*boundsForClip = unclippedDevShapeBounds; |
| 1190 |
} |
| 1191 |
} |
| 1192 |
|
| 1193 |
if (useCache) { |
| 1194 |
static const skgpu::UniqueKey::Domain kDomain = skgpu::UniqueKey::GenerateDomain(); |
| 1195 |
skgpu::UniqueKey::Builder builder(maskKey, kDomain, 5 + 2 + shape.unstyledKeySize(), |
| 1196 |
"Mask Filtered Masks"); |
| 1197 |
|
| 1198 |
// We require the upper left 2x2 of the matrix to match exactly for a cache hit. |
| 1199 |
SkScalar sx = viewMatrix.get(SkMatrix::kMScaleX); |
| 1200 |
SkScalar sy = viewMatrix.get(SkMatrix::kMScaleY); |
| 1201 |
SkScalar kx = viewMatrix.get(SkMatrix::kMSkewX); |
| 1202 |
SkScalar ky = viewMatrix.get(SkMatrix::kMSkewY); |
| 1203 |
SkScalar tx = viewMatrix.get(SkMatrix::kMTransX); |
| 1204 |
SkScalar ty = viewMatrix.get(SkMatrix::kMTransY); |
| 1205 |
// Allow 8 bits each in x and y of subpixel positioning. But, note that we're allowing |
| 1206 |
// reuse for integer translations. |
| 1207 |
SkFixed fracX = SkScalarToFixed(SkScalarFraction(tx)) & 0x0000FF00; |
| 1208 |
SkFixed fracY = SkScalarToFixed(SkScalarFraction(ty)) & 0x0000FF00; |
| 1209 |
|
| 1210 |
builder[0] = SkFloat2Bits(sx); |
| 1211 |
builder[1] = SkFloat2Bits(sy); |
| 1212 |
builder[2] = SkFloat2Bits(kx); |
| 1213 |
builder[3] = SkFloat2Bits(ky); |
| 1214 |
// Distinguish between hairline and filled paths. For hairlines, we also need to include |
| 1215 |
// the cap. (SW grows hairlines by 0.5 pixel with round and square caps). Note that |
| 1216 |
// stroke-and-fill of hairlines is turned into pure fill by SkStrokeRec, so this covers |
| 1217 |
// all cases we might see. |
| 1218 |
uint32_t styleBits = shape.style().isSimpleHairline() |
| 1219 |
? ((shape.style().strokeRec().getCap() << 1) | 1) |
| 1220 |
: 0; |
| 1221 |
builder[4] = fracX | (fracY >> 8) | (styleBits << 16); |
| 1222 |
|
| 1223 |
SkMaskFilterBase::BlurRec rec; |
| 1224 |
SkAssertResult(as_MFB(maskFilter)->asABlur(&rec)); |
| 1225 |
|
| 1226 |
builder[5] = rec.fStyle; // TODO: we could put this with the other style bits |
| 1227 |
builder[6] = SkFloat2Bits(rec.fSigma); |
| 1228 |
shape.writeUnstyledKey(&builder[7]); |
| 1229 |
} |
| 1230 |
#endif |
| 1231 |
|
| 1232 |
return true; |
| 1233 |
} |
| 1234 |
|
| 1235 |
/** |
| 1236 |
* This function is used to implement filters that require an explicit src mask. It should only |
| 1237 |
* be called if can_filter_mask returned true and the maskRect param should be the output from |
| 1238 |
* that call. |
| 1239 |
* Implementations are free to get the GrContext from the src texture in order to create |
| 1240 |
* additional textures and perform multiple passes. |
| 1241 |
*/ |
| 1242 |
static GrSurfaceProxyView filter_mask(GrRecordingContext* context, |
| 1243 |
const SkMaskFilterBase* maskFilter, |
| 1244 |
GrSurfaceProxyView srcView, |
| 1245 |
GrColorType srcColorType, |
| 1246 |
SkAlphaType srcAlphaType, |
| 1247 |
const SkMatrix& ctm, |
| 1248 |
const SkIRect& maskRect) { |
| 1249 |
if (maskFilter->type() != SkMaskFilterBase::Type::kBlur) { |
| 1250 |
return {}; |
| 1251 |
} |
| 1252 |
auto bmf = static_cast<const SkBlurMaskFilterImpl*>(maskFilter); |
| 1253 |
// 'maskRect' isn't snapped to the UL corner but the mask in 'src' is. |
| 1254 |
const SkIRect clipRect = SkIRect::MakeWH(maskRect.width(), maskRect.height()); |
| 1255 |
|
| 1256 |
SkScalar xformedSigma = bmf->computeXformedSigma(ctm); |
| 1257 |
|
| 1258 |
// If we're doing a normal blur, we can clobber the pathTexture in the |
| 1259 |
// gaussianBlur. Otherwise, we need to save it for later compositing. |
| 1260 |
bool isNormalBlur = (kNormal_SkBlurStyle == bmf->blurStyle()); |
| 1261 |
auto srcBounds = SkIRect::MakeSize(srcView.proxy()->dimensions()); |
| 1262 |
auto surfaceDrawContext = GaussianBlur(context, |
| 1263 |
srcView, |
| 1264 |
srcColorType, |
| 1265 |
srcAlphaType, |
| 1266 |
nullptr, |
| 1267 |
clipRect, |
| 1268 |
srcBounds, |
| 1269 |
xformedSigma, |
| 1270 |
xformedSigma, |
| 1271 |
SkTileMode::kClamp); |
| 1272 |
if (!surfaceDrawContext || !surfaceDrawContext->asTextureProxy()) { |
| 1273 |
return {}; |
| 1274 |
} |
| 1275 |
|
| 1276 |
if (!isNormalBlur) { |
| 1277 |
GrPaint paint; |
| 1278 |
// Blend pathTexture over blurTexture. |
| 1279 |
paint.setCoverageFragmentProcessor(GrTextureEffect::Make(std::move(srcView), srcAlphaType)); |
| 1280 |
if (kInner_SkBlurStyle == bmf->blurStyle()) { |
| 1281 |
// inner: dst = dst * src |
| 1282 |
paint.setCoverageSetOpXPFactory(SkRegion::kIntersect_Op); |
| 1283 |
} else if (kSolid_SkBlurStyle == bmf->blurStyle()) { |
| 1284 |
// solid: dst = src + dst - src * dst |
| 1285 |
// = src + (1 - src) * dst |
| 1286 |
paint.setCoverageSetOpXPFactory(SkRegion::kUnion_Op); |
| 1287 |
} else if (kOuter_SkBlurStyle == bmf->blurStyle()) { |
| 1288 |
// outer: dst = dst * (1 - src) |
| 1289 |
// = 0 * src + (1 - src) * dst |
| 1290 |
paint.setCoverageSetOpXPFactory(SkRegion::kDifference_Op); |
| 1291 |
} else { |
| 1292 |
paint.setCoverageSetOpXPFactory(SkRegion::kReplace_Op); |
| 1293 |
} |
| 1294 |
|
| 1295 |
surfaceDrawContext->fillPixelsWithLocalMatrix(nullptr, std::move(paint), clipRect, |
| 1296 |
SkMatrix::I()); |
| 1297 |
} |
| 1298 |
|
| 1299 |
return surfaceDrawContext->readSurfaceView(); |
| 1300 |
} |
| 1301 |
|
| 1302 |
static GrSurfaceProxyView hw_create_filtered_mask(GrDirectContext* dContext, |
| 1303 |
skgpu::ganesh::SurfaceDrawContext* sdc, |
| 1304 |
const SkMatrix& viewMatrix, |
| 1305 |
const GrStyledShape& shape, |
| 1306 |
const SkMaskFilterBase* filter, |
| 1307 |
const SkIRect& unclippedDevShapeBounds, |
| 1308 |
const SkIRect& clipBounds, |
| 1309 |
SkIRect* maskRect, |
| 1310 |
skgpu::UniqueKey* key) { |
| 1311 |
if (!can_filter_mask(filter, shape, unclippedDevShapeBounds, clipBounds, viewMatrix, |
| 1312 |
maskRect)) { |
| 1313 |
return {}; |
| 1314 |
} |
| 1315 |
|
| 1316 |
if (clip_bounds_quick_reject(clipBounds, *maskRect)) { |
| 1317 |
// clipped out |
| 1318 |
return {}; |
| 1319 |
} |
| 1320 |
|
| 1321 |
auto threadSafeCache = dContext->priv().threadSafeCache(); |
| 1322 |
|
| 1323 |
GrSurfaceProxyView lazyView; |
| 1324 |
sk_sp<GrThreadSafeCache::Trampoline> trampoline; |
| 1325 |
|
| 1326 |
if (key->isValid()) { |
| 1327 |
// In this case, we want GPU-filtered masks to have priority over SW-generated ones so |
| 1328 |
// we pre-emptively add a lazy-view to the cache and fill it in later. |
| 1329 |
std::tie(lazyView, trampoline) = GrThreadSafeCache::CreateLazyView( |
| 1330 |
dContext, GrColorType::kAlpha_8, maskRect->size(), |
| 1331 |
kMaskOrigin, SkBackingFit::kApprox); |
| 1332 |
if (!lazyView) { |
| 1333 |
return {}; // fall back to a SW-created mask - 'create_mask_GPU' probably won't succeed |
| 1334 |
} |
| 1335 |
|
| 1336 |
key->setCustomData(create_data(*maskRect, unclippedDevShapeBounds)); |
| 1337 |
auto [cachedView, data] = threadSafeCache->findOrAddWithData(*key, lazyView); |
| 1338 |
if (cachedView != lazyView) { |
| 1339 |
// In this case, the gpu-thread lost out to a recording thread - use its result. |
| 1340 |
SkASSERT(data); |
| 1341 |
SkASSERT(cachedView.asTextureProxy()); |
| 1342 |
SkASSERT(cachedView.origin() == kMaskOrigin); |
| 1343 |
|
| 1344 |
*maskRect = extract_draw_rect_from_data(data.get(), unclippedDevShapeBounds); |
| 1345 |
return cachedView; |
| 1346 |
} |
| 1347 |
} |
| 1348 |
|
| 1349 |
std::unique_ptr<skgpu::ganesh::SurfaceDrawContext> maskSDC( |
| 1350 |
create_mask_GPU(dContext, *maskRect, viewMatrix, shape, sdc->numSamples())); |
| 1351 |
if (!maskSDC) { |
| 1352 |
if (key->isValid()) { |
| 1353 |
// It is very unlikely that 'create_mask_GPU' will fail after 'CreateLazyView' |
| 1354 |
// succeeded but, if it does, remove the lazy-view from the cache and fallback to |
| 1355 |
// a SW-created mask. Note that any recording threads that glommed onto the |
| 1356 |
// lazy-view will have to, later, drop those draws. |
| 1357 |
threadSafeCache->remove(*key); |
| 1358 |
} |
| 1359 |
return {}; |
| 1360 |
} |
| 1361 |
|
| 1362 |
auto filteredMaskView = filter_mask(dContext, filter, |
| 1363 |
maskSDC->readSurfaceView(), |
| 1364 |
maskSDC->colorInfo().colorType(), |
| 1365 |
maskSDC->colorInfo().alphaType(), |
| 1366 |
viewMatrix, |
| 1367 |
*maskRect); |
| 1368 |
if (!filteredMaskView) { |
| 1369 |
if (key->isValid()) { |
| 1370 |
// Remove the lazy-view from the cache and fallback to a SW-created mask. Note that |
| 1371 |
// any recording threads that glommed onto the lazy-view will have to, later, drop |
| 1372 |
// those draws. |
| 1373 |
threadSafeCache->remove(*key); |
| 1374 |
} |
| 1375 |
return {}; |
| 1376 |
} |
| 1377 |
|
| 1378 |
if (key->isValid()) { |
| 1379 |
SkASSERT(filteredMaskView.dimensions() == lazyView.dimensions()); |
| 1380 |
SkASSERT(filteredMaskView.swizzle() == lazyView.swizzle()); |
| 1381 |
SkASSERT(filteredMaskView.origin() == lazyView.origin()); |
| 1382 |
|
| 1383 |
trampoline->fProxy = filteredMaskView.asTextureProxyRef(); |
| 1384 |
return lazyView; |
| 1385 |
} |
| 1386 |
|
| 1387 |
return filteredMaskView; |
| 1388 |
} |
| 1389 |
|
| 1390 |
static void draw_shape_with_mask_filter(GrRecordingContext* rContext, |
| 1391 |
skgpu::ganesh::SurfaceDrawContext* sdc, |
| 1392 |
const GrClip* clip, |
| 1393 |
GrPaint&& paint, |
| 1394 |
const SkMatrix& viewMatrix, |
| 1395 |
const SkMaskFilterBase* maskFilter, |
| 1396 |
const GrStyledShape& origShape) { |
| 1397 |
SkASSERT(maskFilter); |
| 1398 |
|
| 1399 |
const GrStyledShape* shape = &origShape; |
| 1400 |
std::optional<GrStyledShape> tmpShape; |
| 1401 |
|
| 1402 |
if (origShape.style().applies()) { |
| 1403 |
SkScalar styleScale = GrStyle::MatrixToScaleFactor(viewMatrix); |
| 1404 |
if (styleScale == 0) { |
| 1405 |
return; |
| 1406 |
} |
| 1407 |
|
| 1408 |
tmpShape.emplace(origShape.applyStyle(GrStyle::Apply::kPathEffectAndStrokeRec, styleScale)); |
| 1409 |
if (tmpShape->isEmpty()) { |
| 1410 |
return; |
| 1411 |
} |
| 1412 |
|
| 1413 |
shape = &tmpShape.value(); |
| 1414 |
} |
| 1415 |
|
| 1416 |
if (direct_filter_mask(rContext, maskFilter, sdc, std::move(paint), clip, viewMatrix, *shape)) { |
| 1417 |
// the mask filter was able to draw itself directly, so there's nothing |
| 1418 |
// left to do. |
| 1419 |
return; |
| 1420 |
} |
| 1421 |
assert_alive(paint); |
| 1422 |
|
| 1423 |
// If the path is hairline, ignore inverse fill. |
| 1424 |
bool inverseFilled = shape->inverseFilled() && |
| 1425 |
!GrIsStrokeHairlineOrEquivalent(shape->style(), viewMatrix, nullptr); |
| 1426 |
|
| 1427 |
SkIRect unclippedDevShapeBounds, devClipBounds; |
| 1428 |
if (!get_shape_and_clip_bounds(sdc, clip, *shape, viewMatrix, |
| 1429 |
&unclippedDevShapeBounds, &devClipBounds)) { |
| 1430 |
// TODO: just cons up an opaque mask here |
| 1431 |
if (!inverseFilled) { |
| 1432 |
return; |
| 1433 |
} |
| 1434 |
} |
| 1435 |
|
| 1436 |
skgpu::UniqueKey maskKey; |
| 1437 |
SkIRect boundsForClip; |
| 1438 |
if (!compute_key_and_clip_bounds(&maskKey, &boundsForClip, |
| 1439 |
sdc->caps(), |
| 1440 |
viewMatrix, inverseFilled, |
| 1441 |
maskFilter, *shape, |
| 1442 |
unclippedDevShapeBounds, |
| 1443 |
devClipBounds)) { |
| 1444 |
return; // 'shape' was entirely clipped out |
| 1445 |
} |
| 1446 |
|
| 1447 |
GrSurfaceProxyView filteredMaskView; |
| 1448 |
SkIRect maskRect; |
| 1449 |
|
| 1450 |
if (auto dContext = rContext->asDirectContext()) { |
| 1451 |
filteredMaskView = hw_create_filtered_mask(dContext, sdc, |
| 1452 |
viewMatrix, *shape, maskFilter, |
| 1453 |
unclippedDevShapeBounds, boundsForClip, |
| 1454 |
&maskRect, &maskKey); |
| 1455 |
if (filteredMaskView) { |
| 1456 |
if (draw_mask(sdc, clip, viewMatrix, maskRect, std::move(paint), |
| 1457 |
std::move(filteredMaskView))) { |
| 1458 |
// This path is completely drawn |
| 1459 |
return; |
| 1460 |
} |
| 1461 |
assert_alive(paint); |
| 1462 |
} |
| 1463 |
} |
| 1464 |
|
| 1465 |
// Either HW mask rendering failed or we're in a DDL recording thread |
| 1466 |
filteredMaskView = sw_create_filtered_mask(rContext, |
| 1467 |
viewMatrix, *shape, maskFilter, |
| 1468 |
unclippedDevShapeBounds, boundsForClip, |
| 1469 |
&maskRect, &maskKey); |
| 1470 |
if (filteredMaskView) { |
| 1471 |
if (draw_mask(sdc, clip, viewMatrix, maskRect, std::move(paint), |
| 1472 |
std::move(filteredMaskView))) { |
| 1473 |
return; |
| 1474 |
} |
| 1475 |
assert_alive(paint); |
| 1476 |
} |
| 1477 |
} |
| 1478 |
|
| 1479 |
bool ComputeBlurredRRectParams(const SkRRect& srcRRect, |
| 1480 |
const SkRRect& devRRect, |
| 1481 |
SkScalar sigma, |
| 1482 |
SkScalar xformedSigma, |
| 1483 |
SkRRect* rrectToDraw, |
| 1484 |
SkISize* widthHeight, |
| 1485 |
SkScalar rectXs[kBlurRRectMaxDivisions], |
| 1486 |
SkScalar rectYs[kBlurRRectMaxDivisions], |
| 1487 |
SkScalar texXs[kBlurRRectMaxDivisions], |
| 1488 |
SkScalar texYs[kBlurRRectMaxDivisions]) { |
| 1489 |
unsigned int devBlurRadius = 3 * SkScalarCeilToInt(xformedSigma - 1 / 6.0f); |
| 1490 |
SkScalar srcBlurRadius = 3.0f * sigma; |
| 1491 |
|
| 1492 |
const SkRect& devOrig = devRRect.getBounds(); |
| 1493 |
const SkVector& devRadiiUL = devRRect.radii(SkRRect::kUpperLeft_Corner); |
| 1494 |
const SkVector& devRadiiUR = devRRect.radii(SkRRect::kUpperRight_Corner); |
| 1495 |
const SkVector& devRadiiLR = devRRect.radii(SkRRect::kLowerRight_Corner); |
| 1496 |
const SkVector& devRadiiLL = devRRect.radii(SkRRect::kLowerLeft_Corner); |
| 1497 |
|
| 1498 |
const int devLeft = SkScalarCeilToInt(std::max<SkScalar>(devRadiiUL.fX, devRadiiLL.fX)); |
| 1499 |
const int devTop = SkScalarCeilToInt(std::max<SkScalar>(devRadiiUL.fY, devRadiiUR.fY)); |
| 1500 |
const int devRight = SkScalarCeilToInt(std::max<SkScalar>(devRadiiUR.fX, devRadiiLR.fX)); |
| 1501 |
const int devBot = SkScalarCeilToInt(std::max<SkScalar>(devRadiiLL.fY, devRadiiLR.fY)); |
| 1502 |
|
| 1503 |
// This is a conservative check for nine-patchability |
| 1504 |
if (devOrig.fLeft + devLeft + devBlurRadius >= devOrig.fRight - devRight - devBlurRadius || |
| 1505 |
devOrig.fTop + devTop + devBlurRadius >= devOrig.fBottom - devBot - devBlurRadius) { |
| 1506 |
return false; |
| 1507 |
} |
| 1508 |
|
| 1509 |
const SkVector& srcRadiiUL = srcRRect.radii(SkRRect::kUpperLeft_Corner); |
| 1510 |
const SkVector& srcRadiiUR = srcRRect.radii(SkRRect::kUpperRight_Corner); |
| 1511 |
const SkVector& srcRadiiLR = srcRRect.radii(SkRRect::kLowerRight_Corner); |
| 1512 |
const SkVector& srcRadiiLL = srcRRect.radii(SkRRect::kLowerLeft_Corner); |
| 1513 |
|
| 1514 |
const SkScalar srcLeft = std::max<SkScalar>(srcRadiiUL.fX, srcRadiiLL.fX); |
| 1515 |
const SkScalar srcTop = std::max<SkScalar>(srcRadiiUL.fY, srcRadiiUR.fY); |
| 1516 |
const SkScalar srcRight = std::max<SkScalar>(srcRadiiUR.fX, srcRadiiLR.fX); |
| 1517 |
const SkScalar srcBot = std::max<SkScalar>(srcRadiiLL.fY, srcRadiiLR.fY); |
| 1518 |
|
| 1519 |
SkSafeMath safe; |
| 1520 |
int newRRWidth_safe = safe.addInt(devLeft, devRight); |
| 1521 |
newRRWidth_safe = safe.addInt(newRRWidth_safe, safe.mulInt(2, devBlurRadius)); |
| 1522 |
newRRWidth_safe = safe.addInt(newRRWidth_safe, 1); |
| 1523 |
|
| 1524 |
int newRRHeight_safe = safe.addInt(devTop, devBot); |
| 1525 |
newRRHeight_safe = safe.addInt(newRRHeight_safe, safe.mulInt(2, devBlurRadius)); |
| 1526 |
newRRHeight_safe = safe.addInt(newRRHeight_safe, 1); |
| 1527 |
|
| 1528 |
int width_safe = safe.addInt(newRRWidth_safe, safe.mulInt(2, devBlurRadius)); |
| 1529 |
int height_safe = safe.addInt(newRRHeight_safe, safe.mulInt(2, devBlurRadius)); |
| 1530 |
|
| 1531 |
if (!safe.ok()) { |
| 1532 |
return false; |
| 1533 |
} |
| 1534 |
int newRRWidth = newRRWidth_safe; |
| 1535 |
int newRRHeight = newRRHeight_safe; |
| 1536 |
widthHeight->fWidth = width_safe; |
| 1537 |
widthHeight->fHeight = height_safe; |
| 1538 |
|
| 1539 |
const SkRect srcProxyRect = srcRRect.getBounds().makeOutset(srcBlurRadius, srcBlurRadius); |
| 1540 |
|
| 1541 |
rectXs[0] = srcProxyRect.fLeft; |
| 1542 |
rectXs[1] = srcProxyRect.fLeft + 2 * srcBlurRadius + srcLeft; |
| 1543 |
rectXs[2] = srcProxyRect.fRight - 2 * srcBlurRadius - srcRight; |
| 1544 |
rectXs[3] = srcProxyRect.fRight; |
| 1545 |
|
| 1546 |
rectYs[0] = srcProxyRect.fTop; |
| 1547 |
rectYs[1] = srcProxyRect.fTop + 2 * srcBlurRadius + srcTop; |
| 1548 |
rectYs[2] = srcProxyRect.fBottom - 2 * srcBlurRadius - srcBot; |
| 1549 |
rectYs[3] = srcProxyRect.fBottom; |
| 1550 |
|
| 1551 |
texXs[0] = 0.0f; |
| 1552 |
texXs[1] = 2.0f * devBlurRadius + devLeft; |
| 1553 |
texXs[2] = 2.0f * devBlurRadius + devLeft + 1; |
| 1554 |
texXs[3] = SkIntToScalar(widthHeight->fWidth); |
| 1555 |
|
| 1556 |
texYs[0] = 0.0f; |
| 1557 |
texYs[1] = 2.0f * devBlurRadius + devTop; |
| 1558 |
texYs[2] = 2.0f * devBlurRadius + devTop + 1; |
| 1559 |
texYs[3] = SkIntToScalar(widthHeight->fHeight); |
| 1560 |
|
| 1561 |
const SkRect newRect = SkRect::MakeXYWH(SkIntToScalar(devBlurRadius), |
| 1562 |
SkIntToScalar(devBlurRadius), |
| 1563 |
SkIntToScalar(newRRWidth), |
| 1564 |
SkIntToScalar(newRRHeight)); |
| 1565 |
SkVector newRadii[4]; |
| 1566 |
newRadii[0] = {SkScalarCeilToScalar(devRadiiUL.fX), SkScalarCeilToScalar(devRadiiUL.fY)}; |
| 1567 |
newRadii[1] = {SkScalarCeilToScalar(devRadiiUR.fX), SkScalarCeilToScalar(devRadiiUR.fY)}; |
| 1568 |
newRadii[2] = {SkScalarCeilToScalar(devRadiiLR.fX), SkScalarCeilToScalar(devRadiiLR.fY)}; |
| 1569 |
newRadii[3] = {SkScalarCeilToScalar(devRadiiLL.fX), SkScalarCeilToScalar(devRadiiLL.fY)}; |
| 1570 |
|
| 1571 |
rrectToDraw->setRectRadii(newRect, newRadii); |
| 1572 |
return true; |
| 1573 |
} |
| 1574 |
|
| 1575 |
void DrawShapeWithMaskFilter(GrRecordingContext* rContext, |
| 1576 |
skgpu::ganesh::SurfaceDrawContext* sdc, |
| 1577 |
const GrClip* clip, |
| 1578 |
const GrStyledShape& shape, |
| 1579 |
GrPaint&& paint, |
| 1580 |
const SkMatrix& viewMatrix, |
| 1581 |
const SkMaskFilter* mf) { |
| 1582 |
draw_shape_with_mask_filter(rContext, sdc, clip, std::move(paint), |
| 1583 |
viewMatrix, as_MFB(mf), shape); |
| 1584 |
} |
| 1585 |
|
| 1586 |
void DrawShapeWithMaskFilter(GrRecordingContext* rContext, |
| 1587 |
skgpu::ganesh::SurfaceDrawContext* sdc, |
| 1588 |
const GrClip* clip, |
| 1589 |
const SkPaint& paint, |
| 1590 |
const SkMatrix& ctm, |
| 1591 |
const GrStyledShape& shape) { |
| 1592 |
if (rContext->abandoned()) { |
| 1593 |
return; |
| 1594 |
} |
| 1595 |
|
| 1596 |
GrPaint grPaint; |
| 1597 |
if (!SkPaintToGrPaint(sdc, paint, ctm, &grPaint)) { |
| 1598 |
return; |
| 1599 |
} |
| 1600 |
|
| 1601 |
SkMaskFilterBase* mf = as_MFB(paint.getMaskFilter()); |
| 1602 |
if (mf && !GrFragmentProcessors::IsSupported(mf)) { |
| 1603 |
// The MaskFilter wasn't already handled in SkPaintToGrPaint |
| 1604 |
draw_shape_with_mask_filter(rContext, sdc, clip, std::move(grPaint), ctm, mf, shape); |
| 1605 |
} else { |
| 1606 |
sdc->drawShape(clip, std::move(grPaint), sdc->chooseAA(paint), ctm, GrStyledShape(shape)); |
| 1607 |
} |
| 1608 |
} |
| 1609 |
|
| 1610 |
|
| 1611 |
// =================== Gaussian Blur ========================================= |
| 1612 |
|
| 1613 |
namespace { |
| 1614 |
|
| 1615 |
enum class Direction { kX, kY }; |
| 1616 |
|
| 1617 |
std::unique_ptr<GrFragmentProcessor> make_texture_effect(const GrCaps* caps, |
| 1618 |
GrSurfaceProxyView srcView, |
| 1619 |
SkAlphaType srcAlphaType, |
| 1620 |
const GrSamplerState& sampler, |
| 1621 |
const SkIRect& srcSubset, |
| 1622 |
const SkIRect& srcRelativeDstRect, |
| 1623 |
const SkISize& radii) { |
| 1624 |
// It's pretty common to blur a subset of an input texture. In reduced shader mode we always |
| 1625 |
// apply the wrap mode in the shader. |
| 1626 |
if (caps->reducedShaderMode()) { |
| 1627 |
return GrTextureEffect::MakeSubset(std::move(srcView), |
| 1628 |
srcAlphaType, |
| 1629 |
SkMatrix::I(), |
| 1630 |
sampler, |
| 1631 |
SkRect::Make(srcSubset), |
| 1632 |
*caps, |
| 1633 |
GrTextureEffect::kDefaultBorder, |
| 1634 |
/*alwaysUseShaderTileMode=*/true); |
| 1635 |
} else { |
| 1636 |
// Inset because we expect to be invoked at pixel centers |
| 1637 |
SkRect domain = SkRect::Make(srcRelativeDstRect); |
| 1638 |
domain.inset(0.5f, 0.5f); |
| 1639 |
domain.outset(radii.width(), radii.height()); |
| 1640 |
return GrTextureEffect::MakeSubset(std::move(srcView), |
| 1641 |
srcAlphaType, |
| 1642 |
SkMatrix::I(), |
| 1643 |
sampler, |
| 1644 |
SkRect::Make(srcSubset), |
| 1645 |
domain, |
| 1646 |
*caps); |
| 1647 |
} |
| 1648 |
} |
| 1649 |
|
| 1650 |
} // end namespace |
| 1651 |
|
| 1652 |
/** |
| 1653 |
* Draws 'dstRect' into 'surfaceFillContext' evaluating a 1D Gaussian over 'srcView'. The src rect |
| 1654 |
* is 'dstRect' offset by 'dstToSrcOffset'. 'mode' and 'bounds' are applied to the src coords. |
| 1655 |
*/ |
| 1656 |
static void convolve_gaussian_1d(skgpu::ganesh::SurfaceFillContext* sfc, |
| 1657 |
GrSurfaceProxyView srcView, |
| 1658 |
const SkIRect& srcSubset, |
| 1659 |
SkIVector dstToSrcOffset, |
| 1660 |
const SkIRect& dstRect, |
| 1661 |
SkAlphaType srcAlphaType, |
| 1662 |
Direction direction, |
| 1663 |
int radius, |
| 1664 |
float sigma, |
| 1665 |
SkTileMode mode) { |
| 1666 |
SkASSERT(radius && !skgpu::BlurIsEffectivelyIdentity(sigma)); |
| 1667 |
auto srcRect = dstRect.makeOffset(dstToSrcOffset); |
| 1668 |
|
| 1669 |
std::array<SkV4, skgpu::kMaxBlurSamples/2> offsetsAndKernel; |
| 1670 |
skgpu::Compute1DBlurLinearKernel(sigma, radius, offsetsAndKernel); |
| 1671 |
|
| 1672 |
// The child of the 1D linear blur effect must be linearly sampled. |
| 1673 |
GrSamplerState sampler{SkTileModeToWrapMode(mode), GrSamplerState::Filter::kLinear}; |
| 1674 |
|
| 1675 |
SkISize radii = {direction == Direction::kX ? radius : 0, |
| 1676 |
direction == Direction::kY ? radius : 0}; |
| 1677 |
std::unique_ptr<GrFragmentProcessor> child = make_texture_effect(sfc->caps(), |
| 1678 |
std::move(srcView), |
| 1679 |
srcAlphaType, |
| 1680 |
sampler, |
| 1681 |
srcSubset, |
| 1682 |
srcRect, |
| 1683 |
radii); |
| 1684 |
|
| 1685 |
auto conv = GrSkSLFP::Make(skgpu::GetLinearBlur1DEffect(radius), |
| 1686 |
"GaussianBlur1D", |
| 1687 |
/*inputFP=*/nullptr, |
| 1688 |
GrSkSLFP::OptFlags::kCompatibleWithCoverageAsAlpha, |
| 1689 |
"offsetsAndKernel", SkSpan<SkV4>{offsetsAndKernel}, |
| 1690 |
"dir", direction == Direction::kX ? SkV2{1.f, 0.f} |
| 1691 |
: SkV2{0.f, 1.f}, |
| 1692 |
"child", std::move(child)); |
| 1693 |
sfc->fillRectToRectWithFP(srcRect, dstRect, std::move(conv)); |
| 1694 |
} |
| 1695 |
|
| 1696 |
static std::unique_ptr<skgpu::ganesh::SurfaceDrawContext> convolve_gaussian_2d( |
| 1697 |
GrRecordingContext* rContext, |
| 1698 |
GrSurfaceProxyView srcView, |
| 1699 |
GrColorType srcColorType, |
| 1700 |
const SkIRect& srcBounds, |
| 1701 |
const SkIRect& dstBounds, |
| 1702 |
int radiusX, |
| 1703 |
int radiusY, |
| 1704 |
SkScalar sigmaX, |
| 1705 |
SkScalar sigmaY, |
| 1706 |
SkTileMode mode, |
| 1707 |
sk_sp<SkColorSpace> finalCS, |
| 1708 |
SkBackingFit dstFit) { |
| 1709 |
SkASSERT(radiusX && radiusY); |
| 1710 |
SkASSERT(!skgpu::BlurIsEffectivelyIdentity(sigmaX) && |
| 1711 |
!skgpu::BlurIsEffectivelyIdentity(sigmaY)); |
| 1712 |
// Create the sdc with default SkSurfaceProps. Gaussian blurs will soon use a |
| 1713 |
// SurfaceFillContext, at which point the SkSurfaceProps won't exist anymore. |
| 1714 |
auto sdc = skgpu::ganesh::SurfaceDrawContext::Make( |
| 1715 |
rContext, |
| 1716 |
srcColorType, |
| 1717 |
std::move(finalCS), |
| 1718 |
dstFit, |
| 1719 |
dstBounds.size(), |
| 1720 |
SkSurfaceProps(), |
| 1721 |
/*label=*/"SurfaceDrawContext_ConvolveGaussian2d", |
| 1722 |
/* sampleCnt= */ 1, |
| 1723 |
skgpu::Mipmapped::kNo, |
| 1724 |
srcView.proxy()->isProtected(), |
| 1725 |
srcView.origin()); |
| 1726 |
if (!sdc) { |
| 1727 |
return nullptr; |
| 1728 |
} |
| 1729 |
|
| 1730 |
// GaussianBlur() should have downsampled the request until we can handle the 2D blur with |
| 1731 |
// just a uniform array, which is asserted inside the Compute function. |
| 1732 |
const SkISize radii{radiusX, radiusY}; |
| 1733 |
std::array<SkV4, skgpu::kMaxBlurSamples/4> kernel; |
| 1734 |
std::array<SkV4, skgpu::kMaxBlurSamples/2> offsets; |
| 1735 |
skgpu::Compute2DBlurKernel({sigmaX, sigmaY}, radii, kernel); |
| 1736 |
skgpu::Compute2DBlurOffsets(radii, offsets); |
| 1737 |
|
| 1738 |
GrSamplerState sampler{SkTileModeToWrapMode(mode), GrSamplerState::Filter::kNearest}; |
| 1739 |
auto child = make_texture_effect(sdc->caps(), |
| 1740 |
std::move(srcView), |
| 1741 |
kPremul_SkAlphaType, |
| 1742 |
sampler, |
| 1743 |
srcBounds, |
| 1744 |
dstBounds, |
| 1745 |
radii); |
| 1746 |
auto conv = GrSkSLFP::Make(skgpu::GetBlur2DEffect(radii), |
| 1747 |
"GaussianBlur2D", |
| 1748 |
/*inputFP=*/nullptr, |
| 1749 |
GrSkSLFP::OptFlags::kNone, |
| 1750 |
"kernel", SkSpan<SkV4>{kernel}, |
| 1751 |
"offsets", SkSpan<SkV4>{offsets}, |
| 1752 |
"child", std::move(child)); |
| 1753 |
|
| 1754 |
GrPaint paint; |
| 1755 |
paint.setColorFragmentProcessor(std::move(conv)); |
| 1756 |
paint.setPorterDuffXPFactory(SkBlendMode::kSrc); |
| 1757 |
|
| 1758 |
// 'dstBounds' is actually in 'srcView' proxy space. It represents the blurred area from src |
| 1759 |
// space that we want to capture in the new RTC at {0, 0}. Hence, we use its size as the rect to |
| 1760 |
// draw and it directly as the local rect. |
| 1761 |
sdc->fillRectToRect(nullptr, |
| 1762 |
std::move(paint), |
| 1763 |
GrAA::kNo, |
| 1764 |
SkMatrix::I(), |
| 1765 |
SkRect::Make(dstBounds.size()), |
| 1766 |
SkRect::Make(dstBounds)); |
| 1767 |
|
| 1768 |
return sdc; |
| 1769 |
} |
| 1770 |
|
| 1771 |
static std::unique_ptr<skgpu::ganesh::SurfaceDrawContext> convolve_gaussian( |
| 1772 |
GrRecordingContext* rContext, |
| 1773 |
GrSurfaceProxyView srcView, |
| 1774 |
GrColorType srcColorType, |
| 1775 |
SkAlphaType srcAlphaType, |
| 1776 |
SkIRect srcBounds, |
| 1777 |
SkIRect dstBounds, |
| 1778 |
Direction direction, |
| 1779 |
int radius, |
| 1780 |
float sigma, |
| 1781 |
SkTileMode mode, |
| 1782 |
sk_sp<SkColorSpace> finalCS, |
| 1783 |
SkBackingFit fit) { |
| 1784 |
SkASSERT(radius > 0 && !skgpu::BlurIsEffectivelyIdentity(sigma)); |
| 1785 |
// Logically we're creating an infinite blur of 'srcBounds' of 'srcView' with 'mode' tiling |
| 1786 |
// and then capturing the 'dstBounds' portion in a new RTC where the top left of 'dstBounds' is |
| 1787 |
// at {0, 0} in the new RTC. |
| 1788 |
// |
| 1789 |
// Create the sdc with default SkSurfaceProps. Gaussian blurs will soon use a |
| 1790 |
// SurfaceFillContext, at which point the SkSurfaceProps won't exist anymore. |
| 1791 |
auto dstSDC = |
| 1792 |
skgpu::ganesh::SurfaceDrawContext::Make(rContext, |
| 1793 |
srcColorType, |
| 1794 |
std::move(finalCS), |
| 1795 |
fit, |
| 1796 |
dstBounds.size(), |
| 1797 |
SkSurfaceProps(), |
| 1798 |
/*label=*/"SurfaceDrawContext_ConvolveGaussian", |
| 1799 |
/* sampleCnt= */ 1, |
| 1800 |
skgpu::Mipmapped::kNo, |
| 1801 |
srcView.proxy()->isProtected(), |
| 1802 |
srcView.origin()); |
| 1803 |
if (!dstSDC) { |
| 1804 |
return nullptr; |
| 1805 |
} |
| 1806 |
// This represents the translation from 'dstSurfaceDrawContext' coords to 'srcView' coords. |
| 1807 |
auto rtcToSrcOffset = dstBounds.topLeft(); |
| 1808 |
|
| 1809 |
auto srcBackingBounds = SkIRect::MakeSize(srcView.proxy()->backingStoreDimensions()); |
| 1810 |
// We've implemented splitting the dst bounds up into areas that do and do not need to |
| 1811 |
// use shader based tiling but only for some modes... |
| 1812 |
bool canSplit = mode == SkTileMode::kDecal || mode == SkTileMode::kClamp; |
| 1813 |
// ...but it's not worth doing the splitting if we'll get HW tiling instead of shader tiling. |
| 1814 |
bool canHWTile = |
| 1815 |
srcBounds.contains(srcBackingBounds) && |
| 1816 |
!rContext->priv().caps()->reducedShaderMode() && // this mode always uses shader tiling |
| 1817 |
!(mode == SkTileMode::kDecal && !rContext->priv().caps()->clampToBorderSupport()); |
| 1818 |
if (!canSplit || canHWTile) { |
| 1819 |
auto dstRect = SkIRect::MakeSize(dstBounds.size()); |
| 1820 |
convolve_gaussian_1d(dstSDC.get(), |
| 1821 |
std::move(srcView), |
| 1822 |
srcBounds, |
| 1823 |
rtcToSrcOffset, |
| 1824 |
dstRect, |
| 1825 |
srcAlphaType, |
| 1826 |
direction, |
| 1827 |
radius, |
| 1828 |
sigma, |
| 1829 |
mode); |
| 1830 |
return dstSDC; |
| 1831 |
} |
| 1832 |
|
| 1833 |
// 'left' and 'right' are the sub rects of 'srcBounds' where 'mode' must be enforced. |
| 1834 |
// 'mid' is the area where we can ignore the mode because the kernel does not reach to the |
| 1835 |
// edge of 'srcBounds'. |
| 1836 |
SkIRect mid, left, right; |
| 1837 |
// 'top' and 'bottom' are areas of 'dstBounds' that are entirely above/below 'srcBounds'. |
| 1838 |
// These are areas that we can simply clear in the dst in kDecal mode. If 'srcBounds' |
| 1839 |
// straddles the top edge of 'dstBounds' then 'top' will be inverted and we will skip |
| 1840 |
// processing for the rect. Similar for 'bottom'. The positional/directional labels above refer |
| 1841 |
// to the Direction::kX case and one should think of these as 'left' and 'right' for |
| 1842 |
// Direction::kY. |
| 1843 |
SkIRect top, bottom; |
| 1844 |
if (Direction::kX == direction) { |
| 1845 |
top = {dstBounds.left(), dstBounds.top(), dstBounds.right(), srcBounds.top()}; |
| 1846 |
bottom = {dstBounds.left(), srcBounds.bottom(), dstBounds.right(), dstBounds.bottom()}; |
| 1847 |
|
| 1848 |
// Inset for sub-rect of 'srcBounds' where the x-dir kernel doesn't reach the edges, clipped |
| 1849 |
// vertically to dstBounds. |
| 1850 |
int midA = std::max(srcBounds.top(), dstBounds.top()); |
| 1851 |
int midB = std::min(srcBounds.bottom(), dstBounds.bottom()); |
| 1852 |
mid = {srcBounds.left() + radius, midA, srcBounds.right() - radius, midB}; |
| 1853 |
if (mid.isEmpty()) { |
| 1854 |
// There is no middle where the bounds can be ignored. Make the left span the whole |
| 1855 |
// width of dst and we will not draw mid or right. |
| 1856 |
left = {dstBounds.left(), mid.top(), dstBounds.right(), mid.bottom()}; |
| 1857 |
} else { |
| 1858 |
left = {dstBounds.left(), mid.top(), mid.left(), mid.bottom()}; |
| 1859 |
right = {mid.right(), mid.top(), dstBounds.right(), mid.bottom()}; |
| 1860 |
} |
| 1861 |
} else { |
| 1862 |
// This is the same as the x direction code if you turn your head 90 degrees CCW. Swap x and |
| 1863 |
// y and swap top/bottom with left/right. |
| 1864 |
top = {dstBounds.left(), dstBounds.top(), srcBounds.left(), dstBounds.bottom()}; |
| 1865 |
bottom = {srcBounds.right(), dstBounds.top(), dstBounds.right(), dstBounds.bottom()}; |
| 1866 |
|
| 1867 |
int midA = std::max(srcBounds.left(), dstBounds.left()); |
| 1868 |
int midB = std::min(srcBounds.right(), dstBounds.right()); |
| 1869 |
mid = {midA, srcBounds.top() + radius, midB, srcBounds.bottom() - radius}; |
| 1870 |
|
| 1871 |
if (mid.isEmpty()) { |
| 1872 |
left = {mid.left(), dstBounds.top(), mid.right(), dstBounds.bottom()}; |
| 1873 |
} else { |
| 1874 |
left = {mid.left(), dstBounds.top(), mid.right(), mid.top()}; |
| 1875 |
right = {mid.left(), mid.bottom(), mid.right(), dstBounds.bottom()}; |
| 1876 |
} |
| 1877 |
} |
| 1878 |
|
| 1879 |
auto convolve = [&](SkIRect rect) { |
| 1880 |
// Transform rect into the render target's coord system. |
| 1881 |
rect.offset(-rtcToSrcOffset); |
| 1882 |
convolve_gaussian_1d(dstSDC.get(), |
| 1883 |
srcView, |
| 1884 |
srcBounds, |
| 1885 |
rtcToSrcOffset, |
| 1886 |
rect, |
| 1887 |
srcAlphaType, |
| 1888 |
direction, |
| 1889 |
radius, |
| 1890 |
sigma, |
| 1891 |
mode); |
| 1892 |
}; |
| 1893 |
auto clear = [&](SkIRect rect) { |
| 1894 |
// Transform rect into the render target's coord system. |
| 1895 |
rect.offset(-rtcToSrcOffset); |
| 1896 |
dstSDC->clearAtLeast(rect, SK_PMColor4fTRANSPARENT); |
| 1897 |
}; |
| 1898 |
|
| 1899 |
// Doing mid separately will cause two draws to occur (left and right batch together). At |
| 1900 |
// small sizes of mid it is worse to issue more draws than to just execute the slightly |
| 1901 |
// more complicated shader that implements the tile mode across mid. This threshold is |
| 1902 |
// very arbitrary right now. It is believed that a 21x44 mid on a Moto G4 is a significant |
| 1903 |
// regression compared to doing one draw but it has not been locally evaluated or tuned. |
| 1904 |
// The optimal cutoff is likely to vary by GPU. |
| 1905 |
if (!mid.isEmpty() && mid.width() * mid.height() < 256 * 256) { |
| 1906 |
left.join(mid); |
| 1907 |
left.join(right); |
| 1908 |
mid = SkIRect::MakeEmpty(); |
| 1909 |
right = SkIRect::MakeEmpty(); |
| 1910 |
// It's unknown whether for kDecal it'd be better to expand the draw rather than a draw and |
| 1911 |
// up to two clears. |
| 1912 |
if (mode == SkTileMode::kClamp) { |
| 1913 |
left.join(top); |
| 1914 |
left.join(bottom); |
| 1915 |
top = SkIRect::MakeEmpty(); |
| 1916 |
bottom = SkIRect::MakeEmpty(); |
| 1917 |
} |
| 1918 |
} |
| 1919 |
|
| 1920 |
if (!top.isEmpty()) { |
| 1921 |
if (mode == SkTileMode::kDecal) { |
| 1922 |
clear(top); |
| 1923 |
} else { |
| 1924 |
convolve(top); |
| 1925 |
} |
| 1926 |
} |
| 1927 |
|
| 1928 |
if (!bottom.isEmpty()) { |
| 1929 |
if (mode == SkTileMode::kDecal) { |
| 1930 |
clear(bottom); |
| 1931 |
} else { |
| 1932 |
convolve(bottom); |
| 1933 |
} |
| 1934 |
} |
| 1935 |
|
| 1936 |
if (mid.isEmpty()) { |
| 1937 |
convolve(left); |
| 1938 |
} else { |
| 1939 |
convolve(left); |
| 1940 |
convolve(right); |
| 1941 |
convolve(mid); |
| 1942 |
} |
| 1943 |
return dstSDC; |
| 1944 |
} |
| 1945 |
|
| 1946 |
// Expand the contents of 'src' to fit in 'dstSize'. At this point, we are expanding an intermediate |
| 1947 |
// image, so there's no need to account for a proxy offset from the original input. |
| 1948 |
static std::unique_ptr<skgpu::ganesh::SurfaceDrawContext> reexpand( |
| 1949 |
GrRecordingContext* rContext, |
| 1950 |
std::unique_ptr<skgpu::ganesh::SurfaceContext> src, |
| 1951 |
const SkRect& srcBounds, |
| 1952 |
SkISize dstSize, |
| 1953 |
sk_sp<SkColorSpace> colorSpace, |
| 1954 |
SkBackingFit fit) { |
| 1955 |
GrSurfaceProxyView srcView = src->readSurfaceView(); |
| 1956 |
if (!srcView.asTextureProxy()) { |
| 1957 |
return nullptr; |
| 1958 |
} |
| 1959 |
|
| 1960 |
GrColorType srcColorType = src->colorInfo().colorType(); |
| 1961 |
SkAlphaType srcAlphaType = src->colorInfo().alphaType(); |
| 1962 |
|
| 1963 |
#if defined(SK_USE_PADDED_BLUR_UPSCALE) |
| 1964 |
// The blur output completely filled the src SurfaceContext, so that is our subset boundary, |
| 1965 |
// ensuring we don't access undefined pixels in the approx-fit backing texture. |
| 1966 |
SkRect srcContent = SkRect::MakeIWH(src->width(), src->height()); |
| 1967 |
#endif |
| 1968 |
|
| 1969 |
src.reset(); // no longer needed |
| 1970 |
|
| 1971 |
// Create the sdc with default SkSurfaceProps. Gaussian blurs will soon use a |
| 1972 |
// SurfaceFillContext, at which point the SkSurfaceProps won't exist anymore. |
| 1973 |
auto dstSDC = skgpu::ganesh::SurfaceDrawContext::Make(rContext, |
| 1974 |
srcColorType, |
| 1975 |
std::move(colorSpace), |
| 1976 |
fit, |
| 1977 |
dstSize, |
| 1978 |
SkSurfaceProps(), |
| 1979 |
/*label=*/"SurfaceDrawContext_Reexpand", |
| 1980 |
/* sampleCnt= */ 1, |
| 1981 |
skgpu::Mipmapped::kNo, |
| 1982 |
srcView.proxy()->isProtected(), |
| 1983 |
srcView.origin()); |
| 1984 |
if (!dstSDC) { |
| 1985 |
return nullptr; |
| 1986 |
} |
| 1987 |
|
| 1988 |
GrPaint paint; |
| 1989 |
auto fp = GrTextureEffect::MakeSubset(std::move(srcView), |
| 1990 |
srcAlphaType, |
| 1991 |
SkMatrix::I(), |
| 1992 |
GrSamplerState::Filter::kLinear, |
| 1993 |
#if defined(SK_USE_PADDED_BLUR_UPSCALE) |
| 1994 |
srcContent, |
| 1995 |
#else |
| 1996 |
srcBounds, |
| 1997 |
srcBounds, |
| 1998 |
#endif |
| 1999 |
*rContext->priv().caps()); |
| 2000 |
paint.setColorFragmentProcessor(std::move(fp)); |
| 2001 |
paint.setPorterDuffXPFactory(SkBlendMode::kSrc); |
| 2002 |
|
| 2003 |
dstSDC->fillRectToRect( |
| 2004 |
nullptr, std::move(paint), GrAA::kNo, SkMatrix::I(), SkRect::Make(dstSize), srcBounds); |
| 2005 |
|
| 2006 |
return dstSDC; |
| 2007 |
} |
| 2008 |
|
| 2009 |
static std::unique_ptr<skgpu::ganesh::SurfaceDrawContext> two_pass_gaussian( |
| 2010 |
GrRecordingContext* rContext, |
| 2011 |
GrSurfaceProxyView srcView, |
| 2012 |
GrColorType srcColorType, |
| 2013 |
SkAlphaType srcAlphaType, |
| 2014 |
sk_sp<SkColorSpace> colorSpace, |
| 2015 |
SkIRect srcBounds, |
| 2016 |
SkIRect dstBounds, |
| 2017 |
float sigmaX, |
| 2018 |
float sigmaY, |
| 2019 |
int radiusX, |
| 2020 |
int radiusY, |
| 2021 |
SkTileMode mode, |
| 2022 |
SkBackingFit fit) { |
| 2023 |
SkASSERT(radiusX || radiusY); |
| 2024 |
std::unique_ptr<skgpu::ganesh::SurfaceDrawContext> dstSDC; |
| 2025 |
if (radiusX > 0) { |
| 2026 |
SkBackingFit xFit = radiusY > 0 ? SkBackingFit::kApprox : fit; |
| 2027 |
// Expand the dstBounds vertically to produce necessary content for the y-pass. Then we will |
| 2028 |
// clip these in a tile-mode dependent way to ensure the tile-mode gets implemented |
| 2029 |
// correctly. However, if we're not going to do a y-pass then we must use the original |
| 2030 |
// dstBounds without clipping to produce the correct output size. |
| 2031 |
SkIRect xPassDstBounds = dstBounds; |
| 2032 |
if (radiusY) { |
| 2033 |
xPassDstBounds.outset(0, radiusY); |
| 2034 |
if (mode == SkTileMode::kRepeat || mode == SkTileMode::kMirror) { |
| 2035 |
int srcH = srcBounds.height(); |
| 2036 |
int srcTop = srcBounds.top(); |
| 2037 |
if (mode == SkTileMode::kMirror) { |
| 2038 |
srcTop -= srcH; |
| 2039 |
srcH *= 2; |
| 2040 |
} |
| 2041 |
|
| 2042 |
float floatH = srcH; |
| 2043 |
// First row above the dst rect where we should restart the tile mode. |
| 2044 |
int n = sk_float_floor2int_no_saturate((xPassDstBounds.top() - srcTop) / floatH); |
| 2045 |
int topClip = srcTop + n * srcH; |
| 2046 |
|
| 2047 |
// First row above below the dst rect where we should restart the tile mode. |
| 2048 |
n = sk_float_ceil2int_no_saturate((xPassDstBounds.bottom() - srcBounds.bottom()) / |
| 2049 |
floatH); |
| 2050 |
int bottomClip = srcBounds.bottom() + n * srcH; |
| 2051 |
|
| 2052 |
xPassDstBounds.fTop = std::max(xPassDstBounds.top(), topClip); |
| 2053 |
xPassDstBounds.fBottom = std::min(xPassDstBounds.bottom(), bottomClip); |
| 2054 |
} else { |
| 2055 |
if (xPassDstBounds.fBottom <= srcBounds.top()) { |
| 2056 |
if (mode == SkTileMode::kDecal) { |
| 2057 |
return nullptr; |
| 2058 |
} |
| 2059 |
xPassDstBounds.fTop = srcBounds.top(); |
| 2060 |
xPassDstBounds.fBottom = xPassDstBounds.fTop + 1; |
| 2061 |
} else if (xPassDstBounds.fTop >= srcBounds.bottom()) { |
| 2062 |
if (mode == SkTileMode::kDecal) { |
| 2063 |
return nullptr; |
| 2064 |
} |
| 2065 |
xPassDstBounds.fBottom = srcBounds.bottom(); |
| 2066 |
xPassDstBounds.fTop = xPassDstBounds.fBottom - 1; |
| 2067 |
} else { |
| 2068 |
xPassDstBounds.fTop = std::max(xPassDstBounds.fTop, srcBounds.top()); |
| 2069 |
xPassDstBounds.fBottom = std::min(xPassDstBounds.fBottom, srcBounds.bottom()); |
| 2070 |
} |
| 2071 |
int leftSrcEdge = srcBounds.fLeft - radiusX; |
| 2072 |
int rightSrcEdge = srcBounds.fRight + radiusX; |
| 2073 |
if (mode == SkTileMode::kClamp) { |
| 2074 |
// In clamp the column just outside the src bounds has the same value as the |
| 2075 |
// column just inside, unlike decal. |
| 2076 |
leftSrcEdge += 1; |
| 2077 |
rightSrcEdge -= 1; |
| 2078 |
} |
| 2079 |
if (xPassDstBounds.fRight <= leftSrcEdge) { |
| 2080 |
if (mode == SkTileMode::kDecal) { |
| 2081 |
return nullptr; |
| 2082 |
} |
| 2083 |
xPassDstBounds.fLeft = xPassDstBounds.fRight - 1; |
| 2084 |
} else { |
| 2085 |
xPassDstBounds.fLeft = std::max(xPassDstBounds.fLeft, leftSrcEdge); |
| 2086 |
} |
| 2087 |
if (xPassDstBounds.fLeft >= rightSrcEdge) { |
| 2088 |
if (mode == SkTileMode::kDecal) { |
| 2089 |
return nullptr; |
| 2090 |
} |
| 2091 |
xPassDstBounds.fRight = xPassDstBounds.fLeft + 1; |
| 2092 |
} else { |
| 2093 |
xPassDstBounds.fRight = std::min(xPassDstBounds.fRight, rightSrcEdge); |
| 2094 |
} |
| 2095 |
} |
| 2096 |
} |
| 2097 |
dstSDC = convolve_gaussian(rContext, |
| 2098 |
std::move(srcView), |
| 2099 |
srcColorType, |
| 2100 |
srcAlphaType, |
| 2101 |
srcBounds, |
| 2102 |
xPassDstBounds, |
| 2103 |
Direction::kX, |
| 2104 |
radiusX, |
| 2105 |
sigmaX, |
| 2106 |
mode, |
| 2107 |
colorSpace, |
| 2108 |
xFit); |
| 2109 |
if (!dstSDC) { |
| 2110 |
return nullptr; |
| 2111 |
} |
| 2112 |
srcView = dstSDC->readSurfaceView(); |
| 2113 |
SkIVector newDstBoundsOffset = dstBounds.topLeft() - xPassDstBounds.topLeft(); |
| 2114 |
dstBounds = SkIRect::MakeSize(dstBounds.size()).makeOffset(newDstBoundsOffset); |
| 2115 |
srcBounds = SkIRect::MakeSize(xPassDstBounds.size()); |
| 2116 |
} |
| 2117 |
|
| 2118 |
if (!radiusY) { |
| 2119 |
return dstSDC; |
| 2120 |
} |
| 2121 |
|
| 2122 |
return convolve_gaussian(rContext, |
| 2123 |
std::move(srcView), |
| 2124 |
srcColorType, |
| 2125 |
srcAlphaType, |
| 2126 |
srcBounds, |
| 2127 |
dstBounds, |
| 2128 |
Direction::kY, |
| 2129 |
radiusY, |
| 2130 |
sigmaY, |
| 2131 |
mode, |
| 2132 |
std::move(colorSpace), |
| 2133 |
fit); |
| 2134 |
} |
| 2135 |
|
| 2136 |
std::unique_ptr<skgpu::ganesh::SurfaceDrawContext> GaussianBlur(GrRecordingContext* rContext, |
| 2137 |
GrSurfaceProxyView srcView, |
| 2138 |
GrColorType srcColorType, |
| 2139 |
SkAlphaType srcAlphaType, |
| 2140 |
sk_sp<SkColorSpace> colorSpace, |
| 2141 |
SkIRect dstBounds, |
| 2142 |
SkIRect srcBounds, |
| 2143 |
float sigmaX, |
| 2144 |
float sigmaY, |
| 2145 |
SkTileMode mode, |
| 2146 |
SkBackingFit fit) { |
| 2147 |
SkASSERT(rContext); |
| 2148 |
TRACE_EVENT2("skia.gpu", "GaussianBlur", "sigmaX", sigmaX, "sigmaY", sigmaY); |
| 2149 |
|
| 2150 |
if (!srcView.asTextureProxy()) { |
| 2151 |
return nullptr; |
| 2152 |
} |
| 2153 |
|
| 2154 |
int maxRenderTargetSize = rContext->priv().caps()->maxRenderTargetSize(); |
| 2155 |
if (dstBounds.width() > maxRenderTargetSize || dstBounds.height() > maxRenderTargetSize) { |
| 2156 |
return nullptr; |
| 2157 |
} |
| 2158 |
|
| 2159 |
int radiusX = skgpu::BlurSigmaRadius(sigmaX); |
| 2160 |
int radiusY = skgpu::BlurSigmaRadius(sigmaY); |
| 2161 |
// Attempt to reduce the srcBounds in order to detect that we can set the sigmas to zero or |
| 2162 |
// to reduce the amount of work to rescale the source if sigmas are large. TODO: Could consider |
| 2163 |
// how to minimize the required source bounds for repeat/mirror modes. |
| 2164 |
if (mode == SkTileMode::kClamp || mode == SkTileMode::kDecal) { |
| 2165 |
SkIRect reach = dstBounds.makeOutset(radiusX, radiusY); |
| 2166 |
SkIRect intersection; |
| 2167 |
if (!intersection.intersect(reach, srcBounds)) { |
| 2168 |
if (mode == SkTileMode::kDecal) { |
| 2169 |
return nullptr; |
| 2170 |
} else { |
| 2171 |
if (reach.fLeft >= srcBounds.fRight) { |
| 2172 |
srcBounds.fLeft = srcBounds.fRight - 1; |
| 2173 |
} else if (reach.fRight <= srcBounds.fLeft) { |
| 2174 |
srcBounds.fRight = srcBounds.fLeft + 1; |
| 2175 |
} |
| 2176 |
if (reach.fTop >= srcBounds.fBottom) { |
| 2177 |
srcBounds.fTop = srcBounds.fBottom - 1; |
| 2178 |
} else if (reach.fBottom <= srcBounds.fTop) { |
| 2179 |
srcBounds.fBottom = srcBounds.fTop + 1; |
| 2180 |
} |
| 2181 |
} |
| 2182 |
} else { |
| 2183 |
srcBounds = intersection; |
| 2184 |
} |
| 2185 |
} |
| 2186 |
|
| 2187 |
if (mode != SkTileMode::kDecal) { |
| 2188 |
// All non-decal tile modes are equivalent for one pixel width/height src and amount to a |
| 2189 |
// single color value repeated at each column/row. Applying the normalized kernel to that |
| 2190 |
// column/row yields that same color. So no blurring is necessary. |
| 2191 |
if (srcBounds.width() == 1) { |
| 2192 |
sigmaX = 0.f; |
| 2193 |
radiusX = 0; |
| 2194 |
} |
| 2195 |
if (srcBounds.height() == 1) { |
| 2196 |
sigmaY = 0.f; |
| 2197 |
radiusY = 0; |
| 2198 |
} |
| 2199 |
} |
| 2200 |
|
| 2201 |
// If we determined that there is no blurring necessary in either direction then just do a |
| 2202 |
// a draw that applies the tile mode. |
| 2203 |
if (!radiusX && !radiusY) { |
| 2204 |
// Create the sdc with default SkSurfaceProps. Gaussian blurs will soon use a |
| 2205 |
// SurfaceFillContext, at which point the SkSurfaceProps won't exist anymore. |
| 2206 |
auto result = |
| 2207 |
skgpu::ganesh::SurfaceDrawContext::Make(rContext, |
| 2208 |
srcColorType, |
| 2209 |
std::move(colorSpace), |
| 2210 |
fit, |
| 2211 |
dstBounds.size(), |
| 2212 |
SkSurfaceProps(), |
| 2213 |
/*label=*/"SurfaceDrawContext_GaussianBlur", |
| 2214 |
/* sampleCnt= */ 1, |
| 2215 |
skgpu::Mipmapped::kNo, |
| 2216 |
srcView.proxy()->isProtected(), |
| 2217 |
srcView.origin()); |
| 2218 |
if (!result) { |
| 2219 |
return nullptr; |
| 2220 |
} |
| 2221 |
GrSamplerState sampler(SkTileModeToWrapMode(mode), GrSamplerState::Filter::kNearest); |
| 2222 |
auto fp = GrTextureEffect::MakeSubset(std::move(srcView), |
| 2223 |
srcAlphaType, |
| 2224 |
SkMatrix::I(), |
| 2225 |
sampler, |
| 2226 |
SkRect::Make(srcBounds), |
| 2227 |
SkRect::Make(dstBounds), |
| 2228 |
*rContext->priv().caps()); |
| 2229 |
result->fillRectToRectWithFP(dstBounds, SkIRect::MakeSize(dstBounds.size()), std::move(fp)); |
| 2230 |
return result; |
| 2231 |
} |
| 2232 |
|
| 2233 |
// Any sigma higher than the limit for the 1D linear-filtered Gaussian blur is downsampled. If |
| 2234 |
// the sigma in X and Y just so happen to fit in the 2D limit, we'll use that. The 2D limit is |
| 2235 |
// always less than the linear blur sigma limit. |
| 2236 |
static constexpr float kMaxSigma = skgpu::kMaxLinearBlurSigma; |
| 2237 |
if (sigmaX <= kMaxSigma && sigmaY <= kMaxSigma) { |
| 2238 |
// For really small blurs (certainly no wider than 5x5 on desktop GPUs) it is faster to just |
| 2239 |
// launch a single non separable kernel vs two launches. |
| 2240 |
const int kernelSize = skgpu::BlurKernelWidth(radiusX) * skgpu::BlurKernelWidth(radiusY); |
| 2241 |
if (radiusX > 0 && radiusY > 0 && |
| 2242 |
kernelSize <= skgpu::kMaxBlurSamples && |
| 2243 |
!rContext->priv().caps()->reducedShaderMode()) { |
| 2244 |
// Apply the proxy offset to src bounds and offset directly |
| 2245 |
return convolve_gaussian_2d(rContext, |
| 2246 |
std::move(srcView), |
| 2247 |
srcColorType, |
| 2248 |
srcBounds, |
| 2249 |
dstBounds, |
| 2250 |
radiusX, |
| 2251 |
radiusY, |
| 2252 |
sigmaX, |
| 2253 |
sigmaY, |
| 2254 |
mode, |
| 2255 |
std::move(colorSpace), |
| 2256 |
fit); |
| 2257 |
} |
| 2258 |
// This will automatically degenerate into a single pass of X or Y if only one of the |
| 2259 |
// radii are non-zero. |
| 2260 |
SkASSERT(skgpu::BlurLinearKernelWidth(radiusX) <= skgpu::kMaxBlurSamples && |
| 2261 |
skgpu::BlurLinearKernelWidth(radiusY) <= skgpu::kMaxBlurSamples); |
| 2262 |
return two_pass_gaussian(rContext, |
| 2263 |
std::move(srcView), |
| 2264 |
srcColorType, |
| 2265 |
srcAlphaType, |
| 2266 |
std::move(colorSpace), |
| 2267 |
srcBounds, |
| 2268 |
dstBounds, |
| 2269 |
sigmaX, |
| 2270 |
sigmaY, |
| 2271 |
radiusX, |
| 2272 |
radiusY, |
| 2273 |
mode, |
| 2274 |
fit); |
| 2275 |
} |
| 2276 |
|
| 2277 |
GrColorInfo colorInfo(srcColorType, srcAlphaType, colorSpace); |
| 2278 |
auto srcCtx = rContext->priv().makeSC(srcView, colorInfo); |
| 2279 |
SkASSERT(srcCtx); |
| 2280 |
|
| 2281 |
#if defined(SK_USE_PADDED_BLUR_UPSCALE) |
| 2282 |
// When we are in clamp mode any artifacts in the edge pixels due to downscaling may be |
| 2283 |
// exacerbated because of the tile mode. The particularly egregious case is when the original |
| 2284 |
// image has transparent black around the edges and the downscaling pulls in some non-zero |
| 2285 |
// values from the interior. Ultimately it'd be better for performance if the calling code could |
| 2286 |
// give us extra context around the blur to account for this. We don't currently have a good way |
| 2287 |
// to communicate this up stack. So we leave a 1 pixel border around the rescaled src bounds. |
| 2288 |
// We populate the top 1 pixel tall row of this border by rescaling the top row of the original |
| 2289 |
// source bounds into it. Because this is only rescaling in x (i.e. rescaling a 1 pixel high |
| 2290 |
// row into a shorter but still 1 pixel high row) we won't read any interior values. And similar |
| 2291 |
// for the other three borders. We'll adjust the source/dest bounds rescaled blur so that this |
| 2292 |
// border of extra pixels is used as the edge pixels for clamp mode but the dest bounds |
| 2293 |
// corresponds only to the pixels inside the border (the normally rescaled pixels inside this |
| 2294 |
// border). |
| 2295 |
// Moreover, if we clamped the rescaled size to 1 column or row then we still have a sigma |
| 2296 |
// that is greater than kMaxSigma. By using a pad and making the src 3 wide/tall instead of |
| 2297 |
// 1 we can recurse again and do another downscale. Since mirror and repeat modes are trivial |
| 2298 |
// for a single col/row we only add padding based on sigma exceeding kMaxSigma for decal. |
| 2299 |
int padX = mode == SkTileMode::kClamp || (mode == SkTileMode::kDecal && sigmaX > kMaxSigma) ? 1 |
| 2300 |
: 0; |
| 2301 |
int padY = mode == SkTileMode::kClamp || (mode == SkTileMode::kDecal && sigmaY > kMaxSigma) ? 1 |
| 2302 |
: 0; |
| 2303 |
#endif |
| 2304 |
|
| 2305 |
float scaleX = sigmaX > kMaxSigma ? kMaxSigma / sigmaX : 1.f; |
| 2306 |
float scaleY = sigmaY > kMaxSigma ? kMaxSigma / sigmaY : 1.f; |
| 2307 |
// We round down here so that when we recalculate sigmas we know they will be below |
| 2308 |
// kMaxSigma (but clamp to 1 do we don't have an empty texture). |
| 2309 |
SkISize rescaledSize = {std::max(sk_float_floor2int(srcBounds.width() * scaleX), 1), |
| 2310 |
std::max(sk_float_floor2int(srcBounds.height() * scaleY), 1)}; |
| 2311 |
// Compute the sigmas using the actual scale factors used once we integerized the |
| 2312 |
// rescaledSize. |
| 2313 |
scaleX = static_cast<float>(rescaledSize.width()) / srcBounds.width(); |
| 2314 |
scaleY = static_cast<float>(rescaledSize.height()) / srcBounds.height(); |
| 2315 |
sigmaX *= scaleX; |
| 2316 |
sigmaY *= scaleY; |
| 2317 |
|
| 2318 |
#if !defined(SK_USE_PADDED_BLUR_UPSCALE) |
| 2319 |
// Historically, padX and padY were calculated after scaling sigmaX,Y, which meant that they |
| 2320 |
// would never be greater than kMaxSigma. This causes pixel diffs so must be guarded along with |
| 2321 |
// the rest of the padding dst behavior. |
| 2322 |
int padX = mode == SkTileMode::kClamp || (mode == SkTileMode::kDecal && sigmaX > kMaxSigma) ? 1 |
| 2323 |
: 0; |
| 2324 |
int padY = mode == SkTileMode::kClamp || (mode == SkTileMode::kDecal && sigmaY > kMaxSigma) ? 1 |
| 2325 |
: 0; |
| 2326 |
#endif |
| 2327 |
|
| 2328 |
// Create the sdc with default SkSurfaceProps. Gaussian blurs will soon use a |
| 2329 |
// SurfaceFillContext, at which point the SkSurfaceProps won't exist anymore. |
| 2330 |
auto rescaledSDC = skgpu::ganesh::SurfaceDrawContext::Make( |
| 2331 |
srcCtx->recordingContext(), |
| 2332 |
colorInfo.colorType(), |
| 2333 |
colorInfo.refColorSpace(), |
| 2334 |
SkBackingFit::kApprox, |
| 2335 |
{rescaledSize.width() + 2 * padX, rescaledSize.height() + 2 * padY}, |
| 2336 |
SkSurfaceProps(), |
| 2337 |
/*label=*/"RescaledSurfaceDrawContext", |
| 2338 |
/* sampleCnt= */ 1, |
| 2339 |
skgpu::Mipmapped::kNo, |
| 2340 |
srcCtx->asSurfaceProxy()->isProtected(), |
| 2341 |
srcCtx->origin()); |
| 2342 |
if (!rescaledSDC) { |
| 2343 |
return nullptr; |
| 2344 |
} |
| 2345 |
if ((padX || padY) && mode == SkTileMode::kDecal) { |
| 2346 |
rescaledSDC->clear(SkPMColor4f{0, 0, 0, 0}); |
| 2347 |
} |
| 2348 |
if (!srcCtx->rescaleInto(rescaledSDC.get(), |
| 2349 |
SkIRect::MakeSize(rescaledSize).makeOffset(padX, padY), |
| 2350 |
srcBounds, |
| 2351 |
SkSurface::RescaleGamma::kSrc, |
| 2352 |
SkSurface::RescaleMode::kRepeatedLinear)) { |
| 2353 |
return nullptr; |
| 2354 |
} |
| 2355 |
if (mode == SkTileMode::kClamp) { |
| 2356 |
SkASSERT(padX == 1 && padY == 1); |
| 2357 |
// Rather than run a potentially multi-pass rescaler on single rows/columns we just do a |
| 2358 |
// single bilerp draw. If we find this quality unacceptable we should think more about how |
| 2359 |
// to rescale these with better quality but without 4 separate multi-pass downscales. |
| 2360 |
auto cheapDownscale = [&](SkIRect dstRect, SkIRect srcRect) { |
| 2361 |
rescaledSDC->drawTexture(nullptr, |
| 2362 |
srcCtx->readSurfaceView(), |
| 2363 |
srcAlphaType, |
| 2364 |
GrSamplerState::Filter::kLinear, |
| 2365 |
GrSamplerState::MipmapMode::kNone, |
| 2366 |
SkBlendMode::kSrc, |
| 2367 |
SK_PMColor4fWHITE, |
| 2368 |
SkRect::Make(srcRect), |
| 2369 |
SkRect::Make(dstRect), |
| 2370 |
GrQuadAAFlags::kNone, |
| 2371 |
SkCanvas::SrcRectConstraint::kFast_SrcRectConstraint, |
| 2372 |
SkMatrix::I(), |
| 2373 |
nullptr); |
| 2374 |
}; |
| 2375 |
auto [dw, dh] = rescaledSize; |
| 2376 |
// The are the src rows and columns from the source that we will scale into the dst padding. |
| 2377 |
float sLCol = srcBounds.left(); |
| 2378 |
float sTRow = srcBounds.top(); |
| 2379 |
float sRCol = srcBounds.right() - 1; |
| 2380 |
float sBRow = srcBounds.bottom() - 1; |
| 2381 |
|
| 2382 |
int sx = srcBounds.left(); |
| 2383 |
int sy = srcBounds.top(); |
| 2384 |
int sw = srcBounds.width(); |
| 2385 |
int sh = srcBounds.height(); |
| 2386 |
|
| 2387 |
// Downscale the edges from the original source. These draws should batch together (and with |
| 2388 |
// the above interior rescaling when it is a single pass). |
| 2389 |
cheapDownscale(SkIRect::MakeXYWH(0, 1, 1, dh), SkIRect::MakeXYWH(sLCol, sy, 1, sh)); |
| 2390 |
cheapDownscale(SkIRect::MakeXYWH(1, 0, dw, 1), SkIRect::MakeXYWH(sx, sTRow, sw, 1)); |
| 2391 |
cheapDownscale(SkIRect::MakeXYWH(dw + 1, 1, 1, dh), SkIRect::MakeXYWH(sRCol, sy, 1, sh)); |
| 2392 |
cheapDownscale(SkIRect::MakeXYWH(1, dh + 1, dw, 1), SkIRect::MakeXYWH(sx, sBRow, sw, 1)); |
| 2393 |
|
| 2394 |
// Copy the corners from the original source. These would batch with the edges except that |
| 2395 |
// at time of writing we recognize these can use kNearest and downgrade the filter. So they |
| 2396 |
// batch with each other but not the edge draws. |
| 2397 |
cheapDownscale(SkIRect::MakeXYWH(0, 0, 1, 1), SkIRect::MakeXYWH(sLCol, sTRow, 1, 1)); |
| 2398 |
cheapDownscale(SkIRect::MakeXYWH(dw + 1, 0, 1, 1), SkIRect::MakeXYWH(sRCol, sTRow, 1, 1)); |
| 2399 |
cheapDownscale(SkIRect::MakeXYWH(dw + 1, dh + 1, 1, 1), |
| 2400 |
SkIRect::MakeXYWH(sRCol, sBRow, 1, 1)); |
| 2401 |
cheapDownscale(SkIRect::MakeXYWH(0, dh + 1, 1, 1), SkIRect::MakeXYWH(sLCol, sBRow, 1, 1)); |
| 2402 |
} |
| 2403 |
srcView = rescaledSDC->readSurfaceView(); |
| 2404 |
// Drop the contexts so we don't hold the proxies longer than necessary. |
| 2405 |
rescaledSDC.reset(); |
| 2406 |
srcCtx.reset(); |
| 2407 |
|
| 2408 |
// Compute the dst bounds in the scaled down space. First move the origin to be at the top |
| 2409 |
// left since we trimmed off everything above and to the left of the original src bounds during |
| 2410 |
// the rescale. |
| 2411 |
SkRect scaledDstBounds = SkRect::Make(dstBounds.makeOffset(-srcBounds.topLeft())); |
| 2412 |
scaledDstBounds.fLeft *= scaleX; |
| 2413 |
scaledDstBounds.fTop *= scaleY; |
| 2414 |
scaledDstBounds.fRight *= scaleX; |
| 2415 |
scaledDstBounds.fBottom *= scaleY; |
| 2416 |
// Account for padding in our rescaled src, if any. |
| 2417 |
scaledDstBounds.offset(padX, padY); |
| 2418 |
// Turn the scaled down dst bounds into an integer pixel rect, adding 1px of padding to help |
| 2419 |
// with boundary sampling during re-expansion when there are extreme scale factors. This is |
| 2420 |
// particularly important when the blurs extend across Chrome raster tiles; w/o it the re-expand |
| 2421 |
// produces visible seams: crbug.com/1500021. |
| 2422 |
#if defined(SK_USE_PADDED_BLUR_UPSCALE) |
| 2423 |
static constexpr int kDstPadding = 1; |
| 2424 |
#else |
| 2425 |
static constexpr int kDstPadding = 0; |
| 2426 |
#endif |
| 2427 |
auto scaledDstBoundsI = scaledDstBounds.roundOut(); |
| 2428 |
scaledDstBoundsI.outset(kDstPadding, kDstPadding); |
| 2429 |
|
| 2430 |
SkIRect scaledSrcBounds = SkIRect::MakeSize(srcView.dimensions()); |
| 2431 |
auto sdc = GaussianBlur(rContext, |
| 2432 |
std::move(srcView), |
| 2433 |
srcColorType, |
| 2434 |
srcAlphaType, |
| 2435 |
colorSpace, |
| 2436 |
scaledDstBoundsI, |
| 2437 |
scaledSrcBounds, |
| 2438 |
sigmaX, |
| 2439 |
sigmaY, |
| 2440 |
mode, |
| 2441 |
fit); |
| 2442 |
if (!sdc) { |
| 2443 |
return nullptr; |
| 2444 |
} |
| 2445 |
|
| 2446 |
SkASSERT(sdc->width() == scaledDstBoundsI.width() && |
| 2447 |
sdc->height() == scaledDstBoundsI.height()); |
| 2448 |
// We rounded out the integer scaled dst bounds. Select the fractional dst bounds from the |
| 2449 |
// integer dimension blurred result when we scale back up. This also accounts for the padding |
| 2450 |
// added to 'scaledDstBoundsI' when sampling from the blurred result. |
| 2451 |
scaledDstBounds.offset(-scaledDstBoundsI.left(), -scaledDstBoundsI.top()); |
| 2452 |
return reexpand(rContext, |
| 2453 |
std::move(sdc), |
| 2454 |
scaledDstBounds, |
| 2455 |
dstBounds.size(), |
| 2456 |
std::move(colorSpace), |
| 2457 |
fit); |
| 2458 |
} |
| 2459 |
|
| 2460 |
} // namespace GrBlurUtils |