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 #include <string>
14 
15 namespace SkSL {
16 
17 /**
18  * Represents all of the ways that a fragment processor is sampled by its parent.
19  */
20 struct SampleUsage {
21     enum class Kind {
22         // Child is never sampled
23         kNone,
24         // Child is only sampled at the same coordinates as the parent
25         kPassThrough,
26         // Child is sampled with a matrix whose value is uniform
27         kUniformMatrix,
28         // Child is sampled using explicit coordinates
29         kExplicit,
30     };
31 
32     // Make a SampleUsage that corresponds to no sampling of the child at all
33     SampleUsage() = default;
34 
35     // Child is sampled with a matrix whose value is uniform (some expression only involving
36     // literals and uniform variables).
37     static SampleUsage UniformMatrix(std::string expression, bool hasPerspective = true) {
38         return SampleUsage(Kind::kUniformMatrix, std::move(expression), hasPerspective);
39     }
40 
ExplicitSampleUsage41     static SampleUsage Explicit() {
42         return SampleUsage(Kind::kExplicit, "", false);
43     }
44 
PassThroughSampleUsage45     static SampleUsage PassThrough() {
46         return SampleUsage(Kind::kPassThrough, "", false);
47     }
48 
49     SampleUsage merge(const SampleUsage& other);
50 
isSampledSampleUsage51     bool isSampled()       const { return fKind != Kind::kNone; }
isPassThroughSampleUsage52     bool isPassThrough()   const { return fKind == Kind::kPassThrough; }
isExplicitSampleUsage53     bool isExplicit()      const { return fKind == Kind::kExplicit; }
isUniformMatrixSampleUsage54     bool isUniformMatrix() const { return fKind == Kind::kUniformMatrix; }
55 
56     Kind fKind = Kind::kNone;
57     // The uniform expression representing the matrix, or empty for non-matrix sampling
58     std::string fExpression;
59     bool fHasPerspective = false;
60 
SampleUsageSampleUsage61     SampleUsage(Kind kind, std::string expression, bool hasPerspective)
62             : fKind(kind), fExpression(expression), fHasPerspective(hasPerspective) {
63         if (kind == Kind::kUniformMatrix) {
64             SkASSERT(!fExpression.empty());
65         } else {
66             SkASSERT(fExpression.empty() && !fHasPerspective);
67         }
68     }
69 
70     std::string constructor() const;
71 };
72 
73 }  // namespace SkSL
74 
75 #endif
76