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