blob: 128200e33e8a7c969ec869fa1e1c8ea52a561132 [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,
195 int acquireFence, void** outData) const {
196 auto buffer = const_cast<native_handle_t*>(bufferHandle);
197
198 IMapper::Rect accessRegion = sGralloc3Rect(bounds);
199
200 // put acquireFence in a hidl_handle
201 hardware::hidl_handle acquireFenceHandle;
202 NATIVE_HANDLE_DECLARE_STORAGE(acquireFenceStorage, 1, 0);
203 if (acquireFence >= 0) {
204 auto h = native_handle_init(acquireFenceStorage, 1, 0);
205 h->data[0] = acquireFence;
206 acquireFenceHandle = h;
207 }
208
209 Error error;
210 auto ret = mMapper->lock(buffer, usage, accessRegion, acquireFenceHandle,
211 [&](const auto& tmpError, const auto& tmpData) {
212 error = tmpError;
213 if (error != Error::NONE) {
214 return;
215 }
216 *outData = tmpData;
217 });
218
219 // we own acquireFence even on errors
220 if (acquireFence >= 0) {
221 close(acquireFence);
222 }
223
224 error = (ret.isOk()) ? error : kTransactionError;
225
226 ALOGW_IF(error != Error::NONE, "lock(%p, ...) failed: %d", bufferHandle, error);
227
228 return static_cast<status_t>(error);
229}
230
231status_t Gralloc3Mapper::lock(buffer_handle_t bufferHandle, uint64_t usage, const Rect& bounds,
232 int acquireFence, android_ycbcr* ycbcr) const {
233 auto buffer = const_cast<native_handle_t*>(bufferHandle);
234
235 IMapper::Rect accessRegion = sGralloc3Rect(bounds);
236
237 // put acquireFence in a hidl_handle
238 hardware::hidl_handle acquireFenceHandle;
239 NATIVE_HANDLE_DECLARE_STORAGE(acquireFenceStorage, 1, 0);
240 if (acquireFence >= 0) {
241 auto h = native_handle_init(acquireFenceStorage, 1, 0);
242 h->data[0] = acquireFence;
243 acquireFenceHandle = h;
244 }
245
246 YCbCrLayout layout;
247 Error error;
248 auto ret = mMapper->lockYCbCr(buffer, usage, accessRegion, acquireFenceHandle,
249 [&](const auto& tmpError, const auto& tmpLayout) {
250 error = tmpError;
251 if (error != Error::NONE) {
252 return;
253 }
254
255 layout = tmpLayout;
256 });
257
258 if (error == Error::NONE) {
259 ycbcr->y = layout.y;
260 ycbcr->cb = layout.cb;
261 ycbcr->cr = layout.cr;
262 ycbcr->ystride = static_cast<size_t>(layout.yStride);
263 ycbcr->cstride = static_cast<size_t>(layout.cStride);
264 ycbcr->chroma_step = static_cast<size_t>(layout.chromaStep);
265 }
266
267 // we own acquireFence even on errors
268 if (acquireFence >= 0) {
269 close(acquireFence);
270 }
271
272 return static_cast<status_t>((ret.isOk()) ? error : kTransactionError);
273}
274
275int Gralloc3Mapper::unlock(buffer_handle_t bufferHandle) const {
276 auto buffer = const_cast<native_handle_t*>(bufferHandle);
277
278 int releaseFence = -1;
279 Error error;
280 auto ret = mMapper->unlock(buffer, [&](const auto& tmpError, const auto& tmpReleaseFence) {
281 error = tmpError;
282 if (error != Error::NONE) {
283 return;
284 }
285
286 auto fenceHandle = tmpReleaseFence.getNativeHandle();
287 if (fenceHandle && fenceHandle->numFds == 1) {
288 int fd = dup(fenceHandle->data[0]);
289 if (fd >= 0) {
290 releaseFence = fd;
291 } else {
292 ALOGD("failed to dup unlock release fence");
293 sync_wait(fenceHandle->data[0], -1);
294 }
295 }
296 });
297
298 if (!ret.isOk()) {
299 error = kTransactionError;
300 }
301
302 if (error != Error::NONE) {
303 ALOGE("unlock(%p) failed with %d", buffer, error);
304 }
305
306 return releaseFence;
307}
308
309Gralloc3Allocator::Gralloc3Allocator(const Gralloc3Mapper& mapper) : mMapper(mapper) {
310 mAllocator = IAllocator::getService();
311 if (mAllocator == nullptr) {
312 ALOGW("allocator 3.x is not supported");
313 return;
314 }
315}
316
317bool Gralloc3Allocator::isSupported() const {
318 return mAllocator != nullptr;
319}
320
321std::string Gralloc3Allocator::dumpDebugInfo() const {
322 std::string debugInfo;
323
324 mAllocator->dumpDebugInfo([&](const auto& tmpDebugInfo) { debugInfo = tmpDebugInfo.c_str(); });
325
326 return debugInfo;
327}
328
329status_t Gralloc3Allocator::allocate(uint32_t width, uint32_t height, android::PixelFormat format,
330 uint32_t layerCount, uint64_t usage, uint32_t bufferCount,
331 uint32_t* outStride, buffer_handle_t* outBufferHandles) const {
332 IMapper::BufferDescriptorInfo descriptorInfo;
333 sBufferDescriptorInfo(width, height, format, layerCount, usage, &descriptorInfo);
334
335 BufferDescriptor descriptor;
336 status_t error = mMapper.createDescriptor(static_cast<void*>(&descriptorInfo),
337 static_cast<void*>(&descriptor));
338 if (error != NO_ERROR) {
339 return error;
340 }
341
342 auto ret = mAllocator->allocate(descriptor, bufferCount,
343 [&](const auto& tmpError, const auto& tmpStride,
344 const auto& tmpBuffers) {
345 error = static_cast<status_t>(tmpError);
346 if (tmpError != Error::NONE) {
347 return;
348 }
349
350 // import buffers
351 for (uint32_t i = 0; i < bufferCount; i++) {
352 error = mMapper.importBuffer(tmpBuffers[i],
353 &outBufferHandles[i]);
354 if (error != NO_ERROR) {
355 for (uint32_t j = 0; j < i; j++) {
356 mMapper.freeBuffer(outBufferHandles[j]);
357 outBufferHandles[j] = nullptr;
358 }
359 return;
360 }
361 }
362 *outStride = tmpStride;
363 });
364
365 // make sure the kernel driver sees BC_FREE_BUFFER and closes the fds now
366 hardware::IPCThreadState::self()->flushCommands();
367
368 return (ret.isOk()) ? error : static_cast<status_t>(kTransactionError);
369}
370
371} // namespace android