1 /*
2 * Copyright (C) 2023 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #define LOG_TAG "VtsHalCarDisplayTest"
18
19 #include <aidl/Gtest.h>
20 #include <aidl/Vintf.h>
21 #include <aidl/android/frameworks/automotive/display/DisplayDesc.h>
22 #include <aidl/android/frameworks/automotive/display/ICarDisplayProxy.h>
23 #include <aidlcommonsupport/NativeHandle.h>
24 #include <android-base/logging.h>
25 #include <android/binder_ibinder.h>
26 #include <android/binder_manager.h>
27 #include <android/binder_process.h>
28 #include <android/binder_status.h>
29 #include <bufferqueueconverter/BufferQueueConverter.h>
30
31 namespace {
32
33 using ::aidl::android::frameworks::automotive::display::DisplayDesc;
34 using ::aidl::android::frameworks::automotive::display::ICarDisplayProxy;
35 using ::aidl::android::hardware::common::NativeHandle;
36
convertNativeHandleToHGBP(const NativeHandle & aidlHandle)37 android::sp<HGraphicBufferProducer> convertNativeHandleToHGBP(const NativeHandle& aidlHandle) {
38 native_handle_t* handle = ::android::dupFromAidl(aidlHandle);
39 if (handle == nullptr || handle->numFds != 0 ||
40 handle->numInts < std::ceil(sizeof(size_t) / sizeof(int))) {
41 LOG(ERROR) << "Invalid native handle";
42 return nullptr;
43 }
44 android::hardware::hidl_vec<uint8_t> halToken;
45 halToken.setToExternal(reinterpret_cast<uint8_t*>(const_cast<int*>(&(handle->data[1]))),
46 handle->data[0]);
47 android::sp<HGraphicBufferProducer> hgbp =
48 HGraphicBufferProducer::castFrom(::android::retrieveHalInterface(halToken));
49 return std::move(hgbp);
50 }
51
52 } // namespace
53
54 // The main test class for Automotive Display Service
55 class CarDisplayAidlTest : public ::testing::TestWithParam<std::string> {
56 public:
SetUp()57 void SetUp() override {
58 // Make sure we can connect to the service
59 std::string serviceName = GetParam();
60 AIBinder* binder = AServiceManager_waitForService(serviceName.data());
61 ASSERT_NE(binder, nullptr);
62 mDisplayProxy = ICarDisplayProxy::fromBinder(ndk::SpAIBinder(binder));
63 ASSERT_NE(mDisplayProxy, nullptr);
64 LOG(INFO) << "Test target service: " << serviceName;
65
66 loadDisplayList();
67 }
68
TearDown()69 void TearDown() override {
70 mDisplayProxy.reset();
71 mDisplayIds.clear();
72 }
73
74 protected:
loadDisplayList()75 void loadDisplayList() {
76 ASSERT_TRUE(mDisplayProxy->getDisplayIdList(&mDisplayIds).isOk());
77 LOG(INFO) << "We have " << mDisplayIds.size() << " displays.";
78 }
79
80 std::shared_ptr<ICarDisplayProxy> mDisplayProxy;
81 std::vector<int64_t> mDisplayIds;
82 };
83
TEST_P(CarDisplayAidlTest,getIGBPObject)84 TEST_P(CarDisplayAidlTest, getIGBPObject) {
85 LOG(INFO) << "Test getHGraphicBufferProducer method";
86
87 for (const auto& id : mDisplayIds) {
88 // Get a display info.
89 DisplayDesc desc;
90 ASSERT_TRUE(mDisplayProxy->getDisplayInfo(id, &desc).isOk());
91
92 // Get a HGBP object as a native handle object.
93 NativeHandle handle;
94 ASSERT_TRUE(mDisplayProxy->getHGraphicBufferProducer(id, &handle).isOk());
95
96 // Convert a native handle object into a HGBP object.
97 android::sp<android::hardware::graphics::bufferqueue::V2_0::IGraphicBufferProducer>
98 gfxBufferProducer = convertNativeHandleToHGBP(handle);
99 ASSERT_NE(gfxBufferProducer, nullptr);
100
101 // Create a Surface object.
102 android::SurfaceHolderUniquePtr surfaceHolder = getSurfaceFromHGBP(gfxBufferProducer);
103 ASSERT_NE(surfaceHolder, nullptr);
104
105 // Verify the size.
106 ANativeWindow* nativeWindow = getNativeWindow(surfaceHolder.get());
107 ASSERT_EQ(desc.width, ANativeWindow_getWidth(nativeWindow));
108 ASSERT_EQ(desc.height, ANativeWindow_getHeight(nativeWindow));
109 }
110 }
111
TEST_P(CarDisplayAidlTest,showWindow)112 TEST_P(CarDisplayAidlTest, showWindow) {
113 LOG(INFO) << "Test showWindow method";
114 for (const auto& id : mDisplayIds) {
115 ASSERT_TRUE(mDisplayProxy->showWindow(id).isOk());
116 }
117 }
118
TEST_P(CarDisplayAidlTest,hideWindow)119 TEST_P(CarDisplayAidlTest, hideWindow) {
120 LOG(INFO) << "Test hideWindow method";
121
122 for (const auto& id : mDisplayIds) {
123 ASSERT_TRUE(mDisplayProxy->hideWindow(id).isOk());
124 }
125 }
126
TEST_P(CarDisplayAidlTest,getSurface)127 TEST_P(CarDisplayAidlTest, getSurface) {
128 LOG(INFO) << "Test getSurface method";
129
130 for (const auto& id : mDisplayIds) {
131 // Get a display info.
132 DisplayDesc desc;
133 ASSERT_TRUE(mDisplayProxy->getDisplayInfo(id, &desc).isOk());
134
135 // Get a Surface object.
136 aidl::android::view::Surface shimSurface;
137 ASSERT_TRUE(mDisplayProxy->getSurface(id, &shimSurface).isOk());
138
139 // Verify the size.
140 ANativeWindow* nativeWindow = shimSurface.get();
141 ASSERT_EQ(desc.width, ANativeWindow_getWidth(nativeWindow));
142 ASSERT_EQ(desc.height, ANativeWindow_getHeight(nativeWindow));
143 }
144 }
145
146 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(CarDisplayAidlTest);
147 INSTANTIATE_TEST_SUITE_P(
148 PerInstance, CarDisplayAidlTest,
149 testing::ValuesIn(android::getAidlHalInstanceNames(ICarDisplayProxy::descriptor)),
150 android::PrintInstanceNameToString);
151
main(int argc,char ** argv)152 int main(int argc, char** argv) {
153 ::testing::InitGoogleTest(&argc, argv);
154 ABinderProcess_setThreadPoolMaxThreadCount(/* numThreads= */ 1);
155 ABinderProcess_startThreadPool();
156 return RUN_ALL_TESTS();
157 }
158