1 /*
2  * Copyright (C) 2017 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 #include <mapper-vts/2.1/MapperVts.h>
18 
19 namespace android {
20 namespace hardware {
21 namespace graphics {
22 namespace mapper {
23 namespace V2_1 {
24 namespace vts {
25 
26 using V2_0::Error;
27 
28 // abuse VTS to check binary compatibility between BufferDescriptorInfos
29 using OldBufferDescriptorInfo =
30     android::hardware::graphics::mapper::V2_0::IMapper::BufferDescriptorInfo;
31 static_assert(sizeof(OldBufferDescriptorInfo) == sizeof(IMapper::BufferDescriptorInfo) &&
32                   offsetof(OldBufferDescriptorInfo, width) ==
33                       offsetof(IMapper::BufferDescriptorInfo, width) &&
34                   offsetof(OldBufferDescriptorInfo, height) ==
35                       offsetof(IMapper::BufferDescriptorInfo, height) &&
36                   offsetof(OldBufferDescriptorInfo, layerCount) ==
37                       offsetof(IMapper::BufferDescriptorInfo, layerCount) &&
38                   offsetof(OldBufferDescriptorInfo, format) ==
39                       offsetof(IMapper::BufferDescriptorInfo, format) &&
40                   offsetof(OldBufferDescriptorInfo, usage) ==
41                       offsetof(IMapper::BufferDescriptorInfo, usage),
42               "");
43 
Gralloc(bool errOnFailure)44 Gralloc::Gralloc(bool errOnFailure) : V2_0::vts::Gralloc() {
45     if (::testing::Test::HasFatalFailure()) {
46         return;
47     }
48     init(errOnFailure);
49 }
50 
Gralloc(const std::string & allocatorServiceName,const std::string & mapperServiceName,bool errOnFailure)51 Gralloc::Gralloc(const std::string& allocatorServiceName, const std::string& mapperServiceName,
52                  bool errOnFailure)
53     : V2_0::vts::Gralloc(allocatorServiceName, mapperServiceName) {
54     if (::testing::Test::HasFatalFailure()) {
55         return;
56     }
57     init(errOnFailure);
58 }
59 
init(bool errOnFailure)60 void Gralloc::init(bool errOnFailure) {
61     mMapperV2_1 = IMapper::castFrom(V2_0::vts::Gralloc::getMapper());
62     if (errOnFailure) ASSERT_NE(nullptr, mMapperV2_1.get()) << "failed to get mapper 2.1 service";
63 }
64 
getMapper() const65 sp<IMapper> Gralloc::getMapper() const {
66     return mMapperV2_1;
67 }
68 
validateBufferSize(const native_handle_t * bufferHandle,const IMapper::BufferDescriptorInfo & descriptorInfo,uint32_t stride)69 bool Gralloc::validateBufferSize(const native_handle_t* bufferHandle,
70                                  const IMapper::BufferDescriptorInfo& descriptorInfo,
71                                  uint32_t stride) {
72     auto buffer = const_cast<native_handle_t*>(bufferHandle);
73 
74     Error error = mMapperV2_1->validateBufferSize(buffer, descriptorInfo, stride);
75     return error == Error::NONE;
76 }
77 
getTransportSize(const native_handle_t * bufferHandle,uint32_t * outNumFds,uint32_t * outNumInts)78 void Gralloc::getTransportSize(const native_handle_t* bufferHandle, uint32_t* outNumFds,
79                                uint32_t* outNumInts) {
80     auto buffer = const_cast<native_handle_t*>(bufferHandle);
81 
82     *outNumFds = 0;
83     *outNumInts = 0;
84     mMapperV2_1->getTransportSize(
85         buffer, [&](const auto& tmpError, const auto& tmpNumFds, const auto& tmpNumInts) {
86             ASSERT_EQ(Error::NONE, tmpError) << "failed to get transport size";
87             ASSERT_GE(bufferHandle->numFds, int(tmpNumFds)) << "invalid numFds " << tmpNumFds;
88             ASSERT_GE(bufferHandle->numInts, int(tmpNumInts)) << "invalid numInts " << tmpNumInts;
89 
90             *outNumFds = tmpNumFds;
91             *outNumInts = tmpNumInts;
92         });
93 }
94 
createDescriptor(const IMapper::BufferDescriptorInfo & descriptorInfo)95 BufferDescriptor Gralloc::createDescriptor(const IMapper::BufferDescriptorInfo& descriptorInfo) {
96     BufferDescriptor descriptor;
97     mMapperV2_1->createDescriptor_2_1(
98         descriptorInfo, [&](const auto& tmpError, const auto& tmpDescriptor) {
99             ASSERT_EQ(Error::NONE, tmpError) << "failed to create descriptor";
100             descriptor = tmpDescriptor;
101         });
102 
103     return descriptor;
104 }
105 
allocate(const IMapper::BufferDescriptorInfo & descriptorInfo,bool import,uint32_t * outStride)106 const native_handle_t* Gralloc::allocate(const IMapper::BufferDescriptorInfo& descriptorInfo,
107                                          bool import, uint32_t* outStride) {
108     BufferDescriptor descriptor = createDescriptor(descriptorInfo);
109     if (::testing::Test::HasFatalFailure()) {
110         return nullptr;
111     }
112 
113     auto buffers = V2_0::vts::Gralloc::allocate(descriptor, 1, import, outStride);
114     if (::testing::Test::HasFatalFailure()) {
115         return nullptr;
116     }
117 
118     return buffers[0];
119 }
120 
121 }  // namespace vts
122 }  // namespace V2_1
123 }  // namespace mapper
124 }  // namespace graphics
125 }  // namespace hardware
126 }  // namespace android
127