| 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/PrecompileImageFilter.h" |
| 9 |
|
| 10 |
#include "include/gpu/graphite/precompile/PrecompileBlender.h" |
| 11 |
#include "include/gpu/graphite/precompile/PrecompileColorFilter.h" |
| 12 |
#include "include/gpu/graphite/precompile/PrecompileShader.h" |
| 13 |
#include "src/gpu/graphite/Renderer.h" |
| 14 |
#include "src/gpu/graphite/precompile/PrecompileBlenderPriv.h" |
| 15 |
#include "src/gpu/graphite/precompile/PrecompileImageFilterPriv.h" |
| 16 |
#include "src/gpu/graphite/precompile/PrecompileImageFiltersPriv.h" |
| 17 |
#include "src/gpu/graphite/precompile/PrecompileShadersPriv.h" |
| 18 |
|
| 19 |
namespace skgpu::graphite { |
| 20 |
|
| 21 |
using PrecompileShaders::ImageShaderFlags; |
| 22 |
|
| 23 |
//-------------------------------------------------------------------------------------------------- |
| 24 |
PrecompileImageFilter::PrecompileImageFilter(SkSpan<sk_sp<PrecompileImageFilter>> inputs) |
| 25 |
: PrecompileBase(Type::kImageFilter) { |
| 26 |
fInputs.reset(inputs.size()); |
| 27 |
for (size_t i = 0; i < inputs.size(); ++i) { |
| 28 |
fInputs[i] = inputs[i]; |
| 29 |
} |
| 30 |
} |
| 31 |
|
| 32 |
PrecompileImageFilter::~PrecompileImageFilter() = default; |
| 33 |
|
| 34 |
sk_sp<PrecompileColorFilter> PrecompileImageFilter::asAColorFilter() const { |
| 35 |
sk_sp<PrecompileColorFilter> tmp = this->isColorFilterNode(); |
| 36 |
if (!tmp) { |
| 37 |
return nullptr; |
| 38 |
} |
| 39 |
SkASSERT(this->countInputs() == 1); |
| 40 |
if (this->getInput(0)) { |
| 41 |
return nullptr; |
| 42 |
} |
| 43 |
// TODO: as in SkImageFilter::asAColorFilter, handle the special case of |
| 44 |
// affectsTransparentBlack. This is tricky for precompilation since we don't, |
| 45 |
// necessarily, have all the parameters of the ColorFilter in order to evaluate |
| 46 |
// filterColor4f(SkColors::kTransparent) - the normal API's implementation. |
| 47 |
return tmp; |
| 48 |
} |
| 49 |
|
| 50 |
void PrecompileImageFilter::createPipelines( |
| 51 |
const KeyContext& keyContext, |
| 52 |
const RenderPassDesc& renderPassDesc, |
| 53 |
const PaintOptions::ProcessCombination& processCombination) { |
| 54 |
// TODO: we will want to mark already visited nodes to prevent loops and track |
| 55 |
// already created Pipelines so we don't over-generate too much (e.g., if a DAG |
| 56 |
// has multiple blurs we don't want to keep trying to create all the blur pipelines). |
| 57 |
this->onCreatePipelines(keyContext, renderPassDesc, processCombination); |
| 58 |
|
| 59 |
for (const sk_sp<PrecompileImageFilter>& input : fInputs) { |
| 60 |
if (input) { |
| 61 |
input->createPipelines(keyContext, renderPassDesc, processCombination); |
| 62 |
} |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
//-------------------------------------------------------------------------------------------------- |
| 67 |
namespace PrecompileImageFiltersPriv { |
| 68 |
|
| 69 |
void CreateBlurImageFilterPipelines( |
| 70 |
const KeyContext& keyContext, |
| 71 |
const RenderPassDesc& renderPassDesc, |
| 72 |
const PaintOptionsPriv::ProcessCombination& processCombination) { |
| 73 |
|
| 74 |
PaintOptions blurPaintOptions; |
| 75 |
|
| 76 |
// For blur imagefilters we know we don't have alpha-only textures and don't need cubic |
| 77 |
// filtering. |
| 78 |
sk_sp<PrecompileShader> imageShader = PrecompileShaders::Image( |
| 79 |
ImageShaderFlags::kNoAlphaNoCubic); |
| 80 |
|
| 81 |
static const SkBlendMode kBlurBlendModes[] = { SkBlendMode::kSrc }; |
| 82 |
blurPaintOptions.setShaders({{ PrecompileShadersPriv::Blur(imageShader) }}); |
| 83 |
blurPaintOptions.setBlendModes(kBlurBlendModes); |
| 84 |
|
| 85 |
blurPaintOptions.priv().buildCombinations(keyContext, |
| 86 |
DrawTypeFlags::kSimpleShape, |
| 87 |
/* withPrimitiveBlender= */ false, |
| 88 |
Coverage::kSingleChannel, |
| 89 |
renderPassDesc, |
| 90 |
processCombination); |
| 91 |
} |
| 92 |
|
| 93 |
} // namespace PrecompileImageFiltersPriv |
| 94 |
|
| 95 |
//-------------------------------------------------------------------------------------------------- |
| 96 |
class PrecompileBlendFilterImageFilter : public PrecompileImageFilter { |
| 97 |
public: |
| 98 |
PrecompileBlendFilterImageFilter(sk_sp<PrecompileBlender> blender, |
| 99 |
SkSpan<sk_sp<PrecompileImageFilter>> inputs) |
| 100 |
: PrecompileImageFilter(std::move(inputs)) |
| 101 |
, fBlender(std::move(blender)) { |
| 102 |
} |
| 103 |
|
| 104 |
private: |
| 105 |
void onCreatePipelines( |
| 106 |
const KeyContext& keyContext, |
| 107 |
const RenderPassDesc& renderPassDesc, |
| 108 |
const PaintOptionsPriv::ProcessCombination& processCombination) const override { |
| 109 |
|
| 110 |
PaintOptions paintOptions; |
| 111 |
|
| 112 |
sk_sp<PrecompileShader> imageShader = PrecompileShaders::Image( |
| 113 |
ImageShaderFlags::kNoAlphaNoCubic); |
| 114 |
|
| 115 |
sk_sp<PrecompileShader> blendShader = PrecompileShaders::Blend( |
| 116 |
SkSpan<const sk_sp<PrecompileBlender>>(&fBlender, 1), |
| 117 |
{{ imageShader }}, |
| 118 |
{{ imageShader }}); |
| 119 |
|
| 120 |
paintOptions.setShaders({{ std::move(blendShader) }}); |
| 121 |
|
| 122 |
paintOptions.priv().buildCombinations(keyContext, |
| 123 |
DrawTypeFlags::kSimpleShape, |
| 124 |
/* withPrimitiveBlender= */ false, |
| 125 |
Coverage::kSingleChannel, |
| 126 |
renderPassDesc, |
| 127 |
processCombination); |
| 128 |
} |
| 129 |
|
| 130 |
sk_sp<PrecompileBlender> fBlender; |
| 131 |
}; |
| 132 |
|
| 133 |
sk_sp<PrecompileImageFilter> PrecompileImageFilters::Arithmetic( |
| 134 |
sk_sp<PrecompileImageFilter> background, |
| 135 |
sk_sp<PrecompileImageFilter> foreground) { |
| 136 |
return Blend(PrecompileBlenders::Arithmetic(), std::move(background), std::move(foreground)); |
| 137 |
} |
| 138 |
|
| 139 |
sk_sp<PrecompileImageFilter> PrecompileImageFilters::Blend( |
| 140 |
SkBlendMode bm, |
| 141 |
sk_sp<PrecompileImageFilter> background, |
| 142 |
sk_sp<PrecompileImageFilter> foreground) { |
| 143 |
return Blend(PrecompileBlenders::Mode(bm), std::move(background), std::move(foreground)); |
| 144 |
} |
| 145 |
|
| 146 |
sk_sp<PrecompileImageFilter> PrecompileImageFilters::Blend( |
| 147 |
sk_sp<PrecompileBlender> blender, |
| 148 |
sk_sp<PrecompileImageFilter> background, |
| 149 |
sk_sp<PrecompileImageFilter> foreground) { |
| 150 |
|
| 151 |
if (!blender) { |
| 152 |
blender = PrecompileBlenders::Mode(SkBlendMode::kSrcOver); |
| 153 |
} |
| 154 |
|
| 155 |
if (std::optional<SkBlendMode> bm = blender->priv().asBlendMode()) { |
| 156 |
if (bm == SkBlendMode::kSrc) { |
| 157 |
return foreground; |
| 158 |
} else if (bm == SkBlendMode::kDst) { |
| 159 |
return background; |
| 160 |
} else if (bm == SkBlendMode::kClear) { |
| 161 |
return nullptr; // TODO: actually return PrecompileImageFilters::Empty |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
sk_sp<PrecompileImageFilter> inputs[2] = { std::move(background), std::move(foreground) }; |
| 166 |
return sk_make_sp<PrecompileBlendFilterImageFilter>(std::move(blender), inputs); |
| 167 |
} |
| 168 |
|
| 169 |
//-------------------------------------------------------------------------------------------------- |
| 170 |
class PrecompileBlurImageFilter : public PrecompileImageFilter { |
| 171 |
public: |
| 172 |
PrecompileBlurImageFilter(SkSpan<sk_sp<PrecompileImageFilter>> inputs) |
| 173 |
: PrecompileImageFilter(std::move(inputs)) { |
| 174 |
} |
| 175 |
|
| 176 |
private: |
| 177 |
void onCreatePipelines( |
| 178 |
const KeyContext& keyContext, |
| 179 |
const RenderPassDesc& renderPassDesc, |
| 180 |
const PaintOptionsPriv::ProcessCombination& processCombination) const override { |
| 181 |
|
| 182 |
PrecompileImageFiltersPriv::CreateBlurImageFilterPipelines(keyContext, |
| 183 |
renderPassDesc, |
| 184 |
processCombination); |
| 185 |
} |
| 186 |
}; |
| 187 |
|
| 188 |
sk_sp<PrecompileImageFilter> PrecompileImageFilters::Blur( |
| 189 |
sk_sp<PrecompileImageFilter> input) { |
| 190 |
return sk_make_sp<PrecompileBlurImageFilter>(SkSpan(&input, 1)); |
| 191 |
} |
| 192 |
|
| 193 |
//-------------------------------------------------------------------------------------------------- |
| 194 |
class PrecompileColorFilterImageFilter : public PrecompileImageFilter { |
| 195 |
public: |
| 196 |
PrecompileColorFilterImageFilter(sk_sp<PrecompileColorFilter> colorFilter, |
| 197 |
sk_sp<PrecompileImageFilter> input) |
| 198 |
: PrecompileImageFilter(SkSpan(&input, 1)) |
| 199 |
, fColorFilter(std::move(colorFilter)) { |
| 200 |
} |
| 201 |
|
| 202 |
private: |
| 203 |
sk_sp<PrecompileColorFilter> isColorFilterNode() const override { |
| 204 |
return fColorFilter; |
| 205 |
} |
| 206 |
|
| 207 |
void onCreatePipelines( |
| 208 |
const KeyContext& keyContext, |
| 209 |
const RenderPassDesc& renderPassDesc, |
| 210 |
const PaintOptionsPriv::ProcessCombination& processCombination) const override { |
| 211 |
PaintOptions paintOptions; |
| 212 |
|
| 213 |
sk_sp<PrecompileShader> imageShader = PrecompileShaders::Image( |
| 214 |
ImageShaderFlags::kNoAlphaNoCubic); |
| 215 |
|
| 216 |
static const SkBlendMode kBlendModes[] = { SkBlendMode::kDstOut }; |
| 217 |
paintOptions.setShaders({{ std::move(imageShader) }}); |
| 218 |
paintOptions.setColorFilters({{ fColorFilter }}); |
| 219 |
paintOptions.setBlendModes(kBlendModes); |
| 220 |
|
| 221 |
paintOptions.priv().buildCombinations(keyContext, |
| 222 |
DrawTypeFlags::kSimpleShape, |
| 223 |
/* withPrimitiveBlender= */ false, |
| 224 |
Coverage::kSingleChannel, |
| 225 |
renderPassDesc, |
| 226 |
processCombination); |
| 227 |
} |
| 228 |
|
| 229 |
sk_sp<PrecompileColorFilter> fColorFilter; |
| 230 |
}; |
| 231 |
|
| 232 |
sk_sp<PrecompileImageFilter> PrecompileImageFilters::ColorFilter( |
| 233 |
sk_sp<PrecompileColorFilter> colorFilter, |
| 234 |
sk_sp<PrecompileImageFilter> input) { |
| 235 |
if (colorFilter && input) { |
| 236 |
sk_sp<PrecompileColorFilter> inputCF = input->priv().isColorFilterNode(); |
| 237 |
if (inputCF) { |
| 238 |
colorFilter = colorFilter->makeComposed(std::move(inputCF)); |
| 239 |
input = sk_ref_sp(input->priv().getInput(0)); |
| 240 |
} |
| 241 |
} |
| 242 |
|
| 243 |
sk_sp<PrecompileImageFilter> filter = std::move(input); |
| 244 |
if (colorFilter) { |
| 245 |
filter = sk_make_sp<PrecompileColorFilterImageFilter>(std::move(colorFilter), |
| 246 |
std::move(filter)); |
| 247 |
} |
| 248 |
return filter; |
| 249 |
} |
| 250 |
|
| 251 |
//-------------------------------------------------------------------------------------------------- |
| 252 |
class PrecompileDisplacementMapImageFilter : public PrecompileImageFilter { |
| 253 |
public: |
| 254 |
PrecompileDisplacementMapImageFilter(SkSpan<sk_sp<PrecompileImageFilter>> inputs) |
| 255 |
: PrecompileImageFilter(std::move(inputs)) { |
| 256 |
} |
| 257 |
|
| 258 |
private: |
| 259 |
void onCreatePipelines( |
| 260 |
const KeyContext& keyContext, |
| 261 |
const RenderPassDesc& renderPassDesc, |
| 262 |
const PaintOptionsPriv::ProcessCombination& processCombination) const override { |
| 263 |
|
| 264 |
PaintOptions displacement; |
| 265 |
|
| 266 |
// For displacement imagefilters we know we don't have alpha-only textures and don't need |
| 267 |
// cubic filtering. |
| 268 |
sk_sp<PrecompileShader> imageShader = PrecompileShaders::Image( |
| 269 |
ImageShaderFlags::kNoAlphaNoCubic); |
| 270 |
|
| 271 |
displacement.setShaders({{ PrecompileShadersPriv::Displacement(imageShader, imageShader)}}); |
| 272 |
|
| 273 |
displacement.priv().buildCombinations(keyContext, |
| 274 |
DrawTypeFlags::kSimpleShape, |
| 275 |
/* withPrimitiveBlender= */ false, |
| 276 |
Coverage::kSingleChannel, |
| 277 |
renderPassDesc, |
| 278 |
processCombination); |
| 279 |
} |
| 280 |
}; |
| 281 |
|
| 282 |
sk_sp<PrecompileImageFilter> PrecompileImageFilters::DisplacementMap( |
| 283 |
sk_sp<PrecompileImageFilter> input) { |
| 284 |
return sk_make_sp<PrecompileDisplacementMapImageFilter>(SkSpan(&input, 1)); |
| 285 |
} |
| 286 |
|
| 287 |
//-------------------------------------------------------------------------------------------------- |
| 288 |
class PrecompileLightingImageFilter : public PrecompileImageFilter { |
| 289 |
public: |
| 290 |
PrecompileLightingImageFilter(SkSpan<sk_sp<PrecompileImageFilter>> inputs) |
| 291 |
: PrecompileImageFilter(std::move(inputs)) { |
| 292 |
} |
| 293 |
|
| 294 |
private: |
| 295 |
void onCreatePipelines( |
| 296 |
const KeyContext& keyContext, |
| 297 |
const RenderPassDesc& renderPassDesc, |
| 298 |
const PaintOptionsPriv::ProcessCombination& processCombination) const override { |
| 299 |
|
| 300 |
sk_sp<PrecompileShader> imageShader = PrecompileShaders::Image( |
| 301 |
ImageShaderFlags::kNoAlphaNoCubic); |
| 302 |
|
| 303 |
PaintOptions lighting; |
| 304 |
lighting.setShaders({{ PrecompileShadersPriv::Lighting(std::move(imageShader)) }}); |
| 305 |
|
| 306 |
lighting.priv().buildCombinations(keyContext, |
| 307 |
DrawTypeFlags::kSimpleShape, |
| 308 |
/* withPrimitiveBlender= */ false, |
| 309 |
Coverage::kSingleChannel, |
| 310 |
renderPassDesc, |
| 311 |
processCombination); |
| 312 |
} |
| 313 |
}; |
| 314 |
|
| 315 |
sk_sp<PrecompileImageFilter> PrecompileImageFilters::Lighting( |
| 316 |
sk_sp<PrecompileImageFilter> input) { |
| 317 |
return sk_make_sp<PrecompileLightingImageFilter>(SkSpan(&input, 1)); |
| 318 |
} |
| 319 |
|
| 320 |
//-------------------------------------------------------------------------------------------------- |
| 321 |
class PrecompileMatrixConvolutionImageFilter : public PrecompileImageFilter { |
| 322 |
public: |
| 323 |
PrecompileMatrixConvolutionImageFilter(SkSpan<sk_sp<PrecompileImageFilter>> inputs) |
| 324 |
: PrecompileImageFilter(std::move(inputs)) { |
| 325 |
} |
| 326 |
|
| 327 |
private: |
| 328 |
void onCreatePipelines( |
| 329 |
const KeyContext& keyContext, |
| 330 |
const RenderPassDesc& renderPassDesc, |
| 331 |
const PaintOptionsPriv::ProcessCombination& processCombination) const override { |
| 332 |
|
| 333 |
PaintOptions matrixConv; |
| 334 |
|
| 335 |
// For matrix convolution imagefilters we know we don't have alpha-only textures and don't |
| 336 |
// need cubic filtering. |
| 337 |
sk_sp<PrecompileShader> imageShader = PrecompileShaders::Image( |
| 338 |
ImageShaderFlags::kNoAlphaNoCubic); |
| 339 |
|
| 340 |
matrixConv.setShaders({{ PrecompileShadersPriv::MatrixConvolution(imageShader) }}); |
| 341 |
|
| 342 |
matrixConv.priv().buildCombinations(keyContext, |
| 343 |
DrawTypeFlags::kSimpleShape, |
| 344 |
/* withPrimitiveBlender= */ false, |
| 345 |
Coverage::kSingleChannel, |
| 346 |
renderPassDesc, |
| 347 |
processCombination); |
| 348 |
} |
| 349 |
}; |
| 350 |
|
| 351 |
sk_sp<PrecompileImageFilter> PrecompileImageFilters::MatrixConvolution( |
| 352 |
sk_sp<PrecompileImageFilter> input) { |
| 353 |
return sk_make_sp<PrecompileMatrixConvolutionImageFilter>(SkSpan(&input, 1)); |
| 354 |
} |
| 355 |
|
| 356 |
//-------------------------------------------------------------------------------------------------- |
| 357 |
class PrecompileMorphologyImageFilter : public PrecompileImageFilter { |
| 358 |
public: |
| 359 |
PrecompileMorphologyImageFilter(SkSpan<sk_sp<PrecompileImageFilter>> inputs) |
| 360 |
: PrecompileImageFilter(std::move(inputs)) { |
| 361 |
} |
| 362 |
|
| 363 |
private: |
| 364 |
void onCreatePipelines( |
| 365 |
const KeyContext& keyContext, |
| 366 |
const RenderPassDesc& renderPassDesc, |
| 367 |
const PaintOptionsPriv::ProcessCombination& processCombination) const override { |
| 368 |
|
| 369 |
// For morphology imagefilters we know we don't have alpha-only textures and don't need |
| 370 |
// cubic filtering. |
| 371 |
sk_sp<PrecompileShader> imageShader = PrecompileShaders::Image( |
| 372 |
ImageShaderFlags::kNoAlphaNoCubic); |
| 373 |
|
| 374 |
{ |
| 375 |
PaintOptions sparse; |
| 376 |
|
| 377 |
static const SkBlendMode kBlendModes[] = { SkBlendMode::kSrc }; |
| 378 |
sparse.setShaders({{ PrecompileShadersPriv::SparseMorphology(imageShader) }}); |
| 379 |
sparse.setBlendModes(kBlendModes); |
| 380 |
|
| 381 |
sparse.priv().buildCombinations(keyContext, |
| 382 |
DrawTypeFlags::kSimpleShape, |
| 383 |
/* withPrimitiveBlender= */ false, |
| 384 |
Coverage::kSingleChannel, |
| 385 |
renderPassDesc, |
| 386 |
processCombination); |
| 387 |
} |
| 388 |
|
| 389 |
{ |
| 390 |
PaintOptions linear; |
| 391 |
|
| 392 |
static const SkBlendMode kBlendModes[] = { SkBlendMode::kSrcOver }; |
| 393 |
linear.setShaders({{ PrecompileShadersPriv::LinearMorphology(std::move(imageShader))}}); |
| 394 |
linear.setBlendModes(kBlendModes); |
| 395 |
|
| 396 |
linear.priv().buildCombinations(keyContext, |
| 397 |
DrawTypeFlags::kSimpleShape, |
| 398 |
/* withPrimitiveBlender= */ false, |
| 399 |
Coverage::kSingleChannel, |
| 400 |
renderPassDesc, |
| 401 |
processCombination); |
| 402 |
} |
| 403 |
} |
| 404 |
}; |
| 405 |
|
| 406 |
sk_sp<PrecompileImageFilter> PrecompileImageFilters::Morphology( |
| 407 |
sk_sp<PrecompileImageFilter> input) { |
| 408 |
return sk_make_sp<PrecompileMorphologyImageFilter>(SkSpan(&input, 1)); |
| 409 |
} |
| 410 |
|
| 411 |
} // namespace skgpu::graphite |