1 /*
2  * Copyright 2021 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 SKSL_CONSTRUCTOR_DIAGONAL_MATRIX
9 #define SKSL_CONSTRUCTOR_DIAGONAL_MATRIX
10 
11 #include "include/private/SkSLDefines.h"
12 #include "src/sksl/SkSLContext.h"
13 #include "src/sksl/ir/SkSLConstructor.h"
14 #include "src/sksl/ir/SkSLExpression.h"
15 
16 #include <memory>
17 
18 namespace SkSL {
19 
20 /**
21  * Represents the construction of a diagonal matrix, such as `half3x3(n)`.
22  *
23  * These always contain exactly 1 scalar.
24  */
25 class ConstructorDiagonalMatrix final : public SingleArgumentConstructor {
26 public:
27     static constexpr Kind kExpressionKind = Kind::kConstructorDiagonalMatrix;
28 
ConstructorDiagonalMatrix(int offset,const Type & type,std::unique_ptr<Expression> arg)29     ConstructorDiagonalMatrix(int offset, const Type& type, std::unique_ptr<Expression> arg)
30         : INHERITED(offset, kExpressionKind, &type, std::move(arg))
31         , fZeroLiteral(offset, /*value=*/0.0f, &type.componentType()) {}
32 
33     static std::unique_ptr<Expression> Make(const Context& context,
34                                             int offset,
35                                             const Type& type,
36                                             std::unique_ptr<Expression> arg);
37 
clone()38     std::unique_ptr<Expression> clone() const override {
39         return std::make_unique<ConstructorDiagonalMatrix>(fOffset, this->type(),
40                                                            argument()->clone());
41     }
42 
43     const Expression* getConstantSubexpression(int n) const override;
44 
45 private:
46     const FloatLiteral fZeroLiteral;
47     using INHERITED = SingleArgumentConstructor;
48 };
49 
50 }  // namespace SkSL
51 
52 #endif
53