File: PrecompileShader.h

package info (click to toggle)
webkit2gtk 2.51.1-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 455,340 kB
  • sloc: cpp: 3,865,253; javascript: 197,710; ansic: 165,177; python: 49,241; asm: 21,868; ruby: 18,095; perl: 16,926; xml: 4,623; sh: 2,409; yacc: 2,356; java: 2,019; lex: 1,330; pascal: 372; makefile: 210
file content (267 lines) | stat: -rw-r--r-- 13,903 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/*
 * Copyright 2024 Google LLC
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#ifndef skgpu_graphite_precompile_PrecompileShader_DEFINED
#define skgpu_graphite_precompile_PrecompileShader_DEFINED

#include "include/gpu/graphite/precompile/PrecompileBase.h"

#include "include/core/SkBlendMode.h"
#include "include/core/SkImageInfo.h"
#include "include/effects/SkGradientShader.h"

class SkColorSpace;

namespace skgpu::graphite {

class PrecompileBlender;
class PrecompileColorFilter;
class PrecompileShaderPriv;

/** \class PrecompileShader
    This class corresponds to the SkShader class in the main API.
*/
class SK_API PrecompileShader : public PrecompileBase {
public:
    /**
     *  This is the Precompile correlate to SkShader::makeWithLocalMatrix. The actual matrix
     *  involved is abstracted away, except for whether or the not the matrix involves perspective
     *  so the correct generated shader variation is chosen.
     *  The PrecompileShaders::LocalMatrix factory can be used to generate a set of shaders
     *  that would've been generated via multiple makeWithLocalMatrix calls. That is, rather than
     *  performing:
     *     sk_sp<PrecompileShader> option1 = source1->makeWithLocalMatrix(false);
     *     sk_sp<PrecompileShader> option2 = source2->makeWithLocalMatrix(false);
     *  one could call:
     *     sk_sp<PrecompileShader> combinedOptions = LocalMatrix({ source1, source2 }, false);
     */
    sk_sp<PrecompileShader> makeWithLocalMatrix(bool isPerspective) const;

    /**
     *  This is the Precompile correlate to SkShader::makeWithColorFilter.
     *  The PrecompileShaders::ColorFilter factory can be used to generate a set of shaders that
     *  would've been generated via multiple makeWithColorFilter calls. That is, rather than
     *  performing:
     *     sk_sp<PrecompileShader> option1 = source->makeWithColorFilter(colorFilter1);
     *     sk_sp<PrecompileShader> option2 = source->makeWithColorFilter(colorFilter2);
     *  one could call:
     *     sk_sp<PrecompileShader> combinedOptions = ColorFilter({ source },
     *                                                           { colorFilter1, colorFilter2 });
     *  With an alternative use case one could also use the ColorFilter factory thusly:
     *     sk_sp<PrecompileShader> combinedOptions = ColorFilter({ source1, source2 },
     *                                                           { colorFilter });
     */
    sk_sp<PrecompileShader> makeWithColorFilter(sk_sp<PrecompileColorFilter>) const;

    /**
     *  This is the Precompile correlate to SkShader::makeWithWorkingColorSpace.
     *  The PrecompileShaders::WorkingColorSpace factory can be used to generate a set of shaders
     *  that would've been generated via multiple makeWithWorkingColorSpace calls. That is, rather
     *  than performing:
     *     sk_sp<PrecompileShader> option1 = source->makeWithWorkingColorSpace(colorSpace1);
     *     sk_sp<PrecompileShader> option2 = source->makeWithWorkingColorSpace(colorSpace2);
     *  one could call:
     *     sk_sp<PrecompileShader> combinedOptions = WorkingColorSpace({ source },
     *                                                                 { colorSpace1,
     *                                                                   colorSpace2 });
     *  With an alternative use case one could also use the WorkingColorSpace factory thusly:
     *     sk_sp<PrecompileShader> combinedOptions = WorkingColorSpace({ source1, source2 },
     *                                                                 { colorSpace });
     */
    sk_sp<PrecompileShader> makeWithWorkingColorSpace(sk_sp<SkColorSpace> inputCS,
                                                      sk_sp<SkColorSpace> outputCS=nullptr) const;

    // Provides access to functions that aren't part of the public API.
    PrecompileShaderPriv priv();
    const PrecompileShaderPriv priv() const;  // NOLINT(readability-const-return-type)

protected:
    friend class PrecompileShaderPriv;

    PrecompileShader() : PrecompileBase(Type::kShader) {}
    ~PrecompileShader() override;

    virtual bool isConstant(int /* desiredCombination */) const { return false; }

    virtual bool isALocalMatrixShader() const { return false; }
};

