Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 1af5cef

Browse files
authored
[Impeller] Make storage sizes typed. (#53700)
This is similar to our handling of degrees and radians or really any of the stuff in chrono. Storage is always in bytes. Before being displayed, it can be converted into any of unit that makes the most sense. Conversions are non-truncating and everything is constexpr safe.
1 parent 84769c8 commit 1af5cef

14 files changed

+372
-32
lines changed

ci/licenses_golden/excluded_files

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@
133133
../../../flutter/impeller/aiks/canvas_unittests.cc
134134
../../../flutter/impeller/aiks/testing
135135
../../../flutter/impeller/base/README.md
136+
../../../flutter/impeller/base/allocation_size_unittests.cc
136137
../../../flutter/impeller/base/base_unittests.cc
137138
../../../flutter/impeller/compiler/README.md
138139
../../../flutter/impeller/compiler/compiler_unittests.cc

ci/licenses_golden/licenses_flutter

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41853,6 +41853,8 @@ ORIGIN: ../../../flutter/impeller/aiks/picture_recorder.cc + ../../../flutter/LI
4185341853
ORIGIN: ../../../flutter/impeller/aiks/picture_recorder.h + ../../../flutter/LICENSE
4185441854
ORIGIN: ../../../flutter/impeller/base/allocation.cc + ../../../flutter/LICENSE
4185541855
ORIGIN: ../../../flutter/impeller/base/allocation.h + ../../../flutter/LICENSE
41856+
ORIGIN: ../../../flutter/impeller/base/allocation_size.cc + ../../../flutter/LICENSE
41857+
ORIGIN: ../../../flutter/impeller/base/allocation_size.h + ../../../flutter/LICENSE
4185641858
ORIGIN: ../../../flutter/impeller/base/backend_cast.h + ../../../flutter/LICENSE
4185741859
ORIGIN: ../../../flutter/impeller/base/comparable.cc + ../../../flutter/LICENSE
4185841860
ORIGIN: ../../../flutter/impeller/base/comparable.h + ../../../flutter/LICENSE
@@ -44717,6 +44719,8 @@ FILE: ../../../flutter/impeller/aiks/picture_recorder.cc
4471744719
FILE: ../../../flutter/impeller/aiks/picture_recorder.h
4471844720
FILE: ../../../flutter/impeller/base/allocation.cc
4471944721
FILE: ../../../flutter/impeller/base/allocation.h
44722+
FILE: ../../../flutter/impeller/base/allocation_size.cc
44723+
FILE: ../../../flutter/impeller/base/allocation_size.h
4472044724
FILE: ../../../flutter/impeller/base/backend_cast.h
4472144725
FILE: ../../../flutter/impeller/base/comparable.cc
4472244726
FILE: ../../../flutter/impeller/base/comparable.h

impeller/base/BUILD.gn

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ impeller_component("base") {
88
sources = [
99
"allocation.cc",
1010
"allocation.h",
11+
"allocation_size.cc",
12+
"allocation_size.h",
1113
"backend_cast.h",
1214
"comparable.cc",
1315
"comparable.h",
@@ -33,7 +35,10 @@ impeller_component("base") {
3335

3436
impeller_component("base_unittests") {
3537
testonly = true
36-
sources = [ "base_unittests.cc" ]
38+
sources = [
39+
"allocation_size_unittests.cc",
40+
"base_unittests.cc",
41+
]
3742
deps = [
3843
":base",
3944
"//flutter/testing",

impeller/base/allocation.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <memory>
1010

1111
#include "flutter/fml/mapping.h"
12+
#include "impeller/base/allocation_size.h"
1213

1314
namespace impeller {
1415

impeller/base/allocation_size.cc

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
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+
#include "impeller/base/allocation_size.h"
6+
7+
namespace impeller {
8+
9+
//
10+
11+
} // namespace impeller

impeller/base/allocation_size.h

Lines changed: 198 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,198 @@
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_BASE_ALLOCATION_SIZE_H_
6+
#define FLUTTER_IMPELLER_BASE_ALLOCATION_SIZE_H_
7+
8+
#include <cstddef>
9+
#include <cstdint>
10+
11+
namespace impeller {
12+
13+
enum class FromBytesTag { kFromBytes };
14+
15+
//------------------------------------------------------------------------------
16+
/// @brief Represents the size of an allocation in different units.
17+
///
18+
/// Refer to the typedefs for Bytes, KiloBytes, MegaBytes,
19+
/// Gigabytes, KibiBytes, MebiBytes, and GibiBytes below when using.
20+
///
21+
/// Storage and all operations are always on unsigned units of
22+
/// bytes.
23+
///
24+
/// @tparam Period The number of bytes in 1 unit of the allocation size.
25+
///
26+
template <size_t Period>
27+
class AllocationSize {
28+
public:
29+
//----------------------------------------------------------------------------
30+
/// @brief Create a zero allocation size.
31+
///
32+
constexpr AllocationSize() = default;
33+
34+
//----------------------------------------------------------------------------
35+
/// @brief Create an allocation size with the amount in the `Period`
36+
/// number of bytes.
37+
///
38+
/// @param[in] size The size in `Period` number of bytes.
39+
///
40+
explicit constexpr AllocationSize(double size) : bytes_(size * Period) {}
41+
42+
//----------------------------------------------------------------------------
43+
/// @brief Create an allocation size from another instance with a
44+
/// different period.
45+
///
46+
/// @param[in] other The other allocation size.
47+
///
48+
/// @tparam OtherPeriod The period of the other allocation.
49+
///
50+
template <size_t OtherPeriod>
51+
explicit constexpr AllocationSize(const AllocationSize<OtherPeriod>& other)
52+
: bytes_(other.GetByteSize()) {}
53+
54+
//----------------------------------------------------------------------------
55+
/// @brief Create an allocation size with the amount directly specified
56+
/// in bytes.
57+
///
58+
/// @param[in] byte_size The byte size.
59+
/// @param[in] tag A tag for this constructor.
60+
///
61+
constexpr AllocationSize(uint64_t byte_size, FromBytesTag)
62+
: bytes_(byte_size) {}
63+
64+
//----------------------------------------------------------------------------
65+
/// @return The byte size.
66+
///
67+
constexpr uint64_t GetByteSize() const { return bytes_; }
68+
69+
//----------------------------------------------------------------------------
70+
/// @return The number of `Periods` of bytes.
71+
///
72+
constexpr double GetSize() const {
73+
return GetByteSize() / static_cast<double>(Period);
74+
}
75+
76+
//----------------------------------------------------------------------------
77+
/// @brief Convert the allocation size from one unit to another.
78+
///
79+
/// Conversions are non-truncating.
80+
///
81+
/// @tparam AllocationSize The allocation size to convert to.
82+
///
83+
/// @return The new allocation size.
84+
///
85+
template <class AllocationSize>
86+
constexpr AllocationSize ConvertTo() {
87+
return AllocationSize{GetByteSize(), FromBytesTag::kFromBytes};
88+
}
89+
90+
// The following relational operators can be replaced with a defaulted
91+
// spaceship operator post C++20.
92+
93+
constexpr bool operator<(const AllocationSize& other) const {
94+
return bytes_ < other.bytes_;
95+
}
96+
97+
constexpr bool operator>(const AllocationSize& other) const {
98+
return bytes_ > other.bytes_;
99+
}
100+
101+
constexpr bool operator>=(const AllocationSize& other) const {
102+
return bytes_ >= other.bytes_;
103+
}
104+
105+
constexpr bool operator<=(const AllocationSize& other) const {
106+
return bytes_ <= other.bytes_;
107+
}
108+
109+
constexpr bool operator==(const AllocationSize& other) const {
110+
return bytes_ == other.bytes_;
111+
}
112+
113+
constexpr bool operator!=(const AllocationSize& other) const {
114+
return bytes_ != other.bytes_;
115+
}
116+
117+
// Explicit casts.
118+
119+
explicit constexpr operator bool() const { return bytes_ != 0u; }
120+
121+
// Arithmetic operators (overflows are caller error).
122+
123+
constexpr AllocationSize operator+(const AllocationSize& other) const {
124+
return AllocationSize(bytes_ + other.GetByteSize(),
125+
FromBytesTag::kFromBytes);
126+
}
127+
128+
constexpr AllocationSize operator-(const AllocationSize& other) const {
129+
return AllocationSize(bytes_ - other.GetByteSize(),
130+
FromBytesTag::kFromBytes);
131+
}
132+
133+
constexpr AllocationSize& operator+=(const AllocationSize& other) {
134+
bytes_ += other.GetByteSize();
135+
return *this;
136+
}
137+
138+
constexpr AllocationSize& operator-=(const AllocationSize& other) {
139+
bytes_ -= other.GetByteSize();
140+
return *this;
141+
}
142+
143+
private:
144+
uint64_t bytes_ = {};
145+
};
146+
147+
using Bytes = AllocationSize<1u>;
148+
149+
using KiloBytes = AllocationSize<1'000u>;
150+
using MegaBytes = AllocationSize<1'000u * 1'000u>;
151+
using GigaBytes = AllocationSize<1'000u * 1'000u * 1'000u>;
152+
153+
using KibiBytes = AllocationSize<1'024u>;
154+
using MebiBytes = AllocationSize<1'024u * 1'024u>;
155+
using GibiBytes = AllocationSize<1'024u * 1'024u * 1'024u>;
156+
157+
inline namespace allocation_size_literals {
158+
159+
// NOLINTNEXTLINE
160+
constexpr Bytes operator"" _bytes(unsigned long long int size) {
161+
return Bytes{static_cast<double>(size)};
162+
}
163+
164+
// NOLINTNEXTLINE
165+
constexpr KiloBytes operator"" _kb(unsigned long long int size) {
166+
return KiloBytes{static_cast<double>(size)};
167+
}
168+
169+
// NOLINTNEXTLINE
170+
constexpr MegaBytes operator"" _mb(unsigned long long int size) {
171+
return MegaBytes{static_cast<double>(size)};
172+
}
173+
174+
// NOLINTNEXTLINE
175+
constexpr GigaBytes operator"" _gb(unsigned long long int size) {
176+
return GigaBytes{static_cast<double>(size)};
177+
}
178+
179+
// NOLINTNEXTLINE
180+
constexpr KibiBytes operator"" _kib(unsigned long long int size) {
181+
return KibiBytes{static_cast<double>(size)};
182+
}
183+
184+
// NOLINTNEXTLINE
185+
constexpr MebiBytes operator"" _mib(unsigned long long int size) {
186+
return MebiBytes{static_cast<double>(size)};
187+
}
188+
189+
// NOLINTNEXTLINE
190+
constexpr GibiBytes operator"" _gib(unsigned long long int size) {
191+
return GibiBytes{static_cast<double>(size)};
192+
}
193+
194+
} // namespace allocation_size_literals
195+
196+
} // namespace impeller
197+
198+
#endif // FLUTTER_IMPELLER_BASE_ALLOCATION_SIZE_H_
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
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+
#include "flutter/testing/testing.h"
6+
#include "impeller/base/allocation_size.h"
7+
8+
namespace impeller::testing {
9+
10+
TEST(AllocationSizeTest, CanCreateTypedAllocations) {
11+
auto bytes = Bytes{1024};
12+
ASSERT_EQ(bytes.GetByteSize(), 1024u);
13+
14+
auto kilobytes = KiloBytes{5};
15+
ASSERT_EQ(kilobytes.GetByteSize(), 5u * 1e3);
16+
17+
auto megabytes = MegaBytes{5};
18+
ASSERT_EQ(megabytes.GetByteSize(), 5u * 1e6);
19+
20+
auto gigabytes = GigaBytes{5};
21+
ASSERT_EQ(gigabytes.GetByteSize(), 5u * 1e9);
22+
23+
auto kibibytes = KibiBytes{1};
24+
ASSERT_EQ(kibibytes.GetByteSize(), 1024u);
25+
26+
auto mebibytes = MebiBytes{1};
27+
ASSERT_EQ(mebibytes.GetByteSize(), 1048576u);
28+
29+
auto gigibytes = GibiBytes{1};
30+
ASSERT_EQ(gigibytes.GetByteSize(), 1073741824u);
31+
}
32+
33+
TEST(AllocationSizeTest, CanCreateTypedAllocationsWithLiterals) {
34+
using namespace allocation_size_literals;
35+
ASSERT_EQ((1024_bytes).GetByteSize(), 1024u);
36+
ASSERT_EQ((5_kb).GetByteSize(), 5u * 1e3);
37+
ASSERT_EQ((5_mb).GetByteSize(), 5u * 1e6);
38+
ASSERT_EQ((5_gb).GetByteSize(), 5u * 1e9);
39+
ASSERT_EQ((1_kib).GetByteSize(), 1024u);
40+
ASSERT_EQ((1_mib).GetByteSize(), 1048576u);
41+
ASSERT_EQ((1_gib).GetByteSize(), 1073741824u);
42+
}
43+
44+
TEST(AllocationSizeTest, CanConvert) {
45+
using namespace allocation_size_literals;
46+
ASSERT_EQ((5_gb).ConvertTo<MegaBytes>().GetSize(), 5000u);
47+
}
48+
49+
TEST(AllocationSizeTest, ConversionsAreNonTruncating) {
50+
using namespace allocation_size_literals;
51+
ASSERT_DOUBLE_EQ((1500_bytes).ConvertTo<KiloBytes>().GetSize(), 1.5);
52+
ASSERT_EQ((1500_bytes).ConvertTo<KiloBytes>().GetByteSize(), 1500u);
53+
}
54+
55+
TEST(AllocationSizeTest, CanGetFloatValues) {
56+
using namespace allocation_size_literals;
57+
ASSERT_DOUBLE_EQ((1500_bytes).ConvertTo<KiloBytes>().GetSize(), 1.5);
58+
}
59+
60+
TEST(AllocationSizeTest, RelationalOperatorsAreFunctional) {
61+
using namespace allocation_size_literals;
62+
63+
auto a = 1500_bytes;
64+
auto b = 2500_bytes;
65+
auto c = 0_bytes;
66+
67+
ASSERT_TRUE(a != b);
68+
ASSERT_FALSE(a == b);
69+
ASSERT_TRUE(b > a);
70+
ASSERT_TRUE(b >= a);
71+
ASSERT_TRUE(a < b);
72+
ASSERT_TRUE(a <= b);
73+
ASSERT_TRUE(a);
74+
ASSERT_FALSE(c);
75+
}
76+
77+
TEST(AllocationSizeTest, CanCast) {
78+
using namespace allocation_size_literals;
79+
{
80+
auto a = KiloBytes{1500_bytes};
81+
ASSERT_DOUBLE_EQ(a.GetSize(), 1.5);
82+
}
83+
{
84+
auto a = KiloBytes{Bytes{1500}};
85+
ASSERT_DOUBLE_EQ(a.GetSize(), 1.5);
86+
}
87+
88+
ASSERT_DOUBLE_EQ(MebiBytes{Bytes{4194304}}.GetSize(), 4);
89+
}
90+
91+
TEST(AllocationSizeTest, CanPerformSimpleArithmetic) {
92+
using namespace allocation_size_literals;
93+
{
94+
auto a = 100_bytes;
95+
auto b = 200_bytes;
96+
ASSERT_EQ((a + b).GetByteSize(), 300u);
97+
}
98+
{
99+
auto a = 100_bytes;
100+
a += 200_bytes;
101+
ASSERT_EQ(a.GetByteSize(), 300u);
102+
}
103+
{
104+
auto a = 100_bytes;
105+
a -= 50_bytes;
106+
ASSERT_EQ(a.GetByteSize(), 50u);
107+
}
108+
}
109+
110+
} // namespace impeller::testing

impeller/core/allocator.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#define FLUTTER_IMPELLER_CORE_ALLOCATOR_H_
77

88
#include "flutter/fml/mapping.h"
9+
#include "impeller/base/allocation_size.h"
910
#include "impeller/core/device_buffer_descriptor.h"
1011
#include "impeller/core/texture.h"
1112
#include "impeller/core/texture_descriptor.h"
@@ -51,7 +52,7 @@ class Allocator {
5152
virtual void DebugTraceMemoryStatistics() const {};
5253

5354
// Visible for testing.
54-
virtual size_t DebugGetHeapUsage() const { return 0; }
55+
virtual Bytes DebugGetHeapUsage() const { return Bytes{0}; }
5556

5657
protected:
5758
Allocator();

0 commit comments

Comments
 (0)