xref: /aosp_15_r20/external/minigbm/cros_gralloc/gralloc4/CrosGralloc4Allocator.cc (revision d95af8df99a05bcb8679a54dc3ab8e5cd312b38e)
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 aidl::android::hardware::graphics::common::BlendMode;
16 using aidl::android::hardware::graphics::common::Dataspace;
17 using android::hardware::hidl_handle;
18 using android::hardware::hidl_vec;
19 using android::hardware::Return;
20 using android::hardware::Void;
21 using android::hardware::graphics::common::V1_2::BufferUsage;
22 using android::hardware::graphics::common::V1_2::PixelFormat;
23 using android::hardware::graphics::mapper::V4_0::Error;
24 
25 using BufferDescriptorInfo =
26         android::hardware::graphics::mapper::V4_0::IMapper::BufferDescriptorInfo;
27 
init()28 Error CrosGralloc4Allocator::init() {
29     mDriver = cros_gralloc_driver::get_instance();
30     return mDriver ? Error::NONE : Error::NO_RESOURCES;
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         ALOGE("Failed to allocate. Driver is uninitialized.");
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     if (!mDriver->is_supported(&crosDescriptor)) {
50         std::string drmFormatString = get_drm_format_string(crosDescriptor.drm_format);
51         std::string pixelFormatString = getPixelFormatString(descriptor.format);
52         std::string usageString = getUsageString(descriptor.usage);
53         ALOGE("Unsupported combination -- pixel format: %s, drm format:%s, usage: %s",
54               pixelFormatString.c_str(), drmFormatString.c_str(), usageString.c_str());
55         return Error::UNSUPPORTED;
56     }
57 
58     native_handle_t* handle;
59     int ret = mDriver->allocate(&crosDescriptor, &handle);
60     if (ret) {
61         return Error::NO_RESOURCES;
62     }
63 
64     cros_gralloc_handle_t crosHandle = cros_gralloc_convert_handle(handle);
65 
66     outHandle->setTo(handle, /*shouldOwn=*/true);
67     *outStride = crosHandle->pixel_stride;
68 
69     return Error::NONE;
70 }
71 
allocate(const hidl_vec<uint8_t> & descriptor,uint32_t count,allocate_cb hidlCb)72 Return<void> CrosGralloc4Allocator::allocate(const hidl_vec<uint8_t>& descriptor, uint32_t count,
73                                              allocate_cb hidlCb) {
74     hidl_vec<hidl_handle> handles;
75 
76     if (!mDriver) {
77         ALOGE("Failed to allocate. Driver is uninitialized.");
78         hidlCb(Error::NO_RESOURCES, 0, handles);
79         return Void();
80     }
81 
82     BufferDescriptorInfo description;
83 
84     int ret = android::gralloc4::decodeBufferDescriptorInfo(descriptor, &description);
85     if (ret) {
86         ALOGE("Failed to allocate. Failed to decode buffer descriptor: %d.", ret);
87         hidlCb(Error::BAD_DESCRIPTOR, 0, handles);
88         return Void();
89     }
90 
91     handles.resize(count);
92 
93     uint32_t stride = 0;
94     for (int i = 0; i < handles.size(); i++) {
95         Error err = allocate(description, &stride, &(handles[i]));
96         if (err != Error::NONE) {
97             for (int j = 0; j < i; j++) {
98                 mDriver->release(handles[j].getNativeHandle());
99             }
100             handles.resize(0);
101             hidlCb(err, 0, handles);
102             return Void();
103         }
104     }
105 
106     hidlCb(Error::NONE, stride, handles);
107 
108     for (const hidl_handle& handle : handles) {
109         mDriver->release(handle.getNativeHandle());
110     }
111 
112     return Void();
113 }
114