blob: 27d0c60c8576cbb142aa15f34bbff6d68d0e1016 [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;
Marissa Wall63429912019-04-09 14:05:06 -070031using android::hardware::graphics::common::V1_2::BufferUsage;
Marissa Wall925bf7f2018-12-29 14:27:11 -080032using 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 :
Marissa Wall63429912019-04-09 14:05:06 -070047 hardware::hidl_enum_range<hardware::graphics::common::V1_2::BufferUsage>()) {
Marissa Wall925bf7f2018-12-29 14:27:11 -080048 bits = bits | bit;
49 }
Kevin F. Haggerty9ca88af2021-10-27 23:02:31 +010050
51#ifdef ADDNL_GRALLOC_10_USAGE_BITS
52 uint64_t addnl_bits = static_cast<uint64_t>(ADDNL_GRALLOC_10_USAGE_BITS);
53 ALOGI("Adding additional valid usage bits: 0x%" PRIx64, addnl_bits);
54 bits = bits | addnl_bits;
55#endif
56
Marissa Wall925bf7f2018-12-29 14:27:11 -080057 return bits;
58 }();
59 return validUsageBits;
60}
61
62static inline IMapper::Rect sGralloc3Rect(const Rect& rect) {
63 IMapper::Rect outRect{};
64 outRect.left = rect.left;
65 outRect.top = rect.top;
66 outRect.width = rect.width();
67 outRect.height = rect.height();
68 return outRect;
69}
70static inline void sBufferDescriptorInfo(uint32_t width, uint32_t height,
71 android::PixelFormat format, uint32_t layerCount,
72 uint64_t usage,
73 IMapper::BufferDescriptorInfo* outDescriptorInfo) {
74 outDescriptorInfo->width = width;
75 outDescriptorInfo->height = height;
76 outDescriptorInfo->layerCount = layerCount;
Marissa Wall63429912019-04-09 14:05:06 -070077 outDescriptorInfo->format = static_cast<hardware::graphics::common::V1_2::PixelFormat>(format);
Marissa Wall925bf7f2018-12-29 14:27:11 -080078 outDescriptorInfo->usage = usage;
79}
80
81} // anonymous namespace
82
83void Gralloc3Mapper::preload() {
84 android::hardware::preloadPassthroughService<IMapper>();
85}
86
87Gralloc3Mapper::Gralloc3Mapper() {
88 mMapper = IMapper::getService();
89 if (mMapper == nullptr) {
90 ALOGW("mapper 3.x is not supported");
91 return;
92 }
93 if (mMapper->isRemote()) {
94 LOG_ALWAYS_FATAL("gralloc-mapper must be in passthrough mode");
95 }
96}
97
Valerie Hau250c6542019-01-31 14:23:43 -080098bool Gralloc3Mapper::isLoaded() const {
Marissa Wall925bf7f2018-12-29 14:27:11 -080099 return mMapper != nullptr;
100}
101
102status_t Gralloc3Mapper::validateBufferDescriptorInfo(
103 IMapper::BufferDescriptorInfo* descriptorInfo) const {
104 uint64_t validUsageBits = getValidUsageBits();
105
106 if (descriptorInfo->usage & ~validUsageBits) {
107 ALOGE("buffer descriptor contains invalid usage bits 0x%" PRIx64,
108 descriptorInfo->usage & ~validUsageBits);
109 return BAD_VALUE;
110 }
Chris Forbes6c09ee72022-01-26 18:48:55 +1300111
112 // Gralloc3 implementations never understand non-BLOB with GPU_DATA_BUFFER
113 // and do not reliably reject it.
114 if (descriptorInfo->usage & BufferUsage::GPU_DATA_BUFFER &&
115 descriptorInfo->format != hardware::graphics::common::V1_2::PixelFormat::BLOB) {
116 ALOGE("gralloc3 does not support non-BLOB pixel formats with GPU_DATA_BUFFER usage");
117 return BAD_VALUE;
118 }
119
Marissa Wall925bf7f2018-12-29 14:27:11 -0800120 return NO_ERROR;
121}
122
123status_t Gralloc3Mapper::createDescriptor(void* bufferDescriptorInfo,
124 void* outBufferDescriptor) const {
125 IMapper::BufferDescriptorInfo* descriptorInfo =
126 static_cast<IMapper::BufferDescriptorInfo*>(bufferDescriptorInfo);
127 BufferDescriptor* outDescriptor = static_cast<BufferDescriptor*>(outBufferDescriptor);
128
129 status_t status = validateBufferDescriptorInfo(descriptorInfo);
130 if (status != NO_ERROR) {
131 return status;
132 }
133
134 Error error;
135 auto hidl_cb = [&](const auto& tmpError, const auto& tmpDescriptor) {
136 error = tmpError;
137 if (error != Error::NONE) {
138 return;
139 }
140 *outDescriptor = tmpDescriptor;
141 };
142
143 hardware::Return<void> ret = mMapper->createDescriptor(*descriptorInfo, hidl_cb);
144
145 return static_cast<status_t>((ret.isOk()) ? error : kTransactionError);
146}
147
John Reck0ff95c92022-12-08 11:45:29 -0500148status_t Gralloc3Mapper::importBuffer(const native_handle_t* rawHandle,
Marissa Wall925bf7f2018-12-29 14:27:11 -0800149 buffer_handle_t* outBufferHandle) const {
150 Error error;
151 auto ret = mMapper->importBuffer(rawHandle, [&](const auto& tmpError, const auto& tmpBuffer) {
152 error = tmpError;
153 if (error != Error::NONE) {
154 return;
155 }
156 *outBufferHandle = static_cast<buffer_handle_t>(tmpBuffer);
157 });
158
159 return static_cast<status_t>((ret.isOk()) ? error : kTransactionError);
160}
161
162void Gralloc3Mapper::freeBuffer(buffer_handle_t bufferHandle) const {
163 auto buffer = const_cast<native_handle_t*>(bufferHandle);
164 auto ret = mMapper->freeBuffer(buffer);
165
166 auto error = (ret.isOk()) ? static_cast<Error>(ret) : kTransactionError;
167 ALOGE_IF(error != Error::NONE, "freeBuffer(%p) failed with %d", buffer, error);
168}
169
170status_t Gralloc3Mapper::validateBufferSize(buffer_handle_t bufferHandle, uint32_t width,
171 uint32_t height, android::PixelFormat format,
172 uint32_t layerCount, uint64_t usage,
173 uint32_t stride) const {
174 IMapper::BufferDescriptorInfo descriptorInfo;
175 sBufferDescriptorInfo(width, height, format, layerCount, usage, &descriptorInfo);
176
177 auto buffer = const_cast<native_handle_t*>(bufferHandle);
178 auto ret = mMapper->validateBufferSize(buffer, descriptorInfo, stride);
179
180 return static_cast<status_t>((ret.isOk()) ? static_cast<Error>(ret) : kTransactionError);
181}
182
183void Gralloc3Mapper::getTransportSize(buffer_handle_t bufferHandle, uint32_t* outNumFds,
184 uint32_t* outNumInts) const {
185 *outNumFds = uint32_t(bufferHandle->numFds);
186 *outNumInts = uint32_t(bufferHandle->numInts);
187
188 Error error;
189 auto buffer = const_cast<native_handle_t*>(bufferHandle);
190 auto ret = mMapper->getTransportSize(buffer,
191 [&](const auto& tmpError, const auto& tmpNumFds,
192 const auto& tmpNumInts) {
193 error = tmpError;
194 if (error != Error::NONE) {
195 return;
196 }
197 *outNumFds = tmpNumFds;
198 *outNumInts = tmpNumInts;
199 });
200
201 error = (ret.isOk()) ? error : kTransactionError;
202
203 ALOGE_IF(error != Error::NONE, "getTransportSize(%p) failed with %d", buffer, error);
204}
205
206status_t Gralloc3Mapper::lock(buffer_handle_t bufferHandle, uint64_t usage, const Rect& bounds,
Valerie Hau0c9fc362019-01-22 09:17:19 -0800207 int acquireFence, void** outData, int32_t* outBytesPerPixel,
208 int32_t* outBytesPerStride) const {
Marissa Wall925bf7f2018-12-29 14:27:11 -0800209 auto buffer = const_cast<native_handle_t*>(bufferHandle);
210
211 IMapper::Rect accessRegion = sGralloc3Rect(bounds);
212
213 // put acquireFence in a hidl_handle
214 hardware::hidl_handle acquireFenceHandle;
215 NATIVE_HANDLE_DECLARE_STORAGE(acquireFenceStorage, 1, 0);
216 if (acquireFence >= 0) {
217 auto h = native_handle_init(acquireFenceStorage, 1, 0);
218 h->data[0] = acquireFence;
219 acquireFenceHandle = h;
220 }
221
222 Error error;
223 auto ret = mMapper->lock(buffer, usage, accessRegion, acquireFenceHandle,
Valerie Hau0c9fc362019-01-22 09:17:19 -0800224 [&](const auto& tmpError, const auto& tmpData,
225 const auto& tmpBytesPerPixel, const auto& tmpBytesPerStride) {
Marissa Wall925bf7f2018-12-29 14:27:11 -0800226 error = tmpError;
227 if (error != Error::NONE) {
228 return;
229 }
230 *outData = tmpData;
Valerie Hau0c9fc362019-01-22 09:17:19 -0800231 if (outBytesPerPixel) {
232 *outBytesPerPixel = tmpBytesPerPixel;
233 }
234 if (outBytesPerStride) {
235 *outBytesPerStride = tmpBytesPerStride;
236 }
Marissa Wall925bf7f2018-12-29 14:27:11 -0800237 });
238
239 // we own acquireFence even on errors
240 if (acquireFence >= 0) {
241 close(acquireFence);
242 }
243
244 error = (ret.isOk()) ? error : kTransactionError;
245
246 ALOGW_IF(error != Error::NONE, "lock(%p, ...) failed: %d", bufferHandle, error);
247
248 return static_cast<status_t>(error);
249}
250
251status_t Gralloc3Mapper::lock(buffer_handle_t bufferHandle, uint64_t usage, const Rect& bounds,
252 int acquireFence, android_ycbcr* ycbcr) const {
253 auto buffer = const_cast<native_handle_t*>(bufferHandle);
254
255 IMapper::Rect accessRegion = sGralloc3Rect(bounds);
256
257 // put acquireFence in a hidl_handle
258 hardware::hidl_handle acquireFenceHandle;
259 NATIVE_HANDLE_DECLARE_STORAGE(acquireFenceStorage, 1, 0);
260 if (acquireFence >= 0) {
261 auto h = native_handle_init(acquireFenceStorage, 1, 0);
262 h->data[0] = acquireFence;
263 acquireFenceHandle = h;
264 }
265
266 YCbCrLayout layout;
267 Error error;
268 auto ret = mMapper->lockYCbCr(buffer, usage, accessRegion, acquireFenceHandle,
269 [&](const auto& tmpError, const auto& tmpLayout) {
270 error = tmpError;
271 if (error != Error::NONE) {
272 return;
273 }
274
275 layout = tmpLayout;
276 });
277
278 if (error == Error::NONE) {
279 ycbcr->y = layout.y;
280 ycbcr->cb = layout.cb;
281 ycbcr->cr = layout.cr;
282 ycbcr->ystride = static_cast<size_t>(layout.yStride);
283 ycbcr->cstride = static_cast<size_t>(layout.cStride);
284 ycbcr->chroma_step = static_cast<size_t>(layout.chromaStep);
285 }
286
287 // we own acquireFence even on errors
288 if (acquireFence >= 0) {
289 close(acquireFence);
290 }
291
292 return static_cast<status_t>((ret.isOk()) ? error : kTransactionError);
293}
294
295int Gralloc3Mapper::unlock(buffer_handle_t bufferHandle) const {
296 auto buffer = const_cast<native_handle_t*>(bufferHandle);
297
298 int releaseFence = -1;
299 Error error;
300 auto ret = mMapper->unlock(buffer, [&](const auto& tmpError, const auto& tmpReleaseFence) {
301 error = tmpError;
302 if (error != Error::NONE) {
303 return;
304 }
305
306 auto fenceHandle = tmpReleaseFence.getNativeHandle();
307 if (fenceHandle && fenceHandle->numFds == 1) {
308 int fd = dup(fenceHandle->data[0]);
309 if (fd >= 0) {
310 releaseFence = fd;
311 } else {
312 ALOGD("failed to dup unlock release fence");
313 sync_wait(fenceHandle->data[0], -1);
314 }
315 }
316 });
317
318 if (!ret.isOk()) {
319 error = kTransactionError;
320 }
321
322 if (error != Error::NONE) {
323 ALOGE("unlock(%p) failed with %d", buffer, error);
324 }
325
326 return releaseFence;
327}
328
Valerie Hauddbfaeb2019-02-01 09:54:20 -0800329status_t Gralloc3Mapper::isSupported(uint32_t width, uint32_t height, android::PixelFormat format,
330 uint32_t layerCount, uint64_t usage,
331 bool* outSupported) const {
332 IMapper::BufferDescriptorInfo descriptorInfo;
333 sBufferDescriptorInfo(width, height, format, layerCount, usage, &descriptorInfo);
334
335 Error error;
336 auto ret = mMapper->isSupported(descriptorInfo,
337 [&](const auto& tmpError, const auto& tmpSupported) {
338 error = tmpError;
339 if (error != Error::NONE) {
340 return;
341 }
342 if (outSupported) {
343 *outSupported = tmpSupported;
344 }
345 });
346
347 if (!ret.isOk()) {
348 error = kTransactionError;
349 }
350
351 if (error != Error::NONE) {
352 ALOGE("isSupported(%u, %u, %d, %u, ...) failed with %d", width, height, format, layerCount,
353 error);
354 }
355
356 return static_cast<status_t>(error);
357}
358
Marissa Wall925bf7f2018-12-29 14:27:11 -0800359Gralloc3Allocator::Gralloc3Allocator(const Gralloc3Mapper& mapper) : mMapper(mapper) {
360 mAllocator = IAllocator::getService();
361 if (mAllocator == nullptr) {
362 ALOGW("allocator 3.x is not supported");
363 return;
364 }
365}
366
Valerie Hau250c6542019-01-31 14:23:43 -0800367bool Gralloc3Allocator::isLoaded() const {
Marissa Wall925bf7f2018-12-29 14:27:11 -0800368 return mAllocator != nullptr;
369}
370
Marissa Wall22b2de12019-12-02 18:11:43 -0800371std::string Gralloc3Allocator::dumpDebugInfo(bool /*less*/) const {
Marissa Wall925bf7f2018-12-29 14:27:11 -0800372 std::string debugInfo;
373
374 mAllocator->dumpDebugInfo([&](const auto& tmpDebugInfo) { debugInfo = tmpDebugInfo.c_str(); });
375
376 return debugInfo;
377}
378
Marissa Wall22b2de12019-12-02 18:11:43 -0800379status_t Gralloc3Allocator::allocate(std::string /*requestorName*/, uint32_t width, uint32_t height,
380 android::PixelFormat format, uint32_t layerCount,
John Reckd727e9c2023-12-08 11:30:37 -0500381 uint64_t usage, uint32_t* outStride,
Marissa Wall22b2de12019-12-02 18:11:43 -0800382 buffer_handle_t* outBufferHandles, bool importBuffers) const {
Marissa Wall925bf7f2018-12-29 14:27:11 -0800383 IMapper::BufferDescriptorInfo descriptorInfo;
384 sBufferDescriptorInfo(width, height, format, layerCount, usage, &descriptorInfo);
385
386 BufferDescriptor descriptor;
387 status_t error = mMapper.createDescriptor(static_cast<void*>(&descriptorInfo),
388 static_cast<void*>(&descriptor));
389 if (error != NO_ERROR) {
390 return error;
391 }
392
John Reckd727e9c2023-12-08 11:30:37 -0500393 constexpr auto bufferCount = 1;
394
Marissa Wall925bf7f2018-12-29 14:27:11 -0800395 auto ret = mAllocator->allocate(descriptor, bufferCount,
396 [&](const auto& tmpError, const auto& tmpStride,
397 const auto& tmpBuffers) {
398 error = static_cast<status_t>(tmpError);
399 if (tmpError != Error::NONE) {
400 return;
401 }
402
Marissa Wallbfcf81f2019-11-27 10:36:29 -0800403 if (importBuffers) {
404 for (uint32_t i = 0; i < bufferCount; i++) {
405 error = mMapper.importBuffer(tmpBuffers[i],
406 &outBufferHandles[i]);
407 if (error != NO_ERROR) {
408 for (uint32_t j = 0; j < i; j++) {
409 mMapper.freeBuffer(outBufferHandles[j]);
410 outBufferHandles[j] = nullptr;
411 }
412 return;
Marissa Wall925bf7f2018-12-29 14:27:11 -0800413 }
Marissa Wallbfcf81f2019-11-27 10:36:29 -0800414 }
415 } else {
416 for (uint32_t i = 0; i < bufferCount; i++) {
417 outBufferHandles[i] = native_handle_clone(
418 tmpBuffers[i].getNativeHandle());
419 if (!outBufferHandles[i]) {
420 for (uint32_t j = 0; j < i; j++) {
421 auto buffer = const_cast<native_handle_t*>(
422 outBufferHandles[j]);
423 native_handle_close(buffer);
424 native_handle_delete(buffer);
425 outBufferHandles[j] = nullptr;
426 }
427 }
Marissa Wall925bf7f2018-12-29 14:27:11 -0800428 }
429 }
430 *outStride = tmpStride;
431 });
432
433 // make sure the kernel driver sees BC_FREE_BUFFER and closes the fds now
434 hardware::IPCThreadState::self()->flushCommands();
435
436 return (ret.isOk()) ? error : static_cast<status_t>(kTransactionError);
437}
438
439} // namespace android