1 /*
2 * Copyright (C) 2020 Arm Limited. All rights reserved.
3 *
4 * Copyright 2016 The Android Open Source Project
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 #include "GrallocMapper.h"
19 #include "hidl_common/BufferDescriptor.h"
20 #include "hidl_common/MapperMetadata.h"
21 #include "hidl_common/Mapper.h"
22
23 #include "allocator/mali_gralloc_ion.h"
24
25 namespace arm
26 {
27 namespace mapper
28 {
29
30 namespace hidl {
31 using android::hardware::graphics::mapper::V4_0::Error;
32 } // namespace hidl
33 using android::hardware::graphics::mapper::V4_0::BufferDescriptor;
34 using android::hardware::graphics::mapper::V4_0::IMapper;
35 using android::hardware::Return;
36 using android::hardware::hidl_handle;
37 using android::hardware::hidl_vec;
38 using android::hardware::Void;
39
GrallocMapper()40 GrallocMapper::GrallocMapper() {}
41
~GrallocMapper()42 GrallocMapper::~GrallocMapper() {}
43
createDescriptor(const BufferDescriptorInfo & descriptorInfo,createDescriptor_cb hidl_cb)44 Return<void> GrallocMapper::createDescriptor(const BufferDescriptorInfo &descriptorInfo, createDescriptor_cb hidl_cb)
45 {
46 if (common::validateDescriptorInfo(descriptorInfo))
47 {
48 hidl_cb(hidl::Error::NONE, common::grallocEncodeBufferDescriptor<uint8_t>(descriptorInfo));
49 }
50 else
51 {
52 MALI_GRALLOC_LOGE("Invalid attributes to create descriptor for Mapper 3.0");
53 hidl_cb(hidl::Error::BAD_VALUE, BufferDescriptor());
54 }
55
56 return Void();
57 }
58
importBuffer(const hidl_handle & rawHandle,importBuffer_cb hidl_cb)59 Return<void> GrallocMapper::importBuffer(const hidl_handle &rawHandle, importBuffer_cb hidl_cb)
60 {
61 if (!rawHandle.getNativeHandle()) {
62 hidl_cb(hidl::Error::BAD_BUFFER, nullptr);
63 return Void();
64 }
65
66 auto *inHandle = const_cast<native_handle_t *>(rawHandle.getNativeHandle());
67 buffer_handle_t outHandle = nullptr;
68
69 hidl::Error err = static_cast<hidl::Error>(
70 common::importBuffer(inHandle, &outHandle));
71
72 hidl_cb(err, static_cast<void*>(const_cast<native_handle_t*>(outHandle)));
73 return Void();
74 }
75
freeBuffer(void * buffer)76 Return<hidl::Error> GrallocMapper::freeBuffer(void *buffer)
77 {
78 buffer_handle_t handle = common::getBuffer(buffer);
79 if (handle == nullptr) return hidl::Error::BAD_BUFFER;
80 return static_cast<hidl::Error>(common::freeBuffer(handle));
81 }
82
validateBufferSize(void * buffer,const BufferDescriptorInfo & descriptorInfo,uint32_t in_stride)83 Return<hidl::Error> GrallocMapper::validateBufferSize(void *buffer, const BufferDescriptorInfo &descriptorInfo,
84 uint32_t in_stride)
85 {
86 /* All Gralloc allocated buffers must be conform to local descriptor validation */
87 if (!common::validateDescriptorInfo<BufferDescriptorInfo>(descriptorInfo))
88 {
89 MALI_GRALLOC_LOGE("Invalid descriptor attributes for validating buffer size");
90 return hidl::Error::BAD_VALUE;
91 }
92 return static_cast<hidl::Error>(common::validateBufferSize(buffer, descriptorInfo, in_stride));
93 }
94
getFenceFd(const hidl_handle & fenceHandle,int * outFenceFd)95 static bool getFenceFd(const hidl_handle &fenceHandle, int *outFenceFd) {
96 auto const handle = fenceHandle.getNativeHandle();
97 if (handle && handle->numFds > 1) {
98 MALI_GRALLOC_LOGE("Invalid fence handle with %d fds",
99 handle->numFds);
100 return false;
101 }
102
103 *outFenceFd = (handle && handle->numFds == 1) ? handle->data[0] : -1;
104 return true;
105 }
106
lock(void * buffer,uint64_t cpuUsage,const IMapper::Rect & accessRegion,const hidl_handle & acquireFence,lock_cb hidl_cb)107 Return<void> GrallocMapper::lock(void *buffer, uint64_t cpuUsage, const IMapper::Rect &accessRegion,
108 const hidl_handle &acquireFence, lock_cb hidl_cb)
109 {
110 void *outData = nullptr;
111 int fenceFd;
112 if (!getFenceFd(acquireFence, &fenceFd)) {
113 hidl_cb(hidl::Error::BAD_VALUE, nullptr);
114 return Void();
115 }
116
117 hidl::Error err = static_cast<hidl::Error>(
118 common::lock(static_cast<buffer_handle_t>(buffer), cpuUsage,
119 common::GrallocRect(accessRegion), fenceFd, &outData));
120 if (err != hidl::Error::NONE) outData = nullptr;
121 hidl_cb(err, outData);
122 return Void();
123 }
124
125 /*
126 * Populates the HIDL fence handle for the given fence object
127 *
128 * @param fenceFd [in] Fence file descriptor
129 * @param handleStorage [in] HIDL handle storage for fence
130 *
131 * @return HIDL fence handle
132 */
buildFenceHandle(int fenceFd,char * handleStorage)133 static hidl_handle buildFenceHandle(int fenceFd, char *handleStorage) {
134 native_handle_t *handle = nullptr;
135 if (fenceFd >= 0) {
136 handle = native_handle_init(handleStorage, 1, 0);
137 handle->data[0] = fenceFd;
138 }
139
140 return hidl_handle(handle);
141 }
142
unlock(void * buffer,unlock_cb hidl_cb)143 Return<void> GrallocMapper::unlock(void *buffer, unlock_cb hidl_cb)
144 {
145 int fenceFd = -1;
146 const native_handle_t *handle = common::getBuffer(buffer);
147
148 if (handle == nullptr) {
149 hidl_cb(hidl::Error::BAD_BUFFER, nullptr);
150 }
151
152 hidl::Error err =
153 static_cast<hidl::Error>(common::unlock(handle, &fenceFd));
154 if (err == hidl::Error::NONE) {
155 NATIVE_HANDLE_DECLARE_STORAGE(fenceStorage, 1, 0);
156 hidl_cb(err, buildFenceHandle(fenceFd, fenceStorage));
157
158 if (fenceFd >= 0) {
159 close(fenceFd);
160 }
161 } else {
162 hidl_cb(err, nullptr);
163 }
164 return Void();
165 }
166
flushLockedBuffer(void * buffer,flushLockedBuffer_cb hidl_cb)167 Return<void> GrallocMapper::flushLockedBuffer(void *buffer, flushLockedBuffer_cb hidl_cb)
168 {
169 hidl::Error err = static_cast<hidl::Error>(common::flushLockedBuffer(static_cast<buffer_handle_t>(buffer)));
170 hidl_cb(err, hidl_handle{});
171 return Void();
172 }
173
rereadLockedBuffer(void * buffer)174 Return<hidl::Error> GrallocMapper::rereadLockedBuffer(void *buffer)
175 {
176 return static_cast<hidl::Error>(common::rereadLockedBuffer(common::getBuffer(buffer)));
177 }
178
get(void * buffer,const MetadataType & metadataType,IMapper::get_cb hidl_cb)179 Return<void> GrallocMapper::get(void *buffer, const MetadataType &metadataType, IMapper::get_cb hidl_cb)
180 {
181 std::vector<uint8_t> vec;
182 hidl::Error err = static_cast<hidl::Error>(
183 common::get(common::getBuffer(buffer), common::MetadataType(metadataType), vec));
184 hidl_cb(err, hidl_vec(vec));
185 return Void();
186 }
187
set(void * buffer,const MetadataType & metadataType,const hidl_vec<uint8_t> & metadata)188 Return<hidl::Error> GrallocMapper::set(void *buffer, const MetadataType &metadataType, const hidl_vec<uint8_t> &metadata)
189 {
190 buffer_handle_t bufferHandle = common::getBuffer(buffer);
191 return static_cast<hidl::Error>(common::set(bufferHandle, common::MetadataType(metadataType), metadata));
192 }
193
getFromBufferDescriptorInfo(const BufferDescriptorInfo & description,const MetadataType & metadataType,getFromBufferDescriptorInfo_cb hidl_cb)194 Return<void> GrallocMapper::getFromBufferDescriptorInfo(const BufferDescriptorInfo &description,
195 const MetadataType &metadataType,
196 getFromBufferDescriptorInfo_cb hidl_cb)
197 {
198 std::vector<uint8_t> vec;
199 hidl::Error err = static_cast<hidl::Error>(common::getFromBufferDescriptorInfo(
200 description, common::MetadataType(metadataType), vec));
201 hidl_cb(err, vec);
202 return Void();
203 }
204
getTransportSize(void * buffer,getTransportSize_cb hidl_cb)205 Return<void> GrallocMapper::getTransportSize(void *buffer, getTransportSize_cb hidl_cb)
206 {
207 uint32_t outNumFds = 0, outNumInts = 0;
208 buffer_handle_t bufferHandle = common::getBuffer(buffer);
209 hidl::Error err = static_cast<hidl::Error>(common::getTransportSize(bufferHandle, &outNumFds, &outNumInts));
210 hidl_cb(err, outNumFds, outNumInts);
211 return Void();
212 }
213
isSupported(const IMapper::BufferDescriptorInfo & description,isSupported_cb hidl_cb)214 Return<void> GrallocMapper::isSupported(const IMapper::BufferDescriptorInfo &description, isSupported_cb hidl_cb)
215 {
216 if (!common::validateDescriptorInfo<BufferDescriptorInfo>(description))
217 {
218 MALI_GRALLOC_LOGE("Invalid descriptor attributes for validating buffer size");
219 hidl_cb(hidl::Error::BAD_VALUE, false);
220 }
221 hidl_cb(hidl::Error::NONE, common::isSupported(description));
222 return Void();
223 }
224
listSupportedMetadataTypes(listSupportedMetadataTypes_cb hidl_cb)225 Return<void> GrallocMapper::listSupportedMetadataTypes(listSupportedMetadataTypes_cb hidl_cb)
226 {
227 std::vector<common::MetadataTypeDescription> desc = common::listSupportedMetadataTypes();
228 std::vector<IMapper::MetadataTypeDescription> hidl_description(desc.size());
229 std::copy(desc.begin(), desc.end(), hidl_description.begin());
230 hidl_cb(hidl::Error::NONE, hidl_vec(hidl_description));
231 return Void();
232 }
233
dumpBuffer(void * buffer,dumpBuffer_cb hidl_cb)234 Return<void> GrallocMapper::dumpBuffer(void *buffer, dumpBuffer_cb hidl_cb)
235 {
236 common::BufferDump out;
237 hidl::Error err = static_cast<hidl::Error>(common::dumpBuffer(common::getBuffer(buffer), out));
238 hidl_cb(err, static_cast<IMapper::BufferDump>(out));
239 return Void();
240 }
241
dumpBuffers(dumpBuffers_cb hidl_cb)242 Return<void> GrallocMapper::dumpBuffers(dumpBuffers_cb hidl_cb)
243 {
244 auto bufferDump = common::dumpBuffers();
245 std::vector<IMapper::BufferDump> outBufDump;
246 for (auto dump : bufferDump) {
247 outBufDump.push_back(static_cast<IMapper::BufferDump>(dump));
248 }
249 hidl_cb(hidl::Error::NONE, hidl_vec(outBufDump));
250 return Void();
251 }
252
getReservedRegion(void * buffer,getReservedRegion_cb hidl_cb)253 Return<void> GrallocMapper::getReservedRegion(void *buffer, getReservedRegion_cb hidl_cb)
254 {
255 void *reservedRegion = nullptr;
256 uint64_t reservedSize = 0;
257 hidl::Error err = static_cast<hidl::Error>(
258 common::getReservedRegion(static_cast<buffer_handle_t>(buffer),
259 &reservedRegion, reservedSize));
260 if (err != hidl::Error::NONE) {
261 reservedRegion = nullptr;
262 reservedSize = 0;
263 }
264 hidl_cb(err, reservedRegion, reservedSize);
265 return Void();
266 }
267
268 } // namespace mapper
269 } // namespace arm
270
HIDL_FETCH_IMapper(const char *)271 extern "C" IMapper *HIDL_FETCH_IMapper(const char * /* name */)
272 {
273 MALI_GRALLOC_LOGV("Arm Module IMapper %d.%d , pid = %d", GRALLOC_VERSION_MAJOR,
274 (HIDL_MAPPER_VERSION_SCALED - (GRALLOC_VERSION_MAJOR * 100)) / 10, getpid());
275
276 return new arm::mapper::GrallocMapper();
277 }
278