| Flutter Impeller |

size.h
Go to the documentation of this file.
1 // Copyright 2013 The Flutter Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #ifndef FLUTTER_IMPELLER_GEOMETRY_SIZE_H_
6 #define FLUTTER_IMPELLER_GEOMETRY_SIZE_H_
7
8 #include <algorithm>
9 #include <cmath>
10 #include <cstdint>
11 #include <limits>
12 #include <ostream>
13 #include <string>
14
15 #include "impeller/geometry/scalar.h"
16
17 namespace impeller {
18
19 #define ONLY_ON_FLOAT_M(Modifiers, Return) \
20 template <typename U = T> \
21 Modifiers std::enable_if_t<std::is_floating_point_v<U>, Return>
22 #define ONLY_ON_FLOAT(Return) DL_ONLY_ON_FLOAT_M(, Return)
23
24 template <class T>
25 struct TSize {
26 using Type = T;
27
28 Type width = {};
29 Type height = {};
30
31 constexpr TSize() {}
32
33 constexpr TSize(Type width, Type height) : width(width), height(height) {}
34
35 constexpr explicit TSize(Type dimension)
36 : width(dimension), height(dimension) {}
37
38 template <class U>
39 explicit constexpr TSize(const TSize<U>& other)
40 : TSize(static_cast<Type>(other.width), static_cast<Type>(other.height)) {
41 }
42
43 static constexpr TSize MakeWH(Type width, Type height) {
44 return TSize{width, height};
45 }
46
47 static constexpr TSize Infinite() {
48 return TSize{std::numeric_limits<Type>::max(),
49 std::numeric_limits<Type>::max()};
50 }
51
52 constexpr TSize operator*(Scalar scale) const {
53 return {width * scale, height * scale};
54 }
55
56 template <class U, class = std::enable_if_t<std::is_arithmetic_v<U>>>
57 inline TSize operator*=(U scale) {
58 width *= static_cast<Type>(scale);
59 height *= static_cast<Type>(scale);
60 return *this;
61 }
62
63 constexpr TSize operator/(Scalar scale) const {
64 return {static_cast<Scalar>(width) / scale,
65 static_cast<Scalar>(height) / scale};
66 }
67
68 constexpr TSize operator/(const TSize& s) const {
69 return {width / s.width, height / s.height};
70 }
71
72 constexpr bool operator==(const TSize& s) const {
73 return s.width == width && s.height == height;
74 }
75
76 constexpr bool operator!=(const TSize& s) const {
77 return s.width != width || s.height != height;
78 }
79
80 constexpr TSize operator+(const TSize& s) const {
81 return {width + s.width, height + s.height};
82 }
83
84 constexpr TSize operator-(const TSize& s) const {
85 return {width - s.width, height - s.height};
86 }
87
88 constexpr TSize operator-() const { return {-width, -height}; }
89
90 constexpr TSize Min(const TSize& o) const {
91 return {
92 std::min(width, o.width),
93 std::min(height, o.height),
94 };
95 }
96
97 constexpr TSize Max(const TSize& o) const {
98 return {
99 std::max(width, o.width),
100 std::max(height, o.height),
101 };
102 }
103
104 constexpr Type MinDimension() const { return std::min(width, height); }
105
106 constexpr Type MaxDimension() const { return std::max(width, height); }
107
108 constexpr TSize Abs() const { return {std::fabs(width), std::fabs(height)}; }
109
110 constexpr TSize Floor() const {
111 return {std::floor(width), std::floor(height)};
112 }
113
114 constexpr TSize Ceil() const { return {std::ceil(width), std::ceil(height)}; }
115
116 constexpr TSize Round() const {
117 return {std::round(width), std::round(height)};
118 }
119
120 constexpr Type Area() const { return width * height; }
121
122 /// Returns true if either of the width or height are 0, negative, or NaN.
123 constexpr bool IsEmpty() const { return !(width > 0 && height > 0); }
124
125 ONLY_ON_FLOAT_M(constexpr, bool)
126 IsFinite() const { return std::isfinite(width) && std::isfinite(height); }
127
128 constexpr bool IsSquare() const { return width == height; }
129
130 template <class U>
131 static constexpr TSize Ceil(const TSize<U>& other) {
132 return TSize{static_cast<Type>(std::ceil(other.width)),
133 static_cast<Type>(std::ceil(other.height))};
134 }
135
136 /// Return the mip count of the texture.
137 ///
138 /// Note: does not count the final 1x1 mip level, both for practical reasons
139 /// and to workaround driver bugs.
140 constexpr size_t MipCount() const {
141 constexpr size_t minimum_mip = 1u;
142 if (IsEmpty()) {
143 return minimum_mip;
144 }
145 size_t result = std::max(ceil(log2(width)), ceil(log2(height)));
146 // This check avoids creating 1x1 mip levels, which are both pointless
147 // and cause rendering problems on some Adreno GPUs.
148 // See:
149 // * https://github.com/flutter/flutter/issues/160441
150 // * https://github.com/flutter/flutter/issues/159876
151 // * https://github.com/flutter/flutter/issues/160587
152 if (result > 1) {
153 result -= 1;
154 }
155 return std::max(result, minimum_mip);
156 }
157 };
158
159 // RHS algebraic operations with arithmetic types.
160
161 template <class T, class U, class = std::enable_if_t<std::is_arithmetic_v<U>>>
162 constexpr TSize<T> operator*(U s, const TSize<T>& p) {
163 return p * s;
164 }
165
166 template <class T, class U, class = std::enable_if_t<std::is_arithmetic_v<U>>>
167 constexpr TSize<T> operator/(U s, const TSize<T>& p) {
168 return {static_cast<T>(s) / p.width, static_cast<T>(s) / p.height};
169 }
170
171 using Size = TSize<Scalar>;
172 using ISize32 = TSize<int32_t>;
173 using ISize64 = TSize<int64_t>;
174 using ISize = ISize64;
175
176 static_assert(sizeof(Size) == 2 * sizeof(Scalar));
177
178 } // namespace impeller
179
180 namespace std {
181
182 template <class T>
183 inline std::ostream& operator<<(std::ostream& out,
184 const impeller::TSize<T>& s) {
185 out << "(" << s.width << ", " << s.height << ")";
186 return out;
187 }
188
189 } // namespace std
190
191 #endif // FLUTTER_IMPELLER_GEOMETRY_SIZE_H_
impeller::TSize::operator/
constexpr TSize operator/(Scalar scale) const
Definition: size.h:63
impeller::TSize::operator==
constexpr bool operator==(const TSize &s) const
Definition: size.h:72
impeller::Scalar
float Scalar
Definition: scalar.h:18
impeller::TSize::IsFinite
IsFinite() const
Definition: size.h:126
impeller::TSize::IsSquare
constexpr bool IsSquare() const
Definition: size.h:128
impeller::TSize::MinDimension
constexpr Type MinDimension() const
Definition: size.h:104
impeller::TSize::Ceil
constexpr TSize Ceil() const
Definition: size.h:114
impeller::TSize::Min
constexpr TSize Min(const TSize &o) const
Definition: size.h:90
impeller::TSize::MaxDimension
constexpr Type MaxDimension() const
Definition: size.h:106
impeller::TSize::operator*=
TSize operator*=(U scale)
Definition: size.h:57
impeller::TSize::TSize
constexpr TSize()
Definition: size.h:31
std::operator<<
std::ostream & operator<<(std::ostream &out, const impeller::Color &c)
Definition: color.h:926
impeller::operator*
constexpr Color operator*(T value, const Color &c)
Definition: color.h:908
impeller::TSize::operator+
constexpr TSize operator+(const TSize &s) const
Definition: size.h:80
impeller::TSize< Scalar >::Type
Scalar Type
Definition: size.h:26
impeller::TSize
Definition: size.h:25
impeller::TSize::Round
constexpr TSize Round() const
Definition: size.h:116
impeller::TSize::TSize
constexpr TSize(const TSize< U > &other)
Definition: size.h:39
impeller::ISize64
TSize< int64_t > ISize64
Definition: size.h:173
impeller::TSize::operator-
constexpr TSize operator-() const
Definition: size.h:88
impeller::TSize::Max
constexpr TSize Max(const TSize &o) const
Definition: size.h:97
impeller::TSize::Abs
constexpr TSize Abs() const
Definition: size.h:108
impeller::TSize::operator/
constexpr TSize operator/(const TSize &s) const
Definition: size.h:68
impeller::TSize::width
Type width
Definition: size.h:28
scalar.h
impeller::TSize::operator!=
constexpr bool operator!=(const TSize &s) const
Definition: size.h:76
impeller::TSize::Ceil
static constexpr TSize Ceil(const TSize< U > &other)
Definition: size.h:131
impeller::TSize::Area
constexpr Type Area() const
Definition: size.h:120
std
Definition: comparable.h:95
scale
const Scalar scale
Definition: stroke_path_geometry.cc:316
impeller::operator/
constexpr Color operator/(T value, const Color &c)
Definition: color.h:913
impeller::TSize::Infinite
static constexpr TSize Infinite()
Definition: size.h:47
impeller::TSize::height
Type height
Definition: size.h:29
impeller::TSize::IsEmpty
constexpr bool IsEmpty() const
Returns true if either of the width or height are 0, negative, or NaN.
Definition: size.h:123
impeller::TSize::MipCount
constexpr size_t MipCount() const
Definition: size.h:140
impeller::TSize::Floor
constexpr TSize Floor() const
Definition: size.h:110
impeller::TSize::MakeWH
static constexpr TSize MakeWH(Type width, Type height)
Definition: size.h:43
ONLY_ON_FLOAT_M
#define ONLY_ON_FLOAT_M(Modifiers, Return)
Definition: size.h:19
impeller
Definition: allocation.cc:12
impeller::TSize::operator-
constexpr TSize operator-(const TSize &s) const
Definition: size.h:84
impeller::TSize::TSize
constexpr TSize(Type dimension)
Definition: size.h:35
impeller::TSize::TSize
constexpr TSize(Type width, Type height)
Definition: size.h:33
impeller::TSize::operator*
constexpr TSize operator*(Scalar scale) const
Definition: size.h:52