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

Return an empty optional in HardwareBuffer::GetSystemUniqueID if the underlying NDK API is unavailable #51839

Merged
merged 2 commits into from
Apr 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions impeller/toolkit/android/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ source_set("unittests_lib") {
":unittests_fixtures",
"//flutter/testing",
]

defines = [ "TESTING" ]
}

executable("unittests") {
Expand Down
2 changes: 1 addition & 1 deletion impeller/toolkit/android/hardware_buffer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ std::optional<uint64_t> HardwareBuffer::GetSystemUniqueID() const {
std::optional<uint64_t> HardwareBuffer::GetSystemUniqueID(
AHardwareBuffer* buffer) {
if (!GetProcTable().AHardwareBuffer_getId) {
return false;
return std::nullopt;
}
uint64_t out_id = 0u;
if (GetProcTable().AHardwareBuffer_getId(buffer, &out_id) != 0) {
Expand Down
5 changes: 5 additions & 0 deletions impeller/toolkit/android/proc_table.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ const ProcTable& GetProcTable() {
return gProcTable;
}

// Only used by tests.
ProcTable& GetMutableProcTable() {
return const_cast<ProcTable&>(GetProcTable());
}

template <class T>
void ResolveAndroidProc(
AndroidProc<T>& proc,
Expand Down
4 changes: 4 additions & 0 deletions impeller/toolkit/android/proc_table.h
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,10 @@ struct ProcTable {

const ProcTable& GetProcTable();

#ifdef TESTING
ProcTable& GetMutableProcTable();
#endif

} // namespace impeller::android

#endif // FLUTTER_IMPELLER_TOOLKIT_ANDROID_PROC_TABLE_H_
17 changes: 17 additions & 0 deletions impeller/toolkit/android/toolkit_android_unittests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,18 @@

namespace impeller::android::testing {

#define DISABLE_ANDROID_PROC(name) \
struct Disable##name { \
Disable##name() { \
real_proc = GetMutableProcTable().name.proc; \
GetMutableProcTable().name.proc = nullptr; \
} \
~Disable##name() { \
GetMutableProcTable().name.proc = real_proc; \
} \
decltype(name)* real_proc; \
} disable##name;

TEST(ToolkitAndroidTest, CanCreateProcTable) {
ProcTable proc_table;
ASSERT_TRUE(proc_table.IsValid());
Expand Down Expand Up @@ -49,6 +61,11 @@ TEST(ToolkitAndroidTest, CanGetHardwareBufferIDs) {
ASSERT_TRUE(buffer.GetSystemUniqueID().has_value());
}

TEST(ToolkitAndroidTest, HardwareBufferNullIDIfAPIUnavailable) {
DISABLE_ANDROID_PROC(AHardwareBuffer_getId);
ASSERT_FALSE(HardwareBuffer::GetSystemUniqueID(nullptr).has_value());
}

TEST(ToolkitAndroidTest, CanDescribeHardwareBufferHandles) {
if (!HardwareBuffer::IsAvailableOnPlatform()) {
GTEST_SKIP() << "Hardware buffers are not supported on this platform.";
Expand Down