| 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 skgpu_graphite_precompile_PrecompileShader_DEFINED |
| 9 |
#define skgpu_graphite_precompile_PrecompileShader_DEFINED |
| 10 |
|
| 11 |
#include "include/gpu/graphite/precompile/PrecompileBase.h" |
| 12 |
|
| 13 |
#include "include/core/SkBlendMode.h" |
| 14 |
#include "include/core/SkImageInfo.h" |
| 15 |
#include "include/effects/SkGradient.h" |
| 16 |
|
| 17 |
class SkColorSpace; |
| 18 |
|
| 19 |
namespace skgpu::graphite { |
| 20 |
|
| 21 |
class PrecompileBlender; |
| 22 |
class PrecompileColorFilter; |
| 23 |
class PrecompileShaderPriv; |
| 24 |
|
| 25 |
/** \class PrecompileShader |
| 26 |
This class corresponds to the SkShader class in the main API. |
| 27 |
*/ |
| 28 |
class SK_API PrecompileShader : public PrecompileBase { |
| 29 |
public: |
| 30 |
/** |
| 31 |
* This is the Precompile correlate to SkShader::makeWithLocalMatrix. The actual matrix |
| 32 |
* involved is abstracted away, except for whether or the not the matrix involves perspective |
| 33 |
* so the correct generated shader variation is chosen. |
| 34 |
* The PrecompileShaders::LocalMatrix factory can be used to generate a set of shaders |
| 35 |
* that would've been generated via multiple makeWithLocalMatrix calls. That is, rather than |
| 36 |
* performing: |
| 37 |
* sk_sp<PrecompileShader> option1 = source1->makeWithLocalMatrix(false); |
| 38 |
* sk_sp<PrecompileShader> option2 = source2->makeWithLocalMatrix(false); |
| 39 |
* one could call: |
| 40 |
* sk_sp<PrecompileShader> combinedOptions = LocalMatrix({ source1, source2 }, false); |
| 41 |
*/ |
| 42 |
sk_sp<PrecompileShader> makeWithLocalMatrix(bool isPerspective) const; |
| 43 |
|
| 44 |
/** |
| 45 |
* This is the Precompile correlate to SkShader::makeWithColorFilter. |
| 46 |
* The PrecompileShaders::ColorFilter factory can be used to generate a set of shaders that |
| 47 |
* would've been generated via multiple makeWithColorFilter calls. That is, rather than |
| 48 |
* performing: |
| 49 |
* sk_sp<PrecompileShader> option1 = source->makeWithColorFilter(colorFilter1); |
| 50 |
* sk_sp<PrecompileShader> option2 = source->makeWithColorFilter(colorFilter2); |
| 51 |
* one could call: |
| 52 |
* sk_sp<PrecompileShader> combinedOptions = ColorFilter({ source }, |
| 53 |
* { colorFilter1, colorFilter2 }); |
| 54 |
* With an alternative use case one could also use the ColorFilter factory thusly: |
| 55 |
* sk_sp<PrecompileShader> combinedOptions = ColorFilter({ source1, source2 }, |
| 56 |
* { colorFilter }); |
| 57 |
*/ |
| 58 |
sk_sp<PrecompileShader> makeWithColorFilter(sk_sp<PrecompileColorFilter>) const; |
| 59 |
|
| 60 |
/** |
| 61 |
* This is the Precompile correlate to SkShader::makeWithWorkingColorSpace. |
| 62 |
* The PrecompileShaders::WorkingColorSpace factory can be used to generate a set of shaders |
| 63 |
* that would've been generated via multiple makeWithWorkingColorSpace calls. That is, rather |
| 64 |
* than performing: |
| 65 |
* sk_sp<PrecompileShader> option1 = source->makeWithWorkingColorSpace(colorSpace1); |
| 66 |
* sk_sp<PrecompileShader> option2 = source->makeWithWorkingColorSpace(colorSpace2); |
| 67 |
* one could call: |
| 68 |
* sk_sp<PrecompileShader> combinedOptions = WorkingColorSpace({ source }, |
| 69 |
* { colorSpace1, |
| 70 |
* colorSpace2 }); |
| 71 |
* With an alternative use case one could also use the WorkingColorSpace factory thusly: |
| 72 |
* sk_sp<PrecompileShader> combinedOptions = WorkingColorSpace({ source1, source2 }, |
| 73 |
* { colorSpace }); |
| 74 |
*/ |
| 75 |
sk_sp<PrecompileShader> makeWithWorkingColorSpace(sk_sp<SkColorSpace> inputCS, |
| 76 |
sk_sp<SkColorSpace> outputCS=nullptr) const; |
| 77 |
|
| 78 |
// Provides access to functions that aren't part of the public API. |
| 79 |
PrecompileShaderPriv priv(); |
| 80 |
const PrecompileShaderPriv priv() const; // NOLINT(readability-const-return-type) |
| 81 |
|
| 82 |
protected: |
| 83 |
friend class PrecompileShaderPriv; |
| 84 |
|
| 85 |
PrecompileShader() : PrecompileBase(Type::kShader) {} |
| 86 |
~PrecompileShader() override; |
| 87 |
|
| 88 |
virtual bool isConstant(int /* desiredCombination */) const { return false; } |
| 89 |
|
| 90 |
virtual bool isALocalMatrixShader() const { return false; } |
| 91 |
}; |
| 92 |
|
| 93 |
//-------------------------------------------------------------------------------------------------- |
| 94 |
// This is the Precompile correlate to the SkShaders namespace in the main API |
| 95 |
namespace PrecompileShaders { |
| 96 |
// --- This block of eight matches the SkShaders factories in SkShader.h |
| 97 |
// Note that some of the details of the main API have been elided since they don't impact |
| 98 |
// the generated shader (e.g., the color parameter to the Color() factories). |
| 99 |
SK_API sk_sp<PrecompileShader> Empty(); |
| 100 |
SK_API sk_sp<PrecompileShader> Color(); |
| 101 |
SK_API sk_sp<PrecompileShader> Color(sk_sp<SkColorSpace>); |
| 102 |
SK_API sk_sp<PrecompileShader> Blend(SkSpan<const SkBlendMode> blendModes, |
| 103 |
SkSpan<const sk_sp<PrecompileShader>> dsts, |
| 104 |
SkSpan<const sk_sp<PrecompileShader>> srcs); |
| 105 |
SK_API sk_sp<PrecompileShader> Blend(SkSpan<const sk_sp<PrecompileBlender>> blenders, |
| 106 |
SkSpan<const sk_sp<PrecompileShader>> dsts, |
| 107 |
SkSpan<const sk_sp<PrecompileShader>> srcs); |
| 108 |
SK_API sk_sp<PrecompileShader> CoordClamp(SkSpan<const sk_sp<PrecompileShader>>); |
| 109 |
|
| 110 |
enum class ImageShaderFlags : uint16_t { |
| 111 |
kNone = 0, |
| 112 |
|
| 113 |
kCubicSampling = 1 << 1, |
| 114 |
kIncludeAlphaOnly = 1 << 2, |
| 115 |
|
| 116 |
kAll = kCubicSampling | kIncludeAlphaOnly, |
| 117 |
kExcludeCubic = kIncludeAlphaOnly, |
| 118 |
kNoAlphaNoCubic = kNone, |
| 119 |
}; |
| 120 |
|
| 121 |
static constexpr SkTileMode kAllTileModes[] = { |
| 122 |
SkTileMode::kClamp, |
| 123 |
SkTileMode::kRepeat, |
| 124 |
SkTileMode::kMirror, |
| 125 |
SkTileMode::kDecal, |
| 126 |
}; |
| 127 |
|
| 128 |
/** |
| 129 |
In the main Skia API ImageShaders are usually created via a SkImage::makeShader call. |
| 130 |
Since the SkImage used to create the ImageShader is unlikely to be present at precompilation |
| 131 |
time this entry point allows the equivalent precompilation program structure to be created. |
| 132 |
Note that this factory is for non-YUV SkImages, the YUVImage factory (below) should be used |
| 133 |
to represent the shading and sampling required for YUV images. |
| 134 |
|
| 135 |
@param shaderFlags Specify the sampling variations that will be precompiled. |
| 136 |
@param colorInfos a span of SkColorInfos to narrow down the combinations. |
| 137 |
In general, the color info affects the swizzle and colorSpace |
| 138 |
transformation of the final Pipeline. |
| 139 |
@param tileModes a span of SkTileModes to narrow down the combinations. |
| 140 |
The default will generate all possible combinations. Passing an |
| 141 |
empty SkSpan will eliminate the Shader-based sampling combinations. |
| 142 |
@return A precompile shader capturing the specified combinations |
| 143 |
*/ |
| 144 |
SK_API sk_sp<PrecompileShader> Image(ImageShaderFlags = ImageShaderFlags::kAll, |
| 145 |
SkSpan<const SkColorInfo> = {}, |
| 146 |
SkSpan<const SkTileMode> = { kAllTileModes }); |
| 147 |
|
| 148 |
/** DEPRECATED |
| 149 |
* Use above version. |
| 150 |
*/ |
| 151 |
SK_API sk_sp<PrecompileShader> Image(SkSpan<const SkColorInfo> colorInfos, |
| 152 |
SkSpan<const SkTileMode> = { kAllTileModes }); |
| 153 |
|
| 154 |
// As with the above Image call, raw ImageShaders are usually created via an |
| 155 |
// SkImage::makeRawShader call. The RawImage call allows the equivalent precompilation |
| 156 |
// program structure to be created without needing the SkImage. |
| 157 |
SK_API sk_sp<PrecompileShader> RawImage(ImageShaderFlags = ImageShaderFlags::kExcludeCubic, |
| 158 |
SkSpan<const SkColorInfo> = {}, |
| 159 |
SkSpan<const SkTileMode> = { kAllTileModes }); |
| 160 |
|
| 161 |
enum class YUVImageShaderFlags : uint16_t { |
| 162 |
kNone = 0, |
| 163 |
|
| 164 |
kHardwareSamplingNoSwizzle = 1 << 1, |
| 165 |
kHardwareSampling = 1 << 2, |
| 166 |
kShaderBasedSampling = 1 << 3, |
| 167 |
kCubicSampling = 1 << 4, |
| 168 |
|
| 169 |
kExcludeCubic = kHardwareSamplingNoSwizzle | kHardwareSampling | kShaderBasedSampling, |
| 170 |
kNoCubicNoNonSwizzledHW = kHardwareSamplingNoSwizzle | kShaderBasedSampling, |
| 171 |
}; |
| 172 |
|
| 173 |
/** |
| 174 |
In the main Skia API, the specifics of the SkImage used for the SkImage::makeShader call |
| 175 |
can determine whether normal or YUV sampling is required. This entry point allows clients |
| 176 |
to specify that the future image will be a YUV image. |
| 177 |
|
| 178 |
@param shaderFlags Specify the sampling variations that will be precompiled. |
| 179 |
In practice, YUV images are rarely cubic-sampling. |
| 180 |
@param colorInfos a span of SkColorInfos to narrow down the combinations. |
| 181 |
In general, the color info affects the swizzle and colorSpace |
| 182 |
transformation of the final Pipeline. |
| 183 |
@return A precompile shader capturing the specified combinations |
| 184 |
*/ |
| 185 |
SK_API sk_sp<PrecompileShader> YUVImage( |
| 186 |
YUVImageShaderFlags = YUVImageShaderFlags::kExcludeCubic, |
| 187 |
SkSpan<const SkColorInfo> = {}); |
| 188 |
|
| 189 |
// --- This block of two matches the SkShaders factories in SkPerlinNoiseShader.h |
| 190 |
// Again, most of the details have been elided. |
| 191 |
SK_API sk_sp<PrecompileShader> MakeFractalNoise(); |
| 192 |
SK_API sk_sp<PrecompileShader> MakeTurbulence(); |
| 193 |
|
| 194 |
enum class GradientShaderFlags : uint16_t { |
| 195 |
kNone = 0, |
| 196 |
|
| 197 |
kSmall = 1 << 1, |
| 198 |
kMedium = 1 << 2, |
| 199 |
kLarge = 1 << 3, |
| 200 |
|
| 201 |
kAll = kSmall | kMedium | kLarge, |
| 202 |
kNoLarge = kSmall | kMedium, |
| 203 |
}; |
| 204 |
|
| 205 |
// --- This block of four matches all the factories in SkGradientShader (SkGradient.h) |
| 206 |
SK_API sk_sp<PrecompileShader> LinearGradient( |
| 207 |
GradientShaderFlags = GradientShaderFlags::kAll, |
| 208 |
SkGradient::Interpolation = SkGradient::Interpolation()); |
| 209 |
SK_API sk_sp<PrecompileShader> RadialGradient( |
| 210 |
GradientShaderFlags = GradientShaderFlags::kAll, |
| 211 |
SkGradient::Interpolation = SkGradient::Interpolation()); |
| 212 |
SK_API sk_sp<PrecompileShader> TwoPointConicalGradient( |
| 213 |
GradientShaderFlags = GradientShaderFlags::kAll, |
| 214 |
SkGradient::Interpolation = SkGradient::Interpolation()); |
| 215 |
SK_API sk_sp<PrecompileShader> SweepGradient( |
| 216 |
GradientShaderFlags = GradientShaderFlags::kAll, |
| 217 |
SkGradient::Interpolation = SkGradient::Interpolation()); |
| 218 |
|
| 219 |
// Normally, SkPicture shaders are only created via SkPicture::makeShader. Since the |
| 220 |
// SkPicture to be drawn, most likely, won't be available at precompilation time, this |
| 221 |
// entry point can be used to create a precompilation equivalent. |
| 222 |
// Note: this will precompile the program that draws the SkPicture. It, obviously, won't |
| 223 |
// precompile any SkPaints within the SkPicture. |
| 224 |
SK_API sk_sp<PrecompileShader> Picture(); |
| 225 |
|
| 226 |
// Normally, LocalMatrixShaders are only created via SkShader::makeWithLocalMatrix. |
| 227 |
// However, in the combination API, clients may want to create a set of precompile |
| 228 |
// LocalMatrixShaders (i.e., pass an SkSpan to the factory function vs just creating a |
| 229 |
// single option). This entry point allows that use case. |
| 230 |
// Note: PrecompileShader::makeWithLocalMatrix() can still be used and works as expected. |
| 231 |
SK_API sk_sp<PrecompileShader> LocalMatrix(SkSpan<const sk_sp<PrecompileShader>> wrapped, |
| 232 |
bool isPerspective = false); |
| 233 |
|
| 234 |
// Normally, ColorFilterShaders are only created via SkShader::makeWithColorFilter. |
| 235 |
// However, in the combination API, clients may want to create a set of precompile |
| 236 |
// ColorFilterShaders (i.e., pass SkSpans to the factory function vs just creating a |
| 237 |
// single option). This entry point allows that use case. |
| 238 |
// Note: PrecompileShader::makeWithColorFilter can still be used and works as expected. |
| 239 |
SK_API sk_sp<PrecompileShader> ColorFilter( |
| 240 |
SkSpan<const sk_sp<PrecompileShader>> shaders, |
| 241 |
SkSpan<const sk_sp<PrecompileColorFilter>> colorFilters); |
| 242 |
|
| 243 |
// Normally, WorkingColorSpaceShaders are only created via SkShader::makeWithWorkingColorSpace. |
| 244 |
// However, in the combination API, clients may want to create a set of precompile |
| 245 |
// WorkingColorSpaceShaders (i.e., pass SkSpans to the factory function vs just creating a |
| 246 |
// single option). This entry point allows that use case. |
| 247 |
// Note: PrecompileShader::makeWithWorkingColorSpace can still be used and works as expected. |
| 248 |
|
| 249 |
// This variant creates the cross product of `inputSpaces` and `outputSpaces`. |
| 250 |
// An empty list is interpreted as matching either the dst color space (for `inputSpaces`) or |
| 251 |
// matching the input space (for `outputSpaces`). |
| 252 |
SK_API sk_sp<PrecompileShader> WorkingColorSpace( |
| 253 |
SkSpan<const sk_sp<PrecompileShader>> shaders, |
| 254 |
SkSpan<const sk_sp<SkColorSpace>> inputSpaces, |
| 255 |
SkSpan<const sk_sp<SkColorSpace>> outputSpaces = {}); |
| 256 |
|
| 257 |
// This variant takes a span of input and output color spaces pairs explicitly. |
| 258 |
SK_API sk_sp<PrecompileShader> WorkingColorSpaceExplicit( |
| 259 |
SkSpan<const sk_sp<PrecompileShader>> shaders, |
| 260 |
SkSpan<const std::pair</*input =*/sk_sp<SkColorSpace>, |
| 261 |
/*output=*/sk_sp<SkColorSpace>>> inputAndOutputSpaces); |
| 262 |
|
| 263 |
} // namespace PrecompileShaders |
| 264 |
|
| 265 |
} // namespace skgpu::graphite |
| 266 |
|
| 267 |
#endif // skgpu_graphite_precompile_PrecompileShader_DEFINED |