xref: /aosp_15_r20/external/minigbm/cros_gralloc/gralloc4/CrosGralloc4Allocator.cc (revision d95af8df99a05bcb8679a54dc3ab8e5cd312b38e)
1*d95af8dfSAndroid Build Coastguard Worker /*
2*d95af8dfSAndroid Build Coastguard Worker  * Copyright 2020 The Chromium OS Authors. All rights reserved.
3*d95af8dfSAndroid Build Coastguard Worker  * Use of this source code is governed by a BSD-style license that can be
4*d95af8dfSAndroid Build Coastguard Worker  * found in the LICENSE file.
5*d95af8dfSAndroid Build Coastguard Worker  */
6*d95af8dfSAndroid Build Coastguard Worker 
7*d95af8dfSAndroid Build Coastguard Worker #include "cros_gralloc/gralloc4/CrosGralloc4Allocator.h"
8*d95af8dfSAndroid Build Coastguard Worker 
9*d95af8dfSAndroid Build Coastguard Worker #include <android/hardware/graphics/mapper/4.0/IMapper.h>
10*d95af8dfSAndroid Build Coastguard Worker #include <gralloctypes/Gralloc4.h>
11*d95af8dfSAndroid Build Coastguard Worker 
12*d95af8dfSAndroid Build Coastguard Worker #include "cros_gralloc/cros_gralloc_helpers.h"
13*d95af8dfSAndroid Build Coastguard Worker #include "cros_gralloc/gralloc4/CrosGralloc4Utils.h"
14*d95af8dfSAndroid Build Coastguard Worker 
15*d95af8dfSAndroid Build Coastguard Worker using aidl::android::hardware::graphics::common::BlendMode;
16*d95af8dfSAndroid Build Coastguard Worker using aidl::android::hardware::graphics::common::Dataspace;
17*d95af8dfSAndroid Build Coastguard Worker using android::hardware::hidl_handle;
18*d95af8dfSAndroid Build Coastguard Worker using android::hardware::hidl_vec;
19*d95af8dfSAndroid Build Coastguard Worker using android::hardware::Return;
20*d95af8dfSAndroid Build Coastguard Worker using android::hardware::Void;
21*d95af8dfSAndroid Build Coastguard Worker using android::hardware::graphics::common::V1_2::BufferUsage;
22*d95af8dfSAndroid Build Coastguard Worker using android::hardware::graphics::common::V1_2::PixelFormat;
23*d95af8dfSAndroid Build Coastguard Worker using android::hardware::graphics::mapper::V4_0::Error;
24*d95af8dfSAndroid Build Coastguard Worker 
25*d95af8dfSAndroid Build Coastguard Worker using BufferDescriptorInfo =
26*d95af8dfSAndroid Build Coastguard Worker         android::hardware::graphics::mapper::V4_0::IMapper::BufferDescriptorInfo;
27*d95af8dfSAndroid Build Coastguard Worker 
init()28*d95af8dfSAndroid Build Coastguard Worker Error CrosGralloc4Allocator::init() {
29*d95af8dfSAndroid Build Coastguard Worker     mDriver = cros_gralloc_driver::get_instance();
30*d95af8dfSAndroid Build Coastguard Worker     return mDriver ? Error::NONE : Error::NO_RESOURCES;
31*d95af8dfSAndroid Build Coastguard Worker }
32*d95af8dfSAndroid Build Coastguard Worker 
allocate(const BufferDescriptorInfo & descriptor,uint32_t * outStride,hidl_handle * outHandle)33*d95af8dfSAndroid Build Coastguard Worker Error CrosGralloc4Allocator::allocate(const BufferDescriptorInfo& descriptor, uint32_t* outStride,
34*d95af8dfSAndroid Build Coastguard Worker                                       hidl_handle* outHandle) {
35*d95af8dfSAndroid Build Coastguard Worker     if (!mDriver) {
36*d95af8dfSAndroid Build Coastguard Worker         ALOGE("Failed to allocate. Driver is uninitialized.");
37*d95af8dfSAndroid Build Coastguard Worker         return Error::NO_RESOURCES;
38*d95af8dfSAndroid Build Coastguard Worker     }
39*d95af8dfSAndroid Build Coastguard Worker 
40*d95af8dfSAndroid Build Coastguard Worker     if (!outStride || !outHandle) {
41*d95af8dfSAndroid Build Coastguard Worker         return Error::NO_RESOURCES;
42*d95af8dfSAndroid Build Coastguard Worker     }
43*d95af8dfSAndroid Build Coastguard Worker 
44*d95af8dfSAndroid Build Coastguard Worker     struct cros_gralloc_buffer_descriptor crosDescriptor;
45*d95af8dfSAndroid Build Coastguard Worker     if (convertToCrosDescriptor(descriptor, &crosDescriptor)) {
46*d95af8dfSAndroid Build Coastguard Worker         return Error::UNSUPPORTED;
47*d95af8dfSAndroid Build Coastguard Worker     }
48*d95af8dfSAndroid Build Coastguard Worker 
49*d95af8dfSAndroid Build Coastguard Worker     if (!mDriver->is_supported(&crosDescriptor)) {
50*d95af8dfSAndroid Build Coastguard Worker         std::string drmFormatString = get_drm_format_string(crosDescriptor.drm_format);
51*d95af8dfSAndroid Build Coastguard Worker         std::string pixelFormatString = getPixelFormatString(descriptor.format);
52*d95af8dfSAndroid Build Coastguard Worker         std::string usageString = getUsageString(descriptor.usage);
53*d95af8dfSAndroid Build Coastguard Worker         ALOGE("Unsupported combination -- pixel format: %s, drm format:%s, usage: %s",
54*d95af8dfSAndroid Build Coastguard Worker               pixelFormatString.c_str(), drmFormatString.c_str(), usageString.c_str());
55*d95af8dfSAndroid Build Coastguard Worker         return Error::UNSUPPORTED;
56*d95af8dfSAndroid Build Coastguard Worker     }
57*d95af8dfSAndroid Build Coastguard Worker 
58*d95af8dfSAndroid Build Coastguard Worker     native_handle_t* handle;
59*d95af8dfSAndroid Build Coastguard Worker     int ret = mDriver->allocate(&crosDescriptor, &handle);
60*d95af8dfSAndroid Build Coastguard Worker     if (ret) {
61*d95af8dfSAndroid Build Coastguard Worker         return Error::NO_RESOURCES;
62*d95af8dfSAndroid Build Coastguard Worker     }
63*d95af8dfSAndroid Build Coastguard Worker 
64*d95af8dfSAndroid Build Coastguard Worker     cros_gralloc_handle_t crosHandle = cros_gralloc_convert_handle(handle);
65*d95af8dfSAndroid Build Coastguard Worker 
66*d95af8dfSAndroid Build Coastguard Worker     outHandle->setTo(handle, /*shouldOwn=*/true);
67*d95af8dfSAndroid Build Coastguard Worker     *outStride = crosHandle->pixel_stride;
68*d95af8dfSAndroid Build Coastguard Worker 
69*d95af8dfSAndroid Build Coastguard Worker     return Error::NONE;
70*d95af8dfSAndroid Build Coastguard Worker }
71*d95af8dfSAndroid Build Coastguard Worker 
allocate(const hidl_vec<uint8_t> & descriptor,uint32_t count,allocate_cb hidlCb)72*d95af8dfSAndroid Build Coastguard Worker Return<void> CrosGralloc4Allocator::allocate(const hidl_vec<uint8_t>& descriptor, uint32_t count,
73*d95af8dfSAndroid Build Coastguard Worker                                              allocate_cb hidlCb) {
74*d95af8dfSAndroid Build Coastguard Worker     hidl_vec<hidl_handle> handles;
75*d95af8dfSAndroid Build Coastguard Worker 
76*d95af8dfSAndroid Build Coastguard Worker     if (!mDriver) {
77*d95af8dfSAndroid Build Coastguard Worker         ALOGE("Failed to allocate. Driver is uninitialized.");
78*d95af8dfSAndroid Build Coastguard Worker         hidlCb(Error::NO_RESOURCES, 0, handles);
79*d95af8dfSAndroid Build Coastguard Worker         return Void();
80*d95af8dfSAndroid Build Coastguard Worker     }
81*d95af8dfSAndroid Build Coastguard Worker 
82*d95af8dfSAndroid Build Coastguard Worker     BufferDescriptorInfo description;
83*d95af8dfSAndroid Build Coastguard Worker 
84*d95af8dfSAndroid Build Coastguard Worker     int ret = android::gralloc4::decodeBufferDescriptorInfo(descriptor, &description);
85*d95af8dfSAndroid Build Coastguard Worker     if (ret) {
86*d95af8dfSAndroid Build Coastguard Worker         ALOGE("Failed to allocate. Failed to decode buffer descriptor: %d.", ret);
87*d95af8dfSAndroid Build Coastguard Worker         hidlCb(Error::BAD_DESCRIPTOR, 0, handles);
88*d95af8dfSAndroid Build Coastguard Worker         return Void();
89*d95af8dfSAndroid Build Coastguard Worker     }
90*d95af8dfSAndroid Build Coastguard Worker 
91*d95af8dfSAndroid Build Coastguard Worker     handles.resize(count);
92*d95af8dfSAndroid Build Coastguard Worker 
93*d95af8dfSAndroid Build Coastguard Worker     uint32_t stride = 0;
94*d95af8dfSAndroid Build Coastguard Worker     for (int i = 0; i < handles.size(); i++) {
95*d95af8dfSAndroid Build Coastguard Worker         Error err = allocate(description, &stride, &(handles[i]));
96*d95af8dfSAndroid Build Coastguard Worker         if (err != Error::NONE) {
97*d95af8dfSAndroid Build Coastguard Worker             for (int j = 0; j < i; j++) {
98*d95af8dfSAndroid Build Coastguard Worker                 mDriver->release(handles[j].getNativeHandle());
99*d95af8dfSAndroid Build Coastguard Worker             }
100*d95af8dfSAndroid Build Coastguard Worker             handles.resize(0);
101*d95af8dfSAndroid Build Coastguard Worker             hidlCb(err, 0, handles);
102*d95af8dfSAndroid Build Coastguard Worker             return Void();
103*d95af8dfSAndroid Build Coastguard Worker         }
104*d95af8dfSAndroid Build Coastguard Worker     }
105*d95af8dfSAndroid Build Coastguard Worker 
106*d95af8dfSAndroid Build Coastguard Worker     hidlCb(Error::NONE, stride, handles);
107*d95af8dfSAndroid Build Coastguard Worker 
108*d95af8dfSAndroid Build Coastguard Worker     for (const hidl_handle& handle : handles) {
109*d95af8dfSAndroid Build Coastguard Worker         mDriver->release(handle.getNativeHandle());
110*d95af8dfSAndroid Build Coastguard Worker     }
111*d95af8dfSAndroid Build Coastguard Worker 
112*d95af8dfSAndroid Build Coastguard Worker     return Void();
113*d95af8dfSAndroid Build Coastguard Worker }
114