| 1 |
/* |
| 2 |
* Copyright 2014 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/effects/GrPorterDuffXferProcessor.h" |
| 9 |
|
| 10 |
#include "include/core/SkBlendMode.h" |
| 11 |
#include "include/core/SkColor.h" |
| 12 |
#include "include/private/base/SkAssert.h" |
| 13 |
#include "include/private/base/SkFloatingPoint.h" |
| 14 |
#include "include/private/gpu/ganesh/GrTypesPriv.h" |
| 15 |
#include "src/base/SkRandom.h" |
| 16 |
#include "src/core/SkColorData.h" |
| 17 |
#include "src/core/SkSLTypeShared.h" |
| 18 |
#include "src/gpu/Blend.h" |
| 19 |
#include "src/gpu/BlendFormula.h" |
| 20 |
#include "src/gpu/KeyBuilder.h" |
| 21 |
#include "src/gpu/ganesh/GrCaps.h" |
| 22 |
#include "src/gpu/ganesh/GrProcessorAnalysis.h" |
| 23 |
#include "src/gpu/ganesh/GrShaderCaps.h" |
| 24 |
#include "src/gpu/ganesh/GrXferProcessor.h" |
| 25 |
#include "src/gpu/ganesh/glsl/GrGLSLBlend.h" |
| 26 |
#include "src/gpu/ganesh/glsl/GrGLSLFragmentShaderBuilder.h" |
| 27 |
#include "src/gpu/ganesh/glsl/GrGLSLProgramDataManager.h" |
| 28 |
#include "src/gpu/ganesh/glsl/GrGLSLUniformHandler.h" |
| 29 |
|
| 30 |
#include <cstring> |
| 31 |
#include <memory> |
| 32 |
#include <string> |
| 33 |
|
| 34 |
using skgpu::BlendFormula; |
| 35 |
|
| 36 |
class PorterDuffXferProcessor : public GrXferProcessor { |
| 37 |
public: |
| 38 |
PorterDuffXferProcessor(BlendFormula blendFormula, GrProcessorAnalysisCoverage coverage) |
| 39 |
: INHERITED(kPorterDuffXferProcessor_ClassID, /*willReadDstColor=*/false, coverage) |
| 40 |
, fBlendFormula(blendFormula) { |
| 41 |
} |
| 42 |
|
| 43 |
const char* name() const override { return "Porter Duff"; } |
| 44 |
|
| 45 |
std::unique_ptr<ProgramImpl> makeProgramImpl() const override; |
| 46 |
|
| 47 |
BlendFormula getBlendFormula() const { return fBlendFormula; } |
| 48 |
|
| 49 |
private: |
| 50 |
void onAddToKey(const GrShaderCaps&, skgpu::KeyBuilder*) const override; |
| 51 |
|
| 52 |
bool onHasSecondaryOutput() const override { return fBlendFormula.hasSecondaryOutput(); } |
| 53 |
|
| 54 |
void onGetBlendInfo(skgpu::BlendInfo* blendInfo) const override { |
| 55 |
blendInfo->fEquation = fBlendFormula.equation(); |
| 56 |
blendInfo->fSrcBlend = fBlendFormula.srcCoeff(); |
| 57 |
blendInfo->fDstBlend = fBlendFormula.dstCoeff(); |
| 58 |
blendInfo->fWritesColor = fBlendFormula.modifiesDst(); |
| 59 |
} |
| 60 |
|
| 61 |
bool onIsEqual(const GrXferProcessor& xpBase) const override { |
| 62 |
const PorterDuffXferProcessor& xp = xpBase.cast<PorterDuffXferProcessor>(); |
| 63 |
return fBlendFormula == xp.fBlendFormula; |
| 64 |
} |
| 65 |
|
| 66 |
const BlendFormula fBlendFormula; |
| 67 |
|
| 68 |
using INHERITED = GrXferProcessor; |
| 69 |
}; |
| 70 |
|
| 71 |
/////////////////////////////////////////////////////////////////////////////// |
| 72 |
|
| 73 |
static void append_color_output(const PorterDuffXferProcessor& xp, |
| 74 |
GrGLSLXPFragmentBuilder* fragBuilder, |
| 75 |
BlendFormula::OutputType outputType, const char* output, |
| 76 |
const char* inColor, const char* inCoverage) { |
| 77 |
SkASSERT(inCoverage); |
| 78 |
SkASSERT(inColor); |
| 79 |
switch (outputType) { |
| 80 |
case BlendFormula::kNone_OutputType: |
| 81 |
fragBuilder->codeAppendf("%s = half4(0.0);", output); |
| 82 |
break; |
| 83 |
case BlendFormula::kCoverage_OutputType: |
| 84 |
fragBuilder->codeAppendf("%s = %s;", output, inCoverage); |
| 85 |
break; |
| 86 |
case BlendFormula::kModulate_OutputType: |
| 87 |
fragBuilder->codeAppendf("%s = %s * %s;", output, inColor, inCoverage); |
| 88 |
break; |
| 89 |
case BlendFormula::kSAModulate_OutputType: |
| 90 |
fragBuilder->codeAppendf("%s = %s.a * %s;", output, inColor, inCoverage); |
| 91 |
break; |
| 92 |
case BlendFormula::kISAModulate_OutputType: |
| 93 |
fragBuilder->codeAppendf("%s = (1.0 - %s.a) * %s;", output, inColor, inCoverage); |
| 94 |
break; |
| 95 |
case BlendFormula::kISCModulate_OutputType: |
| 96 |
fragBuilder->codeAppendf("%s = (half4(1.0) - %s) * %s;", output, inColor, inCoverage); |
| 97 |
break; |
| 98 |
default: |
| 99 |
SK_ABORT("Unsupported output type."); |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
void PorterDuffXferProcessor::onAddToKey(const GrShaderCaps&, skgpu::KeyBuilder* b) const { |
| 104 |
b->add32(fBlendFormula.primaryOutput() | (fBlendFormula.secondaryOutput() << 3)); |
| 105 |
static_assert(BlendFormula::kLast_OutputType < 8); |
| 106 |
} |
| 107 |
|
| 108 |
std::unique_ptr<GrXferProcessor::ProgramImpl> PorterDuffXferProcessor::makeProgramImpl() const { |
| 109 |
class Impl : public ProgramImpl { |
| 110 |
private: |
| 111 |
void emitOutputsForBlendState(const EmitArgs& args) override { |
| 112 |
const PorterDuffXferProcessor& xp = args.fXP.cast<PorterDuffXferProcessor>(); |
| 113 |
GrGLSLXPFragmentBuilder* fragBuilder = args.fXPFragBuilder; |
| 114 |
|
| 115 |
const BlendFormula& blendFormula = xp.fBlendFormula; |
| 116 |
if (blendFormula.hasSecondaryOutput()) { |
| 117 |
append_color_output(xp, |
| 118 |
fragBuilder, |
| 119 |
blendFormula.secondaryOutput(), |
| 120 |
args.fOutputSecondary, |
| 121 |
args.fInputColor, |
| 122 |
args.fInputCoverage); |
| 123 |
} |
| 124 |
append_color_output(xp, |
| 125 |
fragBuilder, |
| 126 |
blendFormula.primaryOutput(), |
| 127 |
args.fOutputPrimary, |
| 128 |
args.fInputColor, |
| 129 |
args.fInputCoverage); |
| 130 |
} |
| 131 |
}; |
| 132 |
|
| 133 |
return std::make_unique<Impl>(); |
| 134 |
} |
| 135 |
|
| 136 |
/////////////////////////////////////////////////////////////////////////////// |
| 137 |
|
| 138 |
class ShaderPDXferProcessor : public GrXferProcessor { |
| 139 |
public: |
| 140 |
ShaderPDXferProcessor(SkBlendMode xfermode, GrProcessorAnalysisCoverage coverage) |
| 141 |
: INHERITED(kShaderPDXferProcessor_ClassID, /*willReadDstColor=*/true, coverage) |
| 142 |
, fXfermode(xfermode) { |
| 143 |
} |
| 144 |
|
| 145 |
const char* name() const override { return "Porter Duff Shader"; } |
| 146 |
|
| 147 |
std::unique_ptr<ProgramImpl> makeProgramImpl() const override; |
| 148 |
|
| 149 |
private: |
| 150 |
void onAddToKey(const GrShaderCaps&, skgpu::KeyBuilder*) const override; |
| 151 |
|
| 152 |
bool onIsEqual(const GrXferProcessor& xpBase) const override { |
| 153 |
const ShaderPDXferProcessor& xp = xpBase.cast<ShaderPDXferProcessor>(); |
| 154 |
return fXfermode == xp.fXfermode; |
| 155 |
} |
| 156 |
|
| 157 |
const SkBlendMode fXfermode; |
| 158 |
|
| 159 |
using INHERITED = GrXferProcessor; |
| 160 |
}; |
| 161 |
|
| 162 |
/////////////////////////////////////////////////////////////////////////////// |
| 163 |
|
| 164 |
|
| 165 |
void ShaderPDXferProcessor::onAddToKey(const GrShaderCaps&, skgpu::KeyBuilder* b) const { |
| 166 |
b->add32(GrGLSLBlend::BlendKey(fXfermode)); |
| 167 |
} |
| 168 |
|
| 169 |
std::unique_ptr<GrXferProcessor::ProgramImpl> ShaderPDXferProcessor::makeProgramImpl() const { |
| 170 |
class Impl : public ProgramImpl { |
| 171 |
private: |
| 172 |
void emitBlendCodeForDstRead(GrGLSLXPFragmentBuilder* fragBuilder, |
| 173 |
GrGLSLUniformHandler* uniformHandler, |
| 174 |
const char* srcColor, |
| 175 |
const char* srcCoverage, |
| 176 |
const char* dstColor, |
| 177 |
const char* outColor, |
| 178 |
const char* outColorSecondary, |
| 179 |
const GrXferProcessor& proc) override { |
| 180 |
const ShaderPDXferProcessor& xp = proc.cast<ShaderPDXferProcessor>(); |
| 181 |
|
| 182 |
std::string blendExpr = GrGLSLBlend::BlendExpression( |
| 183 |
&xp, uniformHandler, &fBlendUniform, srcColor, dstColor, xp.fXfermode); |
| 184 |
fragBuilder->codeAppendf("%s = %s;", outColor, blendExpr.c_str()); |
| 185 |
|
| 186 |
// Apply coverage. |
| 187 |
DefaultCoverageModulation(fragBuilder, |
| 188 |
srcCoverage, |
| 189 |
dstColor, |
| 190 |
outColor, |
| 191 |
outColorSecondary, |
| 192 |
xp); |
| 193 |
} |
| 194 |
|
| 195 |
void onSetData(const GrGLSLProgramDataManager& pdman, |
| 196 |
const GrXferProcessor& proc) override { |
| 197 |
if (fBlendUniform.isValid()) { |
| 198 |
const ShaderPDXferProcessor& xp = proc.cast<ShaderPDXferProcessor>(); |
| 199 |
GrGLSLBlend::SetBlendModeUniformData(pdman, fBlendUniform, xp.fXfermode); |
| 200 |
} |
| 201 |
} |
| 202 |
|
| 203 |
GrGLSLUniformHandler::UniformHandle fBlendUniform; |
| 204 |
}; |
| 205 |
|
| 206 |
return std::make_unique<Impl>(); |
| 207 |
} |
| 208 |
|
| 209 |
/////////////////////////////////////////////////////////////////////////////// |
| 210 |
|
| 211 |
class PDLCDXferProcessor : public GrXferProcessor { |
| 212 |
public: |
| 213 |
static sk_sp<const GrXferProcessor> Make(SkBlendMode mode, |
| 214 |
const GrProcessorAnalysisColor& inputColor); |
| 215 |
|
| 216 |
const char* name() const override { return "Porter Duff LCD"; } |
| 217 |
|
| 218 |
std::unique_ptr<ProgramImpl> makeProgramImpl() const override; |
| 219 |
|
| 220 |
private: |
| 221 |
PDLCDXferProcessor(const SkPMColor4f& blendConstant, float alpha); |
| 222 |
|
| 223 |
void onAddToKey(const GrShaderCaps&, skgpu::KeyBuilder*) const override {} |
| 224 |
|
| 225 |
void onGetBlendInfo(skgpu::BlendInfo* blendInfo) const override { |
| 226 |
blendInfo->fSrcBlend = skgpu::BlendCoeff::kConstC; |
| 227 |
blendInfo->fDstBlend = skgpu::BlendCoeff::kISC; |
| 228 |
blendInfo->fBlendConstant = fBlendConstant; |
| 229 |
} |
| 230 |
|
| 231 |
bool onIsEqual(const GrXferProcessor& xpBase) const override { |
| 232 |
const PDLCDXferProcessor& xp = xpBase.cast<PDLCDXferProcessor>(); |
| 233 |
if (fBlendConstant != xp.fBlendConstant || fAlpha != xp.fAlpha) { |
| 234 |
return false; |
| 235 |
} |
| 236 |
return true; |
| 237 |
} |
| 238 |
|
| 239 |
SkPMColor4f fBlendConstant; |
| 240 |
float fAlpha; |
| 241 |
|
| 242 |
using INHERITED = GrXferProcessor; |
| 243 |
}; |
| 244 |
|
| 245 |
PDLCDXferProcessor::PDLCDXferProcessor(const SkPMColor4f& blendConstant, float alpha) |
| 246 |
: INHERITED(kPDLCDXferProcessor_ClassID, /*willReadDstColor=*/false, |
| 247 |
GrProcessorAnalysisCoverage::kLCD) |
| 248 |
, fBlendConstant(blendConstant) |
| 249 |
, fAlpha(alpha) { |
| 250 |
} |
| 251 |
|
| 252 |
sk_sp<const GrXferProcessor> PDLCDXferProcessor::Make(SkBlendMode mode, |
| 253 |
const GrProcessorAnalysisColor& color) { |
| 254 |
if (SkBlendMode::kSrcOver != mode) { |
| 255 |
return nullptr; |
| 256 |
} |
| 257 |
SkPMColor4f blendConstantPM; |
| 258 |
if (!color.isConstant(&blendConstantPM)) { |
| 259 |
return nullptr; |
| 260 |
} |
| 261 |
SkColor4f blendConstantUPM = blendConstantPM.unpremul(); |
| 262 |
float alpha = blendConstantUPM.fA; |
| 263 |
blendConstantPM = { blendConstantUPM.fR, blendConstantUPM.fG, blendConstantUPM.fB, 1 }; |
| 264 |
return sk_sp<GrXferProcessor>(new PDLCDXferProcessor(blendConstantPM, alpha)); |
| 265 |
} |
| 266 |
|
| 267 |
std::unique_ptr<GrXferProcessor::ProgramImpl> PDLCDXferProcessor::makeProgramImpl() const { |
| 268 |
class Impl : public ProgramImpl { |
| 269 |
private: |
| 270 |
void emitOutputsForBlendState(const EmitArgs& args) override { |
| 271 |
const char* alpha; |
| 272 |
fAlphaUniform = args.fUniformHandler->addUniform(nullptr, |
| 273 |
kFragment_GrShaderFlag, |
| 274 |
SkSLType::kHalf, |
| 275 |
"alpha", |
| 276 |
&alpha); |
| 277 |
GrGLSLXPFragmentBuilder* fragBuilder = args.fXPFragBuilder; |
| 278 |
// We want to force our primary output to be alpha * Coverage, where alpha is the alpha |
| 279 |
// value of the src color. We know that there are no color stages (or we wouldn't have |
| 280 |
// created this xp) and the r,g, and b channels of the op's input color are baked into |
| 281 |
// the blend constant. |
| 282 |
SkASSERT(args.fInputCoverage); |
| 283 |
fragBuilder->codeAppendf("%s = %s * %s;", |
| 284 |
args.fOutputPrimary, |
| 285 |
alpha, args.fInputCoverage); |
| 286 |
} |
| 287 |
|
| 288 |
void onSetData(const GrGLSLProgramDataManager& pdm, const GrXferProcessor& xp) override { |
| 289 |
float alpha = xp.cast<PDLCDXferProcessor>().fAlpha; |
| 290 |
if (fLastAlpha != alpha) { |
| 291 |
pdm.set1f(fAlphaUniform, alpha); |
| 292 |
fLastAlpha = alpha; |
| 293 |
} |
| 294 |
} |
| 295 |
|
| 296 |
GrGLSLUniformHandler::UniformHandle fAlphaUniform; |
| 297 |
float fLastAlpha = SK_FloatNaN; |
| 298 |
}; |
| 299 |
|
| 300 |
return std::make_unique<Impl>(); |
| 301 |
} |
| 302 |
|
| 303 |
/////////////////////////////////////////////////////////////////////////////// |
| 304 |
|
| 305 |
constexpr GrPorterDuffXPFactory::GrPorterDuffXPFactory(SkBlendMode xfermode) |
| 306 |
: fBlendMode(xfermode) {} |
| 307 |
|
| 308 |
const GrXPFactory* GrPorterDuffXPFactory::Get(SkBlendMode blendMode) { |
| 309 |
SkASSERT((unsigned)blendMode <= (unsigned)SkBlendMode::kLastCoeffMode); |
| 310 |
|
| 311 |
static constexpr const GrPorterDuffXPFactory gClearPDXPF(SkBlendMode::kClear); |
| 312 |
static constexpr const GrPorterDuffXPFactory gSrcPDXPF(SkBlendMode::kSrc); |
| 313 |
static constexpr const GrPorterDuffXPFactory gDstPDXPF(SkBlendMode::kDst); |
| 314 |
static constexpr const GrPorterDuffXPFactory gSrcOverPDXPF(SkBlendMode::kSrcOver); |
| 315 |
static constexpr const GrPorterDuffXPFactory gDstOverPDXPF(SkBlendMode::kDstOver); |
| 316 |
static constexpr const GrPorterDuffXPFactory gSrcInPDXPF(SkBlendMode::kSrcIn); |
| 317 |
static constexpr const GrPorterDuffXPFactory gDstInPDXPF(SkBlendMode::kDstIn); |
| 318 |
static constexpr const GrPorterDuffXPFactory gSrcOutPDXPF(SkBlendMode::kSrcOut); |
| 319 |
static constexpr const GrPorterDuffXPFactory gDstOutPDXPF(SkBlendMode::kDstOut); |
| 320 |
static constexpr const GrPorterDuffXPFactory gSrcATopPDXPF(SkBlendMode::kSrcATop); |
| 321 |
static constexpr const GrPorterDuffXPFactory gDstATopPDXPF(SkBlendMode::kDstATop); |
| 322 |
static constexpr const GrPorterDuffXPFactory gXorPDXPF(SkBlendMode::kXor); |
| 323 |
static constexpr const GrPorterDuffXPFactory gPlusPDXPF(SkBlendMode::kPlus); |
| 324 |
static constexpr const GrPorterDuffXPFactory gModulatePDXPF(SkBlendMode::kModulate); |
| 325 |
static constexpr const GrPorterDuffXPFactory gScreenPDXPF(SkBlendMode::kScreen); |
| 326 |
|
| 327 |
switch (blendMode) { |
| 328 |
case SkBlendMode::kClear: |
| 329 |
return &gClearPDXPF; |
| 330 |
case SkBlendMode::kSrc: |
| 331 |
return &gSrcPDXPF; |
| 332 |
case SkBlendMode::kDst: |
| 333 |
return &gDstPDXPF; |
| 334 |
case SkBlendMode::kSrcOver: |
| 335 |
return &gSrcOverPDXPF; |
| 336 |
case SkBlendMode::kDstOver: |
| 337 |
return &gDstOverPDXPF; |
| 338 |
case SkBlendMode::kSrcIn: |
| 339 |
return &gSrcInPDXPF; |
| 340 |
case SkBlendMode::kDstIn: |
| 341 |
return &gDstInPDXPF; |
| 342 |
case SkBlendMode::kSrcOut: |
| 343 |
return &gSrcOutPDXPF; |
| 344 |
case SkBlendMode::kDstOut: |
| 345 |
return &gDstOutPDXPF; |
| 346 |
case SkBlendMode::kSrcATop: |
| 347 |
return &gSrcATopPDXPF; |
| 348 |
case SkBlendMode::kDstATop: |
| 349 |
return &gDstATopPDXPF; |
| 350 |
case SkBlendMode::kXor: |
| 351 |
return &gXorPDXPF; |
| 352 |
case SkBlendMode::kPlus: |
| 353 |
return &gPlusPDXPF; |
| 354 |
case SkBlendMode::kModulate: |
| 355 |
return &gModulatePDXPF; |
| 356 |
case SkBlendMode::kScreen: |
| 357 |
return &gScreenPDXPF; |
| 358 |
default: |
| 359 |
SK_ABORT("Unexpected blend mode."); |
| 360 |
} |
| 361 |
} |
| 362 |
|
| 363 |
sk_sp<const GrXferProcessor> GrPorterDuffXPFactory::makeXferProcessor( |
| 364 |
const GrProcessorAnalysisColor& color, GrProcessorAnalysisCoverage coverage, |
| 365 |
const GrCaps& caps, GrClampType clampType) const { |
| 366 |
bool isLCD = coverage == GrProcessorAnalysisCoverage::kLCD; |
| 367 |
// See comment in MakeSrcOverXferProcessor about color.isOpaque here |
| 368 |
if (isLCD && |
| 369 |
SkBlendMode::kSrcOver == fBlendMode && color.isConstant() && /*color.isOpaque() &&*/ |
| 370 |
!caps.shaderCaps()->fDualSourceBlendingSupport && |
| 371 |
!caps.shaderCaps()->fDstReadInShaderSupport) { |
| 372 |
// If we don't have dual source blending or in shader dst reads, we fall back to this |
| 373 |
// trick for rendering SrcOver LCD text instead of doing a dst copy. |
| 374 |
return PDLCDXferProcessor::Make(fBlendMode, color); |
| 375 |
} |
| 376 |
BlendFormula blendFormula = [&](){ |
| 377 |
if (isLCD) { |
| 378 |
return skgpu::GetLCDBlendFormula(fBlendMode); |
| 379 |
} |
| 380 |
if (fBlendMode == SkBlendMode::kSrcOver && color.isOpaque() && |
| 381 |
coverage == GrProcessorAnalysisCoverage::kNone && |
| 382 |
caps.shouldCollapseSrcOverToSrcWhenAble()) |
| 383 |
{ |
| 384 |
return skgpu::GetBlendFormula(true, false, SkBlendMode::kSrc); |
| 385 |
} |
| 386 |
return skgpu::GetBlendFormula( |
| 387 |
color.isOpaque(), GrProcessorAnalysisCoverage::kNone != coverage, fBlendMode); |
| 388 |
}(); |
| 389 |
|
| 390 |
// Skia always saturates after the kPlus blend mode, so it requires shader-based blending when |
| 391 |
// pixels aren't guaranteed to automatically be normalized (i.e. any floating point config). |
| 392 |
if ((blendFormula.hasSecondaryOutput() && !caps.shaderCaps()->fDualSourceBlendingSupport) || |
| 393 |
(isLCD && (SkBlendMode::kSrcOver != fBlendMode /*|| !color.isOpaque()*/)) || |
| 394 |
(GrClampType::kAuto != clampType && SkBlendMode::kPlus == fBlendMode)) { |
| 395 |
return sk_sp<const GrXferProcessor>(new ShaderPDXferProcessor(fBlendMode, coverage)); |
| 396 |
} |
| 397 |
return sk_sp<const GrXferProcessor>(new PorterDuffXferProcessor(blendFormula, coverage)); |
| 398 |
} |
| 399 |
|
| 400 |
static inline GrXPFactory::AnalysisProperties analysis_properties( |
| 401 |
const GrProcessorAnalysisColor& color, const GrProcessorAnalysisCoverage& coverage, |
| 402 |
const GrCaps& caps, GrClampType clampType, SkBlendMode mode) { |
| 403 |
using AnalysisProperties = GrXPFactory::AnalysisProperties; |
| 404 |
AnalysisProperties props = AnalysisProperties::kNone; |
| 405 |
bool hasCoverage = GrProcessorAnalysisCoverage::kNone != coverage; |
| 406 |
bool isLCD = GrProcessorAnalysisCoverage::kLCD == coverage; |
| 407 |
BlendFormula formula = [&](){ |
| 408 |
if (isLCD) { |
| 409 |
return skgpu::GetLCDBlendFormula(mode); |
| 410 |
} |
| 411 |
return skgpu::GetBlendFormula(color.isOpaque(), hasCoverage, mode); |
| 412 |
}(); |
| 413 |
|
| 414 |
if (formula.canTweakAlphaForCoverage() && !isLCD) { |
| 415 |
props |= AnalysisProperties::kCompatibleWithCoverageAsAlpha; |
| 416 |
} |
| 417 |
|
| 418 |
if (isLCD) { |
| 419 |
// See comment in MakeSrcOverXferProcessor about color.isOpaque here |
| 420 |
if (SkBlendMode::kSrcOver == mode && color.isConstant() && /*color.isOpaque() &&*/ |
| 421 |
!caps.shaderCaps()->fDualSourceBlendingSupport && |
| 422 |
!caps.shaderCaps()->fDstReadInShaderSupport) { |
| 423 |
props |= AnalysisProperties::kIgnoresInputColor; |
| 424 |
} else { |
| 425 |
// For LCD blending, if the color is not opaque we must read the dst in shader even if |
| 426 |
// we have dual source blending. The opaqueness check must be done after blending so for |
| 427 |
// simplicity we only allow src-over to not take the dst read path (though src, src-in, |
| 428 |
// and DstATop would also work). We also fall into the dst read case for src-over if we |
| 429 |
// do not have dual source blending. |
| 430 |
if (SkBlendMode::kSrcOver != mode || |
| 431 |
/*!color.isOpaque() ||*/ // See comment in MakeSrcOverXferProcessor about isOpaque. |
| 432 |
(formula.hasSecondaryOutput() && !caps.shaderCaps()->fDualSourceBlendingSupport)) { |
| 433 |
props |= AnalysisProperties::kReadsDstInShader; |
| 434 |
} |
| 435 |
} |
| 436 |
} else { |
| 437 |
// With dual-source blending we never need the destination color in the shader. |
| 438 |
if (!caps.shaderCaps()->fDualSourceBlendingSupport) { |
| 439 |
if (formula.hasSecondaryOutput()) { |
| 440 |
props |= AnalysisProperties::kReadsDstInShader; |
| 441 |
} |
| 442 |
} |
| 443 |
} |
| 444 |
|
| 445 |
if (GrClampType::kAuto != clampType && SkBlendMode::kPlus == mode) { |
| 446 |
props |= AnalysisProperties::kReadsDstInShader; |
| 447 |
} |
| 448 |
|
| 449 |
if (!formula.modifiesDst() || !formula.usesInputColor()) { |
| 450 |
props |= AnalysisProperties::kIgnoresInputColor; |
| 451 |
} |
| 452 |
if (formula.unaffectedByDst() || (formula.unaffectedByDstIfOpaque() && color.isOpaque() && |
| 453 |
!hasCoverage)) { |
| 454 |
props |= AnalysisProperties::kUnaffectedByDstValue; |
| 455 |
} |
| 456 |
return props; |
| 457 |
} |
| 458 |
|
| 459 |
GrXPFactory::AnalysisProperties GrPorterDuffXPFactory::analysisProperties( |
| 460 |
const GrProcessorAnalysisColor& color, |
| 461 |
const GrProcessorAnalysisCoverage& coverage, |
| 462 |
const GrCaps& caps, |
| 463 |
GrClampType clampType) const { |
| 464 |
return analysis_properties(color, coverage, caps, clampType, fBlendMode); |
| 465 |
} |
| 466 |
|
| 467 |
GR_DEFINE_XP_FACTORY_TEST(GrPorterDuffXPFactory) |
| 468 |
|
| 469 |
#if defined(GPU_TEST_UTILS) |
| 470 |
const GrXPFactory* GrPorterDuffXPFactory::TestGet(GrProcessorTestData* d) { |
| 471 |
SkBlendMode mode = SkBlendMode(d->fRandom->nextULessThan((int)SkBlendMode::kLastCoeffMode)); |
| 472 |
return GrPorterDuffXPFactory::Get(mode); |
| 473 |
} |
| 474 |
#endif |
| 475 |
|
| 476 |
void GrPorterDuffXPFactory::TestGetXPOutputTypes(const GrXferProcessor* xp, |
| 477 |
int* outPrimary, |
| 478 |
int* outSecondary) { |
| 479 |
if (!!strcmp(xp->name(), "Porter Duff")) { |
| 480 |
*outPrimary = *outSecondary = -1; |
| 481 |
return; |
| 482 |
} |
| 483 |
BlendFormula blendFormula = static_cast<const PorterDuffXferProcessor*>(xp)->getBlendFormula(); |
| 484 |
*outPrimary = blendFormula.primaryOutput(); |
| 485 |
*outSecondary = blendFormula.secondaryOutput(); |
| 486 |
} |
| 487 |
|
| 488 |
//////////////////////////////////////////////////////////////////////////////////////////////// |
| 489 |
// SrcOver Global functions |
| 490 |
//////////////////////////////////////////////////////////////////////////////////////////////// |
| 491 |
const GrXferProcessor& GrPorterDuffXPFactory::SimpleSrcOverXP() { |
| 492 |
static BlendFormula kSrcOverBlendFormula = skgpu::GetBlendFormula( |
| 493 |
/*isOpaque=*/false, /*hasCoverage=*/false, SkBlendMode::kSrcOver); |
| 494 |
static PorterDuffXferProcessor gSrcOverXP(kSrcOverBlendFormula, |
| 495 |
GrProcessorAnalysisCoverage::kSingleChannel); |
| 496 |
return gSrcOverXP; |
| 497 |
} |
| 498 |
|
| 499 |
sk_sp<const GrXferProcessor> GrPorterDuffXPFactory::MakeSrcOverXferProcessor( |
| 500 |
const GrProcessorAnalysisColor& color, GrProcessorAnalysisCoverage coverage, |
| 501 |
const GrCaps& caps) { |
| 502 |
// We want to not make an xfer processor if possible. Thus for the simple case where we are not |
| 503 |
// doing lcd blending we will just use our global SimpleSrcOverXP. This slightly differs from |
| 504 |
// the general case where we convert a src-over blend that has solid coverage and an opaque |
| 505 |
// color to src-mode, which allows disabling of blending. |
| 506 |
if (coverage != GrProcessorAnalysisCoverage::kLCD) { |
| 507 |
if (color.isOpaque() && coverage == GrProcessorAnalysisCoverage::kNone && |
| 508 |
caps.shouldCollapseSrcOverToSrcWhenAble()) { |
| 509 |
BlendFormula blendFormula = skgpu::GetBlendFormula(true, false, SkBlendMode::kSrc); |
| 510 |
return sk_sp<GrXferProcessor>(new PorterDuffXferProcessor(blendFormula, coverage)); |
| 511 |
} |
| 512 |
// We return nullptr here, which our caller interprets as meaning "use SimpleSrcOverXP". |
| 513 |
// We don't simply return the address of that XP here because our caller would have to unref |
| 514 |
// it and since it is a global object and GrProgramElement's ref-cnting system is not thread |
| 515 |
// safe. |
| 516 |
return nullptr; |
| 517 |
} |
| 518 |
|
| 519 |
// Currently up the stack Skia is requiring that the dst is opaque or that the client has said |
| 520 |
// the opaqueness doesn't matter. Thus for src-over we don't need to worry about the src color |
| 521 |
// being opaque or not. This allows us to use faster code paths as well as avoid various bugs |
| 522 |
// that occur with dst reads in the shader blending. For now we disable the check for |
| 523 |
// opaqueness, but in the future we should pass down the knowledge about dst opaqueness and make |
| 524 |
// the correct decision here. |
| 525 |
// |
| 526 |
// This also fixes a chrome bug on macs where we are getting random fuzziness when doing |
| 527 |
// blending in the shader for non opaque sources. |
| 528 |
if (color.isConstant() && /*color.isOpaque() &&*/ |
| 529 |
!caps.shaderCaps()->fDualSourceBlendingSupport && |
| 530 |
!caps.shaderCaps()->fDstReadInShaderSupport) { |
| 531 |
// If we don't have dual source blending or in shader dst reads, we fall |
| 532 |
// back to this trick for rendering SrcOver LCD text instead of doing a |
| 533 |
// dst copy. |
| 534 |
return PDLCDXferProcessor::Make(SkBlendMode::kSrcOver, color); |
| 535 |
} |
| 536 |
|
| 537 |
BlendFormula blendFormula = skgpu::GetLCDBlendFormula(SkBlendMode::kSrcOver); |
| 538 |
// See comment above regarding why the opaque check is commented out here. |
| 539 |
if (/*!color.isOpaque() ||*/ |
| 540 |
(blendFormula.hasSecondaryOutput() && !caps.shaderCaps()->fDualSourceBlendingSupport)) { |
| 541 |
return sk_sp<GrXferProcessor>(new ShaderPDXferProcessor(SkBlendMode::kSrcOver, coverage)); |
| 542 |
} |
| 543 |
return sk_sp<GrXferProcessor>(new PorterDuffXferProcessor(blendFormula, coverage)); |
| 544 |
} |
| 545 |
|
| 546 |
sk_sp<const GrXferProcessor> GrPorterDuffXPFactory::MakeNoCoverageXP(SkBlendMode blendmode) { |
| 547 |
BlendFormula formula = skgpu::GetBlendFormula(false, false, blendmode); |
| 548 |
return sk_make_sp<PorterDuffXferProcessor>(formula, GrProcessorAnalysisCoverage::kNone); |
| 549 |
} |
| 550 |
|
| 551 |
GrXPFactory::AnalysisProperties GrPorterDuffXPFactory::SrcOverAnalysisProperties( |
| 552 |
const GrProcessorAnalysisColor& color, |
| 553 |
const GrProcessorAnalysisCoverage& coverage, |
| 554 |
const GrCaps& caps, |
| 555 |
GrClampType clampType) { |
| 556 |
return analysis_properties(color, coverage, caps, clampType, SkBlendMode::kSrcOver); |
| 557 |
} |