blob: acb6b01188db3d335d7bfcd15e193a5e4bf728b0 [file] [log] [blame]
Marissa Wall925bf7f2018-12-29 14:27:11 -08001/*
2 * Copyright 2016 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#define LOG_TAG "Gralloc3"
18
19#include <hidl/ServiceManagement.h>
20#include <hwbinder/IPCThreadState.h>
21#include <ui/Gralloc3.h>
22
23#include <inttypes.h>
24#include <log/log.h>
25#pragma clang diagnostic push
26#pragma clang diagnostic ignored "-Wzero-length-array"
27#include <sync/sync.h>
28#pragma clang diagnostic pop
29
30using android::hardware::graphics::allocator::V3_0::IAllocator;
31using android::hardware::graphics::common::V1_1::BufferUsage;
32using android::hardware::graphics::mapper::V3_0::BufferDescriptor;
33using android::hardware::graphics::mapper::V3_0::Error;
34using android::hardware::graphics::mapper::V3_0::IMapper;
35using android::hardware::graphics::mapper::V3_0::YCbCrLayout;
36
37namespace android {
38
39namespace {
40
41static constexpr Error kTransactionError = Error::NO_RESOURCES;
42
43uint64_t getValidUsageBits() {
44 static const uint64_t validUsageBits = []() -> uint64_t {
45 uint64_t bits = 0;
46 for (const auto bit :
47 hardware::hidl_enum_range<hardware::graphics::common::V1_0::BufferUsage>()) {
48 bits = bits | bit;
49 }
50 for (const auto bit :
51 hardware::hidl_enum_range<hardware::graphics::common::V1_1::BufferUsage>()) {
52 bits = bits | bit;
53 }
54 return bits;
55 }();
56 return validUsageBits;
57}
58
59static inline IMapper::Rect sGralloc3Rect(const Rect& rect) {
60 IMapper::Rect outRect{};
61 outRect.left = rect.left;
62 outRect.top = rect.top;
63 outRect.width = rect.width();
64 outRect.height = rect.height();
65 return outRect;
66}
67static inline void sBufferDescriptorInfo(uint32_t width, uint32_t height,
68 android::PixelFormat format, uint32_t layerCount,
69 uint64_t usage,
70 IMapper::BufferDescriptorInfo* outDescriptorInfo) {
71 outDescriptorInfo->width = width;
72 outDescriptorInfo->height = height;
73 outDescriptorInfo->layerCount = layerCount;
74 outDescriptorInfo->format = static_cast<hardware::graphics::common::V1_1::PixelFormat>(format);
75 outDescriptorInfo->usage = usage;
76}
77
78} // anonymous namespace
79
80void Gralloc3Mapper::preload() {
81 android::hardware::preloadPassthroughService<IMapper>();
82}
83
84Gralloc3Mapper::Gralloc3Mapper() {
85 mMapper = IMapper::getService();
86 if (mMapper == nullptr) {
87 ALOGW("mapper 3.x is not supported");
88 return;
89 }
90 if (mMapper->isRemote()) {
91 LOG_ALWAYS_FATAL("gralloc-mapper must be in passthrough mode");
92 }
93}
94
95bool Gralloc3Mapper::isSupported() const {
96 return mMapper != nullptr;
97}
98
99status_t Gralloc3Mapper::validateBufferDescriptorInfo(
100 IMapper::BufferDescriptorInfo* descriptorInfo) const {
101 uint64_t validUsageBits = getValidUsageBits();
102
103 if (descriptorInfo->usage & ~validUsageBits) {
104 ALOGE("buffer descriptor contains invalid usage bits 0x%" PRIx64,
105 descriptorInfo->usage & ~validUsageBits);
106 return BAD_VALUE;
107 }
108 return NO_ERROR;
109}
110
111status_t Gralloc3Mapper::createDescriptor(void* bufferDescriptorInfo,
112 void* outBufferDescriptor) const {
113 IMapper::BufferDescriptorInfo* descriptorInfo =
114 static_cast<IMapper::BufferDescriptorInfo*>(bufferDescriptorInfo);
115 BufferDescriptor* outDescriptor = static_cast<BufferDescriptor*>(outBufferDescriptor);
116
117 status_t status = validateBufferDescriptorInfo(descriptorInfo);
118 if (status != NO_ERROR) {
119 return status;
120 }
121
122 Error error;
123 auto hidl_cb = [&](const auto& tmpError, const auto& tmpDescriptor) {
124 error = tmpError;
125 if (error != Error::NONE) {
126 return;
127 }
128 *outDescriptor = tmpDescriptor;
129 };
130
131 hardware::Return<void> ret = mMapper->createDescriptor(*descriptorInfo, hidl_cb);
132
133 return static_cast<status_t>((ret.isOk()) ? error : kTransactionError);
134}
135
136status_t Gralloc3Mapper::importBuffer(const hardware::hidl_handle& rawHandle,
137 buffer_handle_t* outBufferHandle) const {
138 Error error;
139 auto ret = mMapper->importBuffer(rawHandle, [&](const auto& tmpError, const auto& tmpBuffer) {
140 error = tmpError;
141 if (error != Error::NONE) {
142 return;
143 }
144 *outBufferHandle = static_cast<buffer_handle_t>(tmpBuffer);
145 });
146
147 return static_cast<status_t>((ret.isOk()) ? error : kTransactionError);
148}
149
150void Gralloc3Mapper::freeBuffer(buffer_handle_t bufferHandle) const {
151 auto buffer = const_cast<native_handle_t*>(bufferHandle);
152 auto ret = mMapper->freeBuffer(buffer);
153
154 auto error = (ret.isOk()) ? static_cast<Error>(ret) : kTransactionError;
155 ALOGE_IF(error != Error::NONE, "freeBuffer(%p) failed with %d", buffer, error);
156}
157
158status_t Gralloc3Mapper::validateBufferSize(buffer_handle_t bufferHandle, uint32_t width,
159 uint32_t height, android::PixelFormat format,
160 uint32_t layerCount, uint64_t usage,
161 uint32_t stride) const {
162 IMapper::BufferDescriptorInfo descriptorInfo;
163 sBufferDescriptorInfo(width, height, format, layerCount, usage, &descriptorInfo);
164
165 auto buffer = const_cast<native_handle_t*>(bufferHandle);
166 auto ret = mMapper->validateBufferSize(buffer, descriptorInfo, stride);
167
168 return static_cast<status_t>((ret.isOk()) ? static_cast<Error>(ret) : kTransactionError);
169}
170
171void Gralloc3Mapper::getTransportSize(buffer_handle_t bufferHandle, uint32_t* outNumFds,
172 uint32_t* outNumInts) const {
173 *outNumFds = uint32_t(bufferHandle->numFds);
174 *outNumInts = uint32_t(bufferHandle->numInts);
175
176 Error error;
177 auto buffer = const_cast<native_handle_t*>(bufferHandle);
178 auto ret = mMapper->getTransportSize(buffer,
179 [&](const auto& tmpError, const auto& tmpNumFds,
180 const auto& tmpNumInts) {
181 error = tmpError;
182 if (error != Error::NONE) {
183 return;
184 }
185 *outNumFds = tmpNumFds;
186 *outNumInts = tmpNumInts;
187 });
188
189 error = (ret.isOk()) ? error : kTransactionError;
190
191 ALOGE_IF(error != Error::NONE, "getTransportSize(%p) failed with %d", buffer, error);
192}
193
194status_t Gralloc3Mapper::lock(buffer_handle_t bufferHandle, uint64_t usage, const Rect& bounds,
Valerie Hau0c9fc362019-01-22 09:17:19 -0800195 int acquireFence, void** outData, int32_t* outBytesPerPixel,
196 int32_t* outBytesPerStride) const {
Marissa Wall925bf7f2018-12-29 14:27:11 -0800197 auto buffer = const_cast<native_handle_t*>(bufferHandle);
198
199 IMapper::Rect accessRegion = sGralloc3Rect(bounds);
200
201 // put acquireFence in a hidl_handle
202 hardware::hidl_handle acquireFenceHandle;
203 NATIVE_HANDLE_DECLARE_STORAGE(acquireFenceStorage, 1, 0);
204 if (acquireFence >= 0) {
205 auto h = native_handle_init(acquireFenceStorage, 1, 0);
206 h->data[0] = acquireFence;
207 acquireFenceHandle = h;
208 }
209
210 Error error;
211 auto ret = mMapper->lock(buffer, usage, accessRegion, acquireFenceHandle,
Valerie Hau0c9fc362019-01-22 09:17:19 -0800212 [&](const auto& tmpError, const auto& tmpData,
213 const auto& tmpBytesPerPixel, const auto& tmpBytesPerStride) {
Marissa Wall925bf7f2018-12-29 14:27:11 -0800214 error = tmpError;
215 if (error != Error::NONE) {
216 return;
217 }
218 *outData = tmpData;
Valerie Hau0c9fc362019-01-22 09:17:19 -0800219 if (outBytesPerPixel) {
220 *outBytesPerPixel = tmpBytesPerPixel;
221 }
222 if (outBytesPerStride) {
223 *outBytesPerStride = tmpBytesPerStride;
224 }
Marissa Wall925bf7f2018-12-29 14:27:11 -0800225 });
226
227 // we own acquireFence even on errors
228 if (acquireFence >= 0) {
229 close(acquireFence);
230 }
231
232 error = (ret.isOk()) ? error : kTransactionError;
233
234 ALOGW_IF(error != Error::NONE, "lock(%p, ...) failed: %d", bufferHandle, error);
235
236 return static_cast<status_t>(error);
237}
238
239status_t Gralloc3Mapper::lock(buffer_handle_t bufferHandle, uint64_t usage, const Rect& bounds,
240 int acquireFence, android_ycbcr* ycbcr) const {
241 auto buffer = const_cast<native_handle_t*>(bufferHandle);
242
243 IMapper::Rect accessRegion = sGralloc3Rect(bounds);
244
245 // put acquireFence in a hidl_handle
246 hardware::hidl_handle acquireFenceHandle;
247 NATIVE_HANDLE_DECLARE_STORAGE(acquireFenceStorage, 1, 0);
248 if (acquireFence >= 0) {
249 auto h = native_handle_init(acquireFenceStorage, 1, 0);
250 h->data[0] = acquireFence;
251 acquireFenceHandle = h;
252 }
253
254 YCbCrLayout layout;
255 Error error;
256 auto ret = mMapper->lockYCbCr(buffer, usage, accessRegion, acquireFenceHandle,
257 [&](const auto& tmpError, const auto& tmpLayout) {
258 error = tmpError;
259 if (error != Error::NONE) {
260 return;
261 }
262
263 layout = tmpLayout;
264 });
265
266 if (error == Error::NONE) {
267 ycbcr->y = layout.y;
268 ycbcr->cb = layout.cb;
269 ycbcr->cr = layout.cr;
270 ycbcr->ystride = static_cast<size_t>(layout.yStride);
271 ycbcr->cstride = static_cast<size_t>(layout.cStride);
272 ycbcr->chroma_step = static_cast<size_t>(layout.chromaStep);
273 }
274
275 // we own acquireFence even on errors
276 if (acquireFence >= 0) {
277 close(acquireFence);
278 }
279
280 return static_cast<status_t>((ret.isOk()) ? error : kTransactionError);
281}
282
283int Gralloc3Mapper::unlock(buffer_handle_t bufferHandle) const {
284 auto buffer = const_cast<native_handle_t*>(bufferHandle);
285
286 int releaseFence = -1;
287 Error error;
288 auto ret = mMapper->unlock(buffer, [&](const auto& tmpError, const auto& tmpReleaseFence) {
289 error = tmpError;
290 if (error != Error::NONE) {
291 return;
292 }
293
294 auto fenceHandle = tmpReleaseFence.getNativeHandle();
295 if (fenceHandle && fenceHandle->numFds == 1) {
296 int fd = dup(fenceHandle->data[0]);
297 if (fd >= 0) {
298 releaseFence = fd;
299 } else {
300 ALOGD("failed to dup unlock release fence");
301 sync_wait(fenceHandle->data[0], -1);
302 }
303 }
304 });
305
306 if (!ret.isOk()) {
307 error = kTransactionError;
308 }
309
310 if (error != Error::NONE) {
311 ALOGE("unlock(%p) failed with %d", buffer, error);
312 }
313
314 return releaseFence;
315}
316
317Gralloc3Allocator::Gralloc3Allocator(const Gralloc3Mapper& mapper) : mMapper(mapper) {
318 mAllocator = IAllocator::getService();
319 if (mAllocator == nullptr) {
320 ALOGW("allocator 3.x is not supported");
321 return;
322 }
323}
324
325bool Gralloc3Allocator::isSupported() const {
326 return mAllocator != nullptr;
327}
328
329std::string Gralloc3Allocator::dumpDebugInfo() const {
330 std::string debugInfo;
331
332 mAllocator->dumpDebugInfo([&](const auto& tmpDebugInfo) { debugInfo = tmpDebugInfo.c_str(); });
333
334 return debugInfo;
335}
336
337status_t Gralloc3Allocator::allocate(uint32_t width, uint32_t height, android::PixelFormat format,
338 uint32_t layerCount, uint64_t usage, uint32_t bufferCount,
339 uint32_t* outStride, buffer_handle_t* outBufferHandles) const {
340 IMapper::BufferDescriptorInfo descriptorInfo;
341 sBufferDescriptorInfo(width, height, format, layerCount, usage, &descriptorInfo);
342
343 BufferDescriptor descriptor;
344 status_t error = mMapper.createDescriptor(static_cast<void*>(&descriptorInfo),
345 static_cast<void*>(&descriptor));
346 if (error != NO_ERROR) {
347 return error;
348 }
349
350 auto ret = mAllocator->allocate(descriptor, bufferCount,
351 [&](const auto& tmpError, const auto& tmpStride,
352 const auto& tmpBuffers) {
353 error = static_cast<status_t>(tmpError);
354 if (tmpError != Error::NONE) {
355 return;
356 }
357
358 // import buffers
359 for (uint32_t i = 0; i < bufferCount; i++) {
360 error = mMapper.importBuffer(tmpBuffers[i],
361 &outBufferHandles[i]);
362 if (error != NO_ERROR) {
363 for (uint32_t j = 0; j < i; j++) {
364 mMapper.freeBuffer(outBufferHandles[j]);
365 outBufferHandles[j] = nullptr;
366 }
367 return;
368 }
369 }
370 *outStride = tmpStride;
371 });
372
373 // make sure the kernel driver sees BC_FREE_BUFFER and closes the fds now
374 hardware::IPCThreadState::self()->flushCommands();
375
376 return (ret.isOk()) ? error : static_cast<status_t>(kTransactionError);
377}
378
379} // namespace android