blob: f6a26ac80eb5c4d027924fb4a0528bc72769340b [file] [log] [blame]
Chia-I Wu89d09dd2017-02-24 10:41:35 -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
Yuexi Maed2bb4e2017-03-10 00:44:45 -080017#include <VtsHalHidlTargetTestBase.h>
Chia-I Wu89d09dd2017-02-24 10:41:35 -080018
19#include "VtsHalGraphicsMapperTestUtils.h"
20
21namespace android {
22namespace hardware {
23namespace graphics {
24namespace mapper {
25namespace V2_0 {
26namespace tests {
27
28using android::hardware::graphics::allocator::V2_0::Buffer;
29using android::hardware::graphics::allocator::V2_0::BufferDescriptor;
30using android::hardware::graphics::allocator::V2_0::Error;
31
32Mapper::Mapper() { init(); }
33
34void Mapper::init() {
Yuexi Maed2bb4e2017-03-10 00:44:45 -080035 mMapper = ::testing::VtsHalHidlTargetTestBase::getService<IMapper>();
Chia-I Wu89d09dd2017-02-24 10:41:35 -080036 ASSERT_NE(nullptr, mMapper.get()) << "failed to get mapper service";
37 ASSERT_FALSE(mMapper->isRemote()) << "mapper is not in passthrough mode";
38}
39
40Mapper::~Mapper() {
41 for (auto it : mHandles) {
42 while (it.second) {
43 EXPECT_EQ(Error::NONE, mMapper->release(it.first))
44 << "failed to release handle " << it.first;
45 it.second--;
46 }
47 }
48 mHandles.clear();
49}
50
51sp<IMapper> Mapper::getRaw() const { return mMapper; }
52
53void Mapper::retain(const native_handle_t* handle) {
54 Error error = mMapper->retain(handle);
55 ASSERT_EQ(Error::NONE, error) << "failed to retain handle " << handle;
56
57 mHandles[handle]++;
58}
59
60void Mapper::release(const native_handle_t* handle) {
61 Error error = mMapper->release(handle);
62 ASSERT_EQ(Error::NONE, error) << "failed to release handle " << handle;
63
64 if (--mHandles[handle] == 0) {
65 mHandles.erase(handle);
66 }
67}
68
69Mapper::Dimensions Mapper::getDimensions(const native_handle_t* handle) {
70 Dimensions dimensions = {};
71 mMapper->getDimensions(handle, [&](const auto& tmpError, const auto& tmpWidth,
72 const auto& tmpHeight) {
73 ASSERT_EQ(Error::NONE, tmpError)
74 << "failed to get dimensions for handle " << handle;
75 dimensions.width = tmpWidth;
76 dimensions.height = tmpHeight;
77 });
78
79 return dimensions;
80}
81
82PixelFormat Mapper::getFormat(const native_handle_t* handle) {
83 PixelFormat format = static_cast<PixelFormat>(0);
84 mMapper->getFormat(handle, [&](const auto& tmpError, const auto& tmpFormat) {
85 ASSERT_EQ(Error::NONE, tmpError)
86 << "failed to get format for handle " << handle;
87 format = tmpFormat;
88 });
89
90 return format;
91}
92
93uint32_t Mapper::getLayerCount(const native_handle_t* handle) {
94 uint32_t count = 0;
95 mMapper->getLayerCount(
96 handle, [&](const auto& tmpError, const auto& tmpCount) {
97 ASSERT_EQ(Error::NONE, tmpError)
98 << "failed to get layer count for handle " << handle;
99 count = tmpCount;
100 });
101
102 return count;
103}
104
105uint64_t Mapper::getProducerUsageMask(const native_handle_t* handle) {
106 uint64_t usageMask = 0;
107 mMapper->getProducerUsageMask(
108 handle, [&](const auto& tmpError, const auto& tmpUsageMask) {
109 ASSERT_EQ(Error::NONE, tmpError)
110 << "failed to get producer usage mask for handle " << handle;
111 usageMask = tmpUsageMask;
112 });
113
114 return usageMask;
115}
116
117uint64_t Mapper::getConsumerUsageMask(const native_handle_t* handle) {
118 uint64_t usageMask = 0;
119 mMapper->getConsumerUsageMask(
120 handle, [&](const auto& tmpError, const auto& tmpUsageMask) {
121 ASSERT_EQ(Error::NONE, tmpError)
122 << "failed to get consumer usage mask for handle " << handle;
123 usageMask = tmpUsageMask;
124 });
125
126 return usageMask;
127}
128
129BackingStore Mapper::getBackingStore(const native_handle_t* handle) {
130 BackingStore backingStore = 0;
131 mMapper->getBackingStore(
132 handle, [&](const auto& tmpError, const auto& tmpBackingStore) {
133 ASSERT_EQ(Error::NONE, tmpError)
134 << "failed to get backing store for handle " << handle;
135 backingStore = tmpBackingStore;
136 });
137
138 return backingStore;
139}
140
141uint32_t Mapper::getStride(const native_handle_t* handle) {
142 uint32_t stride = 0;
143 mMapper->getStride(handle, [&](const auto& tmpError, const auto& tmpStride) {
144 ASSERT_EQ(Error::NONE, tmpError)
145 << "failed to get stride for handle " << handle;
146 stride = tmpStride;
147 });
148
149 return stride;
150}
151
152void* Mapper::lock(const native_handle_t* handle, uint64_t producerUsageMask,
153 uint64_t consumerUsageMask,
154 const IMapper::Rect& accessRegion, int acquireFence) {
155 NATIVE_HANDLE_DECLARE_STORAGE(acquireFenceStorage, 0, 1);
156 native_handle_t* acquireFenceHandle = nullptr;
157 if (acquireFence >= 0) {
158 acquireFenceHandle = native_handle_init(acquireFenceStorage, 0, 1);
159 acquireFenceHandle->data[0] = acquireFence;
160 }
161
162 void* data = nullptr;
163 mMapper->lock(
164 handle, producerUsageMask, consumerUsageMask, accessRegion,
165 acquireFenceHandle, [&](const auto& tmpError, const auto& tmpData) {
166 ASSERT_EQ(Error::NONE, tmpError) << "failed to lock handle " << handle;
167 data = tmpData;
168 });
169
170 if (acquireFence >= 0) {
171 close(acquireFence);
172 }
173
174 return data;
175}
176
177FlexLayout Mapper::lockFlex(const native_handle_t* handle,
178 uint64_t producerUsageMask,
179 uint64_t consumerUsageMask,
180 const IMapper::Rect& accessRegion,
181 int acquireFence) {
182 NATIVE_HANDLE_DECLARE_STORAGE(acquireFenceStorage, 0, 1);
183 native_handle_t* acquireFenceHandle = nullptr;
184 if (acquireFence >= 0) {
185 acquireFenceHandle = native_handle_init(acquireFenceStorage, 0, 1);
186 acquireFenceHandle->data[0] = acquireFence;
187 }
188
189 FlexLayout layout = {};
190 mMapper->lockFlex(handle, producerUsageMask, consumerUsageMask, accessRegion,
191 acquireFenceHandle,
192 [&](const auto& tmpError, const auto& tmpLayout) {
193 ASSERT_EQ(Error::NONE, tmpError)
194 << "failed to lockFlex handle " << handle;
195 layout = tmpLayout;
196 });
197
198 if (acquireFence >= 0) {
199 close(acquireFence);
200 }
201
202 return layout;
203}
204
205int Mapper::unlock(const native_handle_t* handle) {
206 int releaseFence = -1;
207 mMapper->unlock(handle, [&](const auto& tmpError,
208 const auto& tmpReleaseFence) {
209 ASSERT_EQ(Error::NONE, tmpError) << "failed to unlock handle " << handle;
210
211 auto handle = tmpReleaseFence.getNativeHandle();
212 if (handle) {
213 ASSERT_EQ(0, handle->numInts) << "invalid fence handle " << handle;
214 if (handle->numFds == 1) {
215 releaseFence = dup(handle->data[0]);
216 ASSERT_LT(0, releaseFence) << "failed to dup fence fd";
217 } else {
218 ASSERT_EQ(0, handle->numFds) << " invalid fence handle " << handle;
219 }
220 }
221 });
222
223 return releaseFence;
224}
225
226const native_handle_t* Mapper::allocate(
227 std::unique_ptr<AllocatorClient>& allocatorClient,
228 const IAllocatorClient::BufferDescriptorInfo& info) {
229 BufferDescriptor descriptor = allocatorClient->createDescriptor(info);
230 if (::testing::Test::HasFatalFailure()) {
231 return nullptr;
232 }
233
234 Buffer buffer = allocatorClient->allocate(descriptor);
235 if (::testing::Test::HasFatalFailure()) {
236 allocatorClient->destroyDescriptor(descriptor);
237 return nullptr;
238 }
239
240 const native_handle_t* handle =
241 allocatorClient->exportHandle(descriptor, buffer);
242 if (handle) {
243 retain(handle);
244 }
245
246 allocatorClient->free(buffer);
247 allocatorClient->destroyDescriptor(descriptor);
248
249 return handle;
250}
251
252} // namespace tests
253} // namespace V2_0
254} // namespace mapper
255} // namespace graphics
256} // namespace hardware
257} // namespace android