1 /*
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"
44 namespace vendor {
45 namespace qti {
46 namespace hardware {
47 namespace display {
48 namespace mapper {
49 namespace V4_0 {
50 namespace implementation {
51 
52 using ::android::sp;
53 using ::android::hardware::hidl_array;
54 using ::android::hardware::hidl_handle;
55 using ::android::hardware::hidl_memory;
56 using ::android::hardware::hidl_string;
57 using ::android::hardware::hidl_vec;
58 using ::android::hardware::Return;
59 using ::android::hardware::Void;
60 using ::android::hardware::graphics::common::V1_2::PixelFormat;
61 using ::android::hardware::graphics::mapper::V4_0::Error;
62 using ::android::hardware::graphics::mapper::V4_0::IMapper;
63 using ::android::hidl::base::V1_0::DebugInfo;
64 using ::android::hidl::base::V1_0::IBase;
65 using gralloc::BufferManager;
66 using ::vendor::qti::hardware::display::mapper::V4_0::IQtiMapper;
67 using ::vendor::qti::hardware::display::mapperextensions::V1_1::IQtiMapperExtensions;
68 using ::vendor::qti::hardware::display::mapperextensions::V1_1::implementation::QtiMapperExtensions;
69 
70 using android::hardware::graphics::mapper::V4_0::IMapper;
71 using BufferDescriptorInfo_4_0 =
72     android::hardware::graphics::mapper::V4_0::IMapper::BufferDescriptorInfo;
73 using IMapperBufferDescriptor = android::hardware::graphics::mapper::V4_0::BufferDescriptor;
74 using MetadataType = ::android::hardware::graphics::mapper::V4_0::IMapper::MetadataType;
75 using MetadataTypeDescription =
76     ::android::hardware::graphics::mapper::V4_0::IMapper::MetadataTypeDescription;
77 using IMapper_4_0_Error = ::android::hardware::graphics::mapper::V4_0::Error;
78 
79 class 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 
Encode(const BufferDescriptorInfo_4_0 & bd_info)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) + name_size);
125 
126     int index = 0;
127     uint32_t magic_version = gralloc::kMagicVersion;
128     std::memcpy(&out[index], &magic_version, sizeof(magic_version));
129     index += sizeof(magic_version);
130 
131     std::memcpy(&out[index], &name_size, sizeof(name_size));
132     index += sizeof(name_size);
133 
134     std::memcpy(&out[index], bd_info.name.c_str(), bd_info.name.size());
135     index += name_size;
136 
137     std::memcpy(&out[index], &bd_info.width, sizeof(bd_info.width));
138     index += sizeof(bd_info.width);
139 
140     std::memcpy(&out[index], &bd_info.height, sizeof(bd_info.height));
141     index += sizeof(bd_info.height);
142 
143     std::memcpy(&out[index], &bd_info.layerCount, sizeof(bd_info.layerCount));
144     index += sizeof(bd_info.layerCount);
145 
146     std::memcpy(&out[index], &bd_info.format, sizeof(bd_info.format));
147     index += sizeof(bd_info.format);
148 
149     std::memcpy(&out[index], &bd_info.usage, sizeof(bd_info.usage));
150     index += sizeof(bd_info.usage);
151 
152     std::memcpy(&out[index], &bd_info.reservedSize, sizeof(bd_info.reservedSize));
153 
154     return out;
155   }
Decode(const hidl_vec<uint8_t> & in,gralloc::BufferDescriptor * buf_descriptor)156   static gralloc::Error Decode(const hidl_vec<uint8_t> &in,
157                                gralloc::BufferDescriptor *buf_descriptor) {
158     // First check is to avoid dereferencing if the vector is too short
159     if (in.size() < gralloc::kBufferDescriptorSizeV4) {
160       return gralloc::Error::BAD_DESCRIPTOR;
161     }
162 
163     int index = 0;
164     uint32_t magic_version;
165     std::memcpy(&magic_version, &in[index], sizeof(magic_version));
166     index += sizeof(magic_version);
167 
168     uint64_t name_size;
169     std::memcpy(&name_size, &in[index], sizeof(name_size));
170     index += sizeof(name_size);
171 
172     // The second check validates that the size and magic version are correct
173     if (in.size() != (gralloc::kBufferDescriptorSizeV4 + name_size + sizeof(name_size)) ||
174         magic_version != gralloc::kMagicVersion) {
175       return gralloc::Error::BAD_DESCRIPTOR;
176     }
177 
178     std::string name;
179 
180     name.resize(name_size);
181     std::memcpy(name.data(), &in[index], name.size());
182     index += name_size;
183     buf_descriptor->SetName(name);
184 
185     uint32_t width, height;
186     std::memcpy(&width, &in[index], sizeof(width));
187 
188     index += sizeof(width);
189     std::memcpy(&height, &in[index], sizeof(height));
190     index += sizeof(height);
191     buf_descriptor->SetDimensions(width, height);
192 
193     uint32_t layer_count;
194     std::memcpy(&layer_count, &in[index], sizeof(layer_count));
195     index += sizeof(layer_count);
196     buf_descriptor->SetLayerCount(layer_count);
197 
198     uint32_t format;
199     std::memcpy(&format, &in[index], sizeof(format));
200     index += sizeof(format);
201     buf_descriptor->SetColorFormat(format);
202 
203     uint64_t usage;
204     std::memcpy(&usage, &in[index], sizeof(usage));
205     index += sizeof(usage);
206     buf_descriptor->SetUsage(usage);
207 
208     uint64_t reserved_size;
209     std::memcpy(&reserved_size, &in[index], sizeof(reserved_size));
210     index += sizeof(reserved_size);
211 
212     buf_descriptor->SetReservedSize(reserved_size);
213     return gralloc::Error::NONE;
214   }
215 
216  private:
217   BufferManager *buf_mgr_ = nullptr;
218   Error CreateDescriptor(const BufferDescriptorInfo_4_0 &descriptor_info,
219                          IMapperBufferDescriptor *descriptor);
220   bool ValidDescriptor(const IMapper::BufferDescriptorInfo &bd);
221   bool GetFenceFd(const hidl_handle &fence_handle, int *outFenceFd);
222   void WaitFenceFd(int fence_fd);
223   Error LockBuffer(void *buffer, uint64_t usage, const hidl_handle &acquire_fence,
224                    const IMapper::Rect &access_region);
225 
226   Error DumpBufferMetadata(const private_handle_t *buffer, BufferDump *outBufferDump);
227 
228   hidl_vec<MetadataTypeDescription> metadata_type_descriptions_ = {
229       // MetadataType, description, gettable, settable
230       {android::gralloc4::MetadataType_BufferId, "", true, false},
231       {android::gralloc4::MetadataType_Name, "", true, false},
232       {android::gralloc4::MetadataType_Width, "", true, false},
233       {android::gralloc4::MetadataType_Height, "", true, false},
234       {android::gralloc4::MetadataType_LayerCount, "", true, false},
235       {android::gralloc4::MetadataType_PixelFormatRequested, "", true, false},
236       {android::gralloc4::MetadataType_PixelFormatFourCC, "", true, false},
237       {android::gralloc4::MetadataType_PixelFormatModifier, "", true, false},
238       {android::gralloc4::MetadataType_Usage, "", true, false},
239       {android::gralloc4::MetadataType_AllocationSize, "", true, false},
240       {android::gralloc4::MetadataType_ProtectedContent, "", true, false},
241       {android::gralloc4::MetadataType_ChromaSiting, "", true, false},
242       {android::gralloc4::MetadataType_Compression, "", true, false},
243       {android::gralloc4::MetadataType_Interlaced, "", true, false},
244       {android::gralloc4::MetadataType_PlaneLayouts, "", true, false},
245       {android::gralloc4::MetadataType_Dataspace, "", true, true},
246       {android::gralloc4::MetadataType_BlendMode, "", true, true},
247       {android::gralloc4::MetadataType_Smpte2086, "", true, true},
248       {android::gralloc4::MetadataType_Cta861_3, "", true, true},
249       {android::gralloc4::MetadataType_Smpte2094_40, "", true, true},
250       {android::gralloc4::MetadataType_Crop, "", true, true},
251       {qtigralloc::MetadataType_VTTimestamp, "VT Timestamp", true, true},
252       {qtigralloc::MetadataType_ColorMetadata, "Color metadata", true, true},
253       {qtigralloc::MetadataType_PPParamInterlaced, "Interlaced", true, true},
254       {qtigralloc::MetadataType_VideoPerfMode, "Video perf mode", true, true},
255       {qtigralloc::MetadataType_GraphicsMetadata, "Graphics metadata", true, true},
256       {qtigralloc::MetadataType_UBWCCRStatsInfo, "UBWC stats", true, true},
257       {qtigralloc::MetadataType_RefreshRate, "Refresh rate", true, true},
258       {qtigralloc::MetadataType_MapSecureBuffer, "Secure buffer mappable", true, true},
259       {qtigralloc::MetadataType_LinearFormat, "Linear format", true, true},
260       {qtigralloc::MetadataType_SingleBufferMode, "Single buffer mode flag", true, true},
261       {qtigralloc::MetadataType_CVPMetadata, "CVP metadata", true, true},
262       {qtigralloc::MetadataType_VideoHistogramStats, "Video histogram stats", true, true},
263       {qtigralloc::MetadataType_FD, "fd from private_handle_t", true, false},
264       {qtigralloc::MetadataType_PrivateFlags, "Flags in private_handle_t", true, false},
265       {qtigralloc::MetadataType_AlignedWidthInPixels, "width in private_handle_t", true, false},
266       {qtigralloc::MetadataType_AlignedHeightInPixels, "height in private_handle_t", true, false},
267 #ifdef METADATA_V2
268       {qtigralloc::MetadataType_StandardMetadataStatus, "Is standard metadata set", true, false},
269       {qtigralloc::MetadataType_VendorMetadataStatus, "Is vendor metadata set", true, false},
270 #endif
271 #ifdef QTI_BUFFER_TYPE
272       {qtigralloc::MetadataType_BufferType, "Buffer type from private_handle_t", true, false},
273 #endif
274   };
275 };
276 
277 }  // namespace implementation
278 }  // namespace V4_0
279 }  // namespace mapper
280 }  // namespace display
281 }  // namespace hardware
282 }  // namespace qti
283 }  // namespace vendor
284 
285 #endif  // __QTIMAPPER4_H__
286