| 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 constexpr size_t MipCount() const {
138 constexpr size_t minimum_mip = 1u;
139 if (IsEmpty() || width <= 0 || height <= 0) {
140 return minimum_mip;
141 }
142 size_t result = std::min(log2(width), log2(height));
143 return std::max(result, minimum_mip);
144 }
145 };
146
147 // RHS algebraic operations with arithmetic types.
148
149 template <class T, class U, class = std::enable_if_t<std::is_arithmetic_v<U>>>
150 constexpr TSize<T> operator*(U s, const TSize<T>& p) {
151 return p * s;
152 }
153
154 template <class T, class U, class = std::enable_if_t<std::is_arithmetic_v<U>>>
155 constexpr TSize<T> operator/(U s, const TSize<T>& p) {
156 return {static_cast<T>(s) / p.width, static_cast<T>(s) / p.height};
157 }
158
159 using Size = TSize<Scalar>;
160 using ISize32 = TSize<int32_t>;
161 using ISize64 = TSize<int64_t>;
162 using ISize = ISize64;
163
164 static_assert(sizeof(Size) == 2 * sizeof(Scalar));
165
166 } // namespace impeller
167
168 namespace std {
169
170 template <class T>
171 inline std::ostream& operator<<(std::ostream& out,
172 const impeller::TSize<T>& s) {
173 out << "(" << s.width << ", " << s.height << ")";
174 return out;
175 }
176
177 } // namespace std
178
179 #endif // FLUTTER_IMPELLER_GEOMETRY_SIZE_H_
impeller
Definition: allocation.cc:12
impeller::Scalar
float Scalar
Definition: scalar.h:18
impeller::operator/
constexpr Color operator/(T value, const Color &c)
Definition: color.h:914
impeller::operator*
constexpr Color operator*(T value, const Color &c)
Definition: color.h:909
impeller::ISize64
TSize< int64_t > ISize64
Definition: size.h:161
std
Definition: comparable.h:95
std::operator<<
std::ostream & operator<<(std::ostream &out, const impeller::Color &c)
Definition: color.h:927
scalar.h
ONLY_ON_FLOAT_M
#define ONLY_ON_FLOAT_M(Modifiers, Return)
Definition: size.h:19
scale
const Scalar scale
Definition: stroke_path_geometry.cc:319
impeller::TSize
Definition: size.h:25
impeller::TSize::operator-
constexpr TSize operator-() const
Definition: size.h:88
impeller::TSize::Floor
constexpr TSize Floor() const
Definition: size.h:110
impeller::TSize::operator/
constexpr TSize operator/(Scalar scale) const
Definition: size.h:63
impeller::TSize::Max
constexpr TSize Max(const TSize &o) const
Definition: size.h:97
impeller::TSize::TSize
constexpr TSize(const TSize< U > &other)
Definition: size.h:39
impeller::TSize::Area
constexpr Type Area() const
Definition: size.h:120
impeller::TSize::Min
constexpr TSize Min(const TSize &o) const
Definition: size.h:90
impeller::TSize::Type
T Type
Definition: size.h:26
impeller::TSize::operator*
constexpr TSize operator*(Scalar scale) const
Definition: size.h:52
impeller::TSize::Round
constexpr TSize Round() const
Definition: size.h:116
impeller::TSize::Abs
constexpr TSize Abs() const
Definition: size.h:108
impeller::TSize::MinDimension
constexpr Type MinDimension() const
Definition: size.h:104
impeller::TSize::MaxDimension
constexpr Type MaxDimension() const
Definition: size.h:106
impeller::TSize::Ceil
static constexpr TSize Ceil(const TSize< U > &other)
Definition: size.h:131
impeller::TSize::TSize
constexpr TSize()
Definition: size.h:31
impeller::TSize::height
Type height
Definition: size.h:29
impeller::TSize::operator==
constexpr bool operator==(const TSize &s) const
Definition: size.h:72
impeller::TSize::IsSquare
constexpr bool IsSquare() const
Definition: size.h:128
impeller::TSize::TSize
constexpr TSize(Type dimension)
Definition: size.h:35
impeller::TSize::operator+
constexpr TSize operator+(const TSize &s) const
Definition: size.h:80
impeller::TSize::width
Type width
Definition: size.h:28
impeller::TSize::Ceil
constexpr TSize Ceil() const
Definition: size.h:114
impeller::TSize::operator*=
TSize operator*=(U scale)
Definition: size.h:57
impeller::TSize::operator-
constexpr TSize operator-(const TSize &s) const
Definition: size.h:84
impeller::TSize::TSize
constexpr TSize(Type width, Type height)
Definition: size.h:33
impeller::TSize::MipCount
constexpr size_t MipCount() const
Return the mip count of the texture.
Definition: size.h:137
impeller::TSize::IsFinite
IsFinite() const
Definition: size.h:126
impeller::TSize::MakeWH
static constexpr TSize MakeWH(Type width, Type height)
Definition: size.h:43
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::operator!=
constexpr bool operator!=(const TSize &s) const
Definition: size.h:76
impeller::TSize::operator/
constexpr TSize operator/(const TSize &s) const
Definition: size.h:68
impeller::TSize::Infinite
static constexpr TSize Infinite()
Definition: size.h:47