xref: /aosp_15_r20/external/skia/include/private/SkSLSampleUsage.h (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1 /*
2  * Copyright 2020 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 SkSLSampleUsage_DEFINED
9 #define SkSLSampleUsage_DEFINED
10 
11 #include "include/core/SkTypes.h"
12 
13 namespace SkSL {
14 
15 /**
16  * Represents all of the ways that a fragment processor is sampled by its parent.
17  */
18 class SampleUsage {
19 public:
20     enum class Kind {
21         // Child is never sampled
22         kNone,
23         // Child is only sampled at the same coordinates as the parent
24         kPassThrough,
25         // Child is sampled with a matrix whose value is uniform
26         kUniformMatrix,
27         // Child is sampled with sk_FragCoord.xy
28         kFragCoord,
29         // Child is sampled using explicit coordinates
30         kExplicit,
31     };
32 
33     // Make a SampleUsage that corresponds to no sampling of the child at all
34     SampleUsage() = default;
35 
SampleUsage(Kind kind,bool hasPerspective)36     SampleUsage(Kind kind, bool hasPerspective) : fKind(kind), fHasPerspective(hasPerspective) {
37         if (kind != Kind::kUniformMatrix) {
38             SkASSERT(!fHasPerspective);
39         }
40     }
41 
42     // Child is sampled with a matrix whose value is uniform. The name is fixed.
UniformMatrix(bool hasPerspective)43     static SampleUsage UniformMatrix(bool hasPerspective) {
44         return SampleUsage(Kind::kUniformMatrix, hasPerspective);
45     }
46 
Explicit()47     static SampleUsage Explicit() {
48         return SampleUsage(Kind::kExplicit, false);
49     }
50 
PassThrough()51     static SampleUsage PassThrough() {
52         return SampleUsage(Kind::kPassThrough, false);
53     }
54 
FragCoord()55     static SampleUsage FragCoord() { return SampleUsage(Kind::kFragCoord, false); }
56 
57     bool operator==(const SampleUsage& that) const {
58         return fKind == that.fKind && fHasPerspective == that.fHasPerspective;
59     }
60 
61     bool operator!=(const SampleUsage& that) const { return !(*this == that); }
62 
63     // Arbitrary name used by all uniform sampling matrices
MatrixUniformName()64     static const char* MatrixUniformName() { return "matrix"; }
65 
66     SampleUsage merge(const SampleUsage& other);
67 
kind()68     Kind kind() const { return fKind; }
69 
hasPerspective()70     bool hasPerspective() const { return fHasPerspective; }
71 
isSampled()72     bool isSampled()       const { return fKind != Kind::kNone; }
isPassThrough()73     bool isPassThrough()   const { return fKind == Kind::kPassThrough; }
isExplicit()74     bool isExplicit()      const { return fKind == Kind::kExplicit; }
isUniformMatrix()75     bool isUniformMatrix() const { return fKind == Kind::kUniformMatrix; }
isFragCoord()76     bool isFragCoord()     const { return fKind == Kind::kFragCoord; }
77 
78 private:
79     Kind fKind = Kind::kNone;
80     bool fHasPerspective = false;  // Only valid if fKind is kUniformMatrix
81 };
82 
83 }  // namespace SkSL
84 
85 #endif
86