xref: /aosp_15_r20/external/skia/include/gpu/graphite/precompile/PrecompileBase.h (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1*c8dee2aaSAndroid Build Coastguard Worker /*
2*c8dee2aaSAndroid Build Coastguard Worker  * Copyright 2024 Google LLC
3*c8dee2aaSAndroid Build Coastguard Worker  *
4*c8dee2aaSAndroid Build Coastguard Worker  * Use of this source code is governed by a BSD-style license that can be
5*c8dee2aaSAndroid Build Coastguard Worker  * found in the LICENSE file.
6*c8dee2aaSAndroid Build Coastguard Worker  */
7*c8dee2aaSAndroid Build Coastguard Worker 
8*c8dee2aaSAndroid Build Coastguard Worker #ifndef skgpu_graphite_precompile_PrecompileBase_DEFINED
9*c8dee2aaSAndroid Build Coastguard Worker #define skgpu_graphite_precompile_PrecompileBase_DEFINED
10*c8dee2aaSAndroid Build Coastguard Worker 
11*c8dee2aaSAndroid Build Coastguard Worker #include "include/core/SkRefCnt.h"
12*c8dee2aaSAndroid Build Coastguard Worker #include "include/core/SkSpan.h"
13*c8dee2aaSAndroid Build Coastguard Worker 
14*c8dee2aaSAndroid Build Coastguard Worker namespace skgpu::graphite {
15*c8dee2aaSAndroid Build Coastguard Worker 
16*c8dee2aaSAndroid Build Coastguard Worker class KeyContext;
17*c8dee2aaSAndroid Build Coastguard Worker class PaintParamsKeyBuilder;
18*c8dee2aaSAndroid Build Coastguard Worker class PipelineDataGatherer;
19*c8dee2aaSAndroid Build Coastguard Worker class PrecompileBasePriv;
20*c8dee2aaSAndroid Build Coastguard Worker 
21*c8dee2aaSAndroid Build Coastguard Worker /** \class PrecompileBase
22*c8dee2aaSAndroid Build Coastguard Worker     This is the base class for all the objects that can be attached to PaintOptions.
23*c8dee2aaSAndroid Build Coastguard Worker */
24*c8dee2aaSAndroid Build Coastguard Worker class SK_API PrecompileBase : public SkRefCnt {
25*c8dee2aaSAndroid Build Coastguard Worker public:
26*c8dee2aaSAndroid Build Coastguard Worker     enum class Type {
27*c8dee2aaSAndroid Build Coastguard Worker         kBlender,
28*c8dee2aaSAndroid Build Coastguard Worker         kColorFilter,
29*c8dee2aaSAndroid Build Coastguard Worker         kImageFilter,
30*c8dee2aaSAndroid Build Coastguard Worker         kMaskFilter,
31*c8dee2aaSAndroid Build Coastguard Worker         kShader,
32*c8dee2aaSAndroid Build Coastguard Worker     };
33*c8dee2aaSAndroid Build Coastguard Worker 
type()34*c8dee2aaSAndroid Build Coastguard Worker     Type type() const { return fType; }
35*c8dee2aaSAndroid Build Coastguard Worker 
36*c8dee2aaSAndroid Build Coastguard Worker     // Provides access to functions that aren't part of the public API.
37*c8dee2aaSAndroid Build Coastguard Worker     PrecompileBasePriv priv();
38*c8dee2aaSAndroid Build Coastguard Worker     const PrecompileBasePriv priv() const;  // NOLINT(readability-const-return-type)
39*c8dee2aaSAndroid Build Coastguard Worker 
40*c8dee2aaSAndroid Build Coastguard Worker protected:
PrecompileBase(Type type)41*c8dee2aaSAndroid Build Coastguard Worker     PrecompileBase(Type type) : fType(type) {}
42*c8dee2aaSAndroid Build Coastguard Worker 
numIntrinsicCombinations()43*c8dee2aaSAndroid Build Coastguard Worker     virtual int numIntrinsicCombinations() const { return 1; }
numChildCombinations()44*c8dee2aaSAndroid Build Coastguard Worker     virtual int numChildCombinations() const { return 1; }
45*c8dee2aaSAndroid Build Coastguard Worker 
46*c8dee2aaSAndroid Build Coastguard Worker     /** This call returns the number of combinations generated from this object and its childen.
47*c8dee2aaSAndroid Build Coastguard Worker 
48*c8dee2aaSAndroid Build Coastguard Worker         @return       number of precompilation combinations generated by this object
49*c8dee2aaSAndroid Build Coastguard Worker     */
numCombinations()50*c8dee2aaSAndroid Build Coastguard Worker     int numCombinations() const {
51*c8dee2aaSAndroid Build Coastguard Worker         return this->numIntrinsicCombinations() * this->numChildCombinations();
52*c8dee2aaSAndroid Build Coastguard Worker     }
53*c8dee2aaSAndroid Build Coastguard Worker 
54*c8dee2aaSAndroid Build Coastguard Worker     virtual void addToKey(const KeyContext&,
55*c8dee2aaSAndroid Build Coastguard Worker                           PaintParamsKeyBuilder*,
56*c8dee2aaSAndroid Build Coastguard Worker                           PipelineDataGatherer*,
57*c8dee2aaSAndroid Build Coastguard Worker                           int desiredCombination) const = 0;
58*c8dee2aaSAndroid Build Coastguard Worker 
59*c8dee2aaSAndroid Build Coastguard Worker     // This returns the desired option along with the child options.
60*c8dee2aaSAndroid Build Coastguard Worker     template<typename T>
61*c8dee2aaSAndroid Build Coastguard Worker     static std::pair<sk_sp<T>, int> SelectOption(SkSpan<const sk_sp<T>> options,
62*c8dee2aaSAndroid Build Coastguard Worker                                                  int desiredOption);
63*c8dee2aaSAndroid Build Coastguard Worker 
64*c8dee2aaSAndroid Build Coastguard Worker     // In general, derived classes should use AddToKey to select the desired child option from
65*c8dee2aaSAndroid Build Coastguard Worker     // a span and then have it added to the key with its reduced/nested child option.
66*c8dee2aaSAndroid Build Coastguard Worker     template<typename T>
67*c8dee2aaSAndroid Build Coastguard Worker     static void AddToKey(const KeyContext&,
68*c8dee2aaSAndroid Build Coastguard Worker                          PaintParamsKeyBuilder*,
69*c8dee2aaSAndroid Build Coastguard Worker                          PipelineDataGatherer*,
70*c8dee2aaSAndroid Build Coastguard Worker                          SkSpan<const sk_sp<T>> options,
71*c8dee2aaSAndroid Build Coastguard Worker                          int desiredOption);
72*c8dee2aaSAndroid Build Coastguard Worker 
73*c8dee2aaSAndroid Build Coastguard Worker private:
74*c8dee2aaSAndroid Build Coastguard Worker     friend class PrecompileBasePriv;
75*c8dee2aaSAndroid Build Coastguard Worker 
76*c8dee2aaSAndroid Build Coastguard Worker     friend class PaintOptions;          // for access to SelectOption
77*c8dee2aaSAndroid Build Coastguard Worker     friend class PrecompileBlenderList; // ""
78*c8dee2aaSAndroid Build Coastguard Worker 
79*c8dee2aaSAndroid Build Coastguard Worker     Type fType;
80*c8dee2aaSAndroid Build Coastguard Worker };
81*c8dee2aaSAndroid Build Coastguard Worker 
82*c8dee2aaSAndroid Build Coastguard Worker } // namespace skgpu::graphite
83*c8dee2aaSAndroid Build Coastguard Worker 
84*c8dee2aaSAndroid Build Coastguard Worker #endif // skgpu_graphite_precompile_PrecompileBase_DEFINED
85