| Flutter Impeller |

toolkit_android_unittests.cc
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 #include "flutter/fml/synchronization/waitable_event.h"
6 #include "flutter/testing/testing.h"
7 #include "impeller/toolkit/android/choreographer.h"
8 #include "impeller/toolkit/android/hardware_buffer.h"
9 #include "impeller/toolkit/android/proc_table.h"
10 #include "impeller/toolkit/android/surface_control.h"
11 #include "impeller/toolkit/android/surface_transaction.h"
12
13 namespace impeller::android::testing {
14
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
27 TEST(ToolkitAndroidTest, CanCreateProcTable) {
28 ProcTable proc_table;
29 ASSERT_TRUE(proc_table.IsValid());
30 }
31
32 TEST(ToolkitAndroidTest, GuardsAgainstZeroSizedDescriptors) {
33 auto desc = HardwareBufferDescriptor::MakeForSwapchainImage({0, 0});
34 ASSERT_GT(desc.size.width, 0u);
35 ASSERT_GT(desc.size.height, 0u);
36 }
37
38 TEST(ToolkitAndroidTest, CanCreateHardwareBuffer) {
39 if (!HardwareBuffer::IsAvailableOnPlatform()) {
40 GTEST_SKIP() << "Hardware buffers are not supported on this platform.";
41 }
42 ASSERT_TRUE(HardwareBuffer::IsAvailableOnPlatform());
43 auto desc = HardwareBufferDescriptor::MakeForSwapchainImage({100, 100});
44 ASSERT_TRUE(desc.IsAllocatable());
45 HardwareBuffer buffer(desc);
46 ASSERT_TRUE(buffer.IsValid());
47 }
48
49 TEST(ToolkitAndroidTest, CanGetHardwareBufferIDs) {
50 if (!HardwareBuffer::IsAvailableOnPlatform()) {
51 GTEST_SKIP() << "Hardware buffers are not supported on this platform.";
52 }
53 ASSERT_TRUE(HardwareBuffer::IsAvailableOnPlatform());
54 if (!GetProcTable().AHardwareBuffer_getId.IsAvailable()) {
55 GTEST_SKIP() << "Hardware buffer IDs are not available on this platform.";
56 }
57 auto desc = HardwareBufferDescriptor::MakeForSwapchainImage({100, 100});
58 ASSERT_TRUE(desc.IsAllocatable());
59 HardwareBuffer buffer(desc);
60 ASSERT_TRUE(buffer.IsValid());
61 ASSERT_TRUE(buffer.GetSystemUniqueID().has_value());
62 }
63
64 TEST(ToolkitAndroidTest, HardwareBufferNullIDIfAPIUnavailable) {
65 DISABLE_ANDROID_PROC(AHardwareBuffer_getId);
66 ASSERT_FALSE(HardwareBuffer::GetSystemUniqueID(nullptr).has_value());
67 }
68
69 TEST(ToolkitAndroidTest, CanDescribeHardwareBufferHandles) {
70 if (!HardwareBuffer::IsAvailableOnPlatform()) {
71 GTEST_SKIP() << "Hardware buffers are not supported on this platform.";
72 }
73 ASSERT_TRUE(HardwareBuffer::IsAvailableOnPlatform());
74 auto desc = HardwareBufferDescriptor::MakeForSwapchainImage({100, 100});
75 ASSERT_TRUE(desc.IsAllocatable());
76 HardwareBuffer buffer(desc);
77 ASSERT_TRUE(buffer.IsValid());
78 auto a_desc = HardwareBuffer::Describe(buffer.GetHandle());
79 ASSERT_TRUE(a_desc.has_value());
80 ASSERT_EQ(a_desc->width, 100u); // NOLINT
81 ASSERT_EQ(a_desc->height, 100u); // NOLINT
82 }
83
84 TEST(ToolkitAndroidTest, CanApplySurfaceTransaction) {
85 if (!SurfaceTransaction::IsAvailableOnPlatform()) {
86 GTEST_SKIP() << "Surface controls are not supported on this platform.";
87 }
88 ASSERT_TRUE(SurfaceTransaction::IsAvailableOnPlatform());
89 SurfaceTransaction transaction;
90 ASSERT_TRUE(transaction.IsValid());
91 fml::AutoResetWaitableEvent event;
92 ASSERT_TRUE(transaction.Apply([&event](auto) { event.Signal(); }));
93 event.Wait();
94 }
95
96 TEST(ToolkitAndroidTest, SurfacControlsAreAvailable) {
97 if (!SurfaceControl::IsAvailableOnPlatform()) {
98 GTEST_SKIP() << "Surface controls are not supported on this platform.";
99 }
100 ASSERT_TRUE(SurfaceControl::IsAvailableOnPlatform());
101 }
102
103 TEST(ToolkitAndroidTest, ChoreographerIsAvailable) {
104 if (!Choreographer::IsAvailableOnPlatform()) {
105 GTEST_SKIP() << "Choreographer is not supported on this platform.";
106 }
107 ASSERT_TRUE(Choreographer::IsAvailableOnPlatform());
108 }
109
110 TEST(ToolkitAndroidTest, CanPostAndNotWaitForFrameCallbacks) {
111 if (!Choreographer::IsAvailableOnPlatform()) {
112 GTEST_SKIP() << "Choreographer is not supported on this platform.";
113 }
114 const auto& choreographer = Choreographer::GetInstance();
115 ASSERT_TRUE(choreographer.IsValid());
116 ASSERT_TRUE(choreographer.PostFrameCallback([](auto) {}));
117 }
118
119 TEST(ToolkitAndroidTest, CanPostAndWaitForFrameCallbacks) {
120 if (!Choreographer::IsAvailableOnPlatform()) {
121 GTEST_SKIP() << "Choreographer is not supported on this platform.";
122 }
123 if ((true)) {
124 GTEST_SKIP()
125 << "Disabled till the test harness is in an Android activity. "
126 "Running it without one will hang because the choreographer "
127 "frame callback will never execute.";
128 }
129 const auto& choreographer = Choreographer::GetInstance();
130 ASSERT_TRUE(choreographer.IsValid());
131 fml::AutoResetWaitableEvent event;
132 ASSERT_TRUE(choreographer.PostFrameCallback(
133 [&event](auto point) { event.Signal(); }));
134 event.Wait();
135 }
136
137 } // namespace impeller::android::testing
proc_table.h
choreographer.h
impeller::android::Choreographer::IsAvailableOnPlatform
static bool IsAvailableOnPlatform()
Definition: choreographer.cc:78
impeller::android::Choreographer::GetInstance
static Choreographer & GetInstance()
Create or get the thread local instance of a choreographer. A message loop will be setup on the calli...
Definition: choreographer.cc:11
impeller::android::HardwareBuffer
A wrapper for AHardwareBuffer https://developer.android.com/ndk/reference/group/a-hardware-buffer.
Definition: hardware_buffer.h:95
impeller::android::HardwareBuffer::IsAvailableOnPlatform
static bool IsAvailableOnPlatform()
Definition: hardware_buffer.cc:107
impeller::android::HardwareBuffer::GetHandle
AHardwareBuffer * GetHandle() const
Definition: hardware_buffer.cc:83
impeller::android::HardwareBuffer::IsValid
bool IsValid() const
Definition: hardware_buffer.cc:79
impeller::android::HardwareBuffer::Describe
static std::optional< AHardwareBuffer_Desc > Describe(AHardwareBuffer *buffer)
Definition: hardware_buffer.cc:127
impeller::android::HardwareBuffer::GetSystemUniqueID
std::optional< uint64_t > GetSystemUniqueID() const
Get the system wide unique ID of the hardware buffer if possible. This is only available on Android A...
Definition: hardware_buffer.cc:111
impeller::android::SurfaceControl::IsAvailableOnPlatform
static bool IsAvailableOnPlatform()
Definition: surface_control.cc:50
impeller::android::SurfaceTransaction
A wrapper for ASurfaceTransaction. https://developer.android.com/ndk/reference/group/native-activity#...
Definition: surface_transaction.h:54
impeller::android::SurfaceTransaction::IsValid
bool IsValid() const
Definition: surface_transaction.cc:23
impeller::android::SurfaceTransaction::Apply
bool Apply(OnCompleteCallback callback=nullptr)
Applies the updated encoded in the transaction and invokes the callback when the updated are complete...
Definition: surface_transaction.cc:31
impeller::android::SurfaceTransaction::IsAvailableOnPlatform
static bool IsAvailableOnPlatform()
Definition: surface_transaction.cc:115
hardware_buffer.h
impeller::android::testing
Definition: ahb_texture_source_vk_unittests.cc:16
impeller::android::testing::TEST
TEST(AndroidVulkanTest, CanImportRGBA)
Definition: ahb_texture_source_vk_unittests.cc:40
impeller::android::GetProcTable
const ProcTable & GetProcTable()
Definition: proc_table.cc:12
impeller::android::HardwareBufferDescriptor::MakeForSwapchainImage
static HardwareBufferDescriptor MakeForSwapchainImage(const ISize &size)
Create a descriptor of the given size that is suitable for use as a swapchain image.
Definition: hardware_buffer.cc:87
impeller::android::ProcTable
The table of Android procs that are resolved dynamically.
Definition: proc_table.h:97
impeller::android::ProcTable::IsValid
bool IsValid() const
If a valid proc table could be setup. This may fail in case of setup on non-Android platforms.
Definition: proc_table.cc:65
surface_control.h
surface_transaction.h
DISABLE_ANDROID_PROC
#define DISABLE_ANDROID_PROC(name)
Definition: toolkit_android_unittests.cc:15