| 1 |
/* |
| 2 |
* Copyright 2024 Google LLC |
| 3 |
* |
| 4 |
* Use of this source code is governed by a BSD-style license that can be |
| 5 |
* found in the LICENSE file. |
| 6 |
*/ |
| 7 |
|
| 8 |
#include "include/gpu/graphite/precompile/PaintOptions.h" |
| 9 |
|
| 10 |
#include "include/gpu/graphite/precompile/PrecompileBlender.h" |
| 11 |
#include "include/gpu/graphite/precompile/PrecompileColorFilter.h" |
| 12 |
#include "include/gpu/graphite/precompile/PrecompileImageFilter.h" |
| 13 |
#include "include/gpu/graphite/precompile/PrecompileMaskFilter.h" |
| 14 |
#include "include/gpu/graphite/precompile/PrecompileShader.h" |
| 15 |
#include "src/gpu/graphite/Caps.h" |
| 16 |
#include "src/gpu/graphite/ContextUtils.h" |
| 17 |
#include "src/gpu/graphite/KeyContext.h" |
| 18 |
#include "src/gpu/graphite/PaintParamsKey.h" |
| 19 |
#include "src/gpu/graphite/PipelineData.h" |
| 20 |
#include "src/gpu/graphite/PrecompileInternal.h" |
| 21 |
#include "src/gpu/graphite/RenderPassDesc.h" |
| 22 |
#include "src/gpu/graphite/Renderer.h" |
| 23 |
#include "src/gpu/graphite/ShaderCodeDictionary.h" |
| 24 |
#include "src/gpu/graphite/precompile/PaintOption.h" |
| 25 |
#include "src/gpu/graphite/precompile/PaintOptionsPriv.h" |
| 26 |
#include "src/gpu/graphite/precompile/PrecompileBaseComplete.h" |
| 27 |
#include "src/gpu/graphite/precompile/PrecompileBasePriv.h" |
| 28 |
#include "src/gpu/graphite/precompile/PrecompileBlenderPriv.h" |
| 29 |
#include "src/gpu/graphite/precompile/PrecompileShaderPriv.h" |
| 30 |
#include "src/gpu/graphite/precompile/PrecompileShadersPriv.h" |
| 31 |
|
| 32 |
namespace skgpu::graphite { |
| 33 |
|
| 34 |
PaintOptions::PaintOptions() = default; |
| 35 |
PaintOptions::PaintOptions(const PaintOptions&) = default; |
| 36 |
PaintOptions::~PaintOptions() = default; |
| 37 |
PaintOptions& PaintOptions::operator=(const PaintOptions&) = default; |
| 38 |
|
| 39 |
void PaintOptions::setShaders(SkSpan<const sk_sp<PrecompileShader>> shaders) { |
| 40 |
fShaderOptions.clear(); |
| 41 |
fShaderOptions.push_back_n(shaders.size(), shaders.data()); |
| 42 |
} |
| 43 |
|
| 44 |
void PaintOptions::setImageFilters(SkSpan<const sk_sp<PrecompileImageFilter>> imageFilters) { |
| 45 |
fImageFilterOptions.clear(); |
| 46 |
fImageFilterOptions.push_back_n(imageFilters.size(), imageFilters.data()); |
| 47 |
} |
| 48 |
|
| 49 |
void PaintOptions::setMaskFilters(SkSpan<const sk_sp<PrecompileMaskFilter>> maskFilters) { |
| 50 |
fMaskFilterOptions.clear(); |
| 51 |
fMaskFilterOptions.push_back_n(maskFilters.size(), maskFilters.data()); |
| 52 |
} |
| 53 |
|
| 54 |
void PaintOptions::setColorFilters(SkSpan<const sk_sp<PrecompileColorFilter>> colorFilters) { |
| 55 |
fColorFilterOptions.clear(); |
| 56 |
fColorFilterOptions.push_back_n(colorFilters.size(), colorFilters.data()); |
| 57 |
} |
| 58 |
|
| 59 |
void PaintOptions::addColorFilter(sk_sp<PrecompileColorFilter> cf) { |
| 60 |
fColorFilterOptions.push_back(std::move(cf)); |
| 61 |
} |
| 62 |
|
| 63 |
void PaintOptions::setBlendModes(SkSpan<const SkBlendMode> blendModes) { |
| 64 |
fBlendModeOptions.clear(); |
| 65 |
fBlendModeOptions.push_back_n(blendModes.size(), blendModes.data()); |
| 66 |
} |
| 67 |
|
| 68 |
void PaintOptions::setBlenders(SkSpan<const sk_sp<PrecompileBlender>> blenders) { |
| 69 |
for (const sk_sp<PrecompileBlender>& b: blenders) { |
| 70 |
if (b->priv().asBlendMode().has_value()) { |
| 71 |
fBlendModeOptions.push_back(b->priv().asBlendMode().value()); |
| 72 |
} else { |
| 73 |
fBlenderOptions.push_back(b); |
| 74 |
} |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
void PaintOptions::setClipShaders(SkSpan<const sk_sp<PrecompileShader>> clipShaders) { |
| 79 |
// In the normal API this modification happens in SkDevice::clipShader() |
| 80 |
fClipShaderOptions.reserve(2 * clipShaders.size()); |
| 81 |
for (const sk_sp<PrecompileShader>& cs : clipShaders) { |
| 82 |
// All clipShaders get wrapped in a CTMShader ... |
| 83 |
sk_sp<PrecompileShader> withCTM = cs ? PrecompileShadersPriv::CTM({{ cs }}) : nullptr; |
| 84 |
// and, if it is a SkClipOp::kDifference clip, an additional ColorFilterShader |
| 85 |
sk_sp<PrecompileShader> inverted = |
| 86 |
withCTM ? withCTM->makeWithColorFilter(PrecompileColorFilters::Blend()) |
| 87 |
: nullptr; |
| 88 |
|
| 89 |
fClipShaderOptions.emplace_back(std::move(withCTM)); |
| 90 |
fClipShaderOptions.emplace_back(std::move(inverted)); |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
int PaintOptions::numShaderCombinations() const { |
| 95 |
int numShaderCombinations = 0; |
| 96 |
for (const sk_sp<PrecompileShader>& s : fShaderOptions) { |
| 97 |
numShaderCombinations += s->numCombinations(); |
| 98 |
} |
| 99 |
|
| 100 |
// If no shader option is specified we will add a solid color shader option |
| 101 |
return numShaderCombinations ? numShaderCombinations : 1; |
| 102 |
} |
| 103 |
|
| 104 |
int PaintOptions::numColorFilterCombinations() const { |
| 105 |
int numColorFilterCombinations = 0; |
| 106 |
for (const sk_sp<PrecompileColorFilter>& cf : fColorFilterOptions) { |
| 107 |
if (!cf) { |
| 108 |
++numColorFilterCombinations; |
| 109 |
} else { |
| 110 |
numColorFilterCombinations += cf->numCombinations(); |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
// If no color filter options are specified we will use the unmodified result color |
| 115 |
return numColorFilterCombinations ? numColorFilterCombinations : 1; |
| 116 |
} |
| 117 |
|
| 118 |
int PaintOptions::numBlendCombinations() const { |
| 119 |
int numBlendCombos = fBlendModeOptions.size(); |
| 120 |
for (const sk_sp<PrecompileBlender>& b: fBlenderOptions) { |
| 121 |
SkASSERT(!b->priv().asBlendMode().has_value()); |
| 122 |
numBlendCombos += b->priv().numChildCombinations(); |
| 123 |
} |
| 124 |
|
| 125 |
if (!numBlendCombos) { |
| 126 |
// If the user didn't specify a blender we will fall back to kSrcOver blending |
| 127 |
numBlendCombos = 1; |
| 128 |
} |
| 129 |
|
| 130 |
return numBlendCombos; |
| 131 |
} |
| 132 |
|
| 133 |
int PaintOptions::numClipShaderCombinations() const { |
| 134 |
int numClipShaderCombos = 0; |
| 135 |
for (const sk_sp<PrecompileShader>& cs: fClipShaderOptions) { |
| 136 |
if (cs) { |
| 137 |
numClipShaderCombos += cs->priv().numChildCombinations(); |
| 138 |
} else { |
| 139 |
++numClipShaderCombos; |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
// If no clipShader options are specified we will just have the unclipped options |
| 144 |
return numClipShaderCombos ? numClipShaderCombos : 1; |
| 145 |
} |
| 146 |
|
| 147 |
int PaintOptions::numCombinations() const { |
| 148 |
return this->numShaderCombinations() * |
| 149 |
this->numColorFilterCombinations() * |
| 150 |
this->numBlendCombinations() * |
| 151 |
this->numClipShaderCombinations(); |
| 152 |
} |
| 153 |
|
| 154 |
void PaintOptions::createKey(const KeyContext& keyContext, |
| 155 |
TextureFormat targetFormat, |
| 156 |
int desiredCombination, |
| 157 |
bool addPrimitiveBlender, |
| 158 |
bool addAnalyticClip, |
| 159 |
Coverage coverage) const { |
| 160 |
SkDEBUGCODE(keyContext.paintParamsKeyBuilder()->checkReset();) |
| 161 |
SkASSERT(desiredCombination < this->numCombinations()); |
| 162 |
|
| 163 |
const int numClipShaderCombos = this->numClipShaderCombinations(); |
| 164 |
const int numBlendModeCombos = this->numBlendCombinations(); |
| 165 |
const int numColorFilterCombinations = this->numColorFilterCombinations(); |
| 166 |
|
| 167 |
const int desiredClipShaderCombination = desiredCombination % numClipShaderCombos; |
| 168 |
int remainingCombinations = desiredCombination / numClipShaderCombos; |
| 169 |
|
| 170 |
const int desiredBlendCombination = remainingCombinations % numBlendModeCombos; |
| 171 |
remainingCombinations /= numBlendModeCombos; |
| 172 |
|
| 173 |
const int desiredColorFilterCombination = remainingCombinations % numColorFilterCombinations; |
| 174 |
remainingCombinations /= numColorFilterCombinations; |
| 175 |
|
| 176 |
const int desiredShaderCombination = remainingCombinations; |
| 177 |
SkASSERT(desiredShaderCombination < this->numShaderCombinations()); |
| 178 |
|
| 179 |
auto clipShader = PrecompileBase::SelectOption(SkSpan(fClipShaderOptions), |
| 180 |
desiredClipShaderCombination); |
| 181 |
|
| 182 |
std::pair<sk_sp<PrecompileBlender>, int> finalBlender; |
| 183 |
if (desiredBlendCombination < fBlendModeOptions.size()) { |
| 184 |
finalBlender = { PrecompileBlenders::Mode(fBlendModeOptions[desiredBlendCombination]), 0 }; |
| 185 |
} else { |
| 186 |
finalBlender = PrecompileBase::SelectOption( |
| 187 |
SkSpan(fBlenderOptions), |
| 188 |
desiredBlendCombination - fBlendModeOptions.size()); |
| 189 |
} |
| 190 |
if (!finalBlender.first) { |
| 191 |
finalBlender = { PrecompileBlenders::Mode(SkBlendMode::kSrcOver), 0 }; |
| 192 |
} |
| 193 |
|
| 194 |
PaintOption option(fPaintColorIsOpaque, |
| 195 |
finalBlender, |
| 196 |
PrecompileBase::SelectOption(SkSpan(fShaderOptions), |
| 197 |
desiredShaderCombination), |
| 198 |
PrecompileBase::SelectOption(SkSpan(fColorFilterOptions), |
| 199 |
desiredColorFilterCombination), |
| 200 |
addPrimitiveBlender, |
| 201 |
fPrimitiveBlendMode, |
| 202 |
fSkipColorXform, |
| 203 |
clipShader, |
| 204 |
coverage, |
| 205 |
targetFormat, |
| 206 |
fDither, |
| 207 |
addAnalyticClip); |
| 208 |
|
| 209 |
option.toKey(keyContext); |
| 210 |
} |
| 211 |
|
| 212 |
namespace { |
| 213 |
|
| 214 |
void create_image_drawing_pipelines(const KeyContext& keyContext, |
| 215 |
const PaintOptions& orig, |
| 216 |
const RenderPassDesc& renderPassDesc, |
| 217 |
const PaintOptionsPriv::ProcessCombination& processCombination) { |
| 218 |
PaintOptions imagePaintOptions; |
| 219 |
|
| 220 |
// For imagefilters we know we don't have alpha-only textures and don't need cubic filtering. |
| 221 |
sk_sp<PrecompileShader> imageShader = PrecompileShaders::Image( |
| 222 |
PrecompileShaders::ImageShaderFlags::kNoAlphaNoCubic); |
| 223 |
|
| 224 |
imagePaintOptions.setShaders({{ imageShader }}); |
| 225 |
imagePaintOptions.setBlendModes(orig.getBlendModes()); |
| 226 |
imagePaintOptions.setBlenders(orig.getBlenders()); |
| 227 |
imagePaintOptions.setColorFilters(orig.getColorFilters()); |
| 228 |
imagePaintOptions.priv().addColorFilter(nullptr); |
| 229 |
|
| 230 |
imagePaintOptions.priv().buildCombinations(keyContext, |
| 231 |
DrawTypeFlags::kSimpleShape, |
| 232 |
/* withPrimitiveBlender= */ false, |
| 233 |
Coverage::kSingleChannel, |
| 234 |
renderPassDesc, |
| 235 |
processCombination); |
| 236 |
} |
| 237 |
|
| 238 |
} // anonymous namespace |
| 239 |
|
| 240 |
void PaintOptions::buildCombinations( |
| 241 |
const KeyContext& keyContext, |
| 242 |
DrawTypeFlags drawTypes, |
| 243 |
bool withPrimitiveBlender, |
| 244 |
Coverage coverage, |
| 245 |
const RenderPassDesc& renderPassDesc, |
| 246 |
const ProcessCombination& processCombination) const { |
| 247 |
if (!fImageFilterOptions.empty() || !fMaskFilterOptions.empty()) { |
| 248 |
// TODO: split this out into a create_restore_draw_pipelines method |
| 249 |
PaintOptions tmp = *this; |
| 250 |
|
| 251 |
// When image filtering, the original blend mode is taken over by the restore paint |
| 252 |
tmp.setImageFilters({}); |
| 253 |
tmp.setMaskFilters({}); |
| 254 |
tmp.addBlendMode(SkBlendMode::kSrcOver); |
| 255 |
|
| 256 |
if (!fImageFilterOptions.empty()) { |
| 257 |
std::vector<sk_sp<PrecompileColorFilter>> newCFs(tmp.fColorFilterOptions.begin(), |
| 258 |
tmp.fColorFilterOptions.end()); |
| 259 |
if (newCFs.empty()) { |
| 260 |
// TODO: I (robertphillips) believe this is unnecessary and is just a result of the |
| 261 |
// base SkPaint generated in the PaintParamsKeyTest not correctly taking CFIFs into |
| 262 |
// account. |
| 263 |
newCFs.push_back(nullptr); |
| 264 |
} |
| 265 |
|
| 266 |
// As in SkCanvasPriv::ImageToColorFilter, we fuse CFIFs into the base draw's CFs. |
| 267 |
// TODO: in SkCanvasPriv::ImageToColorFilter this fusing of CFIFs and CFs is skipped |
| 268 |
// when there is a maskfilter. For now we over-generate. |
| 269 |
for (const sk_sp<PrecompileImageFilter>& o : fImageFilterOptions) { |
| 270 |
sk_sp<PrecompileColorFilter> imageFiltersCF = o ? o->asAColorFilter() : nullptr; |
| 271 |
if (imageFiltersCF) { |
| 272 |
if (!tmp.fColorFilterOptions.empty()) { |
| 273 |
for (const sk_sp<PrecompileColorFilter>& cf : tmp.fColorFilterOptions) { |
| 274 |
// TODO: if a CFIF was fully handled here it should be removed from the |
| 275 |
// later loop over fImageFilterOptions. For now we over-generate. |
| 276 |
sk_sp<PrecompileColorFilter> newCF = imageFiltersCF->makeComposed(cf); |
| 277 |
newCFs.push_back(std::move(newCF)); |
| 278 |
} |
| 279 |
} else { |
| 280 |
newCFs.push_back(imageFiltersCF); |
| 281 |
} |
| 282 |
} |
| 283 |
} |
| 284 |
|
| 285 |
tmp.setColorFilters(newCFs); |
| 286 |
} |
| 287 |
|
| 288 |
tmp.buildCombinations(keyContext, drawTypes, withPrimitiveBlender, coverage, renderPassDesc, |
| 289 |
processCombination); |
| 290 |
|
| 291 |
create_image_drawing_pipelines(keyContext, *this, renderPassDesc, processCombination); |
| 292 |
|
| 293 |
for (const sk_sp<PrecompileImageFilter>& o : fImageFilterOptions) { |
| 294 |
o->createPipelines(keyContext, renderPassDesc, processCombination); |
| 295 |
} |
| 296 |
for (const sk_sp<PrecompileMaskFilter>& o : fMaskFilterOptions) { |
| 297 |
o->createPipelines(keyContext, *this, renderPassDesc, processCombination); |
| 298 |
} |
| 299 |
} else { |
| 300 |
int numCombinations = this->numCombinations(); |
| 301 |
for (int i = 0; i < numCombinations; ++i) { |
| 302 |
// Since the precompilation path's uniforms aren't used and don't change the key, |
| 303 |
// the exact layout doesn't matter |
| 304 |
|
| 305 |
keyContext.pipelineDataGatherer()->resetForDraw(); |
| 306 |
|
| 307 |
this->createKey(keyContext, renderPassDesc.fColorAttachment.fFormat, |
| 308 |
i, withPrimitiveBlender, |
| 309 |
SkToBool(drawTypes & DrawTypeFlags::kAnalyticClip), coverage); |
| 310 |
|
| 311 |
// The 'findOrCreate' calls lockAsKey on builder and then destroys the returned |
| 312 |
// PaintParamsKey. This serves to reset the builder. |
| 313 |
UniquePaintParamsID paintID = keyContext.dict()->findOrCreate( |
| 314 |
keyContext.paintParamsKeyBuilder()); |
| 315 |
|
| 316 |
processCombination(paintID, drawTypes, withPrimitiveBlender, coverage, renderPassDesc); |
| 317 |
} |
| 318 |
} |
| 319 |
} |
| 320 |
|
| 321 |
void PaintOptionsPriv::addColorFilter(sk_sp<PrecompileColorFilter> cf) { |
| 322 |
fPaintOptions->addColorFilter(std::move(cf)); |
| 323 |
} |
| 324 |
|
| 325 |
} // namespace skgpu::graphite |