summaryrefslogtreecommitdiffstats
blob: 0aaa9260185c521cb623a2ef259c257ab68e9056 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*
 * Copyright (C) 2017 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <mapper-vts/2.1/MapperVts.h>

#include <VtsHalHidlTargetTestBase.h>

namespace android {
namespace hardware {
namespace graphics {
namespace mapper {
namespace V2_1 {
namespace vts {

using V2_0::Error;

// abuse VTS to check binary compatibility between BufferDescriptorInfos
using OldBufferDescriptorInfo =
    android::hardware::graphics::mapper::V2_0::IMapper::BufferDescriptorInfo;
static_assert(sizeof(OldBufferDescriptorInfo) == sizeof(IMapper::BufferDescriptorInfo) &&
                  offsetof(OldBufferDescriptorInfo, width) ==
                      offsetof(IMapper::BufferDescriptorInfo, width) &&
                  offsetof(OldBufferDescriptorInfo, height) ==
                      offsetof(IMapper::BufferDescriptorInfo, height) &&
                  offsetof(OldBufferDescriptorInfo, layerCount) ==
                      offsetof(IMapper::BufferDescriptorInfo, layerCount) &&
                  offsetof(OldBufferDescriptorInfo, format) ==
                      offsetof(IMapper::BufferDescriptorInfo, format) &&
                  offsetof(OldBufferDescriptorInfo, usage) ==
                      offsetof(IMapper::BufferDescriptorInfo, usage),
              "");

Gralloc::Gralloc(const std::string& allocatorServiceName, const std::string& mapperServiceName)
    : V2_0::vts::Gralloc(allocatorServiceName, mapperServiceName) {
    if (::testing::Test::HasFatalFailure()) {
        return;
    }
    init();
}

void Gralloc::init() {
    mMapperV2_1 = IMapper::castFrom(V2_0::vts::Gralloc::getMapper());
    ASSERT_NE(nullptr, mMapperV2_1.get()) << "failed to get mapper 2.1 service";
}

sp<IMapper> Gralloc::getMapper() const {
    return mMapperV2_1;
}

bool Gralloc::validateBufferSize(const native_handle_t* bufferHandle,
                                 const IMapper::BufferDescriptorInfo& descriptorInfo,
                                 uint32_t stride) {
    auto buffer = const_cast<native_handle_t*>(bufferHandle);

    Error error = mMapperV2_1->validateBufferSize(buffer, descriptorInfo, stride);
    return error == Error::NONE;
}

void Gralloc::getTransportSize(const native_handle_t* bufferHandle, uint32_t* outNumFds,
                               uint32_t* outNumInts) {
    auto buffer = const_cast<native_handle_t*>(bufferHandle);

    *outNumFds = 0;
    *outNumInts = 0;
    mMapperV2_1->getTransportSize(
        buffer, [&](const auto& tmpError, const auto& tmpNumFds, const auto& tmpNumInts) {
            ASSERT_EQ(Error::NONE, tmpError) << "failed to get transport size";
            ASSERT_GE(bufferHandle->numFds, int(tmpNumFds)) << "invalid numFds " << tmpNumFds;
            ASSERT_GE(bufferHandle->numInts, int(tmpNumInts)) << "invalid numInts " << tmpNumInts;

            *outNumFds = tmpNumFds;
            *outNumInts = tmpNumInts;
        });
}

BufferDescriptor Gralloc::createDescriptor(const IMapper::BufferDescriptorInfo& descriptorInfo) {
    BufferDescriptor descriptor;
    mMapperV2_1->createDescriptor_2_1(
        descriptorInfo, [&](const auto& tmpError, const auto& tmpDescriptor) {
            ASSERT_EQ(Error::NONE, tmpError) << "failed to create descriptor";
            descriptor = tmpDescriptor;
        });

    return descriptor;
}

const native_handle_t* Gralloc::allocate(const IMapper::BufferDescriptorInfo& descriptorInfo,
                                         bool import, uint32_t* outStride) {
    BufferDescriptor descriptor = createDescriptor(descriptorInfo);
    if (::testing::Test::HasFatalFailure()) {
        return nullptr;
    }

    auto buffers = V2_0::vts::Gralloc::allocate(descriptor, 1, import, outStride);
    if (::testing::Test::HasFatalFailure()) {
        return nullptr;
    }

    return buffers[0];
}

}  // namespace vts
}  // namespace V2_1
}  // namespace mapper
}  // namespace graphics
}  // namespace hardware
}  // namespace android