2 * Copyright 2020 The Chromium OS Authors. All rights reserved.
3 * Use of this source code is governed by a BSD-style license that can be
4 * found in the LICENSE file.
7 #include "cros_gralloc/gralloc4/CrosGralloc4Allocator.h"
9 #include <android/hardware/graphics/mapper/4.0/IMapper.h>
10 #include <gralloctypes/Gralloc4.h>
12 #include "cros_gralloc/cros_gralloc_helpers.h"
13 #include "cros_gralloc/gralloc4/CrosGralloc4Utils.h"
15 using android::hardware::hidl_handle;
16 using android::hardware::hidl_vec;
17 using android::hardware::Return;
18 using android::hardware::Void;
19 using android::hardware::graphics::common::V1_2::BufferUsage;
20 using android::hardware::graphics::common::V1_2::PixelFormat;
21 using android::hardware::graphics::mapper::V4_0::Error;
23 using BufferDescriptorInfo =
24 android::hardware::graphics::mapper::V4_0::IMapper::BufferDescriptorInfo;
26 CrosGralloc4Allocator::CrosGralloc4Allocator() : mDriver(std::make_unique<cros_gralloc_driver>()) {
27 if (mDriver->init()) {
28 drv_log("Failed to initialize driver.\n");
33 Error CrosGralloc4Allocator::allocate(const BufferDescriptorInfo& descriptor, uint32_t* outStride,
34 hidl_handle* outHandle) {
36 drv_log("Failed to allocate. Driver is uninitialized.\n");
37 return Error::NO_RESOURCES;
40 if (!outStride || !outHandle) {
41 return Error::NO_RESOURCES;
44 struct cros_gralloc_buffer_descriptor crosDescriptor;
45 if (convertToCrosDescriptor(descriptor, &crosDescriptor)) {
46 return Error::UNSUPPORTED;
49 bool supported = mDriver->is_supported(&crosDescriptor);
50 if (!supported && (descriptor.usage & BufferUsage::COMPOSER_OVERLAY)) {
51 crosDescriptor.use_flags &= ~BO_USE_SCANOUT;
52 supported = mDriver->is_supported(&crosDescriptor);
56 std::string drmFormatString = getDrmFormatString(crosDescriptor.drm_format);
57 std::string pixelFormatString = getPixelFormatString(descriptor.format);
58 std::string usageString = getUsageString(descriptor.usage);
59 drv_log("Unsupported combination -- pixel format: %s, drm format:%s, usage: %s\n",
60 pixelFormatString.c_str(), drmFormatString.c_str(), usageString.c_str());
61 return Error::UNSUPPORTED;
64 buffer_handle_t handle;
65 int ret = mDriver->allocate(&crosDescriptor, &handle);
67 return Error::NO_RESOURCES;
70 cros_gralloc_handle_t crosHandle = cros_gralloc_convert_handle(handle);
72 return Error::NO_RESOURCES;
76 *outStride = crosHandle->pixel_stride;
81 Return<void> CrosGralloc4Allocator::allocate(const hidl_vec<uint8_t>& descriptor, uint32_t count,
83 hidl_vec<hidl_handle> handles;
86 drv_log("Failed to allocate. Driver is uninitialized.\n");
87 hidlCb(Error::NO_RESOURCES, 0, handles);
91 BufferDescriptorInfo description;
93 int ret = android::gralloc4::decodeBufferDescriptorInfo(descriptor, &description);
95 drv_log("Failed to allocate. Failed to decode buffer descriptor: %d.\n", ret);
96 hidlCb(Error::BAD_DESCRIPTOR, 0, handles);
100 handles.resize(count);
103 for (int i = 0; i < handles.size(); i++) {
104 Error err = allocate(description, &stride, &(handles[i]));
105 if (err != Error::NONE) {
106 for (int j = 0; j < i; j++) {
107 mDriver->release(handles[j].getNativeHandle());
110 hidlCb(err, 0, handles);
115 hidlCb(Error::NONE, stride, handles);
117 for (const hidl_handle& handle : handles) {
118 mDriver->release(handle.getNativeHandle());