| 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 |
#ifndef SkKnownRuntimeEffects_DEFINED |
| 9 |
#define SkKnownRuntimeEffects_DEFINED |
| 10 |
|
| 11 |
#include "include/core/SkRefCnt.h" |
| 12 |
#include <cstdint> |
| 13 |
|
| 14 |
class SkRuntimeEffect; |
| 15 |
|
| 16 |
namespace SkKnownRuntimeEffects { |
| 17 |
|
| 18 |
// We allocate the keys in blocks in the order: |
| 19 |
// Skia builtins |
| 20 |
// Skia known runtime effects |
| 21 |
// First party client (i.e., Chrome and Android) known runtime effects |
| 22 |
// unknown runtime effects (on a first come, first served basis -> unstable) |
| 23 |
// |
| 24 |
// WARNING: If any of these values are changed, UniqueKeys that have stably-keyed effects |
| 25 |
// will need to be invalidated. |
| 26 |
// TODO(b/238759147): add a revision number that can drive the invalidation. |
| 27 |
static constexpr int kSkiaBuiltInReservedCnt = 500; |
| 28 |
static constexpr int kSkiaKnownRuntimeEffectsReservedCnt = 500; |
| 29 |
static constexpr int kUserDefinedKnownRuntimeEffectsReservedCnt = 100; |
| 30 |
|
| 31 |
static constexpr int kSkiaKnownRuntimeEffectsStart = kSkiaBuiltInReservedCnt; |
| 32 |
static constexpr int kSkiaKnownRuntimeEffectsEnd = kSkiaKnownRuntimeEffectsStart + |
| 33 |
kSkiaKnownRuntimeEffectsReservedCnt; |
| 34 |
|
| 35 |
static constexpr int kUserDefinedKnownRuntimeEffectsStart = kSkiaKnownRuntimeEffectsEnd; |
| 36 |
static constexpr int kUserDefinedKnownRuntimeEffectsEnd = |
| 37 |
kUserDefinedKnownRuntimeEffectsStart + kUserDefinedKnownRuntimeEffectsReservedCnt; |
| 38 |
|
| 39 |
static constexpr int kUnknownRuntimeEffectIDStart = kUserDefinedKnownRuntimeEffectsEnd; |
| 40 |
|
| 41 |
// All six 1DBlur* stable keys must be consecutive after 1DBlurBase and |
| 42 |
// there is no 1DBlur24 bc for large kernels we bin by a multiple of eight. |
| 43 |
// Similarly, all six 2DBlur* stable keys must be consecutive after 2DBlurBase and |
| 44 |
// there is no 2DBlur24 bc for large kernels we bin by a multiple of eight. |
| 45 |
// As for the macros: |
| 46 |
// M(X) is for standard entries |
| 47 |
// M1(X) is for helper values that should be skipped in a switch statement |
| 48 |
// M2(X,Y) is for entries that have an initializer (Y) |
| 49 |
#define SK_ALL_STABLEKEYS(M, M1, M2) \ |
| 50 |
M2(Invalid, Start) \ |
| 51 |
M1(1DBlurBase) \ |
| 52 |
M2(1DBlur4, 1DBlurBase) \ |
| 53 |
M(1DBlur8) \ |
| 54 |
M(1DBlur12) \ |
| 55 |
M(1DBlur16) \ |
| 56 |
M(1DBlur20) \ |
| 57 |
M(1DBlur28) \ |
| 58 |
M1(2DBlurBase) \ |
| 59 |
M2(2DBlur4, 2DBlurBase) \ |
| 60 |
M(2DBlur8) \ |
| 61 |
M(2DBlur12) \ |
| 62 |
M(2DBlur16) \ |
| 63 |
M(2DBlur20) \ |
| 64 |
M(2DBlur28) \ |
| 65 |
M(Blend) \ |
| 66 |
M(Decal) \ |
| 67 |
M(Displacement) \ |
| 68 |
M(Lighting) \ |
| 69 |
M(LinearMorphology) \ |
| 70 |
M(Magnifier) \ |
| 71 |
M(MatrixConvUniforms) \ |
| 72 |
M(MatrixConvTexSm) \ |
| 73 |
M(MatrixConvTexLg) \ |
| 74 |
M(Normal) \ |
| 75 |
M(SparseMorphology) \ |
| 76 |
M(Arithmetic) \ |
| 77 |
M(HighContrast) \ |
| 78 |
M(Lerp) \ |
| 79 |
M(Luma) \ |
| 80 |
M(Overdraw) |
| 81 |
|
| 82 |
// WARNING: If any of the existing values are changed, UniqueKeys that have stably-keyed effects |
| 83 |
// will need to be invalidated. (Adding new values to the end of the enum should be fine though.) |
| 84 |
// TODO(b/238759147): add a revision number that can drive the invalidation |
| 85 |
enum class StableKey : uint32_t { |
| 86 |
kStart = kSkiaKnownRuntimeEffectsStart, |
| 87 |
|
| 88 |
#define M(type) k##type, |
| 89 |
#define M1(type) k##type, |
| 90 |
#define M2(type, initializer) k##type = k##initializer, |
| 91 |
SK_ALL_STABLEKEYS(M, M1, M2) |
| 92 |
#undef M2 |
| 93 |
#undef M1 |
| 94 |
#undef M |
| 95 |
|
| 96 |
kLast = kOverdraw, |
| 97 |
}; |
| 98 |
|
| 99 |
static const int kStableKeyCnt = static_cast<int>(StableKey::kLast) - |
| 100 |
static_cast<int>(StableKey::kStart) + 1; |
| 101 |
|
| 102 |
// kStart cannot be allowed to be zero since that is used as the not-a-stable-key value in the |
| 103 |
// serialized runtime effects (c.f., SkRuntimeShader::flatten) |
| 104 |
static_assert(static_cast<uint32_t>(StableKey::kStart) != 0); |
| 105 |
|
| 106 |
static_assert(static_cast<int>(StableKey::kLast) < kSkiaKnownRuntimeEffectsEnd); |
| 107 |
|
| 108 |
// Is 'candidate' a viable StableKey value. Note that this includes the kInvalid value. |
| 109 |
bool IsSkiaKnownRuntimeEffect(int candidate); |
| 110 |
|
| 111 |
bool IsUserDefinedRuntimeEffect(int candidate); |
| 112 |
|
| 113 |
// This call provides a rough check on 'candidate's viability as a user-defined known |
| 114 |
// run-time effect stable key. Every use of this method should be followed up with a call to |
| 115 |
// ShaderCodeDictionary::isUserDefinedKnownRuntimeEffect - which provides tighter bounds. |
| 116 |
bool IsViableUserDefinedKnownRuntimeEffect(int candidate); |
| 117 |
|
| 118 |
// Validate 'candidate' before calling GetKnownRuntimeEffect |
| 119 |
sk_sp<SkRuntimeEffect> MaybeGetKnownRuntimeEffect(uint32_t candidate); |
| 120 |
|
| 121 |
const SkRuntimeEffect* GetKnownRuntimeEffect(StableKey); |
| 122 |
|
| 123 |
static_assert(static_cast<int>(StableKey::kInvalid) == static_cast<int>(StableKey::kStart)); |
| 124 |
|
| 125 |
static_assert(static_cast<int>(StableKey::k1DBlur4) == static_cast<int>(StableKey::k1DBlurBase)); |
| 126 |
static_assert(static_cast<int>(StableKey::k1DBlur8) == static_cast<int>(StableKey::k1DBlurBase)+1); |
| 127 |
static_assert(static_cast<int>(StableKey::k1DBlur12) == static_cast<int>(StableKey::k1DBlurBase)+2); |
| 128 |
static_assert(static_cast<int>(StableKey::k1DBlur16) == static_cast<int>(StableKey::k1DBlurBase)+3); |
| 129 |
static_assert(static_cast<int>(StableKey::k1DBlur20) == static_cast<int>(StableKey::k1DBlurBase)+4); |
| 130 |
static_assert(static_cast<int>(StableKey::k1DBlur28) == static_cast<int>(StableKey::k1DBlurBase)+5); |
| 131 |
|
| 132 |
static_assert(static_cast<int>(StableKey::k2DBlur4) == static_cast<int>(StableKey::k2DBlurBase)); |
| 133 |
static_assert(static_cast<int>(StableKey::k2DBlur8) == static_cast<int>(StableKey::k2DBlurBase)+1); |
| 134 |
static_assert(static_cast<int>(StableKey::k2DBlur12) == static_cast<int>(StableKey::k2DBlurBase)+2); |
| 135 |
static_assert(static_cast<int>(StableKey::k2DBlur16) == static_cast<int>(StableKey::k2DBlurBase)+3); |
| 136 |
static_assert(static_cast<int>(StableKey::k2DBlur20) == static_cast<int>(StableKey::k2DBlurBase)+4); |
| 137 |
static_assert(static_cast<int>(StableKey::k2DBlur28) == static_cast<int>(StableKey::k2DBlurBase)+5); |
| 138 |
|
| 139 |
} // namespace SkKnownRuntimeEffects |
| 140 |
|
| 141 |
#endif // SkKnownRuntimeEffects_DEFINED |