| 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 "include/core/SkBlendMode.h" |
| 9 |
#include "include/core/SkCanvas.h" |
| 10 |
#include "include/core/SkColor.h" |
| 11 |
#include "include/core/SkColorSpace.h" |
| 12 |
#include "include/core/SkImage.h" |
| 13 |
#include "include/core/SkImageInfo.h" |
| 14 |
#include "include/core/SkMaskFilter.h" |
| 15 |
#include "include/core/SkMatrix.h" |
| 16 |
#include "include/core/SkPaint.h" |
| 17 |
#include "include/core/SkPath.h" |
| 18 |
#include "include/core/SkRRect.h" |
| 19 |
#include "include/core/SkRect.h" |
| 20 |
#include "include/core/SkRefCnt.h" |
| 21 |
#include "include/core/SkSamplingOptions.h" |
| 22 |
#include "include/core/SkScalar.h" |
| 23 |
#include "include/core/SkSize.h" |
| 24 |
#include "include/core/SkTileMode.h" |
| 25 |
#include "include/gpu/GpuTypes.h" |
| 26 |
#include "include/gpu/ganesh/GrContextOptions.h" |
| 27 |
#include "include/gpu/ganesh/GrRecordingContext.h" |
| 28 |
#include "include/private/base/SkAssert.h" |
| 29 |
#include "include/private/base/SkPoint_impl.h" |
| 30 |
#include "include/private/base/SkTPin.h" |
| 31 |
#include "include/private/base/SkTemplates.h" |
| 32 |
#include "include/private/gpu/ganesh/GrImageContext.h" |
| 33 |
#include "include/private/gpu/ganesh/GrTypesPriv.h" |
| 34 |
#include "src/base/SkTLazy.h" |
| 35 |
#include "src/core/SkColorData.h" |
| 36 |
#include "src/core/SkRRectPriv.h" |
| 37 |
#include "src/core/SkSpecialImage.h" |
| 38 |
#include "src/gpu/BlurUtils.h" |
| 39 |
#include "src/gpu/Swizzle.h" |
| 40 |
#include "src/gpu/TiledTextureUtils.h" |
| 41 |
#include "src/gpu/ganesh/Device.h" |
| 42 |
#include "src/gpu/ganesh/GrBlurUtils.h" |
| 43 |
#include "src/gpu/ganesh/GrCaps.h" |
| 44 |
#include "src/gpu/ganesh/GrColorInfo.h" |
| 45 |
#include "src/gpu/ganesh/GrColorSpaceXform.h" |
| 46 |
#include "src/gpu/ganesh/GrFPArgs.h" |
| 47 |
#include "src/gpu/ganesh/GrFragmentProcessor.h" |
| 48 |
#include "src/gpu/ganesh/GrFragmentProcessors.h" |
| 49 |
#include "src/gpu/ganesh/GrOpsTypes.h" |
| 50 |
#include "src/gpu/ganesh/GrPaint.h" |
| 51 |
#include "src/gpu/ganesh/GrRecordingContextPriv.h" |
| 52 |
#include "src/gpu/ganesh/GrSamplerState.h" |
| 53 |
#include "src/gpu/ganesh/GrSurfaceProxy.h" |
| 54 |
#include "src/gpu/ganesh/GrSurfaceProxyPriv.h" |
| 55 |
#include "src/gpu/ganesh/GrSurfaceProxyView.h" |
| 56 |
#include "src/gpu/ganesh/GrTextureProxy.h" |
| 57 |
#include "src/gpu/ganesh/SkGr.h" |
| 58 |
#include "src/gpu/ganesh/SurfaceDrawContext.h" |
| 59 |
#include "src/gpu/ganesh/effects/GrBlendFragmentProcessor.h" |
| 60 |
#include "src/gpu/ganesh/effects/GrTextureEffect.h" |
| 61 |
#include "src/gpu/ganesh/geometry/GrRect.h" |
| 62 |
#include "src/gpu/ganesh/geometry/GrStyledShape.h" |
| 63 |
#include "src/gpu/ganesh/image/GrImageUtils.h" |
| 64 |
#include "src/gpu/ganesh/image/SkImage_Ganesh.h" |
| 65 |
#include "src/gpu/ganesh/image/SkSpecialImage_Ganesh.h" |
| 66 |
#include "src/image/SkImage_Base.h" |
| 67 |
#include "src/shaders/SkShaderBase.h" |
| 68 |
|
| 69 |
#include <memory> |
| 70 |
#include <tuple> |
| 71 |
#include <utility> |
| 72 |
|
| 73 |
class GrClip; |
| 74 |
|
| 75 |
using namespace skia_private; |
| 76 |
|
| 77 |
namespace { |
| 78 |
|
| 79 |
inline bool use_shader(bool textureIsAlphaOnly, const SkPaint& paint) { |
| 80 |
return textureIsAlphaOnly && paint.getShader(); |
| 81 |
} |
| 82 |
|
| 83 |
////////////////////////////////////////////////////////////////////////////// |
| 84 |
// Helper functions for dropping src rect subset with GrSamplerState::Filter::kLinear. |
| 85 |
|
| 86 |
static const SkScalar kColorBleedTolerance = 0.001f; |
| 87 |
|
| 88 |
bool has_aligned_samples(const SkRect& srcRect, const SkRect& transformedRect) { |
| 89 |
// detect pixel disalignment |
| 90 |
if (SkScalarAbs(SkScalarRoundToScalar(transformedRect.left()) - transformedRect.left()) < kColorBleedTolerance && |
| 91 |
SkScalarAbs(SkScalarRoundToScalar(transformedRect.top()) - transformedRect.top()) < kColorBleedTolerance && |
| 92 |
SkScalarAbs(transformedRect.width() - srcRect.width()) < kColorBleedTolerance && |
| 93 |
SkScalarAbs(transformedRect.height() - srcRect.height()) < kColorBleedTolerance) { |
| 94 |
return true; |
| 95 |
} |
| 96 |
return false; |
| 97 |
} |
| 98 |
|
| 99 |
bool may_color_bleed(const SkRect& srcRect, |
| 100 |
const SkRect& transformedRect, |
| 101 |
const SkMatrix& m, |
| 102 |
int numSamples) { |
| 103 |
// Only gets called if has_aligned_samples returned false. |
| 104 |
// So we can assume that sampling is axis aligned but not texel aligned. |
| 105 |
SkASSERT(!has_aligned_samples(srcRect, transformedRect)); |
| 106 |
SkRect innerSrcRect(srcRect), innerTransformedRect, outerTransformedRect(transformedRect); |
| 107 |
if (numSamples > 1) { |
| 108 |
innerSrcRect.inset(SK_Scalar1, SK_Scalar1); |
| 109 |
} else { |
| 110 |
innerSrcRect.inset(SK_ScalarHalf, SK_ScalarHalf); |
| 111 |
} |
| 112 |
m.mapRect(&innerTransformedRect, innerSrcRect); |
| 113 |
|
| 114 |
// The gap between outerTransformedRect and innerTransformedRect |
| 115 |
// represents the projection of the source border area, which is |
| 116 |
// problematic for color bleeding. We must check whether any |
| 117 |
// destination pixels sample the border area. |
| 118 |
outerTransformedRect.inset(kColorBleedTolerance, kColorBleedTolerance); |
| 119 |
innerTransformedRect.outset(kColorBleedTolerance, kColorBleedTolerance); |
| 120 |
SkIRect outer, inner; |
| 121 |
outerTransformedRect.round(&outer); |
| 122 |
innerTransformedRect.round(&inner); |
| 123 |
// If the inner and outer rects round to the same result, it means the |
| 124 |
// border does not overlap any pixel centers. Yay! |
| 125 |
return inner != outer; |
| 126 |
} |
| 127 |
|
| 128 |
bool can_ignore_linear_filtering_subset(const SkRect& srcSubset, |
| 129 |
const SkMatrix& srcRectToDeviceSpace, |
| 130 |
int numSamples) { |
| 131 |
if (srcRectToDeviceSpace.rectStaysRect()) { |
| 132 |
// sampling is axis-aligned |
| 133 |
SkRect transformedRect; |
| 134 |
srcRectToDeviceSpace.mapRect(&transformedRect, srcSubset); |
| 135 |
|
| 136 |
if (has_aligned_samples(srcSubset, transformedRect) || |
| 137 |
!may_color_bleed(srcSubset, transformedRect, srcRectToDeviceSpace, numSamples)) { |
| 138 |
return true; |
| 139 |
} |
| 140 |
} |
| 141 |
return false; |
| 142 |
} |
| 143 |
|
| 144 |
////////////////////////////////////////////////////////////////////////////// |
| 145 |
// Helper functions for drawing an image with ganesh::SurfaceDrawContext |
| 146 |
|
| 147 |
/** |
| 148 |
* Checks whether the paint is compatible with using SurfaceDrawContext::drawTexture. It is more |
| 149 |
* efficient than the SkImage general case. |
| 150 |
*/ |
| 151 |
bool can_use_draw_texture(const SkPaint& paint, const SkSamplingOptions& sampling) { |
| 152 |
return (!paint.getColorFilter() && !paint.getShader() && !paint.getMaskFilter() && |
| 153 |
!paint.getImageFilter() && !paint.getBlender() && !sampling.isAniso() && |
| 154 |
!sampling.useCubic && sampling.mipmap == SkMipmapMode::kNone); |
| 155 |
} |
| 156 |
|
| 157 |
SkPMColor4f texture_color(SkColor4f paintColor, float entryAlpha, GrColorType srcColorType, |
| 158 |
const GrColorInfo& dstColorInfo) { |
| 159 |
paintColor.fA *= entryAlpha; |
| 160 |
if (GrColorTypeIsAlphaOnly(srcColorType)) { |
| 161 |
return SkColor4fPrepForDst(paintColor, dstColorInfo).premul(); |
| 162 |
} else { |
| 163 |
float paintAlpha = SkTPin(paintColor.fA, 0.f, 1.f); |
| 164 |
return { paintAlpha, paintAlpha, paintAlpha, paintAlpha }; |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
// Assumes srcRect and dstRect have already been optimized to fit the proxy |
| 169 |
void draw_texture(skgpu::ganesh::SurfaceDrawContext* sdc, |
| 170 |
const GrClip* clip, |
| 171 |
const SkMatrix& ctm, |
| 172 |
const SkPaint& paint, |
| 173 |
GrSamplerState::Filter filter, |
| 174 |
const SkRect& srcRect, |
| 175 |
const SkRect& dstRect, |
| 176 |
const SkPoint dstClip[4], |
| 177 |
GrQuadAAFlags aaFlags, |
| 178 |
SkCanvas::SrcRectConstraint constraint, |
| 179 |
GrSurfaceProxyView view, |
| 180 |
const GrColorInfo& srcColorInfo) { |
| 181 |
if (GrColorTypeIsAlphaOnly(srcColorInfo.colorType())) { |
| 182 |
view.concatSwizzle(skgpu::Swizzle("aaaa")); |
| 183 |
} |
| 184 |
const GrColorInfo& dstInfo = sdc->colorInfo(); |
| 185 |
auto textureXform = GrColorSpaceXform::Make(srcColorInfo, sdc->colorInfo()); |
| 186 |
GrSurfaceProxy* proxy = view.proxy(); |
| 187 |
// Must specify the strict constraint when the proxy is not functionally exact and the src |
| 188 |
// rect would access pixels outside the proxy's content area without the constraint. |
| 189 |
if (constraint != SkCanvas::kStrict_SrcRectConstraint && !proxy->isFunctionallyExact()) { |
| 190 |
// Conservative estimate of how much a coord could be outset from src rect: |
| 191 |
// 1/2 pixel for AA and 1/2 pixel for linear filtering |
| 192 |
float buffer = 0.5f * (aaFlags != GrQuadAAFlags::kNone) + |
| 193 |
GrTextureEffect::kLinearInset * (filter == GrSamplerState::Filter::kLinear); |
| 194 |
SkRect safeBounds = proxy->getBoundsRect(); |
| 195 |
safeBounds.inset(buffer, buffer); |
| 196 |
if (!safeBounds.contains(srcRect)) { |
| 197 |
constraint = SkCanvas::kStrict_SrcRectConstraint; |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
SkPMColor4f color = texture_color(paint.getColor4f(), 1.f, srcColorInfo.colorType(), dstInfo); |
| 202 |
if (dstClip) { |
| 203 |
// Get source coords corresponding to dstClip |
| 204 |
SkPoint srcQuad[4]; |
| 205 |
GrMapRectPoints(dstRect, srcRect, dstClip, srcQuad, 4); |
| 206 |
|
| 207 |
sdc->drawTextureQuad(clip, |
| 208 |
std::move(view), |
| 209 |
srcColorInfo.colorType(), |
| 210 |
srcColorInfo.alphaType(), |
| 211 |
filter, |
| 212 |
GrSamplerState::MipmapMode::kNone, |
| 213 |
paint.getBlendMode_or(SkBlendMode::kSrcOver), |
| 214 |
color, |
| 215 |
srcQuad, |
| 216 |
dstClip, |
| 217 |
aaFlags, |
| 218 |
constraint == SkCanvas::kStrict_SrcRectConstraint ? &srcRect : nullptr, |
| 219 |
ctm, |
| 220 |
std::move(textureXform)); |
| 221 |
} else { |
| 222 |
sdc->drawTexture(clip, |
| 223 |
std::move(view), |
| 224 |
srcColorInfo.alphaType(), |
| 225 |
filter, |
| 226 |
GrSamplerState::MipmapMode::kNone, |
| 227 |
paint.getBlendMode_or(SkBlendMode::kSrcOver), |
| 228 |
color, |
| 229 |
srcRect, |
| 230 |
dstRect, |
| 231 |
aaFlags, |
| 232 |
constraint, |
| 233 |
ctm, |
| 234 |
std::move(textureXform)); |
| 235 |
} |
| 236 |
} |
| 237 |
|
| 238 |
SkFilterMode downgrade_to_filter(const SkSamplingOptions& sampling) { |
| 239 |
SkFilterMode filter = sampling.filter; |
| 240 |
if (sampling.isAniso() || sampling.useCubic || sampling.mipmap != SkMipmapMode::kNone) { |
| 241 |
// if we were "fancier" than just bilerp, only do bilerp |
| 242 |
filter = SkFilterMode::kLinear; |
| 243 |
} |
| 244 |
return filter; |
| 245 |
} |
| 246 |
|
| 247 |
} // anonymous namespace |
| 248 |
|
| 249 |
|
| 250 |
////////////////////////////////////////////////////////////////////////////// |
| 251 |
|
| 252 |
namespace skgpu::ganesh { |
| 253 |
|
| 254 |
void Device::drawEdgeAAImage(const SkImage* image, |
| 255 |
const SkRect& src, |
| 256 |
const SkRect& dst, |
| 257 |
const SkPoint dstClip[4], |
| 258 |
SkCanvas::QuadAAFlags canvasAAFlags, |
| 259 |
const SkMatrix& localToDevice, |
| 260 |
const SkSamplingOptions& sampling, |
| 261 |
const SkPaint& paint, |
| 262 |
SkCanvas::SrcRectConstraint constraint, |
| 263 |
const SkMatrix& srcToDst, |
| 264 |
SkTileMode tm) { |
| 265 |
GrRecordingContext* rContext = fContext.get(); |
| 266 |
SurfaceDrawContext* sdc = fSurfaceDrawContext.get(); |
| 267 |
const GrClip* clip = this->clip(); |
| 268 |
|
| 269 |
GrQuadAAFlags aaFlags = SkToGrQuadAAFlags(canvasAAFlags); |
| 270 |
auto ib = as_IB(image); |
| 271 |
if (tm == SkTileMode::kClamp && !ib->isYUVA() && can_use_draw_texture(paint, sampling)) { |
| 272 |
// We've done enough checks above to allow us to pass ClampNearest() and not check for |
| 273 |
// scaling adjustments. |
| 274 |
auto [view, ct] = skgpu::ganesh::AsView(rContext, |
| 275 |
image, |
| 276 |
skgpu::Mipmapped::kNo, |
| 277 |
this->targetProxy()); |
| 278 |
if (!view) { |
| 279 |
return; |
| 280 |
} |
| 281 |
GrColorInfo info(image->imageInfo().colorInfo()); |
| 282 |
info = info.makeColorType(ct); |
| 283 |
draw_texture(sdc, |
| 284 |
clip, |
| 285 |
localToDevice, |
| 286 |
paint, |
| 287 |
sampling.filter, |
| 288 |
src, |
| 289 |
dst, |
| 290 |
dstClip, |
| 291 |
aaFlags, |
| 292 |
constraint, |
| 293 |
std::move(view), |
| 294 |
info); |
| 295 |
return; |
| 296 |
} |
| 297 |
|
| 298 |
const SkMaskFilter* mf = paint.getMaskFilter(); |
| 299 |
|
| 300 |
// The shader expects proper local coords, so we can't replace local coords with texture coords |
| 301 |
// if the shader will be used. If we have a mask filter we will change the underlying geometry |
| 302 |
// that is rendered. |
| 303 |
bool canUseTextureCoordsAsLocalCoords = !use_shader(image->isAlphaOnly(), paint) && !mf; |
| 304 |
|
| 305 |
// Specifying the texture coords as local coordinates is an attempt to enable more GrDrawOp |
| 306 |
// combining by not baking anything about the srcRect, dstRect, or ctm, into the texture |
| 307 |
// FP. In the future this should be an opaque optimization enabled by the combination of |
| 308 |
// GrDrawOp/GP and FP. |
| 309 |
if (GrFragmentProcessors::IsSupported(mf)) { |
| 310 |
mf = nullptr; |
| 311 |
} |
| 312 |
|
| 313 |
bool restrictToSubset = SkCanvas::kStrict_SrcRectConstraint == constraint; |
| 314 |
|
| 315 |
// If we have to outset for AA then we will generate texture coords outside the src rect. The |
| 316 |
// same happens for any mask filter that extends the bounds rendered in the dst. |
| 317 |
// This is conservative as a mask filter does not have to expand the bounds rendered. |
| 318 |
bool coordsAllInsideSrcRect = aaFlags == GrQuadAAFlags::kNone && !mf; |
| 319 |
|
| 320 |
// Check for optimization to drop the src rect constraint when using linear filtering. |
| 321 |
// TODO: Just rely on image to handle this. |
| 322 |
if (sampling.isAniso() && !sampling.useCubic && sampling.filter == SkFilterMode::kLinear && |
| 323 |
restrictToSubset && sampling.mipmap == SkMipmapMode::kNone && coordsAllInsideSrcRect && |
| 324 |
!ib->isYUVA()) { |
| 325 |
SkMatrix combinedMatrix; |
| 326 |
combinedMatrix.setConcat(localToDevice, srcToDst); |
| 327 |
if (can_ignore_linear_filtering_subset(src, combinedMatrix, sdc->numSamples())) { |
| 328 |
restrictToSubset = false; |
| 329 |
} |
| 330 |
} |
| 331 |
|
| 332 |
SkMatrix textureMatrix; |
| 333 |
if (canUseTextureCoordsAsLocalCoords) { |
| 334 |
textureMatrix = SkMatrix::I(); |
| 335 |
} else { |
| 336 |
if (auto inv = srcToDst.invert()) { |
| 337 |
textureMatrix = *inv; |
| 338 |
} else { |
| 339 |
return; |
| 340 |
} |
| 341 |
} |
| 342 |
const SkRect* subset = restrictToSubset ? &src : nullptr; |
| 343 |
const SkRect* domain = coordsAllInsideSrcRect ? &src : nullptr; |
| 344 |
SkTileMode tileModes[] = {tm, tm}; |
| 345 |
std::unique_ptr<GrFragmentProcessor> fp = skgpu::ganesh::AsFragmentProcessor( |
| 346 |
sdc, image, sampling, tileModes, textureMatrix, subset, domain); |
| 347 |
fp = GrColorSpaceXformEffect::Make( |
| 348 |
std::move(fp), image->imageInfo().colorInfo(), sdc->colorInfo()); |
| 349 |
if (image->isAlphaOnly()) { |
| 350 |
if (const auto* shader = as_SB(paint.getShader())) { |
| 351 |
auto shaderFP = GrFragmentProcessors::Make(shader, |
| 352 |
GrFPArgs(sdc, |
| 353 |
&sdc->colorInfo(), |
| 354 |
sdc->surfaceProps(), |
| 355 |
GrFPArgs::Scope::kDefault), |
| 356 |
localToDevice); |
| 357 |
if (!shaderFP) { |
| 358 |
return; |
| 359 |
} |
| 360 |
fp = GrBlendFragmentProcessor::Make<SkBlendMode::kDstIn>(std::move(fp), |
| 361 |
std::move(shaderFP)); |
| 362 |
} else { |
| 363 |
// Multiply the input (paint) color by the texture (alpha) |
| 364 |
fp = GrFragmentProcessor::MulInputByChildAlpha(std::move(fp)); |
| 365 |
} |
| 366 |
} |
| 367 |
|
| 368 |
GrPaint grPaint; |
| 369 |
if (!SkPaintToGrPaintReplaceShader(sdc, paint, localToDevice, std::move(fp), &grPaint)) { |
| 370 |
return; |
| 371 |
} |
| 372 |
|
| 373 |
if (!mf) { |
| 374 |
// Can draw the image directly (any mask filter on the paint was converted to an FP already) |
| 375 |
if (dstClip) { |
| 376 |
SkPoint srcClipPoints[4]; |
| 377 |
SkPoint* srcClip = nullptr; |
| 378 |
if (canUseTextureCoordsAsLocalCoords) { |
| 379 |
// Calculate texture coordinates that match the dst clip |
| 380 |
GrMapRectPoints(dst, src, dstClip, srcClipPoints, 4); |
| 381 |
srcClip = srcClipPoints; |
| 382 |
} |
| 383 |
sdc->fillQuadWithEdgeAA(clip, std::move(grPaint), aaFlags, localToDevice, |
| 384 |
dstClip, srcClip); |
| 385 |
} else { |
| 386 |
// Provide explicit texture coords when possible, otherwise rely on texture matrix |
| 387 |
sdc->fillRectWithEdgeAA(clip, std::move(grPaint), aaFlags, localToDevice, dst, |
| 388 |
canUseTextureCoordsAsLocalCoords ? &src : nullptr); |
| 389 |
} |
| 390 |
} else { |
| 391 |
// Must draw the mask filter as a GrStyledShape. For now, this loses the per-edge AA |
| 392 |
// information since it always draws with AA, but that should not be noticeable since the |
| 393 |
// mask filter is probably a blur. |
| 394 |
GrStyledShape shape; |
| 395 |
if (dstClip) { |
| 396 |
// Represent it as an SkPath formed from the dstClip |
| 397 |
shape = GrStyledShape(SkPath::Polygon({dstClip, 4}, true)); |
| 398 |
} else { |
| 399 |
shape = GrStyledShape(dst); |
| 400 |
} |
| 401 |
|
| 402 |
GrBlurUtils::DrawShapeWithMaskFilter( |
| 403 |
rContext, sdc, clip, shape, std::move(grPaint), localToDevice, mf); |
| 404 |
} |
| 405 |
} |
| 406 |
|
| 407 |
void Device::drawSpecial(SkSpecialImage* special, |
| 408 |
const SkMatrix& localToDevice, |
| 409 |
const SkSamplingOptions& origSampling, |
| 410 |
const SkPaint& paint, |
| 411 |
SkCanvas::SrcRectConstraint constraint) { |
| 412 |
SkASSERT(!paint.getMaskFilter() && !paint.getImageFilter()); |
| 413 |
SkASSERT(special->isGaneshBacked()); |
| 414 |
|
| 415 |
SkRect src = SkRect::Make(special->subset()); |
| 416 |
SkRect dst = SkRect::MakeWH(special->width(), special->height()); |
| 417 |
SkMatrix srcToDst = SkMatrix::RectToRectOrIdentity(src, dst); |
| 418 |
|
| 419 |
SkSamplingOptions sampling = SkSamplingOptions(downgrade_to_filter(origSampling)); |
| 420 |
GrAA aa = fSurfaceDrawContext->chooseAA(paint); |
| 421 |
SkCanvas::QuadAAFlags aaFlags = (aa == GrAA::kYes) ? SkCanvas::kAll_QuadAAFlags |
| 422 |
: SkCanvas::kNone_QuadAAFlags; |
| 423 |
|
| 424 |
GrSurfaceProxyView view = SkSpecialImages::AsView(this->recordingContext(), special); |
| 425 |
if (!view) { |
| 426 |
// This shouldn't happen since we shouldn't be mixing SkSpecialImage subclasses but |
| 427 |
// returning early should avoid problems in release builds. |
| 428 |
SkASSERT(false); |
| 429 |
return; |
| 430 |
} |
| 431 |
|
| 432 |
if (constraint == SkCanvas::kFast_SrcRectConstraint) { |
| 433 |
// If 'fast' was requested, we assume the caller has done sufficient analysis to know the |
| 434 |
// logical dimensions are safe (which is true for FilterResult, the only current caller that |
| 435 |
// passes in 'fast'). Without exactify'ing the proxy, GrTextureEffect would re-introduce |
| 436 |
// subset clamping. |
| 437 |
view.proxy()->priv().exactify(); |
| 438 |
} |
| 439 |
|
| 440 |
SkImage_Ganesh image(sk_ref_sp(special->getContext()), |
| 441 |
special->uniqueID(), |
| 442 |
std::move(view), |
| 443 |
special->colorInfo()); |
| 444 |
// In most cases this ought to hit draw_texture since there won't be a color filter, |
| 445 |
// alpha-only texture+shader, or a high filter quality. |
| 446 |
this->drawEdgeAAImage(&image, |
| 447 |
src, |
| 448 |
dst, |
| 449 |
/* dstClip= */nullptr, |
| 450 |
aaFlags, |
| 451 |
localToDevice, |
| 452 |
sampling, |
| 453 |
paint, |
| 454 |
constraint, |
| 455 |
srcToDst, |
| 456 |
SkTileMode::kClamp); |
| 457 |
} |
| 458 |
|
| 459 |
void Device::drawCoverageMask(const SkSpecialImage* mask, |
| 460 |
const SkMatrix& maskToDevice, |
| 461 |
const SkSamplingOptions& sampling, |
| 462 |
const SkPaint& paint) { |
| 463 |
// Use the active local-to-device transform for this since it determines the |
| 464 |
// local coords for evaluating the skpaint, whereas the provided 'maskToDevice' |
| 465 |
// just places the coverage mask. |
| 466 |
SkMatrix localToDevice = this->localToDevice(); |
| 467 |
auto deviceToLocal = localToDevice.invert(); |
| 468 |
if (!deviceToLocal) { |
| 469 |
return; |
| 470 |
} |
| 471 |
|
| 472 |
GrSurfaceProxyView view = SkSpecialImages::AsView(this->recordingContext(), mask); |
| 473 |
if (!view) { |
| 474 |
// This shouldn't happen since we shouldn't be mixing SkSpecialImage subclasses but |
| 475 |
// returning early should avoid problems in release builds. |
| 476 |
SkASSERT(false); |
| 477 |
return; |
| 478 |
} |
| 479 |
|
| 480 |
SkTileMode tileModes[] = {SkTileMode::kDecal, SkTileMode::kDecal}; |
| 481 |
|
| 482 |
auto deviceToMask = maskToDevice.invert(); |
| 483 |
if (!deviceToMask) { |
| 484 |
return; |
| 485 |
} |
| 486 |
// 'textureMaskSpace' needs to map from local coords -> mask coords -> texture coords. |
| 487 |
SkMatrix textureMaskSpace = localToDevice; |
| 488 |
textureMaskSpace.postConcat(*deviceToMask); |
| 489 |
textureMaskSpace.postTranslate(mask->subset().fLeft, mask->subset().fTop); |
| 490 |
|
| 491 |
SkRect maskSubset = SkRect::Make(mask->subset()); |
| 492 |
std::unique_ptr<GrFragmentProcessor> coverageFP = skgpu::ganesh::MakeFragmentProcessorFromView( |
| 493 |
this->recordingContext(), std::move(view), mask->alphaType(), sampling, tileModes, |
| 494 |
textureMaskSpace, &maskSubset, &maskSubset); |
| 495 |
coverageFP = GrFragmentProcessor::SwizzleOutput(std::move(coverageFP), skgpu::Swizzle("aaaa")); |
| 496 |
|
| 497 |
|
| 498 |
SurfaceDrawContext* sdc = fSurfaceDrawContext.get(); |
| 499 |
GrPaint grPaint; |
| 500 |
// Any shading is done in local space which we want to draw to the device. |
| 501 |
SkPaintToGrPaint(sdc, paint, localToDevice, &grPaint); |
| 502 |
grPaint.setCoverageFragmentProcessor(std::move(coverageFP)); |
| 503 |
|
| 504 |
GrAA aa = fSurfaceDrawContext->chooseAA(paint); |
| 505 |
SkCanvas::QuadAAFlags aaFlags = (aa == GrAA::kYes) ? SkCanvas::kAll_QuadAAFlags |
| 506 |
: SkCanvas::kNone_QuadAAFlags; |
| 507 |
|
| 508 |
SkMatrix maskToLocal = SkMatrix::Concat(*deviceToLocal, maskToDevice); |
| 509 |
SkRect maskRect = SkRect::MakeWH(mask->width(), mask->height()); |
| 510 |
// 'local' are mask points transformed to get local space. 'localToDevice' may |
| 511 |
// have a perspective transform that 'maskToDevice' doesn't so this is necessary. |
| 512 |
SkPoint local[4]; |
| 513 |
maskToLocal.mapRectToQuad(local, maskRect); |
| 514 |
|
| 515 |
sdc->fillQuadWithEdgeAA(this->clip(), |
| 516 |
std::move(grPaint), |
| 517 |
SkToGrQuadAAFlags(aaFlags), |
| 518 |
localToDevice, |
| 519 |
local, |
| 520 |
nullptr); |
| 521 |
} |
| 522 |
|
| 523 |
void Device::drawImageQuadDirect(const SkImage* image, |
| 524 |
const SkRect& srcRect, |
| 525 |
const SkRect& dstRect, |
| 526 |
const SkPoint dstClip[4], |
| 527 |
SkCanvas::QuadAAFlags aaFlags, |
| 528 |
const SkMatrix* preViewMatrix, |
| 529 |
const SkSamplingOptions& origSampling, |
| 530 |
const SkPaint& paint, |
| 531 |
SkCanvas::SrcRectConstraint constraint) { |
| 532 |
SkRect src; |
| 533 |
SkRect dst; |
| 534 |
SkMatrix srcToDst; |
| 535 |
auto mode = TiledTextureUtils::OptimizeSampleArea(SkISize::Make(image->width(), |
| 536 |
image->height()), |
| 537 |
srcRect, dstRect, dstClip, |
| 538 |
&src, &dst, &srcToDst); |
| 539 |
if (mode == TiledTextureUtils::ImageDrawMode::kSkip) { |
| 540 |
return; |
| 541 |
} |
| 542 |
|
| 543 |
if (src.contains(image->bounds())) { |
| 544 |
constraint = SkCanvas::kFast_SrcRectConstraint; |
| 545 |
} |
| 546 |
// Depending on the nature of image, it can flow through more or less optimal pipelines |
| 547 |
SkTileMode tileMode = mode == TiledTextureUtils::ImageDrawMode::kDecal ? SkTileMode::kDecal |
| 548 |
: SkTileMode::kClamp; |
| 549 |
|
| 550 |
// Get final CTM matrix |
| 551 |
SkMatrix ctm = this->localToDevice(); |
| 552 |
if (preViewMatrix) { |
| 553 |
ctm.preConcat(*preViewMatrix); |
| 554 |
} |
| 555 |
|
| 556 |
SkSamplingOptions sampling = origSampling; |
| 557 |
bool sharpenMM = fContext->priv().options().fSharpenMipmappedTextures; |
| 558 |
if (sampling.mipmap != SkMipmapMode::kNone && |
| 559 |
TiledTextureUtils::CanDisableMipmap(ctm, srcToDst, sharpenMM)) { |
| 560 |
sampling = SkSamplingOptions(sampling.filter); |
| 561 |
} |
| 562 |
|
| 563 |
this->drawEdgeAAImage(image, |
| 564 |
src, |
| 565 |
dst, |
| 566 |
dstClip, |
| 567 |
aaFlags, |
| 568 |
ctm, |
| 569 |
sampling, |
| 570 |
paint, |
| 571 |
constraint, |
| 572 |
srcToDst, |
| 573 |
tileMode); |
| 574 |
} |
| 575 |
|
| 576 |
void Device::drawEdgeAAImageSet(const SkCanvas::ImageSetEntry set[], int count, |
| 577 |
const SkPoint dstClips[], const SkMatrix preViewMatrices[], |
| 578 |
const SkSamplingOptions& sampling, const SkPaint& paint, |
| 579 |
SkCanvas::SrcRectConstraint constraint) { |
| 580 |
SkASSERT(count > 0); |
| 581 |
if (!can_use_draw_texture(paint, sampling)) { |
| 582 |
// Send every entry through drawImageQuad() to handle the more complicated paint |
| 583 |
int dstClipIndex = 0; |
| 584 |
for (int i = 0; i < count; ++i) { |
| 585 |
// Only no clip or quad clip are supported |
| 586 |
SkASSERT(!set[i].fHasClip || dstClips); |
| 587 |
SkASSERT(set[i].fMatrixIndex < 0 || preViewMatrices); |
| 588 |
|
| 589 |
SkTCopyOnFirstWrite<SkPaint> entryPaint(paint); |
| 590 |
if (set[i].fAlpha != 1.f) { |
| 591 |
auto paintAlpha = paint.getAlphaf(); |
| 592 |
entryPaint.writable()->setAlphaf(paintAlpha * set[i].fAlpha); |
| 593 |
} |
| 594 |
this->drawImageQuadDirect( |
| 595 |
set[i].fImage.get(), set[i].fSrcRect, set[i].fDstRect, |
| 596 |
set[i].fHasClip ? dstClips + dstClipIndex : nullptr, |
| 597 |
static_cast<SkCanvas::QuadAAFlags>(set[i].fAAFlags), |
| 598 |
set[i].fMatrixIndex < 0 ? nullptr : preViewMatrices + set[i].fMatrixIndex, |
| 599 |
sampling, *entryPaint, constraint); |
| 600 |
dstClipIndex += 4 * set[i].fHasClip; |
| 601 |
} |
| 602 |
return; |
| 603 |
} |
| 604 |
|
| 605 |
GrSamplerState::Filter filter = sampling.filter == SkFilterMode::kNearest |
| 606 |
? GrSamplerState::Filter::kNearest |
| 607 |
: GrSamplerState::Filter::kLinear; |
| 608 |
SkBlendMode mode = paint.getBlendMode_or(SkBlendMode::kSrcOver); |
| 609 |
|
| 610 |
AutoTArray<GrTextureSetEntry> textures(count); |
| •611 |
// We accumulate compatible proxies until we find an incompatible one or reach the end and |
| 612 |
// issue the accumulated 'n' draws starting at 'base'. 'p' represents the number of proxy |
| 613 |
// switches that occur within the 'n' entries. |
| 614 |
int base = 0, n = 0, p = 0; |
| •615 |
auto draw = [&](int nextBase, bool setMayHavePersp) { |
| 616 |
if (n > 0) { |
| 617 |
auto textureXform = GrColorSpaceXform::Make(set[base].fImage->imageInfo().colorInfo(), |
| 618 |
fSurfaceDrawContext->colorInfo()); |
| 619 |
fSurfaceDrawContext->drawTextureSet(this->clip(), |
| 620 |
textures.get() + base, |
| 621 |
n, |
| 622 |
p, |
| 623 |
filter, |
| 624 |
GrSamplerState::MipmapMode::kNone, |
| 625 |
mode, |
| 626 |
constraint, |
| 627 |
this->localToDevice(), |
| •628 |
std::move(textureXform), |
| 629 |
setMayHavePersp); |
| 630 |
} |
| 631 |
base = nextBase; |
| 632 |
n = 0; |
| 633 |
p = 0; |
| 634 |
}; |
| •635 |
// This is a conservatively computed property of the image set that disables a fast path |
| 636 |
// in TextureOp::AddTextureSetOps. |
| 637 |
bool setMayHavePersp = this->localToDevice().hasPerspective(); |
| 638 |
int dstClipIndex = 0; |
| 639 |
for (int i = 0; i < count; ++i) { |
| 640 |
SkASSERT(!set[i].fHasClip || dstClips); |
| 641 |
SkASSERT(set[i].fMatrixIndex < 0 || preViewMatrices); |
| 642 |
|
| 643 |
// Manage the dst clip pointer tracking before any continues are used so we don't lose |
| 644 |
// our place in the dstClips array. |
| 645 |
const SkPoint* clip = set[i].fHasClip ? dstClips + dstClipIndex : nullptr; |
| 646 |
dstClipIndex += 4 * set[i].fHasClip; |
| 647 |
|
| 648 |
// The default SkDevice implementation is based on drawImageRect which does not allow |
| 649 |
// non-sorted src rects. TODO: Decide this is OK or make sure we handle it. |
| 650 |
if (!set[i].fSrcRect.isSorted()) { |
| •651 |
draw(i + 1, setMayHavePersp); |
| 652 |
continue; |
| 653 |
} |
| 654 |
|
| 655 |
GrSurfaceProxyView view; |
| 656 |
const SkImage_Base* image = as_IB(set[i].fImage.get()); |
| 657 |
// Extract view from image, but skip YUV images so they get processed through |
| 658 |
// drawImageQuad and the proper effect to dynamically sample their planes. |
| 659 |
if (!image->isYUVA()) { |
| 660 |
std::tie(view, std::ignore) = |
| 661 |
skgpu::ganesh::AsView(this->recordingContext(), |
| 662 |
image, |
| 663 |
skgpu::Mipmapped::kNo, |
| 664 |
this->targetProxy()); |
| 665 |
if (image->isAlphaOnly()) { |
| 666 |
skgpu::Swizzle swizzle = skgpu::Swizzle::Concat(view.swizzle(), |
| 667 |
skgpu::Swizzle("aaaa")); |
| 668 |
view = {view.detachProxy(), view.origin(), swizzle}; |
| 669 |
} |
| 670 |
} |
| 671 |
|
| 672 |
if (!view) { |
| 673 |
// This image can't go through the texture op, send through general image pipeline |
| 674 |
// after flushing current batch. |
| •675 |
draw(i + 1, setMayHavePersp); |
| 676 |
SkTCopyOnFirstWrite<SkPaint> entryPaint(paint); |
| 677 |
if (set[i].fAlpha != 1.f) { |
| 678 |
auto paintAlpha = paint.getAlphaf(); |
| 679 |
entryPaint.writable()->setAlphaf(paintAlpha * set[i].fAlpha); |
| 680 |
} |
| 681 |
this->drawImageQuadDirect( |
| 682 |
image, set[i].fSrcRect, set[i].fDstRect, clip, |
| 683 |
static_cast<SkCanvas::QuadAAFlags>(set[i].fAAFlags), |
| 684 |
set[i].fMatrixIndex < 0 ? nullptr : preViewMatrices + set[i].fMatrixIndex, |
| 685 |
sampling, *entryPaint, constraint); |
| 686 |
continue; |
| 687 |
} |
| 688 |
|
| 689 |
textures[i].fProxyView = std::move(view); |
| 690 |
textures[i].fSrcAlphaType = image->alphaType(); |
| 691 |
textures[i].fSrcRect = set[i].fSrcRect; |
| 692 |
textures[i].fDstRect = set[i].fDstRect; |
| 693 |
textures[i].fDstClipQuad = clip; |
| 694 |
textures[i].fPreViewMatrix = |
| 695 |
set[i].fMatrixIndex < 0 ? nullptr : preViewMatrices + set[i].fMatrixIndex; |
| •696 |
if (textures[i].fPreViewMatrix && textures[i].fPreViewMatrix->hasPerspective()) { |
| 697 |
// Once set, this flag stays on for the rest of the image set |
| 698 |
setMayHavePersp = true; |
| 699 |
} |
| 700 |
textures[i].fColor = texture_color(paint.getColor4f(), set[i].fAlpha, |
| 701 |
SkColorTypeToGrColorType(image->colorType()), |
| 702 |
fSurfaceDrawContext->colorInfo()); |
| 703 |
textures[i].fAAFlags = SkToGrQuadAAFlags(set[i].fAAFlags); |
| 704 |
|
| 705 |
if (n > 0 && |
| 706 |
(!GrTextureProxy::ProxiesAreCompatibleAsDynamicState( |
| 707 |
textures[i].fProxyView.proxy(), |
| 708 |
textures[base].fProxyView.proxy()) || |
| 709 |
textures[i].fProxyView.swizzle() != textures[base].fProxyView.swizzle() || |
| 710 |
set[i].fImage->alphaType() != set[base].fImage->alphaType() || |
| 711 |
!SkColorSpace::Equals(set[i].fImage->colorSpace(), set[base].fImage->colorSpace()))) { |
| •712 |
draw(i, setMayHavePersp); |
| 713 |
} |
| 714 |
// Whether or not we submitted a draw in the above if(), this ith entry is in the current |
| 715 |
// set being accumulated so increment n, and increment p if proxies are different. |
| 716 |
++n; |
| 717 |
if (n == 1 || textures[i - 1].fProxyView.proxy() != textures[i].fProxyView.proxy()) { |
| 718 |
// First proxy or a different proxy (that is compatible, otherwise we'd have drawn up |
| 719 |
// to i - 1). |
| 720 |
++p; |
| 721 |
} |
| 722 |
} |
| •723 |
draw(count, setMayHavePersp); |
| 724 |
} |
| 725 |
|
| 726 |
bool Device::drawBlurredRRect(const SkRRect& rrect, const SkPaint& paint, float deviceSigma) { |
| 727 |
SkMatrix localToDevice = this->localToDevice(); |
| 728 |
|
| 729 |
SurfaceDrawContext* sdc = fSurfaceDrawContext.get(); |
| 730 |
const GrClip* clip = this->clip(); |
| 731 |
GrRecordingContext* context = this->recordingContext(); |
| 732 |
|
| 733 |
SkPaint skPaint = paint; |
| 734 |
skPaint.setMaskFilter(nullptr); |
| 735 |
GrPaint grPaint; |
| 736 |
SkPaintToGrPaint(sdc, skPaint, localToDevice, &grPaint); |
| 737 |
|
| 738 |
if (skgpu::BlurIsEffectivelyIdentity(deviceSigma)) { |
| 739 |
sdc->drawShape(clip, std::move(grPaint), GrAA::kYes, localToDevice, GrStyledShape(rrect)); |
| 740 |
return true; |
| 741 |
} |
| 742 |
|
| 743 |
std::unique_ptr<GrFragmentProcessor> fp; |
| 744 |
|
| 745 |
auto devRRect = rrect.transform(localToDevice); |
| 746 |
|
| 747 |
bool devRRectIsRect = devRRect.has_value() && (*devRRect).isRect(); |
| 748 |
bool devRRectIsCircle = devRRect.has_value() && SkRRectPriv::IsCircle(*devRRect); |
| 749 |
|
| 750 |
bool canBeRect = (rrect.isRect() && localToDevice.preservesRightAngles()) || devRRectIsRect; |
| 751 |
bool canBeCircle = (SkRRectPriv::IsCircle(rrect) && localToDevice.isSimilarity()) || |
| 752 |
devRRectIsCircle; |
| 753 |
|
| 754 |
if (canBeRect || canBeCircle) { |
| 755 |
if (canBeRect) { |
| 756 |
fp = GrBlurUtils::MakeRectBlur( |
| 757 |
context, |
| 758 |
*context->priv().caps()->shaderCaps(), |
| 759 |
rrect.rect(), |
| 760 |
devRRectIsRect ? std::optional((*devRRect).rect()) : std::nullopt, |
| 761 |
localToDevice, |
| 762 |
deviceSigma); |
| 763 |
} else { |
| 764 |
SkRect devBounds; |
| 765 |
if (devRRectIsCircle) { |
| 766 |
devBounds = devRRect->getBounds(); |
| 767 |
} else { |
| 768 |
SkPoint center = localToDevice.mapPoint(rrect.getBounds().center()); |
| 769 |
SkScalar radius = localToDevice.mapVector(0, rrect.width()/2.f).length(); |
| 770 |
devBounds = {center.x() - radius, |
| 771 |
center.y() - radius, |
| 772 |
center.x() + radius, |
| 773 |
center.y() + radius}; |
| 774 |
} |
| 775 |
fp = GrBlurUtils::MakeCircleBlur(context, devBounds, deviceSigma); |
| 776 |
} |
| 777 |
|
| 778 |
if (!fp) { |
| 779 |
return false; |
| 780 |
} |
| 781 |
|
| 782 |
SkRect srcProxyRect = rrect.rect(); |
| 783 |
// Determine how much to outset the src rect to ensure we hit pixels within three sigma. |
| 784 |
SkScalar outsetX = 3.0f*deviceSigma; |
| 785 |
SkScalar outsetY = 3.0f*deviceSigma; |
| 786 |
if (localToDevice.isScaleTranslate()) { |
| 787 |
outsetX /= SkScalarAbs(localToDevice.getScaleX()); |
| 788 |
outsetY /= SkScalarAbs(localToDevice.getScaleY()); |
| 789 |
} else { |
| 790 |
SkSize scale; |
| 791 |
if (!localToDevice.decomposeScale(&scale, nullptr)) { |
| 792 |
return false; |
| 793 |
} |
| 794 |
outsetX /= scale.width(); |
| 795 |
outsetY /= scale.height(); |
| 796 |
} |
| 797 |
srcProxyRect.outset(outsetX, outsetY); |
| 798 |
grPaint.setCoverageFragmentProcessor(std::move(fp)); |
| 799 |
sdc->drawRect(clip, std::move(grPaint), GrAA::kNo, localToDevice, srcProxyRect); |
| 800 |
return true; |
| 801 |
} |
| 802 |
if (!localToDevice.rectStaysRect()) { |
| 803 |
return false; |
| 804 |
} |
| 805 |
if (!devRRect.has_value() || !SkRRectPriv::AllCornersCircular(*devRRect)) { |
| 806 |
return false; |
| 807 |
} |
| 808 |
|
| 809 |
auto deviceToLocal = localToDevice.invert(); |
| 810 |
if (!deviceToLocal) { |
| 811 |
return false; |
| 812 |
} |
| 813 |
float localSigma = deviceToLocal->mapRadius(deviceSigma); |
| 814 |
|
| 815 |
fp = GrBlurUtils::MakeRRectBlur(context, localSigma, deviceSigma, rrect, *devRRect); |
| 816 |
if (!fp) { |
| 817 |
return false; |
| 818 |
} |
| 819 |
SkRect srcProxyRect = rrect.rect(); |
| 820 |
srcProxyRect.outset(3.0f*localSigma, 3.0f*localSigma); |
| 821 |
grPaint.setCoverageFragmentProcessor(std::move(fp)); |
| 822 |
sdc->drawRect(clip, std::move(grPaint), GrAA::kNo, localToDevice, srcProxyRect); |
| 823 |
return true; |
| 824 |
} |
| 825 |
|
| 826 |
} // namespace skgpu::ganesh |