1 /*
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.
5 */
6
7 #include "cros_gralloc/gralloc4/CrosGralloc4Allocator.h"
8
9 #include <android/hardware/graphics/mapper/4.0/IMapper.h>
10 #include <gralloctypes/Gralloc4.h>
11
12 #include "cros_gralloc/cros_gralloc_helpers.h"
13 #include "cros_gralloc/gralloc4/CrosGralloc4Utils.h"
14
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;
22
23 using BufferDescriptorInfo =
24 android::hardware::graphics::mapper::V4_0::IMapper::BufferDescriptorInfo;
25
CrosGralloc4Allocator()26 CrosGralloc4Allocator::CrosGralloc4Allocator() : mDriver(std::make_unique<cros_gralloc_driver>()) {
27 if (mDriver->init()) {
28 drv_log("Failed to initialize driver.\n");
29 mDriver = nullptr;
30 }
31 }
32
allocate(const BufferDescriptorInfo & descriptor,uint32_t * outStride,hidl_handle * outHandle)33 Error CrosGralloc4Allocator::allocate(const BufferDescriptorInfo& descriptor, uint32_t* outStride,
34 hidl_handle* outHandle) {
35 if (!mDriver) {
36 drv_log("Failed to allocate. Driver is uninitialized.\n");
37 return Error::NO_RESOURCES;
38 }
39
40 if (!outStride || !outHandle) {
41 return Error::NO_RESOURCES;
42 }
43
44 struct cros_gralloc_buffer_descriptor crosDescriptor;
45 if (convertToCrosDescriptor(descriptor, &crosDescriptor)) {
46 return Error::UNSUPPORTED;
47 }
48
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);
53 }
54
55 if (!supported) {
56 std::string drmFormatString = get_drm_format_string(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;
62 }
63
64 buffer_handle_t handle;
65 int ret = mDriver->allocate(&crosDescriptor, &handle);
66 if (ret) {
67 return Error::NO_RESOURCES;
68 }
69
70 cros_gralloc_handle_t crosHandle = cros_gralloc_convert_handle(handle);
71 if (!crosHandle) {
72 return Error::NO_RESOURCES;
73 }
74
75 *outHandle = handle;
76 *outStride = crosHandle->pixel_stride;
77
78 return Error::NONE;
79 }
80
allocate(const hidl_vec<uint8_t> & descriptor,uint32_t count,allocate_cb hidlCb)81 Return<void> CrosGralloc4Allocator::allocate(const hidl_vec<uint8_t>& descriptor, uint32_t count,
82 allocate_cb hidlCb) {
83 hidl_vec<hidl_handle> handles;
84
85 if (!mDriver) {
86 drv_log("Failed to allocate. Driver is uninitialized.\n");
87 hidlCb(Error::NO_RESOURCES, 0, handles);
88 return Void();
89 }
90
91 BufferDescriptorInfo description;
92
93 int ret = android::gralloc4::decodeBufferDescriptorInfo(descriptor, &description);
94 if (ret) {
95 drv_log("Failed to allocate. Failed to decode buffer descriptor: %d.\n", ret);
96 hidlCb(Error::BAD_DESCRIPTOR, 0, handles);
97 return Void();
98 }
99
100 handles.resize(count);
101
102 uint32_t stride = 0;
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());
108 }
109 handles.resize(0);
110 hidlCb(err, 0, handles);
111 return Void();
112 }
113 }
114
115 hidlCb(Error::NONE, stride, handles);
116
117 for (const hidl_handle& handle : handles) {
118 mDriver->release(handle.getNativeHandle());
119 }
120
121 return Void();
122 }
123