1 /* 2 * Copyright 2022 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 SkKeyHelpers_DEFINED 9 #define SkKeyHelpers_DEFINED 10 11 #ifdef SK_GRAPHITE_ENABLED 12 #include "experimental/graphite/include/Context.h" 13 #endif 14 15 #include "include/core/SkBlendMode.h" 16 #include "include/core/SkShader.h" 17 #include "include/core/SkTileMode.h" 18 19 enum class SkBackend : uint8_t; 20 class SkPaintParamsKey; 21 class SkPaintParamsKeyBuilder; 22 class SkShaderCodeDictionary; 23 class SkUniformBlock; 24 25 // The KeyHelpers can be used to manually construct an SkPaintParamsKey 26 27 namespace DepthStencilOnlyBlock { 28 29 void AddToKey(SkShaderCodeDictionary*, 30 SkBackend, 31 SkPaintParamsKeyBuilder*, 32 SkUniformBlock*); 33 #ifdef SK_DEBUG 34 void Dump(const SkPaintParamsKey&, int headerOffset); 35 #endif 36 37 } // namespace DepthStencilOnlyBlock 38 39 namespace SolidColorShaderBlock { 40 41 void AddToKey(SkShaderCodeDictionary*, 42 SkBackend, 43 SkPaintParamsKeyBuilder*, 44 SkUniformBlock*, 45 const SkColor4f&); 46 #ifdef SK_DEBUG 47 void Dump(const SkPaintParamsKey&, int headerOffset); 48 #endif 49 50 } // namespace SolidColorShaderBlock 51 52 // TODO: move this functionality to the SkLinearGradient, SkRadialGradient, etc classes 53 namespace GradientShaderBlocks { 54 55 struct GradientData { 56 // TODO: For the sprint we only support 4 stops in the gradients 57 static constexpr int kMaxStops = 4; 58 59 // This ctor is used during pre-compilation when we don't have enough information to 60 // extract uniform data. However, we must be able to provide enough data to make all the 61 // relevant decisions about which code snippets to use. 62 GradientData(SkShader::GradientType, 63 SkTileMode, 64 int numStops); 65 66 // This ctor is used when extracting information from PaintParams. It must provide 67 // enough data to generate the uniform data the selected code snippet will require. 68 GradientData(SkShader::GradientType, 69 SkPoint point0, SkPoint point1, 70 float radius0, float radius1, 71 SkTileMode, 72 int numStops, 73 SkColor4f* colors, 74 float* offsets); 75 76 bool operator==(const GradientData& rhs) const { 77 return fType == rhs.fType && 78 fPoints[0] == rhs.fPoints[0] && 79 fPoints[1] == rhs.fPoints[1] && 80 fRadii[0] == rhs.fRadii[0] && 81 fRadii[1] == rhs.fRadii[1] && 82 fTM == rhs.fTM && 83 fNumStops == rhs.fNumStops && 84 !memcmp(fColor4fs, rhs.fColor4fs, sizeof(fColor4fs)) && 85 !memcmp(fOffsets, rhs.fOffsets, sizeof(fOffsets)); 86 } 87 bool operator!=(const GradientData& rhs) const { return !(*this == rhs); } 88 89 SkShader::GradientType fType; 90 SkPoint fPoints[2]; 91 float fRadii[2]; 92 SkTileMode fTM; 93 int fNumStops; 94 SkColor4f fColor4fs[kMaxStops]; 95 float fOffsets[kMaxStops]; 96 }; 97 98 void AddToKey(SkShaderCodeDictionary*, 99 SkBackend, 100 SkPaintParamsKeyBuilder*, 101 SkUniformBlock*, 102 const GradientData&); 103 #ifdef SK_DEBUG 104 void Dump(const SkPaintParamsKey&, int headerOffset); 105 #endif 106 107 } // namespace GradientShaderBlocks 108 109 namespace ImageShaderBlock { 110 111 struct ImageData { 112 bool operator==(const ImageData& rhs) const { 113 return fTileModes[0] == rhs.fTileModes[0] && 114 fTileModes[1] == rhs.fTileModes[1]; 115 } 116 bool operator!=(const ImageData& rhs) const { return !(*this == rhs); } 117 118 // TODO: add the other image shader parameters that could impact code snippet selection 119 // (e.g., sampling options, subsetting, etc.) 120 SkTileMode fTileModes[2]; 121 }; 122 123 void AddToKey(SkShaderCodeDictionary*, 124 SkBackend, 125 SkPaintParamsKeyBuilder*, 126 SkUniformBlock*, 127 const ImageData&); 128 #ifdef SK_DEBUG 129 void Dump(const SkPaintParamsKey&, int headerOffset); 130 #endif 131 132 } // namespace ImageShaderBlock 133 134 namespace BlendShaderBlock { 135 136 struct BlendData { 137 SkShader* fDst; 138 SkShader* fSrc; 139 // TODO: add support for blenders 140 SkBlendMode fBM; 141 }; 142 143 void AddToKey(SkShaderCodeDictionary*, 144 SkBackend, 145 SkPaintParamsKeyBuilder*, 146 SkUniformBlock*, 147 const BlendData&); 148 #ifdef SK_DEBUG 149 void Dump(const SkPaintParamsKey&, int headerOffset); 150 #endif 151 152 } // namespace BlendShaderBlock 153 154 namespace BlendModeBlock { 155 156 void AddToKey(SkShaderCodeDictionary*, 157 SkBackend, 158 SkPaintParamsKeyBuilder*, 159 SkUniformBlock*, 160 SkBlendMode); 161 #ifdef SK_DEBUG 162 void Dump(const SkPaintParamsKey&, int headerOffset); 163 #endif 164 165 } // namespace BlendModeBlock 166 167 #ifdef SK_GRAPHITE_ENABLED 168 // Bridge between the combinations system and the SkPaintParamsKey 169 std::unique_ptr<SkPaintParamsKey> CreateKey(SkShaderCodeDictionary*, 170 SkBackend, 171 skgpu::ShaderCombo::ShaderType, 172 SkTileMode, 173 SkBlendMode); 174 #endif 175 176 #endif // SkKeyHelpers_DEFINED 177