//--------------------------------------------------------------------------------------------------
// This is the Precompile correlate to the SkShaders namespace in the main API
namespace PrecompileShaders {
    // --- This block of eight matches the SkShaders factories in SkShader.h
    // Note that some of the details of the main API have been elided since they don't impact
    // the generated shader (e.g., the color parameter to the Color() factories).
    SK_API sk_sp<PrecompileShader> Empty();
    SK_API sk_sp<PrecompileShader> Color();
    SK_API sk_sp<PrecompileShader> Color(sk_sp<SkColorSpace>);
    SK_API sk_sp<PrecompileShader> Blend(SkSpan<const SkBlendMode> blendModes,
                                         SkSpan<const sk_sp<PrecompileShader>> dsts,
                                         SkSpan<const sk_sp<PrecompileShader>> srcs);
    SK_API sk_sp<PrecompileShader> Blend(SkSpan<const sk_sp<PrecompileBlender>> blenders,
                                         SkSpan<const sk_sp<PrecompileShader>> dsts,
                                         SkSpan<const sk_sp<PrecompileShader>> srcs);
    SK_API sk_sp<PrecompileShader> CoordClamp(SkSpan<const sk_sp<PrecompileShader>>);

    enum class ImageShaderFlags : uint16_t {
        kNone                 = 0,

        kCubicSampling        = 1 << 1,
        kIncludeAlphaOnly     = 1 << 2,

        kAll = kCubicSampling | kIncludeAlphaOnly,
        kExcludeCubic = kIncludeAlphaOnly,
        kNoAlphaNoCubic = kNone,
    };

    static constexpr SkTileMode kAllTileModes[] = {
        SkTileMode::kClamp,
        SkTileMode::kRepeat,
        SkTileMode::kMirror,
        SkTileMode::kDecal,
    };

    /**
        In the main Skia API ImageShaders are usually created via a SkImage::makeShader call.
        Since the SkImage used to create the ImageShader is unlikely to be present at precompilation
        time this entry point allows the equivalent precompilation program structure to be created.
        Note that this factory is for non-YUV SkImages, the YUVImage factory (below) should be used
        to represent the shading and sampling required for YUV images.

        @param shaderFlags Specify the sampling variations that will be precompiled.
        @param colorInfos a span of SkColorInfos to narrow down the combinations.
                          In general, the color info affects the swizzle and colorSpace
                          transformation of the final Pipeline.
        @param tileModes a span of SkTileModes to narrow down the combinations.
                         The default will generate all possible combinations. Passing an
                         empty SkSpan will eliminate the Shader-based sampling combinations.
        @return A precompile shader capturing the specified combinations
    */
    SK_API sk_sp<PrecompileShader> Image(ImageShaderFlags = ImageShaderFlags::kAll,
                                         SkSpan<const SkColorInfo> = {},
                                         SkSpan<const SkTileMode> = { kAllTileModes });

    /** DEPRECATED
     *  Use above version.
     */
    SK_API sk_sp<PrecompileShader> Image(SkSpan<const SkColorInfo> colorInfos,
                                         SkSpan<const SkTileMode> = { kAllTileModes });

    // As with the above Image call, raw ImageShaders are usually created via an
    // SkImage::makeRawShader call. The RawImage call allows the equivalent precompilation
    // program structure to be created without needing the SkImage.
    SK_API sk_sp<PrecompileShader> RawImage(ImageShaderFlags = ImageShaderFlags::kExcludeCubic,
                                            SkSpan<const SkColorInfo> = {},
                                            SkSpan<const SkTileMode> = { kAllTileModes });

    enum class YUVImageShaderFlags : uint16_t {
        kNone                       = 0,

        kHardwareSamplingNoSwizzle  = 1 << 1,
        kHardwareSampling           = 1 << 2,
        kShaderBasedSampling        = 1 << 3,
        kCubicSampling              = 1 << 4,

        kExcludeCubic = kHardwareSamplingNoSwizzle | kHardwareSampling | kShaderBasedSampling,
        kNoCubicNoNonSwizzledHW = kHardwareSamplingNoSwizzle | kShaderBasedSampling,
    };

    /**
        In the main Skia API, the specifics of the SkImage used for the SkImage::makeShader call
        can determine whether normal or YUV sampling is required. This entry point allows clients
        to specify that the future image will be a YUV image.

        @param shaderFlags Specify the sampling variations that will be precompiled.
                           In practice, YUV images are rarely cubic-sampling.
        @param colorInfos a span of SkColorInfos to narrow down the combinations.
                          In general, the color info affects the swizzle and colorSpace
                          transformation of the final Pipeline.
        @return A precompile shader capturing the specified combinations
    */
    SK_API sk_sp<PrecompileShader> YUVImage(
            YUVImageShaderFlags = YUVImageShaderFlags::kExcludeCubic,
            SkSpan<const SkColorInfo> = {});

