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) +
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 
132     std::memcpy(&out[index], &name_size, sizeof(name_size));
133     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 
153     std::memcpy(&out[index], &bd_info.reservedSize, sizeof(bd_info.reservedSize));
154 
155     return out;
156   }
Decode(const hidl_vec<uint8_t> & in,gralloc::BufferDescriptor * buf_descriptor)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 
169     uint64_t name_size;
170     std::memcpy(&name_size, &in[index], sizeof(name_size));
171     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},
251       {android::gralloc4::MetadataType_Crop, "", true, true},
252       {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},
266       {qtigralloc::MetadataType_AlignedWidthInPixels, "width in private_handle_t", true, false},
267       {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
272 #ifdef QTI_BUFFER_TYPE
273       {qtigralloc::MetadataType_BufferType, "Buffer type from private_handle_t", true, false},
274 #endif
275   };
276 };
277 
278 }  // namespace implementation
279 }  // namespace V4_0
280 }  // namespace mapper
281 }  // namespace display
282 }  // namespace hardware
283 }  // namespace qti
284 }  // namespace vendor
285 
286 #endif  // __QTIMAPPER4_H__
287