| 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 #pragma once
6
7 #include <algorithm>
8 #include <cmath>
9 #include <limits>
10 #include <ostream>
11 #include <string>
12
13 #include "impeller/geometry/scalar.h"
14
15 namespace impeller {
16
17 template <class T>
18 struct TSize {
19 using Type = T;
20
21 Type width = {};
22 Type height = {};
23
24 constexpr TSize() {}
25
26 constexpr TSize(Type width, Type height) : width(width), height(height) {}
27
28 template <class U>
29 explicit constexpr TSize(const TSize<U>& other)
30 : TSize(static_cast<Type>(other.width), static_cast<Type>(other.height)) {
31 }
32
33 static constexpr TSize MakeWH(Type width, Type height) {
34 return TSize{width, height};
35 }
36
37 static constexpr TSize Infinite() {
38 return TSize{std::numeric_limits<Type>::max(),
39 std::numeric_limits<Type>::max()};
40 }
41
42 constexpr TSize operator*(Scalar scale) const {
43 return {width * scale, height * scale};
44 }
45
46 constexpr TSize operator/(Scalar scale) const {
47 return {static_cast<Scalar>(width) / scale,
48 static_cast<Scalar>(height) / scale};
49 }
50
51 constexpr TSize operator/(const TSize& s) const {
52 return {width / s.width, height / s.height};
53 }
54
55 constexpr bool operator==(const TSize& s) const {
56 return s.width == width && s.height == height;
57 }
58
59 constexpr bool operator!=(const TSize& s) const {
60 return s.width != width || s.height != height;
61 }
62
63 constexpr TSize operator+(const TSize& s) const {
64 return {width + s.width, height + s.height};
65 }
66
67 constexpr TSize operator-(const TSize& s) const {
68 return {width - s.width, height - s.height};
69 }
70
71 constexpr TSize Min(const TSize& o) const {
72 return {
73 std::min(width, o.width),
74 std::min(height, o.height),
75 };
76 }
77
78 constexpr TSize Max(const TSize& o) const {
79 return {
80 std::max(width, o.width),
81 std::max(height, o.height),
82 };
83 }
84
85 constexpr TSize Abs() const { return {std::fabs(width), std::fabs(height)}; }
86
87 constexpr TSize Floor() const {
88 return {std::floor(width), std::floor(height)};
89 }
90
91 constexpr TSize Ceil() const { return {std::ceil(width), std::ceil(height)}; }
92
93 constexpr TSize Round() const {
94 return {std::round(width), std::round(height)};
95 }
96
97 constexpr Type Area() const { return width * height; }
98
99 constexpr bool IsPositive() const { return width > 0 && height > 0; }
100
101 constexpr bool IsNegative() const { return width < 0 || height < 0; }
102
103 constexpr bool IsZero() const { return width == 0 || height == 0; }
104
105 constexpr bool IsEmpty() const { return IsNegative() || IsZero(); }
106
107 template <class U>
108 static constexpr TSize Ceil(const TSize<U>& other) {
109 return TSize{static_cast<Type>(std::ceil(other.width)),
110 static_cast<Type>(std::ceil(other.height))};
111 }
112
113 constexpr size_t MipCount() const {
114 constexpr size_t minimum_mip = 1u;
115 if (!IsPositive()) {
116 return minimum_mip;
117 }
118 size_t result = std::max(ceil(log2(width)), ceil(log2(height)));
119 return std::max(result, minimum_mip);
120 }
121 };
122
123 // RHS algebraic operations with arithmetic types.
124
125 template <class T, class U, class = std::enable_if_t<std::is_arithmetic_v<U>>>
126 constexpr TSize<T> operator*(U s, const TSize<T>& p) {
127 return p * s;
128 }
129
130 template <class T, class U, class = std::enable_if_t<std::is_arithmetic_v<U>>>
131 constexpr TSize<T> operator/(U s, const TSize<T>& p) {
132 return {static_cast<T>(s) / p.x, static_cast<T>(s) / p.y};
133 }
134
135 using Size = TSize<Scalar>;
136 using ISize = TSize<int64_t>;
137
138 static_assert(sizeof(Size) == 2 * sizeof(Scalar));
139
140 } // namespace impeller
141
142 namespace std {
143
144 template <class T>
145 inline std::ostream& operator<<(std::ostream& out,
146 const impeller::TSize<T>& s) {
147 out << "(" << s.width << ", " << s.height << ")";
148 return out;
149 }
150
151 } // namespace std
impeller::TSize::operator/
constexpr TSize operator/(Scalar scale) const
Definition: size.h:46
impeller::TSize::operator==
constexpr bool operator==(const TSize &s) const
Definition: size.h:55
impeller::Scalar
float Scalar
Definition: scalar.h:15
impeller::TSize::Ceil
constexpr TSize Ceil() const
Definition: size.h:91
impeller::TSize::Min
constexpr TSize Min(const TSize &o) const
Definition: size.h:71
impeller::TSize::TSize
constexpr TSize()
Definition: size.h:24
std::operator<<
std::ostream & operator<<(std::ostream &out, const impeller::Color &c)
Definition: color.h:945
impeller::operator*
constexpr Color operator*(T value, const Color &c)
Definition: color.h:895
impeller::TSize::operator+
constexpr TSize operator+(const TSize &s) const
Definition: size.h:63
impeller::TSize::Type
T Type
Definition: size.h:19
impeller::TSize
Definition: size.h:18
impeller::TSize::Round
constexpr TSize Round() const
Definition: size.h:93
impeller::TSize::IsNegative
constexpr bool IsNegative() const
Definition: size.h:101
impeller::TSize::TSize
constexpr TSize(const TSize< U > &other)
Definition: size.h:29
impeller::TSize::Max
constexpr TSize Max(const TSize &o) const
Definition: size.h:78
impeller::TSize::Abs
constexpr TSize Abs() const
Definition: size.h:85
impeller::TSize::IsZero
constexpr bool IsZero() const
Definition: size.h:103
impeller::TSize::operator/
constexpr TSize operator/(const TSize &s) const
Definition: size.h:51
impeller::TSize::width
Type width
Definition: size.h:21
scalar.h
impeller::TSize::operator!=
constexpr bool operator!=(const TSize &s) const
Definition: size.h:59
impeller::TSize::Ceil
static constexpr TSize Ceil(const TSize< U > &other)
Definition: size.h:108
impeller::TSize::Area
constexpr Type Area() const
Definition: size.h:97
std
Definition: comparable.h:98
impeller::operator/
constexpr Color operator/(T value, const Color &c)
Definition: color.h:900
impeller::TSize::Infinite
static constexpr TSize Infinite()
Definition: size.h:37
impeller::TSize::height
Type height
Definition: size.h:22
impeller::TSize::IsEmpty
constexpr bool IsEmpty() const
Definition: size.h:105
impeller::TSize::IsPositive
constexpr bool IsPositive() const
Definition: size.h:99
impeller::TSize::MipCount
constexpr size_t MipCount() const
Definition: size.h:113
impeller::TSize::Floor
constexpr TSize Floor() const
Definition: size.h:87
impeller::TSize::MakeWH
static constexpr TSize MakeWH(Type width, Type height)
Definition: size.h:33
impeller
Definition: aiks_context.cc:10
impeller::TSize::operator-
constexpr TSize operator-(const TSize &s) const
Definition: size.h:67
impeller::TSize::TSize
constexpr TSize(Type width, Type height)
Definition: size.h:26
impeller::TSize::operator*
constexpr TSize operator*(Scalar scale) const
Definition: size.h:42