• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/CrosGralloc4Metadata.h"
14 #include "cros_gralloc/gralloc4/CrosGralloc4Utils.h"
15 
16 using aidl::android::hardware::graphics::common::BlendMode;
17 using aidl::android::hardware::graphics::common::Dataspace;
18 using android::hardware::hidl_handle;
19 using android::hardware::hidl_vec;
20 using android::hardware::Return;
21 using android::hardware::Void;
22 using android::hardware::graphics::common::V1_2::BufferUsage;
23 using android::hardware::graphics::common::V1_2::PixelFormat;
24 using android::hardware::graphics::mapper::V4_0::Error;
25 
26 using BufferDescriptorInfo =
27         android::hardware::graphics::mapper::V4_0::IMapper::BufferDescriptorInfo;
28 
init()29 Error CrosGralloc4Allocator::init() {
30     mDriver = cros_gralloc_driver::get_instance();
31     return mDriver ? Error::NONE : Error::NO_RESOURCES;
32 }
33 
initializeMetadata(cros_gralloc_handle_t crosHandle,const struct cros_gralloc_buffer_descriptor & crosDescriptor)34 Error CrosGralloc4Allocator::initializeMetadata(
35         cros_gralloc_handle_t crosHandle,
36         const struct cros_gralloc_buffer_descriptor& crosDescriptor) {
37     if (!mDriver) {
38         drv_log("Failed to initializeMetadata. Driver is uninitialized.\n");
39         return Error::NO_RESOURCES;
40     }
41 
42     if (!crosHandle) {
43         drv_log("Failed to initializeMetadata. Invalid handle.\n");
44         return Error::BAD_BUFFER;
45     }
46 
47     void* addr;
48     uint64_t size;
49     int ret = mDriver->get_reserved_region(crosHandle, &addr, &size);
50     if (ret) {
51         drv_log("Failed to getReservedRegion.\n");
52         return Error::NO_RESOURCES;
53     }
54 
55     CrosGralloc4Metadata* crosMetadata = reinterpret_cast<CrosGralloc4Metadata*>(addr);
56 
57     snprintf(crosMetadata->name, CROS_GRALLOC4_METADATA_MAX_NAME_SIZE, "%s",
58              crosDescriptor.name.c_str());
59     crosMetadata->dataspace = Dataspace::UNKNOWN;
60     crosMetadata->blendMode = BlendMode::INVALID;
61 
62     return Error::NONE;
63 }
64 
allocate(const BufferDescriptorInfo & descriptor,uint32_t * outStride,hidl_handle * outHandle)65 Error CrosGralloc4Allocator::allocate(const BufferDescriptorInfo& descriptor, uint32_t* outStride,
66                                       hidl_handle* outHandle) {
67     if (!mDriver) {
68         drv_log("Failed to allocate. Driver is uninitialized.\n");
69         return Error::NO_RESOURCES;
70     }
71 
72     if (!outStride || !outHandle) {
73         return Error::NO_RESOURCES;
74     }
75 
76     struct cros_gralloc_buffer_descriptor crosDescriptor;
77     if (convertToCrosDescriptor(descriptor, &crosDescriptor)) {
78         return Error::UNSUPPORTED;
79     }
80 
81     crosDescriptor.reserved_region_size += sizeof(CrosGralloc4Metadata);
82 
83     if (!mDriver->is_supported(&crosDescriptor)) {
84         std::string drmFormatString = get_drm_format_string(crosDescriptor.drm_format);
85         std::string pixelFormatString = getPixelFormatString(descriptor.format);
86         std::string usageString = getUsageString(descriptor.usage);
87         drv_log("Unsupported combination -- pixel format: %s, drm format:%s, usage: %s\n",
88                 pixelFormatString.c_str(), drmFormatString.c_str(), usageString.c_str());
89         return Error::UNSUPPORTED;
90     }
91 
92     native_handle_t* handle;
93     int ret = mDriver->allocate(&crosDescriptor, &handle);
94     if (ret) {
95         return Error::NO_RESOURCES;
96     }
97 
98     cros_gralloc_handle_t crosHandle = cros_gralloc_convert_handle(handle);
99 
100     Error error = initializeMetadata(crosHandle, crosDescriptor);
101     if (error != Error::NONE) {
102         mDriver->release(handle);
103         native_handle_close(handle);
104         native_handle_delete(handle);
105         return error;
106     }
107 
108     outHandle->setTo(handle, /*shouldOwn=*/true);
109     *outStride = crosHandle->pixel_stride;
110 
111     return Error::NONE;
112 }
113 
allocate(const hidl_vec<uint8_t> & descriptor,uint32_t count,allocate_cb hidlCb)114 Return<void> CrosGralloc4Allocator::allocate(const hidl_vec<uint8_t>& descriptor, uint32_t count,
115                                              allocate_cb hidlCb) {
116     hidl_vec<hidl_handle> handles;
117 
118     if (!mDriver) {
119         drv_log("Failed to allocate. Driver is uninitialized.\n");
120         hidlCb(Error::NO_RESOURCES, 0, handles);
121         return Void();
122     }
123 
124     BufferDescriptorInfo description;
125 
126     int ret = android::gralloc4::decodeBufferDescriptorInfo(descriptor, &description);
127     if (ret) {
128         drv_log("Failed to allocate. Failed to decode buffer descriptor: %d.\n", ret);
129         hidlCb(Error::BAD_DESCRIPTOR, 0, handles);
130         return Void();
131     }
132 
133     handles.resize(count);
134 
135     uint32_t stride = 0;
136     for (int i = 0; i < handles.size(); i++) {
137         Error err = allocate(description, &stride, &(handles[i]));
138         if (err != Error::NONE) {
139             for (int j = 0; j < i; j++) {
140                 mDriver->release(handles[j].getNativeHandle());
141             }
142             handles.resize(0);
143             hidlCb(err, 0, handles);
144             return Void();
145         }
146     }
147 
148     hidlCb(Error::NONE, stride, handles);
149 
150     for (const hidl_handle& handle : handles) {
151         mDriver->release(handle.getNativeHandle());
152     }
153 
154     return Void();
155 }
156