blob: 0aaa9260185c521cb623a2ef259c257ab68e9056 [file] [log] [blame]
Chia-I Wuefd99202017-12-15 14:09:02 -08001/*
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
21namespace android {
22namespace hardware {
23namespace graphics {
24namespace mapper {
25namespace V2_1 {
26namespace vts {
27
28using V2_0::Error;
29
30// abuse VTS to check binary compatibility between BufferDescriptorInfos
31using OldBufferDescriptorInfo =
32 android::hardware::graphics::mapper::V2_0::IMapper::BufferDescriptorInfo;
33static_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
46Gralloc::Gralloc(const std::string& allocatorServiceName, const std::string& mapperServiceName)
47 : V2_0::vts::Gralloc(allocatorServiceName, mapperServiceName) {
48 if (::testing::Test::HasFatalFailure()) {
49 return;
50 }
51 init();
52}
53
54void Gralloc::init() {
55 mMapperV2_1 = IMapper::castFrom(V2_0::vts::Gralloc::getMapper());
56 ASSERT_NE(nullptr, mMapperV2_1.get()) << "failed to get mapper 2.1 service";
57}
58
59sp<IMapper> Gralloc::getMapper() const {
60 return mMapperV2_1;
61}
62
63bool Gralloc::validateBufferSize(const native_handle_t* bufferHandle,
64 const IMapper::BufferDescriptorInfo& descriptorInfo,
65 uint32_t stride) {
66 auto buffer = const_cast<native_handle_t*>(bufferHandle);
67
68 Error error = mMapperV2_1->validateBufferSize(buffer, descriptorInfo, stride);
69 return error == Error::NONE;
70}
71
72void Gralloc::getTransportSize(const native_handle_t* bufferHandle, uint32_t* outNumFds,
73 uint32_t* outNumInts) {
74 auto buffer = const_cast<native_handle_t*>(bufferHandle);
75
76 *outNumFds = 0;
77 *outNumInts = 0;
78 mMapperV2_1->getTransportSize(
79 buffer, [&](const auto& tmpError, const auto& tmpNumFds, const auto& tmpNumInts) {
80 ASSERT_EQ(Error::NONE, tmpError) << "failed to get transport size";
81 ASSERT_GE(bufferHandle->numFds, int(tmpNumFds)) << "invalid numFds " << tmpNumFds;
82 ASSERT_GE(bufferHandle->numInts, int(tmpNumInts)) << "invalid numInts " << tmpNumInts;
83
84 *outNumFds = tmpNumFds;
85 *outNumInts = tmpNumInts;
86 });
87}
88
89BufferDescriptor Gralloc::createDescriptor(const IMapper::BufferDescriptorInfo& descriptorInfo) {
90 BufferDescriptor descriptor;
91 mMapperV2_1->createDescriptor_2_1(
92 descriptorInfo, [&](const auto& tmpError, const auto& tmpDescriptor) {
93 ASSERT_EQ(Error::NONE, tmpError) << "failed to create descriptor";
94 descriptor = tmpDescriptor;
95 });
96
97 return descriptor;
98}
99
100const native_handle_t* Gralloc::allocate(const IMapper::BufferDescriptorInfo& descriptorInfo,
101 bool import, uint32_t* outStride) {
102 BufferDescriptor descriptor = createDescriptor(descriptorInfo);
103 if (::testing::Test::HasFatalFailure()) {
104 return nullptr;
105 }
106
107 auto buffers = V2_0::vts::Gralloc::allocate(descriptor, 1, import, outStride);
108 if (::testing::Test::HasFatalFailure()) {
109 return nullptr;
110 }
111
112 return buffers[0];
113}
114
115} // namespace vts
116} // namespace V2_1
117} // namespace mapper
118} // namespace graphics
119} // namespace hardware
120} // namespace android