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