blob: 3c5137fc2c5e327693a97bf2883cf725b53b37c1 [file] [log] [blame]
Tharaga Balachandran74ab1112020-01-08 17:17:56 -05001/*
2 * Copyright (c) 2018-2020 The Linux Foundation. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above
10 * copyright notice, this list of conditions and the following
11 * disclaimer in the documentation and/or other materials provided
12 * with the distribution.
13 * * Neither the name of The Linux Foundation nor the names of its
14 * contributors may be used to endorse or promote products derived
15 * from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
18 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
21 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
24 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
26 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30#ifndef __QTIMAPPER4_H__
31#define __QTIMAPPER4_H__
32
33#include <QtiGralloc.h>
34#include <gralloctypes/Gralloc4.h>
35#include <hidl/MQDescriptor.h>
36#include <hidl/Status.h>
37#include <vendor/qti/hardware/display/mapper/4.0/IQtiMapper.h>
38
39#include <algorithm>
40#include <string>
41
42#include "QtiMapperExtensions.h"
43#include "gr_buf_mgr.h"
44namespace vendor {
45namespace qti {
46namespace hardware {
47namespace display {
48namespace mapper {
49namespace V4_0 {
50namespace implementation {
51
52using ::android::sp;
53using ::android::hardware::hidl_array;
54using ::android::hardware::hidl_handle;
55using ::android::hardware::hidl_memory;
56using ::android::hardware::hidl_string;
57using ::android::hardware::hidl_vec;
58using ::android::hardware::Return;
59using ::android::hardware::Void;
60using ::android::hardware::graphics::common::V1_2::PixelFormat;
61using ::android::hardware::graphics::mapper::V4_0::Error;
62using ::android::hardware::graphics::mapper::V4_0::IMapper;
63using ::android::hidl::base::V1_0::DebugInfo;
64using ::android::hidl::base::V1_0::IBase;
65using gralloc::BufferManager;
66using ::vendor::qti::hardware::display::mapper::V4_0::IQtiMapper;
67using ::vendor::qti::hardware::display::mapperextensions::V1_1::IQtiMapperExtensions;
68using ::vendor::qti::hardware::display::mapperextensions::V1_1::implementation::QtiMapperExtensions;
69
70using android::hardware::graphics::mapper::V4_0::IMapper;
71using BufferDescriptorInfo_4_0 =
72 android::hardware::graphics::mapper::V4_0::IMapper::BufferDescriptorInfo;
73using IMapperBufferDescriptor = android::hardware::graphics::mapper::V4_0::BufferDescriptor;
74using MetadataType = ::android::hardware::graphics::mapper::V4_0::IMapper::MetadataType;
75using MetadataTypeDescription =
76 ::android::hardware::graphics::mapper::V4_0::IMapper::MetadataTypeDescription;
77using IMapper_4_0_Error = ::android::hardware::graphics::mapper::V4_0::Error;
78
79class QtiMapper : public IQtiMapper {
80 public:
81 QtiMapper();
82 // Methods from ::android::hardware::graphics::mapper::V2_0::IMapper follow.
83 Return<void> createDescriptor(const BufferDescriptorInfo_4_0 &descriptor_info,
84 createDescriptor_cb hidl_cb) override;
85 Return<void> importBuffer(const hidl_handle &raw_handle, importBuffer_cb hidl_cb) override;
86 Return<Error> freeBuffer(void *buffer) override;
87 Return<void> lock(void *buffer, uint64_t cpu_usage, const IMapper::Rect &access_region,
88 const hidl_handle &acquire_fence, lock_cb hidl_cb) override;
89 Return<void> unlock(void *buffer, unlock_cb hidl_cb) override;
90
91 // Methods from ::android::hardware::graphics::mapper::V2_1::IMapper follow.
92 Return<Error> validateBufferSize(void *buffer, const BufferDescriptorInfo_4_0 &descriptorInfo,
93 uint32_t stride) override;
94 Return<void> getTransportSize(void *buffer, getTransportSize_cb hidl_cb) override;
95
96 Return<void> isSupported(const BufferDescriptorInfo_4_0 &descriptor_info,
97 isSupported_cb hidl_cb) override;
98
99 Return<void> getMapperExtensions(getMapperExtensions_cb hidl_cb);
100 sp<mapperextensions::V1_1::IQtiMapperExtensions> extensions_ = nullptr;
101
102 // Methods from ::android::hardware::graphics::mapper::V4:0::IMapper follow.
103 Return<void> get(void *buffer, const MetadataType &metadataType, get_cb hidl_cb) override;
104 Return<Error> set(void *buffer, const MetadataType &metadataType,
105 const hidl_vec<uint8_t> &metadata) override;
106 Return<void> getFromBufferDescriptorInfo(const BufferDescriptorInfo &description,
107 const MetadataType &metadataType,
108 getFromBufferDescriptorInfo_cb hidl_cb) override;
109 Return<void> flushLockedBuffer(void *buffer, flushLockedBuffer_cb hidl_cb);
110 Return<Error> rereadLockedBuffer(void *buffer);
111 Return<void> listSupportedMetadataTypes(listSupportedMetadataTypes_cb hidl_cb);
112 Return<void> getReservedRegion(void *buffer, getReservedRegion_cb _hidl_cb);
113 Return<void> dumpBuffer(void *buffer, dumpBuffer_cb _hidl_cb);
114 Return<void> dumpBuffers(dumpBuffers_cb _hidl_cb);
115
116 hidl_vec<uint8_t> Encode(const BufferDescriptorInfo_4_0 &bd_info) {
117 hidl_vec<uint8_t> out;
118
119 uint64_t name_size = bd_info.name.size();
120
121 /* Name length is variable, need to store string prepended with size
122 * The rest of the packet size is constant
123 */
124 out.resize(gralloc::kBufferDescriptorSizeV4 + sizeof(name_size) +
125 static_cast<size_t>(name_size));
126
127 size_t index = 0;
128 uint32_t magic_version = gralloc::kMagicVersion;
129 std::memcpy(&out[index], &magic_version, sizeof(magic_version));
130 index += sizeof(magic_version);
131
Yichi Chena8304132020-03-23 12:23:48 +0800132 std::memcpy(&out[index], &name_size, sizeof(name_size));
Tharaga Balachandran74ab1112020-01-08 17:17:56 -0500133 index += sizeof(name_size);
134
135 std::memcpy(&out[index], bd_info.name.c_str(), bd_info.name.size());
136 index += name_size;
137
138 std::memcpy(&out[index], &bd_info.width, sizeof(bd_info.width));
139 index += sizeof(bd_info.width);
140
141 std::memcpy(&out[index], &bd_info.height, sizeof(bd_info.height));
142 index += sizeof(bd_info.height);
143
144 std::memcpy(&out[index], &bd_info.layerCount, sizeof(bd_info.layerCount));
145 index += sizeof(bd_info.layerCount);
146
147 std::memcpy(&out[index], &bd_info.format, sizeof(bd_info.format));
148 index += sizeof(bd_info.format);
149
150 std::memcpy(&out[index], &bd_info.usage, sizeof(bd_info.usage));
151 index += sizeof(bd_info.usage);
152
Tharaga Balachandran40648032020-05-01 10:54:31 -0400153 std::memcpy(&out[index], &bd_info.reservedSize, sizeof(bd_info.reservedSize));
Tharaga Balachandran74ab1112020-01-08 17:17:56 -0500154
155 return out;
156 }
157 static gralloc::Error Decode(const hidl_vec<uint8_t> &in,
158 gralloc::BufferDescriptor *buf_descriptor) {
159 // First check is to avoid dereferencing if the vector is too short
160 if (in.size() < gralloc::kBufferDescriptorSizeV4) {
161 return gralloc::Error::BAD_DESCRIPTOR;
162 }
163
164 size_t index = 0;
165 uint32_t magic_version;
166 std::memcpy(&magic_version, &in[index], sizeof(magic_version));
167 index += sizeof(magic_version);
168
Yichi Chena8304132020-03-23 12:23:48 +0800169 uint64_t name_size;
170 std::memcpy(&name_size, &in[index], sizeof(name_size));
Tharaga Balachandran74ab1112020-01-08 17:17:56 -0500171 index += sizeof(name_size);
172
173 // The second check validates that the size and magic version are correct
174 if (in.size() != (gralloc::kBufferDescriptorSizeV4 + name_size + sizeof(name_size)) ||
175 magic_version != gralloc::kMagicVersion) {
176 return gralloc::Error::BAD_DESCRIPTOR;
177 }
178
179 std::string name;
180
181 name.resize(static_cast<size_t>(name_size));
182 std::memcpy(name.data(), &in[index], name.size());
183 index += name_size;
184 buf_descriptor->SetName(name);
185
186 int32_t width, height;
187 std::memcpy(&width, &in[index], sizeof(width));
188
189 index += sizeof(width);
190 std::memcpy(&height, &in[index], sizeof(height));
191 index += sizeof(height);
192 buf_descriptor->SetDimensions(width, height);
193
194 uint32_t layer_count;
195 std::memcpy(&layer_count, &in[index], sizeof(layer_count));
196 index += sizeof(layer_count);
197 buf_descriptor->SetLayerCount(layer_count);
198
199 int32_t format;
200 std::memcpy(&format, &in[index], sizeof(format));
201 index += sizeof(format);
202 buf_descriptor->SetColorFormat(format);
203
204 uint64_t usage;
205 std::memcpy(&usage, &in[index], sizeof(usage));
206 index += sizeof(usage);
207 buf_descriptor->SetUsage(usage);
208
209 uint64_t reserved_size;
210 std::memcpy(&reserved_size, &in[index], sizeof(reserved_size));
211 index += sizeof(reserved_size);
212
213 buf_descriptor->SetReservedSize(reserved_size);
214 return gralloc::Error::NONE;
215 }
216
217 private:
218 BufferManager *buf_mgr_ = nullptr;
219 Error CreateDescriptor(const BufferDescriptorInfo_4_0 &descriptor_info,
220 IMapperBufferDescriptor *descriptor);
221 bool ValidDescriptor(const IMapper::BufferDescriptorInfo &bd);
222 bool GetFenceFd(const hidl_handle &fence_handle, int *outFenceFd);
223 void WaitFenceFd(int fence_fd);
224 Error LockBuffer(void *buffer, uint64_t usage, const hidl_handle &acquire_fence,
225 const IMapper::Rect &access_region);
226
227 Error DumpBufferMetadata(const private_handle_t *buffer, BufferDump *outBufferDump);
228
229 hidl_vec<MetadataTypeDescription> metadata_type_descriptions_ = {
230 // MetadataType, description, gettable, settable
231 {android::gralloc4::MetadataType_BufferId, "", true, false},
232 {android::gralloc4::MetadataType_Name, "", true, false},
233 {android::gralloc4::MetadataType_Width, "", true, false},
234 {android::gralloc4::MetadataType_Height, "", true, false},
235 {android::gralloc4::MetadataType_LayerCount, "", true, false},
236 {android::gralloc4::MetadataType_PixelFormatRequested, "", true, false},
237 {android::gralloc4::MetadataType_PixelFormatFourCC, "", true, false},
238 {android::gralloc4::MetadataType_PixelFormatModifier, "", true, false},
239 {android::gralloc4::MetadataType_Usage, "", true, false},
240 {android::gralloc4::MetadataType_AllocationSize, "", true, false},
241 {android::gralloc4::MetadataType_ProtectedContent, "", true, false},
242 {android::gralloc4::MetadataType_ChromaSiting, "", true, false},
243 {android::gralloc4::MetadataType_Compression, "", true, false},
244 {android::gralloc4::MetadataType_Interlaced, "", true, false},
245 {android::gralloc4::MetadataType_PlaneLayouts, "", true, false},
246 {android::gralloc4::MetadataType_Dataspace, "", true, true},
247 {android::gralloc4::MetadataType_BlendMode, "", true, true},
248 {android::gralloc4::MetadataType_Smpte2086, "", true, true},
249 {android::gralloc4::MetadataType_Cta861_3, "", true, true},
250 {android::gralloc4::MetadataType_Smpte2094_40, "", true, true},
Tharaga Balachandran95175bb2020-03-20 13:26:55 -0400251 {android::gralloc4::MetadataType_Crop, "", true, true},
Tharaga Balachandran74ab1112020-01-08 17:17:56 -0500252 {qtigralloc::MetadataType_VTTimestamp, "VT Timestamp", true, true},
253 {qtigralloc::MetadataType_ColorMetadata, "Color metadata", true, true},
254 {qtigralloc::MetadataType_PPParamInterlaced, "Interlaced", true, true},
255 {qtigralloc::MetadataType_VideoPerfMode, "Video perf mode", true, true},
256 {qtigralloc::MetadataType_GraphicsMetadata, "Graphics metadata", true, true},
257 {qtigralloc::MetadataType_UBWCCRStatsInfo, "UBWC stats", true, true},
258 {qtigralloc::MetadataType_RefreshRate, "Refresh rate", true, true},
259 {qtigralloc::MetadataType_MapSecureBuffer, "Secure buffer mappable", true, true},
260 {qtigralloc::MetadataType_LinearFormat, "Linear format", true, true},
261 {qtigralloc::MetadataType_SingleBufferMode, "Single buffer mode flag", true, true},
262 {qtigralloc::MetadataType_CVPMetadata, "CVP metadata", true, true},
263 {qtigralloc::MetadataType_VideoHistogramStats, "Video histogram stats", true, true},
264 {qtigralloc::MetadataType_FD, "fd from private_handle_t", true, false},
265 {qtigralloc::MetadataType_PrivateFlags, "Flags in private_handle_t", true, false},
Tharaga Balachandran70b03c72020-03-10 14:00:15 -0400266 {qtigralloc::MetadataType_AlignedWidthInPixels, "width in private_handle_t", true, false},
Tharaga Balachandran40648032020-05-01 10:54:31 -0400267 {qtigralloc::MetadataType_AlignedHeightInPixels, "height in private_handle_t", true, false},
268#ifdef METADATA_V2
269 {qtigralloc::MetadataType_StandardMetadataStatus, "Is standard metadata set", true, false},
270 {qtigralloc::MetadataType_VendorMetadataStatus, "Is vendor metadata set", true, false}
271#endif
Tharaga Balachandran74ab1112020-01-08 17:17:56 -0500272 };
273};
274
275} // namespace implementation
276} // namespace V4_0
277} // namespace mapper
278} // namespace display
279} // namespace hardware
280} // namespace qti
281} // namespace vendor
282
283#endif // __QTIMAPPER4_H__