| 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/PrecompileRuntimeEffect.h" |
| 9 |
|
| 10 |
#include "include/effects/SkRuntimeEffect.h" |
| 11 |
#include "include/gpu/graphite/precompile/PrecompileBase.h" |
| 12 |
#include "include/gpu/graphite/precompile/PrecompileBlender.h" |
| 13 |
#include "include/gpu/graphite/precompile/PrecompileColorFilter.h" |
| 14 |
#include "include/gpu/graphite/precompile/PrecompileShader.h" |
| 15 |
#include "include/private/base/SkTArray.h" |
| 16 |
#include "src/core/SkRuntimeEffectPriv.h" |
| 17 |
#include "src/gpu/graphite/KeyContext.h" |
| 18 |
#include "src/gpu/graphite/KeyHelpers.h" |
| 19 |
#include "src/gpu/graphite/PaintParams.h" |
| 20 |
#include "src/gpu/graphite/PaintParamsKey.h" |
| 21 |
#include "src/gpu/graphite/precompile/PrecompileBaseComplete.h" |
| 22 |
#include "src/gpu/graphite/precompile/PrecompileBasePriv.h" |
| 23 |
|
| 24 |
namespace skgpu::graphite { |
| 25 |
|
| 26 |
namespace { |
| 27 |
|
| 28 |
#ifdef SK_DEBUG |
| 29 |
|
| 30 |
bool precompilebase_is_valid_as_child(const PrecompileBase *child) { |
| 31 |
if (!child) { |
| 32 |
return true; |
| 33 |
} |
| 34 |
|
| 35 |
switch (child->type()) { |
| 36 |
case PrecompileBase::Type::kShader: |
| 37 |
case PrecompileBase::Type::kColorFilter: |
| 38 |
case PrecompileBase::Type::kBlender: |
| 39 |
return true; |
| 40 |
default: |
| 41 |
return false; |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
#endif // SK_DEBUG |
| 46 |
|
| 47 |
int num_options_in_set(const SkSpan<const sk_sp<PrecompileBase>>& optionSet) { |
| 48 |
int numOptions = 0; |
| 49 |
for (const sk_sp<PrecompileBase>& childOption : optionSet) { |
| 50 |
// A missing child will fall back to a passthrough object |
| 51 |
if (childOption) { |
| 52 |
numOptions += childOption->priv().numCombinations(); |
| 53 |
} else { |
| 54 |
++numOptions; |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
return numOptions; |
| 59 |
} |
| 60 |
|
| 61 |
// Convert from runtime effect type to precompile type |
| 62 |
PrecompileBase::Type to_precompile_type(SkRuntimeEffect::ChildType childType) { |
| 63 |
switch(childType) { |
| 64 |
case SkRuntimeEffect::ChildType::kShader: return PrecompileBase::Type::kShader; |
| 65 |
case SkRuntimeEffect::ChildType::kColorFilter: return PrecompileBase::Type::kColorFilter; |
| 66 |
case SkRuntimeEffect::ChildType::kBlender: return PrecompileBase::Type::kBlender; |
| 67 |
} |
| 68 |
SkUNREACHABLE; |
| 69 |
} |
| 70 |
|
| 71 |
bool children_are_valid(SkRuntimeEffect* effect, |
| 72 |
SkSpan<const PrecompileChildOptions> childOptions) { |
| 73 |
SkSpan<const SkRuntimeEffect::Child> childInfo = effect->children(); |
| 74 |
if (childOptions.size() != childInfo.size()) { |
| 75 |
return false; |
| 76 |
} |
| 77 |
|
| 78 |
for (size_t i = 0; i < childInfo.size(); ++i) { |
| 79 |
const PrecompileBase::Type expectedType = to_precompile_type(childInfo[i].type); |
| 80 |
for (const sk_sp<PrecompileBase>& childOption : childOptions[i]) { |
| 81 |
if (childOption && expectedType != childOption->type()) { |
| 82 |
return false; |
| 83 |
} |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
return true; |
| 88 |
} |
| 89 |
|
| 90 |
} // anonymous namespace |
| 91 |
|
| 92 |
template<typename T> |
| 93 |
class PrecompileRTEffect : public T { |
| 94 |
public: |
| 95 |
PrecompileRTEffect(sk_sp<SkRuntimeEffect> effect, |
| 96 |
SkSpan<const PrecompileChildOptions> childOptions) |
| 97 |
: fEffect(std::move(effect)) { |
| 98 |
fChildOptions.reserve(childOptions.size()); |
| 99 |
for (PrecompileChildOptions c : childOptions) { |
| 100 |
fChildOptions.push_back({ c.begin(), c.end() }); |
| 101 |
} |
| 102 |
|
| 103 |
fNumSlotCombinations.reserve(childOptions.size()); |
| 104 |
fNumChildCombinations = 1; |
| 105 |
for (const std::vector<sk_sp<PrecompileBase>>& optionSet : fChildOptions) { |
| 106 |
fNumSlotCombinations.push_back(num_options_in_set(optionSet)); |
| 107 |
fNumChildCombinations *= fNumSlotCombinations.back(); |
| 108 |
} |
| 109 |
|
| 110 |
SkASSERT(fChildOptions.size() == fEffect->children().size()); |
| 111 |
} |
| 112 |
|
| 113 |
private: |
| 114 |
int numChildCombinations() const override { return fNumChildCombinations; } |
| 115 |
|
| 116 |
void addToKey(const KeyContext& keyContext, int desiredCombination) const override { |
| 117 |
|
| 118 |
SkASSERT(desiredCombination < this->numCombinations()); |
| 119 |
|
| 120 |
SkSpan<const SkRuntimeEffect::Child> childInfo = fEffect->children(); |
| 121 |
|
| 122 |
if (!RuntimeEffectBlock::BeginBlock(keyContext, { fEffect })) { |
| 123 |
RuntimeEffectBlock::AddNoOpEffect(keyContext, fEffect.get()); |
| 124 |
return; |
| 125 |
} |
| 126 |
|
| 127 |
int remainingCombinations = desiredCombination; |
| 128 |
|
| 129 |
for (size_t rowIndex = 0; rowIndex < fChildOptions.size(); ++rowIndex) { |
| 130 |
const std::vector<sk_sp<PrecompileBase>>& slotOptions = fChildOptions[rowIndex]; |
| 131 |
int numSlotCombinations = fNumSlotCombinations[rowIndex]; |
| 132 |
|
| 133 |
const int slotOption = remainingCombinations % numSlotCombinations; |
| 134 |
remainingCombinations /= numSlotCombinations; |
| 135 |
|
| 136 |
auto [option, childOptions] = PrecompileBase::SelectOption( |
| 137 |
SkSpan<const sk_sp<PrecompileBase>>(slotOptions), |
| 138 |
slotOption); |
| 139 |
|
| 140 |
KeyContext childContext = keyContext.forRuntimeEffect(fEffect.get(), rowIndex); |
| 141 |
|
| 142 |
SkASSERT(precompilebase_is_valid_as_child(option.get())); |
| 143 |
if (option) { |
| 144 |
option->priv().addToKey(childContext, childOptions); |
| 145 |
} else { |
| 146 |
SkASSERT(childOptions == 0); |
| 147 |
|
| 148 |
// We don't have a child effect. Substitute in a no-op effect. |
| 149 |
switch (childInfo[rowIndex].type) { |
| 150 |
case SkRuntimeEffect::ChildType::kShader: |
| 151 |
// A missing shader returns transparent black |
| 152 |
SolidColorShaderBlock::AddBlock(childContext, SK_PMColor4fTRANSPARENT); |
| 153 |
break; |
| 154 |
|
| 155 |
case SkRuntimeEffect::ChildType::kColorFilter: |
| 156 |
// A "passthrough" shader returns the input color as-is. |
| 157 |
keyContext.paintParamsKeyBuilder()->addBlock( |
| 158 |
BuiltInCodeSnippetID::kPriorOutput); |
| 159 |
break; |
| 160 |
|
| 161 |
case SkRuntimeEffect::ChildType::kBlender: |
| 162 |
// A "passthrough" blender performs `blend_src_over(src, dest)`. |
| 163 |
AddFixedBlendMode(childContext, SkBlendMode::kSrcOver); |
| 164 |
break; |
| 165 |
} |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
RuntimeEffectBlock::HandleIntrinsics(keyContext, fEffect.get()); |
| 170 |
|
| 171 |
keyContext.paintParamsKeyBuilder()->endBlock(); |
| 172 |
} |
| 173 |
|
| 174 |
sk_sp<SkRuntimeEffect> fEffect; |
| 175 |
std::vector<std::vector<sk_sp<PrecompileBase>>> fChildOptions; |
| 176 |
skia_private::TArray<int> fNumSlotCombinations; |
| 177 |
int fNumChildCombinations; |
| 178 |
}; |
| 179 |
|
| 180 |
sk_sp<PrecompileShader> PrecompileRuntimeEffects::MakePrecompileShader( |
| 181 |
sk_sp<SkRuntimeEffect> effect, |
| 182 |
SkSpan<const PrecompileChildOptions> childOptions) { |
| 183 |
if (!effect || !effect->allowShader()) { |
| 184 |
return nullptr; |
| 185 |
} |
| 186 |
|
| 187 |
if (!children_are_valid(effect.get(), childOptions)) { |
| 188 |
return nullptr; |
| 189 |
} |
| 190 |
|
| 191 |
return sk_make_sp<PrecompileRTEffect<PrecompileShader>>(std::move(effect), childOptions); |
| 192 |
} |
| 193 |
|
| 194 |
sk_sp<PrecompileColorFilter> PrecompileRuntimeEffects::MakePrecompileColorFilter( |
| 195 |
sk_sp<SkRuntimeEffect> effect, |
| 196 |
SkSpan<const PrecompileChildOptions> childOptions) { |
| 197 |
if (!effect || !effect->allowColorFilter()) { |
| 198 |
return nullptr; |
| 199 |
} |
| 200 |
|
| 201 |
if (!children_are_valid(effect.get(), childOptions)) { |
| 202 |
return nullptr; |
| 203 |
} |
| 204 |
|
| 205 |
return sk_make_sp<PrecompileRTEffect<PrecompileColorFilter>>(std::move(effect), childOptions); |
| 206 |
} |
| 207 |
|
| 208 |
sk_sp<PrecompileBlender> PrecompileRuntimeEffects::MakePrecompileBlender( |
| 209 |
sk_sp<SkRuntimeEffect> effect, |
| 210 |
SkSpan<const PrecompileChildOptions> childOptions) { |
| 211 |
if (!effect || !effect->allowBlender()) { |
| 212 |
return nullptr; |
| 213 |
} |
| 214 |
|
| 215 |
if (!children_are_valid(effect.get(), childOptions)) { |
| 216 |
return nullptr; |
| 217 |
} |
| 218 |
|
| 219 |
return sk_make_sp<PrecompileRTEffect<PrecompileBlender>>(std::move(effect), childOptions); |
| 220 |
} |
| 221 |
|
| 222 |
} // namespace skgpu::graphite |