Flutter Engine
The Flutter Engine
SkSLConstructorMatrixResize.cpp
Go to the documentation of this file.
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
9
12
13namespace SkSL {
14
15std::unique_ptr<Expression> ConstructorMatrixResize::Make(const Context& context,
17 const Type& type,
18 std::unique_ptr<Expression> arg) {
21 SkASSERT(arg->type().componentType().matches(type.componentType()));
22
23 // If the matrix isn't actually changing size, return it as-is.
24 if (type.rows() == arg->type().rows() && type.columns() == arg->type().columns()) {
25 return arg;
26 }
27
28 return std::make_unique<ConstructorMatrixResize>(pos, type, std::move(arg));
29}
30
31std::optional<double> ConstructorMatrixResize::getConstantValue(int n) const {
32 int rows = this->type().rows();
33 int row = n % rows;
34 int col = n / rows;
35
36 SkASSERT(col >= 0);
37 SkASSERT(row >= 0);
38 SkASSERT(col < this->type().columns());
39 SkASSERT(row < this->type().rows());
40
41 // GLSL resize matrices are of the form:
42 // |m m 0|
43 // |m m 0|
44 // |0 0 1|
45 // Where `m` is the matrix being wrapped, and other cells contain the identity matrix.
46
47 // Forward `getConstantValue` to the wrapped matrix if the position is in its bounds.
48 if (col < this->argument()->type().columns() && row < this->argument()->type().rows()) {
49 // Recalculate `n` in terms of the inner matrix's dimensions.
50 n = row + (col * this->argument()->type().rows());
51 return this->argument()->getConstantValue(n);
52 }
53
54 // Synthesize an identity matrix for out-of-bounds positions.
55 return (col == row) ? 1.0 : 0.0;
56}
57
58} // namespace SkSL
SkPoint pos
#define SkASSERT(cond)
Definition: SkAssert.h:116
GLenum type
static std::unique_ptr< Expression > Make(const Context &context, Position pos, const Type &type, std::unique_ptr< Expression > arg)
std::optional< double > getConstantValue(int n) const override
const Type & type() const
std::unique_ptr< Expression > & argument()
bool isAllowedInES2(const Context &context) const
virtual int rows() const
Definition: SkSLType.h:438
virtual const Type & componentType() const
Definition: SkSLType.h:404
virtual bool isMatrix() const
Definition: SkSLType.h:528
virtual int columns() const
Definition: SkSLType.h:429