Flutter Engine
 
Loading...
Searching...
No Matches
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
16
17namespace 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
24template <class T>
25struct TSize {
26 using Type = T;
27
28 Type width = {};
30
31 constexpr TSize() {}
32
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
149template <class T, class U, class = std::enable_if_t<std::is_arithmetic_v<U>>>
150constexpr TSize<T> operator*(U s, const TSize<T>& p) {
151 return p * s;
152}
153
154template <class T, class U, class = std::enable_if_t<std::is_arithmetic_v<U>>>
155constexpr 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
163
164static_assert(sizeof(Size) == 2 * sizeof(Scalar));
165
166} // namespace impeller
167
168namespace std {
169
170template <class T>
171inline 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_
#define ONLY_ON_FLOAT_M(Modifiers, Return)
Definition point.h:21
float Scalar
Definition scalar.h:19
constexpr Color operator/(T value, const Color &c)
Definition color.h:914
TSize< Scalar > Size
Definition size.h:159
constexpr Color operator*(T value, const Color &c)
Definition color.h:909
TSize< int64_t > ISize64
Definition size.h:161
Definition ref_ptr.h:261
std::ostream & operator<<(std::ostream &out, const impeller::Arc &a)
Definition arc.h:141
constexpr TSize operator-() const
Definition size.h:88
constexpr TSize Floor() const
Definition size.h:110
constexpr TSize operator/(Scalar scale) const
Definition size.h:63
constexpr TSize Max(const TSize &o) const
Definition size.h:97
constexpr TSize(const TSize< U > &other)
Definition size.h:39
constexpr Type Area() const
Definition size.h:120
constexpr TSize Min(const TSize &o) const
Definition size.h:90
constexpr TSize operator*(Scalar scale) const
Definition size.h:52
constexpr TSize Round() const
Definition size.h:116
constexpr TSize Abs() const
Definition size.h:108
constexpr Type MinDimension() const
Definition size.h:104
constexpr Type MaxDimension() const
Definition size.h:106
static constexpr TSize Ceil(const TSize< U > &other)
Definition size.h:131
constexpr TSize()
Definition size.h:31
Type height
Definition size.h:29
constexpr bool operator==(const TSize &s) const
Definition size.h:72
constexpr bool IsSquare() const
Definition size.h:128
constexpr TSize(Type dimension)
Definition size.h:35
constexpr TSize operator+(const TSize &s) const
Definition size.h:80
Type width
Definition size.h:28
constexpr TSize Ceil() const
Definition size.h:114
TSize operator*=(U scale)
Definition size.h:57
constexpr TSize operator-(const TSize &s) const
Definition size.h:84
constexpr TSize(Type width, Type height)
Definition size.h:33
constexpr size_t MipCount() const
Return the mip count of the texture.
Definition size.h:137
IsFinite() const
Definition size.h:126
static constexpr TSize MakeWH(Type width, Type height)
Definition size.h:43
constexpr bool IsEmpty() const
Returns true if either of the width or height are 0, negative, or NaN.
Definition size.h:123
constexpr bool operator!=(const TSize &s) const
Definition size.h:76
constexpr TSize operator/(const TSize &s) const
Definition size.h:68
static constexpr TSize Infinite()
Definition size.h:47