Skip to content

Commit

Permalink
Return an empty optional in HardwareBuffer::GetSystemUniqueID if the …
Browse files Browse the repository at this point in the history
…underlying NDK API is unavailable (flutter#51839)
  • Loading branch information
jason-simmons authored and Jasguerrero committed Apr 4, 2024
1 parent 9755090 commit 958384b
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 1 deletion.
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

0 comments on commit 958384b

Please sign in to comment.