1 /*
2  * Copyright (C) 2016 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 "VtsHalGraphicsMapperV2_1TargetTest"
18 
19 #include <android-base/logging.h>
20 #include <android/hardware/graphics/mapper/2.1/IMapper.h>
21 #include <gtest/gtest.h>
22 #include <hidl/GtestPrinter.h>
23 #include <hidl/ServiceManagement.h>
24 #include <mapper-vts/2.1/MapperVts.h>
25 
26 namespace android {
27 namespace hardware {
28 namespace graphics {
29 namespace mapper {
30 namespace V2_1 {
31 namespace vts {
32 namespace {
33 
34 using android::hardware::graphics::allocator::V2_0::IAllocator;
35 using android::hardware::graphics::common::V1_1::BufferUsage;
36 using android::hardware::graphics::common::V1_1::PixelFormat;
37 using V2_0::Error;
38 
39 class GraphicsMapperHidlTest
40     : public ::testing::TestWithParam<std::tuple<std::string, std::string>> {
41   protected:
SetUp()42     void SetUp() override {
43         ASSERT_NO_FATAL_FAILURE(mGralloc = std::make_unique<Gralloc>(std::get<0>(GetParam()),
44                                                                      std::get<1>(GetParam())));
45 
46         mDummyDescriptorInfo.width = 64;
47         mDummyDescriptorInfo.height = 64;
48         mDummyDescriptorInfo.layerCount = 1;
49         mDummyDescriptorInfo.format = PixelFormat::RGBA_8888;
50         mDummyDescriptorInfo.usage =
51             static_cast<uint64_t>(BufferUsage::CPU_WRITE_OFTEN | BufferUsage::CPU_READ_OFTEN);
52     }
53 
TearDown()54     void TearDown() override {}
55 
56     std::unique_ptr<Gralloc> mGralloc;
57     IMapper::BufferDescriptorInfo mDummyDescriptorInfo{};
58 };
59 
60 /**
61  * Test that IMapper::validateBufferSize works.
62  */
TEST_P(GraphicsMapperHidlTest,ValidateBufferSizeBasic)63 TEST_P(GraphicsMapperHidlTest, ValidateBufferSizeBasic) {
64     const native_handle_t* bufferHandle;
65     uint32_t stride;
66     ASSERT_NO_FATAL_FAILURE(bufferHandle = mGralloc->allocate(mDummyDescriptorInfo, true, &stride));
67 
68     ASSERT_TRUE(mGralloc->validateBufferSize(bufferHandle, mDummyDescriptorInfo, stride));
69 
70     ASSERT_NO_FATAL_FAILURE(mGralloc->freeBuffer(bufferHandle));
71 }
72 
73 /**
74  * Test IMapper::validateBufferSize with invalid buffers.
75  */
TEST_P(GraphicsMapperHidlTest,ValidateBufferSizeBadBuffer)76 TEST_P(GraphicsMapperHidlTest, ValidateBufferSizeBadBuffer) {
77     native_handle_t* invalidHandle = nullptr;
78     Error ret = mGralloc->getMapper()->validateBufferSize(invalidHandle, mDummyDescriptorInfo,
79                                                           mDummyDescriptorInfo.width);
80     ASSERT_EQ(Error::BAD_BUFFER, ret)
81         << "validateBufferSize with nullptr did not fail with BAD_BUFFER";
82 
83     invalidHandle = native_handle_create(0, 0);
84     ret = mGralloc->getMapper()->validateBufferSize(invalidHandle, mDummyDescriptorInfo,
85                                                     mDummyDescriptorInfo.width);
86     ASSERT_EQ(Error::BAD_BUFFER, ret)
87         << "validateBufferSize with invalid handle did not fail with BAD_BUFFER";
88     native_handle_delete(invalidHandle);
89 
90     native_handle_t* rawBufferHandle;
91     ASSERT_NO_FATAL_FAILURE(rawBufferHandle = const_cast<native_handle_t*>(
92                                 mGralloc->allocate(mDummyDescriptorInfo, false)));
93     ret = mGralloc->getMapper()->validateBufferSize(rawBufferHandle, mDummyDescriptorInfo,
94                                                     mDummyDescriptorInfo.width);
95     ASSERT_EQ(Error::BAD_BUFFER, ret)
96         << "validateBufferSize with raw buffer handle did not fail with BAD_BUFFER";
97     ASSERT_NO_FATAL_FAILURE(mGralloc->freeBuffer(rawBufferHandle));
98 }
99 
100 /**
101  * Test IMapper::validateBufferSize with invalid descriptor and/or stride.
102  */
TEST_P(GraphicsMapperHidlTest,ValidateBufferSizeBadValue)103 TEST_P(GraphicsMapperHidlTest, ValidateBufferSizeBadValue) {
104     auto info = mDummyDescriptorInfo;
105     info.width = 1024;
106     info.height = 1024;
107     info.layerCount = 1;
108     info.format = PixelFormat::RGBA_8888;
109 
110     native_handle_t* bufferHandle;
111     uint32_t stride;
112     ASSERT_NO_FATAL_FAILURE(
113         bufferHandle = const_cast<native_handle_t*>(mGralloc->allocate(info, true, &stride)));
114 
115     // All checks below test if a 8MB buffer can fit in a 4MB buffer.
116     info.width *= 2;
117     Error ret = mGralloc->getMapper()->validateBufferSize(bufferHandle, info, stride);
118     ASSERT_EQ(Error::BAD_VALUE, ret)
119         << "validateBufferSize with bad width did not fail with BAD_VALUE";
120     info.width /= 2;
121 
122     info.height *= 2;
123     ret = mGralloc->getMapper()->validateBufferSize(bufferHandle, info, stride);
124     ASSERT_EQ(Error::BAD_VALUE, ret)
125         << "validateBufferSize with bad height did not fail with BAD_VALUE";
126     info.height /= 2;
127 
128     info.layerCount *= 2;
129     ret = mGralloc->getMapper()->validateBufferSize(bufferHandle, info, stride);
130     ASSERT_EQ(Error::BAD_VALUE, ret)
131         << "validateBufferSize with bad layer count did not fail with BAD_VALUE";
132     info.layerCount /= 2;
133 
134     info.format = PixelFormat::RGBA_FP16;
135     ret = mGralloc->getMapper()->validateBufferSize(bufferHandle, info, stride);
136     ASSERT_EQ(Error::BAD_VALUE, ret)
137         << "validateBufferSize with bad format did not fail with BAD_VALUE";
138     info.format = PixelFormat::RGBA_8888;
139 
140     ret = mGralloc->getMapper()->validateBufferSize(bufferHandle, mDummyDescriptorInfo, stride * 2);
141     ASSERT_EQ(Error::BAD_VALUE, ret)
142         << "validateBufferSize with bad stride did not fail with BAD_VALUE";
143 
144     ASSERT_NO_FATAL_FAILURE(mGralloc->freeBuffer(bufferHandle));
145 }
146 
147 /**
148  * Test IMapper::getTransportSize.
149  */
TEST_P(GraphicsMapperHidlTest,GetTransportSizeBasic)150 TEST_P(GraphicsMapperHidlTest, GetTransportSizeBasic) {
151     const native_handle_t* bufferHandle;
152     uint32_t numFds;
153     uint32_t numInts;
154     ASSERT_NO_FATAL_FAILURE(bufferHandle = mGralloc->allocate(mDummyDescriptorInfo, true));
155     ASSERT_NO_FATAL_FAILURE(mGralloc->getTransportSize(bufferHandle, &numFds, &numInts));
156     ASSERT_NO_FATAL_FAILURE(mGralloc->freeBuffer(bufferHandle));
157 }
158 
159 /**
160  * Test IMapper::getTransportSize with invalid buffers.
161  */
TEST_P(GraphicsMapperHidlTest,GetTransportSizeBadBuffer)162 TEST_P(GraphicsMapperHidlTest, GetTransportSizeBadBuffer) {
163     native_handle_t* invalidHandle = nullptr;
164     mGralloc->getMapper()->getTransportSize(
165         invalidHandle, [&](const auto& tmpError, const auto&, const auto&) {
166             ASSERT_EQ(Error::BAD_BUFFER, tmpError)
167                 << "getTransportSize with nullptr did not fail with BAD_BUFFER";
168         });
169 
170     invalidHandle = native_handle_create(0, 0);
171     mGralloc->getMapper()->getTransportSize(
172         invalidHandle, [&](const auto& tmpError, const auto&, const auto&) {
173             ASSERT_EQ(Error::BAD_BUFFER, tmpError)
174                 << "getTransportSize with invalid handle did not fail with BAD_BUFFER";
175         });
176     native_handle_delete(invalidHandle);
177 
178     native_handle_t* rawBufferHandle;
179     ASSERT_NO_FATAL_FAILURE(rawBufferHandle = const_cast<native_handle_t*>(
180                                 mGralloc->allocate(mDummyDescriptorInfo, false)));
181     mGralloc->getMapper()->getTransportSize(
182             rawBufferHandle, [&](const auto& tmpError, const auto&, const auto&) {
183                 ASSERT_EQ(Error::BAD_BUFFER, tmpError)
184                         << "getTransportSize with raw buffer handle did not fail with BAD_BUFFER";
185             });
186     ASSERT_NO_FATAL_FAILURE(mGralloc->freeBuffer(rawBufferHandle));
187 }
188 
189 /**
190  * Test IMapper::createDescriptor with valid descriptor info.
191  */
TEST_P(GraphicsMapperHidlTest,CreateDescriptor_2_1Basic)192 TEST_P(GraphicsMapperHidlTest, CreateDescriptor_2_1Basic) {
193     ASSERT_NO_FATAL_FAILURE(mGralloc->createDescriptor(mDummyDescriptorInfo));
194 }
195 
196 /**
197  * Test IMapper::createDescriptor with invalid descriptor info.
198  */
TEST_P(GraphicsMapperHidlTest,CreateDescriptor_2_1Negative)199 TEST_P(GraphicsMapperHidlTest, CreateDescriptor_2_1Negative) {
200     auto info = mDummyDescriptorInfo;
201     info.width = 0;
202     mGralloc->getMapper()->createDescriptor_2_1(info, [&](const auto& tmpError, const auto&) {
203         EXPECT_EQ(Error::BAD_VALUE, tmpError) << "createDescriptor did not fail with BAD_VALUE";
204     });
205 }
206 
207 INSTANTIATE_TEST_CASE_P(
208         PerInstance, GraphicsMapperHidlTest,
209         testing::Combine(
210                 testing::ValuesIn(
211                         android::hardware::getAllHalInstanceNames(IAllocator::descriptor)),
212                 testing::ValuesIn(android::hardware::getAllHalInstanceNames(IMapper::descriptor))),
213         android::hardware::PrintInstanceTupleNameToString<>);
214 
215 }  // namespace
216 }  // namespace vts
217 }  // namespace V2_1
218 }  // namespace mapper
219 }  // namespace graphics
220 }  // namespace hardware
221 }  // namespace android