| 1 |
/* |
| 2 |
* Copyright 2010 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/SkGr.h" |
| 9 |
|
| 10 |
#include "include/core/SkAlphaType.h" |
| 11 |
#include "include/core/SkBitmap.h" |
| 12 |
#include "include/core/SkColorFilter.h" |
| 13 |
#include "include/core/SkData.h" |
| 14 |
#include "include/core/SkImageInfo.h" |
| 15 |
#include "include/core/SkMatrix.h" |
| 16 |
#include "include/core/SkPaint.h" |
| 17 |
#include "include/core/SkPixelRef.h" |
| 18 |
#include "include/core/SkPoint.h" |
| 19 |
#include "include/core/SkRect.h" |
| 20 |
#include "include/core/SkSize.h" |
| 21 |
#include "include/core/SkSurfaceProps.h" |
| 22 |
#include "include/effects/SkRuntimeEffect.h" |
| 23 |
#include "include/gpu/ganesh/GrBackendSurface.h" |
| 24 |
#include "include/gpu/ganesh/GrRecordingContext.h" |
| 25 |
#include "include/gpu/ganesh/GrTypes.h" |
| 26 |
#include "include/private/SkIDChangeListener.h" |
| 27 |
#include "include/private/base/SkTPin.h" |
| 28 |
#include "include/private/gpu/ganesh/GrTypesPriv.h" |
| 29 |
#include "src/core/SkBlenderBase.h" |
| 30 |
#include "src/core/SkMessageBus.h" |
| 31 |
#include "src/core/SkPaintPriv.h" |
| 32 |
#include "src/core/SkRuntimeEffectPriv.h" |
| 33 |
#include "src/gpu/DitherUtils.h" |
| 34 |
#include "src/gpu/ResourceKey.h" |
| 35 |
#include "src/gpu/Swizzle.h" |
| 36 |
#include "src/gpu/ganesh/GrCaps.h" |
| 37 |
#include "src/gpu/ganesh/GrColorInfo.h" |
| 38 |
#include "src/gpu/ganesh/GrColorSpaceXform.h" |
| 39 |
#include "src/gpu/ganesh/GrFPArgs.h" |
| 40 |
#include "src/gpu/ganesh/GrFragmentProcessor.h" |
| 41 |
#include "src/gpu/ganesh/GrFragmentProcessors.h" |
| 42 |
#include "src/gpu/ganesh/GrPaint.h" |
| 43 |
#include "src/gpu/ganesh/GrProxyProvider.h" |
| 44 |
#include "src/gpu/ganesh/GrRecordingContextPriv.h" |
| 45 |
#include "src/gpu/ganesh/GrSurfaceProxy.h" |
| 46 |
#include "src/gpu/ganesh/GrSurfaceProxyView.h" |
| 47 |
#include "src/gpu/ganesh/GrTextureProxy.h" |
| 48 |
#include "src/gpu/ganesh/GrXferProcessor.h" |
| 49 |
#include "src/gpu/ganesh/SurfaceDrawContext.h" |
| 50 |
#include "src/gpu/ganesh/effects/GrSkSLFP.h" |
| 51 |
#include "src/gpu/ganesh/effects/GrTextureEffect.h" |
| 52 |
#include "src/gpu/ganesh/image/GrMippedBitmap.h" |
| 53 |
#include "src/shaders/SkShaderBase.h" |
| 54 |
|
| 55 |
#include <optional> |
| 56 |
#include <utility> |
| 57 |
|
| 58 |
class SkBlender; |
| 59 |
class SkColorSpace; |
| 60 |
enum SkColorType : int; |
| 61 |
|
| 62 |
void GrMakeKeyFromImageID(skgpu::UniqueKey* key, uint32_t imageID, const SkIRect& imageBounds) { |
| 63 |
SkASSERT(key); |
| 64 |
SkASSERT(imageID); |
| 65 |
SkASSERT(!imageBounds.isEmpty()); |
| 66 |
static const skgpu::UniqueKey::Domain kImageIDDomain = skgpu::UniqueKey::GenerateDomain(); |
| 67 |
skgpu::UniqueKey::Builder builder(key, kImageIDDomain, 5, "Image"); |
| 68 |
builder[0] = imageID; |
| 69 |
builder[1] = imageBounds.fLeft; |
| 70 |
builder[2] = imageBounds.fTop; |
| 71 |
builder[3] = imageBounds.fRight; |
| 72 |
builder[4] = imageBounds.fBottom; |
| 73 |
} |
| 74 |
|
| 75 |
//////////////////////////////////////////////////////////////////////////////// |
| 76 |
|
| 77 |
sk_sp<SkIDChangeListener> GrMakeUniqueKeyInvalidationListener(skgpu::UniqueKey* key, |
| 78 |
uint32_t contextID) { |
| 79 |
class Listener : public SkIDChangeListener { |
| 80 |
public: |
| 81 |
Listener(const skgpu::UniqueKey& key, uint32_t contextUniqueID) |
| 82 |
: fMsg(key, contextUniqueID) {} |
| 83 |
|
| 84 |
void changed() override { |
| 85 |
SkMessageBus<skgpu::UniqueKeyInvalidatedMessage, uint32_t>::Post(fMsg); |
| 86 |
} |
| 87 |
|
| 88 |
private: |
| 89 |
skgpu::UniqueKeyInvalidatedMessage fMsg; |
| 90 |
}; |
| 91 |
|
| 92 |
auto listener = sk_make_sp<Listener>(*key, contextID); |
| 93 |
|
| 94 |
// We stick a SkData on the key that calls invalidateListener in its destructor. |
| 95 |
auto invalidateListener = [](const void* ptr, void* /*context*/) { |
| 96 |
auto listener = reinterpret_cast<const sk_sp<Listener>*>(ptr); |
| 97 |
(*listener)->markShouldDeregister(); |
| 98 |
delete listener; |
| 99 |
}; |
| 100 |
auto data = SkData::MakeWithProc(new sk_sp<Listener>(listener), |
| 101 |
sizeof(sk_sp<Listener>), |
| 102 |
invalidateListener, |
| 103 |
nullptr); |
| 104 |
SkASSERT(!key->getCustomData()); |
| 105 |
key->setCustomData(std::move(data)); |
| 106 |
return listener; |
| 107 |
} |
| 108 |
|
| 109 |
sk_sp<GrSurfaceProxy> GrCopyBaseMipMapToTextureProxy(GrRecordingContext* ctx, |
| 110 |
sk_sp<GrSurfaceProxy> baseProxy, |
| 111 |
GrSurfaceOrigin origin, |
| 112 |
std::string_view label, |
| 113 |
skgpu::Budgeted budgeted) { |
| 114 |
SkASSERT(baseProxy); |
| 115 |
|
| 116 |
// We don't allow this for promise proxies i.e. if they need mips they need to give them |
| 117 |
// to us upfront. |
| 118 |
if (baseProxy->isPromiseProxy()) { |
| 119 |
return nullptr; |
| 120 |
} |
| 121 |
if (!ctx->priv().caps()->isFormatCopyable(baseProxy->backendFormat())) { |
| 122 |
return nullptr; |
| 123 |
} |
| 124 |
auto copy = GrSurfaceProxy::Copy(ctx, |
| 125 |
std::move(baseProxy), |
| 126 |
origin, |
| 127 |
skgpu::Mipmapped::kYes, |
| 128 |
SkBackingFit::kExact, |
| 129 |
budgeted, |
| 130 |
label); |
| 131 |
if (!copy) { |
| 132 |
return nullptr; |
| 133 |
} |
| 134 |
SkASSERT(copy->asTextureProxy()); |
| 135 |
return copy; |
| 136 |
} |
| 137 |
|
| 138 |
GrSurfaceProxyView GrCopyBaseMipMapToView(GrRecordingContext* context, |
| 139 |
GrSurfaceProxyView src, |
| 140 |
skgpu::Budgeted budgeted) { |
| 141 |
auto origin = src.origin(); |
| 142 |
auto swizzle = src.swizzle(); |
| 143 |
auto proxy = src.refProxy(); |
| 144 |
return {GrCopyBaseMipMapToTextureProxy( |
| 145 |
context, proxy, origin, /*label=*/"CopyBaseMipMapToView", budgeted), |
| 146 |
origin, |
| 147 |
swizzle}; |
| 148 |
} |
| 149 |
|
| 150 |
static skgpu::Mipmapped adjust_mipmapped(skgpu::Mipmapped mipmapped, |
| 151 |
const SkBitmap& bitmap, |
| 152 |
const GrCaps* caps) { |
| 153 |
if (!caps->mipmapSupport() || bitmap.dimensions().area() <= 1) { |
| 154 |
return skgpu::Mipmapped::kNo; |
| 155 |
} |
| 156 |
return mipmapped; |
| 157 |
} |
| 158 |
|
| 159 |
static GrColorType choose_bmp_texture_colortype(const GrCaps* caps, const SkBitmap& bitmap) { |
| 160 |
GrColorType ct = SkColorTypeToGrColorType(bitmap.info().colorType()); |
| 161 |
if (caps->getDefaultBackendFormat(ct, GrRenderable::kNo).isValid()) { |
| 162 |
return ct; |
| 163 |
} |
| 164 |
return GrColorType::kRGBA_8888; |
| 165 |
} |
| 166 |
|
| 167 |
static sk_sp<GrTextureProxy> make_bmp_proxy(GrProxyProvider* proxyProvider, |
| 168 |
const GrMippedBitmap& orig, |
| 169 |
GrColorType ct, |
| 170 |
skgpu::Mipmapped mipmapped, |
| 171 |
SkBackingFit fit, |
| 172 |
skgpu::Budgeted budgeted) { |
| 173 |
std::optional<GrMippedBitmap> bmpToUpload; |
| 174 |
if (ct != SkColorTypeToGrColorType(orig.colorType())) { |
| 175 |
SkColorType skCT = GrColorTypeToSkColorType(ct); |
| 176 |
SkBitmap bitmap = orig.bitmap(); |
| 177 |
SkBitmap tmp; |
| 178 |
if (!tmp.tryAllocPixels(bitmap.info().makeColorType(skCT)) || |
| 179 |
!bitmap.readPixels(tmp.pixmap())) { |
| 180 |
return {}; |
| 181 |
} |
| 182 |
tmp.setImmutable(); |
| 183 |
bmpToUpload = GrMippedBitmap(tmp, /*mipmaps=*/nullptr); |
| 184 |
} else { |
| 185 |
bmpToUpload = orig; |
| 186 |
} |
| 187 |
auto proxy = |
| 188 |
proxyProvider->createProxyFromBitmap(bmpToUpload.value(), mipmapped, fit, budgeted); |
| 189 |
SkASSERT(!proxy || mipmapped == skgpu::Mipmapped::kNo || |
| 190 |
proxy->mipmapped() == skgpu::Mipmapped::kYes); |
| 191 |
return proxy; |
| 192 |
} |
| 193 |
|
| 194 |
std::tuple<GrSurfaceProxyView, GrColorType> GrMakeCachedBitmapProxyView( |
| 195 |
GrRecordingContext* rContext, |
| 196 |
const GrMippedBitmap& bm, |
| 197 |
std::string_view label, |
| 198 |
skgpu::Mipmapped mipmapped) { |
| 199 |
SkBitmap bitmap = bm.bitmap(); |
| 200 |
if (!bitmap.peekPixels(nullptr)) { |
| 201 |
return {}; |
| 202 |
} |
| 203 |
|
| 204 |
GrProxyProvider* proxyProvider = rContext->priv().proxyProvider(); |
| 205 |
const GrCaps* caps = rContext->priv().caps(); |
| 206 |
|
| 207 |
skgpu::UniqueKey key; |
| 208 |
SkIPoint origin = bitmap.pixelRefOrigin(); |
| 209 |
SkIRect subset = SkIRect::MakePtSize(origin, bitmap.dimensions()); |
| 210 |
GrMakeKeyFromImageID(&key, bitmap.pixelRef()->getGenerationID(), subset); |
| 211 |
|
| 212 |
mipmapped = adjust_mipmapped(mipmapped, bitmap, caps); |
| 213 |
GrColorType ct = choose_bmp_texture_colortype(caps, bitmap); |
| 214 |
|
| 215 |
auto installKey = [&](GrTextureProxy* proxy) { |
| 216 |
auto listener = GrMakeUniqueKeyInvalidationListener(&key, proxyProvider->contextID()); |
| 217 |
bitmap.pixelRef()->addGenIDChangeListener(std::move(listener)); |
| 218 |
proxyProvider->assignUniqueKeyToProxy(key, proxy); |
| 219 |
}; |
| 220 |
|
| 221 |
sk_sp<GrTextureProxy> proxy = proxyProvider->findOrCreateProxyByUniqueKey(key); |
| 222 |
if (!proxy) { |
| 223 |
proxy = make_bmp_proxy( |
| 224 |
proxyProvider, bm, ct, mipmapped, SkBackingFit::kExact, skgpu::Budgeted::kYes); |
| 225 |
if (!proxy) { |
| 226 |
return {}; |
| 227 |
} |
| 228 |
SkASSERT(mipmapped == skgpu::Mipmapped::kNo || |
| 229 |
proxy->mipmapped() == skgpu::Mipmapped::kYes); |
| 230 |
installKey(proxy.get()); |
| 231 |
} |
| 232 |
|
| 233 |
skgpu::Swizzle swizzle = caps->getReadSwizzle(proxy->backendFormat(), ct); |
| 234 |
if (mipmapped == skgpu::Mipmapped::kNo || proxy->mipmapped() == skgpu::Mipmapped::kYes) { |
| 235 |
return {{std::move(proxy), kTopLeft_GrSurfaceOrigin, swizzle}, ct}; |
| 236 |
} |
| 237 |
|
| 238 |
// We need a mipped proxy, but we found a proxy earlier that wasn't mipped. Thus we generate |
| 239 |
// a new mipped surface and copy the original proxy into the base layer. We will then let |
| 240 |
// the gpu generate the rest of the mips. |
| 241 |
auto mippedProxy = GrCopyBaseMipMapToTextureProxy( |
| 242 |
rContext, proxy, kTopLeft_GrSurfaceOrigin, /*label=*/"MakeCachedBitmapProxyView"); |
| 243 |
if (!mippedProxy) { |
| 244 |
// We failed to make a mipped proxy with the base copied into it. This could have |
| 245 |
// been from failure to make the proxy or failure to do the copy. Thus we will fall |
| 246 |
// back to just using the non mipped proxy; See skbug.com/40038328. |
| 247 |
return {{std::move(proxy), kTopLeft_GrSurfaceOrigin, swizzle}, ct}; |
| 248 |
} |
| 249 |
// In this case we are stealing the key from the original proxy which should only happen |
| 250 |
// when we have just generated mipmaps for an originally unmipped proxy/texture. This |
| 251 |
// means that all future uses of the key will access the mipmapped version. The texture |
| 252 |
// backing the unmipped version will remain in the resource cache until the last texture |
| 253 |
// proxy referencing it is deleted at which time it too will be deleted or recycled. |
| 254 |
SkASSERT(proxy->getUniqueKey() == key); |
| 255 |
proxyProvider->removeUniqueKeyFromProxy(proxy.get()); |
| 256 |
installKey(mippedProxy->asTextureProxy()); |
| 257 |
return {{std::move(mippedProxy), kTopLeft_GrSurfaceOrigin, swizzle}, ct}; |
| 258 |
} |
| 259 |
|
| 260 |
std::tuple<GrSurfaceProxyView, GrColorType> GrMakeUncachedBitmapProxyView( |
| 261 |
GrRecordingContext* rContext, |
| 262 |
const GrMippedBitmap& bitmap, |
| 263 |
skgpu::Mipmapped mipmapped, |
| 264 |
SkBackingFit fit, |
| 265 |
skgpu::Budgeted budgeted) { |
| 266 |
GrProxyProvider* proxyProvider = rContext->priv().proxyProvider(); |
| 267 |
const GrCaps* caps = rContext->priv().caps(); |
| 268 |
|
| 269 |
mipmapped = adjust_mipmapped(mipmapped, bitmap.bitmap(), caps); |
| 270 |
GrColorType ct = choose_bmp_texture_colortype(caps, bitmap.bitmap()); |
| 271 |
|
| 272 |
if (auto proxy = make_bmp_proxy(proxyProvider, bitmap, ct, mipmapped, fit, budgeted)) { |
| 273 |
skgpu::Swizzle swizzle = caps->getReadSwizzle(proxy->backendFormat(), ct); |
| 274 |
SkASSERT(mipmapped == skgpu::Mipmapped::kNo || |
| 275 |
proxy->mipmapped() == skgpu::Mipmapped::kYes); |
| 276 |
return {{std::move(proxy), kTopLeft_GrSurfaceOrigin, swizzle}, ct}; |
| 277 |
} |
| 278 |
return {}; |
| 279 |
} |
| 280 |
/////////////////////////////////////////////////////////////////////////////// |
| 281 |
|
| 282 |
SkPMColor4f SkColorToPMColor4f(SkColor c, const GrColorInfo& colorInfo) { |
| 283 |
SkColor4f color = SkColor4f::FromColor(c); |
| 284 |
if (auto* xform = colorInfo.colorSpaceXformFromSRGB()) { |
| 285 |
color = xform->apply(color); |
| 286 |
} |
| 287 |
return color.premul(); |
| 288 |
} |
| 289 |
|
| 290 |
SkColor4f SkColor4fPrepForDst(SkColor4f color, const GrColorInfo& colorInfo) { |
| 291 |
if (auto* xform = colorInfo.colorSpaceXformFromSRGB()) { |
| 292 |
color = xform->apply(color); |
| 293 |
} |
| 294 |
return color; |
| 295 |
} |
| 296 |
|
| 297 |
/////////////////////////////////////////////////////////////////////////////// |
| 298 |
|
| 299 |
static inline bool blender_requires_shader(const SkBlender* blender) { |
| 300 |
SkASSERT(blender); |
| 301 |
std::optional<SkBlendMode> mode = as_BB(blender)->asBlendMode(); |
| 302 |
return !mode.has_value() || *mode != SkBlendMode::kDst; |
| 303 |
} |
| 304 |
|
| 305 |
|
| 306 |
#ifndef SK_IGNORE_GPU_DITHER |
| 307 |
static std::unique_ptr<GrFragmentProcessor> make_dither_effect( |
| 308 |
GrRecordingContext* rContext, |
| 309 |
std::unique_ptr<GrFragmentProcessor> inputFP, |
| 310 |
float range, |
| 311 |
const GrCaps* caps) { |
| 312 |
if (range == 0 || inputFP == nullptr) { |
| 313 |
return inputFP; |
| 314 |
} |
| 315 |
|
| 316 |
if (caps->avoidDithering()) { |
| 317 |
return inputFP; |
| 318 |
} |
| 319 |
|
| 320 |
// We used to use integer math on sk_FragCoord, when supported, and a fallback using floating |
| 321 |
// point (on a 4x4 rather than 8x8 grid). Now we precompute a 8x8 table in a texture because |
| 322 |
// it was shown to be significantly faster on several devices. Test was done with the following |
| 323 |
// running in viewer with the stats layer enabled and looking at total frame time: |
| 324 |
// SkRandom r; |
| 325 |
// for (int i = 0; i < N; ++i) { |
| 326 |
// SkColor c[2] = {r.nextU(), r.nextU()}; |
| 327 |
// SkPoint pts[2] = {{r.nextRangeScalar(0, 500), r.nextRangeScalar(0, 500)}, |
| 328 |
// {r.nextRangeScalar(0, 500), r.nextRangeScalar(0, 500)}}; |
| 329 |
// SkPaint p; |
| 330 |
// p.setDither(true); |
| 331 |
// p.setShader(SkGradientShader::MakeLinear(pts, c, nullptr, 2, SkTileMode::kRepeat)); |
| 332 |
// canvas->drawPaint(p); |
| 333 |
// } |
| 334 |
// Device GPU N no dither int math dither table dither |
| 335 |
// Linux desktop QuadroP1000 5000 304ms 400ms (1.31x) 383ms (1.26x) |
| 336 |
// TecnoSpark3Pro PowerVRGE8320 200 299ms 820ms (2.74x) 592ms (1.98x) |
| 337 |
// Pixel 4 Adreno640 500 110ms 221ms (2.01x) 214ms (1.95x) |
| 338 |
// Galaxy S20 FE Mali-G77 MP11 600 165ms 360ms (2.18x) 260ms (1.58x) |
| 339 |
static const auto gLUT = GrMippedBitmap(skgpu::MakeDitherLUT()); |
| 340 |
auto [tex, ct] = GrMakeCachedBitmapProxyView( |
| 341 |
rContext, gLUT, /*label=*/"MakeDitherEffect", skgpu::Mipmapped::kNo); |
| 342 |
if (!tex) { |
| 343 |
return inputFP; |
| 344 |
} |
| 345 |
SkASSERT(ct == GrColorType::kAlpha_8); |
| 346 |
GrSamplerState sampler(GrSamplerState::WrapMode::kRepeat, SkFilterMode::kNearest); |
| 347 |
auto te = GrTextureEffect::Make( |
| 348 |
std::move(tex), kPremul_SkAlphaType, SkMatrix::I(), sampler, *caps); |
| 349 |
static const SkRuntimeEffect* effect = SkMakeRuntimeEffect(SkRuntimeEffect::MakeForShader, |
| 350 |
"uniform half range;" |
| 351 |
"uniform shader inputFP;" |
| 352 |
"uniform shader table;" |
| 353 |
"half4 main(float2 xy) {" |
| 354 |
"half4 color = inputFP.eval(xy);" |
| 355 |
"half value = table.eval(sk_FragCoord.xy).a - 0.5;" // undo the bias in the table |
| 356 |
// For each color channel, add the random offset to the channel value and then clamp |
| 357 |
// between 0 and alpha to keep the color premultiplied. |
| 358 |
"return half4(clamp(color.rgb + value * range, 0.0, color.a), color.a);" |
| 359 |
"}" |
| 360 |
); |
| 361 |
return GrSkSLFP::Make(effect, "Dither", /*inputFP=*/nullptr, |
| 362 |
GrSkSLFP::OptFlags::kPreservesOpaqueInput, |
| 363 |
"range", range, |
| 364 |
"inputFP", std::move(inputFP), |
| 365 |
"table", GrSkSLFP::IgnoreOptFlags(std::move(te))); |
| 366 |
} |
| 367 |
#endif |
| 368 |
|
| 369 |
static inline bool skpaint_to_grpaint_impl( |
| 370 |
skgpu::ganesh::SurfaceDrawContext* sdc, |
| 371 |
const SkPaint& skPaint, |
| 372 |
const SkMatrix& ctm, |
| 373 |
std::optional<std::unique_ptr<GrFragmentProcessor>> shaderFP, |
| 374 |
SkBlender* primColorBlender, |
| 375 |
GrPaint* grPaint) { |
| 376 |
const GrColorInfo& dstColorInfo = sdc->colorInfo(); |
| 377 |
const SkSurfaceProps& surfaceProps = sdc->surfaceProps(); |
| 378 |
// Convert SkPaint color to 4f format in the destination color space |
| 379 |
SkColor4f origColor = SkColor4fPrepForDst(skPaint.getColor4f(), dstColorInfo); |
| 380 |
|
| 381 |
GrFPArgs fpArgs(sdc, &dstColorInfo, surfaceProps, GrFPArgs::Scope::kDefault); |
| 382 |
|
| 383 |
// Setup the initial color considering the shader, the SkPaint color, and the presence or not |
| 384 |
// of per-vertex colors. |
| 385 |
std::unique_ptr<GrFragmentProcessor> paintFP; |
| 386 |
const bool gpProvidesShader = shaderFP.has_value() && !*shaderFP; |
| 387 |
if (!primColorBlender || blender_requires_shader(primColorBlender)) { |
| 388 |
if (shaderFP.has_value()) { |
| 389 |
paintFP = std::move(*shaderFP); |
| 390 |
} else { |
| 391 |
if (const SkShaderBase* shader = as_SB(skPaint.getShader())) { |
| 392 |
paintFP = GrFragmentProcessors::Make(shader, fpArgs, ctm); |
| 393 |
if (paintFP == nullptr) { |
| 394 |
return false; |
| 395 |
} |
| 396 |
} |
| 397 |
} |
| 398 |
} |
| 399 |
|
| 400 |
// Set this in below cases if the output of the shader/paint-color/paint-alpha/primXfermode is |
| 401 |
// a known constant value. In that case we can simply apply a color filter during this |
| 402 |
// conversion without converting the color filter to a GrFragmentProcessor. |
| 403 |
bool applyColorFilterToPaintColor = false; |
| 404 |
if (paintFP) { |
| 405 |
if (primColorBlender) { |
| 406 |
// There is a blend between the primitive color and the shader color. The shader sees |
| 407 |
// the opaque paint color. The shader's output is blended using the provided mode by |
| 408 |
// the primitive color. The blended color is then modulated by the paint's alpha. |
| 409 |
|
| 410 |
// The geometry processor will insert the primitive color to start the color chain, so |
| 411 |
// the GrPaint color will be ignored. |
| 412 |
|
| 413 |
SkPMColor4f shaderInput = origColor.makeOpaque().premul(); |
| 414 |
paintFP = GrFragmentProcessor::OverrideInput(std::move(paintFP), shaderInput); |
| 415 |
paintFP = GrFragmentProcessors::Make(as_BB(primColorBlender), |
| 416 |
/*srcFP=*/std::move(paintFP), |
| 417 |
/*dstFP=*/nullptr, |
| 418 |
fpArgs); |
| 419 |
if (!paintFP) { |
| 420 |
return false; |
| 421 |
} |
| 422 |
|
| 423 |
// We can ignore origColor here - alpha is unchanged by gamma |
| 424 |
float paintAlpha = skPaint.getColor4f().fA; |
| 425 |
if (1.0f != paintAlpha) { |
| 426 |
// No gamut conversion - paintAlpha is a (linear) alpha value, splatted to all |
| 427 |
// color channels. It's value should be treated as the same in ANY color space. |
| 428 |
paintFP = GrFragmentProcessor::ModulateRGBA( |
| 429 |
std::move(paintFP), {paintAlpha, paintAlpha, paintAlpha, paintAlpha}); |
| 430 |
} |
| 431 |
} else { |
| 432 |
float paintAlpha = skPaint.getColor4f().fA; |
| 433 |
if (paintAlpha != 1.0f) { |
| 434 |
// This invokes the shader's FP tree with an opaque version of the paint color, |
| 435 |
// then multiplies the final result by the incoming (paint) alpha. |
| 436 |
// We're actually putting the *unpremul* paint color on the GrPaint. This is okay, |
| 437 |
// because the shader is supposed to see the original (opaque) RGB from the paint. |
| 438 |
// ApplyPaintAlpha then creates a valid premul color by applying the paint alpha. |
| 439 |
// Think of this as equivalent to (but faster than) putting origColor.premul() on |
| 440 |
// the GrPaint, and ApplyPaintAlpha unpremuling it before passing it to the child. |
| 441 |
paintFP = GrFragmentProcessor::ApplyPaintAlpha(std::move(paintFP)); |
| 442 |
grPaint->setColor4f({origColor.fR, origColor.fG, origColor.fB, origColor.fA}); |
| 443 |
} else { |
| 444 |
// paintFP will ignore its input color, so we must disable coverage-as-alpha. |
| 445 |
// TODO(skbug.com/40043035): The alternative would be to always use ApplyPaintAlpha, but |
| 446 |
// we'd need to measure the cost of that shader math against the CAA benefit. |
| 447 |
paintFP = GrFragmentProcessor::DisableCoverageAsAlpha(std::move(paintFP)); |
| 448 |
grPaint->setColor4f(origColor.premul()); |
| 449 |
} |
| 450 |
} |
| 451 |
} else { |
| 452 |
if (primColorBlender) { |
| 453 |
// The primitive itself has color (e.g. interpolated vertex color) and this is what |
| 454 |
// the GP will output. Thus, we must get the paint color in separately below as a color |
| 455 |
// FP. This could be made more efficient if the relevant GPs used GrPaint color and |
| 456 |
// took the SkBlender to apply with primitive color. As it stands changing the SkPaint |
| 457 |
// color will break batches. |
| 458 |
grPaint->setColor4f(SK_PMColor4fWHITE); // won't be used. |
| 459 |
if (blender_requires_shader(primColorBlender)) { |
| 460 |
paintFP = GrFragmentProcessor::MakeColor(origColor.makeOpaque().premul()); |
| 461 |
paintFP = GrFragmentProcessors::Make(as_BB(primColorBlender), |
| 462 |
/*srcFP=*/std::move(paintFP), |
| 463 |
/*dstFP=*/nullptr, |
| 464 |
fpArgs); |
| 465 |
if (!paintFP) { |
| 466 |
return false; |
| 467 |
} |
| 468 |
} |
| 469 |
|
| 470 |
// The paint's *alpha* is applied after the paint/primitive color blend: |
| 471 |
// We can ignore origColor here - alpha is unchanged by gamma |
| 472 |
float paintAlpha = skPaint.getColor4f().fA; |
| 473 |
if (paintAlpha != 1.0f) { |
| 474 |
// No gamut conversion - paintAlpha is a (linear) alpha value, splatted to all |
| 475 |
// color channels. It's value should be treated as the same in ANY color space. |
| 476 |
paintFP = GrFragmentProcessor::ModulateRGBA( |
| 477 |
std::move(paintFP), {paintAlpha, paintAlpha, paintAlpha, paintAlpha}); |
| 478 |
} |
| 479 |
} else { |
| 480 |
// No shader, no primitive color. |
| 481 |
grPaint->setColor4f(origColor.premul()); |
| 482 |
// We can do this if there isn't a GP that is acting as the shader. |
| 483 |
applyColorFilterToPaintColor = !gpProvidesShader; |
| 484 |
} |
| 485 |
} |
| 486 |
|
| 487 |
SkColorFilter* colorFilter = skPaint.getColorFilter(); |
| 488 |
if (colorFilter) { |
| 489 |
if (applyColorFilterToPaintColor) { |
| 490 |
SkColorSpace* dstCS = dstColorInfo.colorSpace(); |
| 491 |
grPaint->setColor4f(colorFilter->filterColor4f(origColor, dstCS, dstCS).premul()); |
| 492 |
} else { |
| 493 |
auto [success, fp] = GrFragmentProcessors::Make( |
| 494 |
sdc, colorFilter, std::move(paintFP), dstColorInfo, surfaceProps); |
| 495 |
if (!success) { |
| 496 |
return false; |
| 497 |
} |
| 498 |
paintFP = std::move(fp); |
| 499 |
} |
| 500 |
} |
| 501 |
|
| 502 |
if (auto maskFilter = skPaint.getMaskFilter()) { |
| 503 |
if (auto mfFP = GrFragmentProcessors::Make(maskFilter, fpArgs, ctm)) { |
| 504 |
grPaint->setCoverageFragmentProcessor(std::move(mfFP)); |
| 505 |
} |
| 506 |
} |
| 507 |
|
| 508 |
#ifndef SK_IGNORE_GPU_DITHER |
| 509 |
SkColorType ct = GrColorTypeToSkColorType(dstColorInfo.colorType()); |
| 510 |
if (paintFP != nullptr && ( |
| 511 |
surfaceProps.isAlwaysDither() || SkPaintPriv::ShouldDither(skPaint, ct))) { |
| 512 |
float ditherRange = skgpu::DitherRangeForConfig(ct); |
| 513 |
GrRecordingContext* context = sdc->recordingContext(); |
| 514 |
paintFP = make_dither_effect( |
| 515 |
context, std::move(paintFP), ditherRange, context->priv().caps()); |
| 516 |
} |
| 517 |
#endif |
| 518 |
|
| 519 |
// Note that for the final blend onto the canvas, we should prefer to use the GrXferProcessor |
| 520 |
// instead of a SkBlendModeBlender to perform the blend. The Xfer processor is able to perform |
| 521 |
// coefficient-based blends directly, without readback. This will be much more efficient. |
| 522 |
if (auto bm = skPaint.asBlendMode()) { |
| 523 |
// When the xfermode is null on the SkPaint (meaning kSrcOver) we need the XPFactory field |
| 524 |
// on the GrPaint to also be null (also kSrcOver). |
| 525 |
SkASSERT(!grPaint->getXPFactory()); |
| 526 |
if (bm.value() != SkBlendMode::kSrcOver) { |
| 527 |
grPaint->setXPFactory(GrXPFactory::FromBlendMode(bm.value())); |
| 528 |
} |
| 529 |
} else { |
| 530 |
// Apply a custom blend against the surface color, and force the XP to kSrc so that the |
| 531 |
// computed result is applied directly to the canvas while still honoring the alpha. |
| 532 |
paintFP = GrFragmentProcessors::Make(as_BB(skPaint.getBlender()), |
| 533 |
std::move(paintFP), |
| 534 |
GrFragmentProcessor::SurfaceColor(), |
| 535 |
fpArgs); |
| 536 |
if (!paintFP) { |
| 537 |
return false; |
| 538 |
} |
| 539 |
grPaint->setXPFactory(GrXPFactory::FromBlendMode(SkBlendMode::kSrc)); |
| 540 |
} |
| 541 |
|
| 542 |
if (GrColorTypeClampType(dstColorInfo.colorType()) == GrClampType::kManual) { |
| 543 |
if (paintFP != nullptr) { |
| 544 |
paintFP = GrFragmentProcessor::ClampOutput(std::move(paintFP)); |
| 545 |
} else { |
| 546 |
auto color = grPaint->getColor4f(); |
| 547 |
grPaint->setColor4f({SkTPin(color.fR, 0.f, 1.f), |
| 548 |
SkTPin(color.fG, 0.f, 1.f), |
| 549 |
SkTPin(color.fB, 0.f, 1.f), |
| 550 |
SkTPin(color.fA, 0.f, 1.f)}); |
| 551 |
} |
| 552 |
} |
| 553 |
|
| 554 |
if (paintFP) { |
| 555 |
grPaint->setColorFragmentProcessor(std::move(paintFP)); |
| 556 |
} |
| 557 |
|
| 558 |
return true; |
| 559 |
} |
| 560 |
|
| 561 |
bool SkPaintToGrPaint(skgpu::ganesh::SurfaceDrawContext* sdc, |
| 562 |
const SkPaint& skPaint, |
| 563 |
const SkMatrix& ctm, |
| 564 |
GrPaint* grPaint) { |
| 565 |
return skpaint_to_grpaint_impl(sdc, |
| 566 |
skPaint, |
| 567 |
ctm, |
| 568 |
/*shaderFP=*/std::nullopt, |
| 569 |
/*primColorBlender=*/nullptr, |
| 570 |
grPaint); |
| 571 |
} |
| 572 |
|
| 573 |
/** Replaces the SkShader (if any) on skPaint with the passed in GrFragmentProcessor. */ |
| 574 |
bool SkPaintToGrPaintReplaceShader(skgpu::ganesh::SurfaceDrawContext* sdc, |
| 575 |
const SkPaint& skPaint, |
| 576 |
const SkMatrix& ctm, |
| 577 |
std::unique_ptr<GrFragmentProcessor> shaderFP, |
| 578 |
GrPaint* grPaint) { |
| 579 |
return skpaint_to_grpaint_impl(sdc, |
| 580 |
skPaint, |
| 581 |
ctm, |
| 582 |
std::move(shaderFP), |
| 583 |
/*primColorBlender=*/nullptr, |
| 584 |
grPaint); |
| 585 |
} |
| 586 |
|
| 587 |
/** Blends the SkPaint's shader (or color if no shader) with a per-primitive color which must |
| 588 |
be setup as a vertex attribute using the specified SkBlender. */ |
| 589 |
bool SkPaintToGrPaintWithBlend(skgpu::ganesh::SurfaceDrawContext* sdc, |
| 590 |
const SkPaint& skPaint, |
| 591 |
const SkMatrix& ctm, |
| 592 |
SkBlender* primColorBlender, |
| 593 |
GrPaint* grPaint) { |
| 594 |
return skpaint_to_grpaint_impl(sdc, |
| 595 |
skPaint, |
| 596 |
ctm, |
| 597 |
/*shaderFP=*/std::nullopt, |
| 598 |
primColorBlender, |
| 599 |
grPaint); |
| 600 |
} |