    // --- This block of two matches the SkShaders factories in SkPerlinNoiseShader.h
    // Again, most of the details have been elided.
    SK_API sk_sp<PrecompileShader> MakeFractalNoise();
    SK_API sk_sp<PrecompileShader> MakeTurbulence();

    enum class GradientShaderFlags : uint16_t {
        kNone    = 0,

        kSmall   = 1 << 1,
        kMedium  = 1 << 2,
        kLarge   = 1 << 3,

        kAll     = kSmall | kMedium | kLarge,
        kNoLarge = kSmall | kMedium,
    };

    // --- This block of four matches all the factories in SkGradientShader (SkGradientShader.h)
    SK_API sk_sp<PrecompileShader> LinearGradient(
            GradientShaderFlags = GradientShaderFlags::kAll,
            SkGradientShader::Interpolation = SkGradientShader::Interpolation());
    SK_API sk_sp<PrecompileShader> RadialGradient(
            GradientShaderFlags = GradientShaderFlags::kAll,
            SkGradientShader::Interpolation = SkGradientShader::Interpolation());
    SK_API sk_sp<PrecompileShader> TwoPointConicalGradient(
            GradientShaderFlags = GradientShaderFlags::kAll,
            SkGradientShader::Interpolation = SkGradientShader::Interpolation());
    SK_API sk_sp<PrecompileShader> SweepGradient(
            GradientShaderFlags = GradientShaderFlags::kAll,
            SkGradientShader::Interpolation = SkGradientShader::Interpolation());

    // Normally, SkPicture shaders are only created via SkPicture::makeShader. Since the
    // SkPicture to be drawn, most likely, won't be available at precompilation time, this
    // entry point can be used to create a precompilation equivalent.
    // Note: this will precompile the program that draws the SkPicture. It, obviously, won't
    // precompile any SkPaints within the SkPicture.
    SK_API sk_sp<PrecompileShader> Picture();

    // Normally, LocalMatrixShaders are only created via SkShader::makeWithLocalMatrix.
    // However, in the combination API, clients may want to create a set of precompile
    // LocalMatrixShaders (i.e., pass an SkSpan to the factory function vs just creating a
    // single option). This entry point allows that use case.
    // Note: PrecompileShader::makeWithLocalMatrix() can still be used and works as expected.
    SK_API sk_sp<PrecompileShader> LocalMatrix(SkSpan<const sk_sp<PrecompileShader>> wrapped,
                                               bool isPerspective = false);

    // Normally, ColorFilterShaders are only created via SkShader::makeWithColorFilter.
    // However, in the combination API, clients may want to create a set of precompile
    // ColorFilterShaders (i.e., pass SkSpans to the factory function vs just creating a
    // single option). This entry point allows that use case.
    // Note: PrecompileShader::makeWithColorFilter can still be used and works as expected.
    SK_API sk_sp<PrecompileShader> ColorFilter(
            SkSpan<const sk_sp<PrecompileShader>> shaders,
            SkSpan<const sk_sp<PrecompileColorFilter>> colorFilters);

    // Normally, WorkingColorSpaceShaders are only created via SkShader::makeWithWorkingColorSpace.
    // However, in the combination API, clients may want to create a set of precompile
    // WorkingColorSpaceShaders (i.e., pass SkSpans to the factory function vs just creating a
    // single option). This entry point allows that use case.
    // Note: PrecompileShader::makeWithWorkingColorSpace can still be used and works as expected.

    // This variant creates the cross product of `inputSpaces` and `outputSpaces`.
    // An empty list is interpreted as matching either the dst color space (for `inputSpaces`) or
    // matching the input space (for `outputSpaces`).
    SK_API sk_sp<PrecompileShader> WorkingColorSpace(
            SkSpan<const sk_sp<PrecompileShader>> shaders,
            SkSpan<const sk_sp<SkColorSpace>> inputSpaces,
            SkSpan<const sk_sp<SkColorSpace>> outputSpaces = {});

    // This variant takes a span of input and output color spaces pairs explicitly.
    SK_API sk_sp<PrecompileShader> WorkingColorSpaceExplicit(
            SkSpan<const sk_sp<PrecompileShader>> shaders,
            SkSpan<const std::pair</*input =*/sk_sp<SkColorSpace>,
                                   /*output=*/sk_sp<SkColorSpace>>> inputAndOutputSpaces);

} // namespace PrecompileShaders

} // namespace skgpu::graphite

#endif // skgpu_graphite_precompile_PrecompileShader_DEFINED