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

Commit 84c258a

Browse files
Return an empty optional in HardwareBuffer::GetSystemUniqueID if the underlying NDK API is unavailable (#51839)
1 parent 0e84f61 commit 84c258a

File tree

5 files changed

+29
-1
lines changed

5 files changed

+29
-1
lines changed

impeller/toolkit/android/BUILD.gn

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ source_set("unittests_lib") {
5050
":unittests_fixtures",
5151
"//flutter/testing",
5252
]
53+
54+
defines = [ "TESTING" ]
5355
}
5456

5557
executable("unittests") {

impeller/toolkit/android/hardware_buffer.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ std::optional<uint64_t> HardwareBuffer::GetSystemUniqueID() const {
108108
std::optional<uint64_t> HardwareBuffer::GetSystemUniqueID(
109109
AHardwareBuffer* buffer) {
110110
if (!GetProcTable().AHardwareBuffer_getId) {
111-
return false;
111+
return std::nullopt;
112112
}
113113
uint64_t out_id = 0u;
114114
if (GetProcTable().AHardwareBuffer_getId(buffer, &out_id) != 0) {

impeller/toolkit/android/proc_table.cc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ const ProcTable& GetProcTable() {
1414
return gProcTable;
1515
}
1616

17+
// Only used by tests.
18+
ProcTable& GetMutableProcTable() {
19+
return const_cast<ProcTable&>(GetProcTable());
20+
}
21+
1722
template <class T>
1823
void ResolveAndroidProc(
1924
AndroidProc<T>& proc,

impeller/toolkit/android/proc_table.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,10 @@ struct ProcTable {
124124

125125
const ProcTable& GetProcTable();
126126

127+
#ifdef TESTING
128+
ProcTable& GetMutableProcTable();
129+
#endif
130+
127131
} // namespace impeller::android
128132

129133
#endif // FLUTTER_IMPELLER_TOOLKIT_ANDROID_PROC_TABLE_H_

impeller/toolkit/android/toolkit_android_unittests.cc

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,18 @@
1212

1313
namespace impeller::android::testing {
1414

15+
#define DISABLE_ANDROID_PROC(name) \
16+
struct Disable##name { \
17+
Disable##name() { \
18+
real_proc = GetMutableProcTable().name.proc; \
19+
GetMutableProcTable().name.proc = nullptr; \
20+
} \
21+
~Disable##name() { \
22+
GetMutableProcTable().name.proc = real_proc; \
23+
} \
24+
decltype(name)* real_proc; \
25+
} disable##name;
26+
1527
TEST(ToolkitAndroidTest, CanCreateProcTable) {
1628
ProcTable proc_table;
1729
ASSERT_TRUE(proc_table.IsValid());
@@ -49,6 +61,11 @@ TEST(ToolkitAndroidTest, CanGetHardwareBufferIDs) {
4961
ASSERT_TRUE(buffer.GetSystemUniqueID().has_value());
5062
}
5163

64+
TEST(ToolkitAndroidTest, HardwareBufferNullIDIfAPIUnavailable) {
65+
DISABLE_ANDROID_PROC(AHardwareBuffer_getId);
66+
ASSERT_FALSE(HardwareBuffer::GetSystemUniqueID(nullptr).has_value());
67+
}
68+
5269
TEST(ToolkitAndroidTest, CanDescribeHardwareBufferHandles) {
5370
if (!HardwareBuffer::IsAvailableOnPlatform()) {
5471
GTEST_SKIP() << "Hardware buffers are not supported on this platform.";

0 commit comments

Comments
 (0)