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