Tharaga Balachandran | 74ab111 | 2020-01-08 17:17:56 -0500 | [diff] [blame] | 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 | |
| 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 | out[index] = static_cast<uint8_t> (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 | // Cap the reserved region size at one page (4096 bytes) |
| 154 | uint64_t reserved_size = std::min(bd_info.reservedSize, (uint64_t)4096); |
| 155 | std::memcpy(&out[index], &reserved_size, sizeof(reserved_size)); |
| 156 | |
| 157 | return out; |
| 158 | } |
| 159 | static gralloc::Error Decode(const hidl_vec<uint8_t> &in, |
| 160 | gralloc::BufferDescriptor *buf_descriptor) { |
| 161 | // First check is to avoid dereferencing if the vector is too short |
| 162 | if (in.size() < gralloc::kBufferDescriptorSizeV4) { |
| 163 | return gralloc::Error::BAD_DESCRIPTOR; |
| 164 | } |
| 165 | |
| 166 | size_t index = 0; |
| 167 | uint32_t magic_version; |
| 168 | std::memcpy(&magic_version, &in[index], sizeof(magic_version)); |
| 169 | index += sizeof(magic_version); |
| 170 | |
| 171 | uint64_t name_size = in[index]; |
| 172 | index += sizeof(name_size); |
| 173 | |
| 174 | // The second check validates that the size and magic version are correct |
| 175 | if (in.size() != (gralloc::kBufferDescriptorSizeV4 + name_size + sizeof(name_size)) || |
| 176 | magic_version != gralloc::kMagicVersion) { |
| 177 | return gralloc::Error::BAD_DESCRIPTOR; |
| 178 | } |
| 179 | |
| 180 | std::string name; |
| 181 | |
| 182 | name.resize(static_cast<size_t>(name_size)); |
| 183 | std::memcpy(name.data(), &in[index], name.size()); |
| 184 | index += name_size; |
| 185 | buf_descriptor->SetName(name); |
| 186 | |
| 187 | int32_t width, height; |
| 188 | std::memcpy(&width, &in[index], sizeof(width)); |
| 189 | |
| 190 | index += sizeof(width); |
| 191 | std::memcpy(&height, &in[index], sizeof(height)); |
| 192 | index += sizeof(height); |
| 193 | buf_descriptor->SetDimensions(width, height); |
| 194 | |
| 195 | uint32_t layer_count; |
| 196 | std::memcpy(&layer_count, &in[index], sizeof(layer_count)); |
| 197 | index += sizeof(layer_count); |
| 198 | buf_descriptor->SetLayerCount(layer_count); |
| 199 | |
| 200 | int32_t format; |
| 201 | std::memcpy(&format, &in[index], sizeof(format)); |
| 202 | index += sizeof(format); |
| 203 | buf_descriptor->SetColorFormat(format); |
| 204 | |
| 205 | uint64_t usage; |
| 206 | std::memcpy(&usage, &in[index], sizeof(usage)); |
| 207 | index += sizeof(usage); |
| 208 | buf_descriptor->SetUsage(usage); |
| 209 | |
| 210 | uint64_t reserved_size; |
| 211 | std::memcpy(&reserved_size, &in[index], sizeof(reserved_size)); |
| 212 | index += sizeof(reserved_size); |
| 213 | |
| 214 | buf_descriptor->SetReservedSize(reserved_size); |
| 215 | return gralloc::Error::NONE; |
| 216 | } |
| 217 | |
| 218 | private: |
| 219 | BufferManager *buf_mgr_ = nullptr; |
| 220 | Error CreateDescriptor(const BufferDescriptorInfo_4_0 &descriptor_info, |
| 221 | IMapperBufferDescriptor *descriptor); |
| 222 | bool ValidDescriptor(const IMapper::BufferDescriptorInfo &bd); |
| 223 | bool GetFenceFd(const hidl_handle &fence_handle, int *outFenceFd); |
| 224 | void WaitFenceFd(int fence_fd); |
| 225 | Error LockBuffer(void *buffer, uint64_t usage, const hidl_handle &acquire_fence, |
| 226 | const IMapper::Rect &access_region); |
| 227 | |
| 228 | Error DumpBufferMetadata(const private_handle_t *buffer, BufferDump *outBufferDump); |
| 229 | |
| 230 | hidl_vec<MetadataTypeDescription> metadata_type_descriptions_ = { |
| 231 | // MetadataType, description, gettable, settable |
| 232 | {android::gralloc4::MetadataType_BufferId, "", true, false}, |
| 233 | {android::gralloc4::MetadataType_Name, "", true, false}, |
| 234 | {android::gralloc4::MetadataType_Width, "", true, false}, |
| 235 | {android::gralloc4::MetadataType_Height, "", true, false}, |
| 236 | {android::gralloc4::MetadataType_LayerCount, "", true, false}, |
| 237 | {android::gralloc4::MetadataType_PixelFormatRequested, "", true, false}, |
| 238 | {android::gralloc4::MetadataType_PixelFormatFourCC, "", true, false}, |
| 239 | {android::gralloc4::MetadataType_PixelFormatModifier, "", true, false}, |
| 240 | {android::gralloc4::MetadataType_Usage, "", true, false}, |
| 241 | {android::gralloc4::MetadataType_AllocationSize, "", true, false}, |
| 242 | {android::gralloc4::MetadataType_ProtectedContent, "", true, false}, |
| 243 | {android::gralloc4::MetadataType_ChromaSiting, "", true, false}, |
| 244 | {android::gralloc4::MetadataType_Compression, "", true, false}, |
| 245 | {android::gralloc4::MetadataType_Interlaced, "", true, false}, |
| 246 | {android::gralloc4::MetadataType_PlaneLayouts, "", true, false}, |
| 247 | {android::gralloc4::MetadataType_Dataspace, "", true, true}, |
| 248 | {android::gralloc4::MetadataType_BlendMode, "", true, true}, |
| 249 | {android::gralloc4::MetadataType_Smpte2086, "", true, true}, |
| 250 | {android::gralloc4::MetadataType_Cta861_3, "", true, true}, |
| 251 | {android::gralloc4::MetadataType_Smpte2094_40, "", true, true}, |
Tharaga Balachandran | 95175bb | 2020-03-20 13:26:55 -0400 | [diff] [blame^] | 252 | {android::gralloc4::MetadataType_Crop, "", true, true}, |
Tharaga Balachandran | 74ab111 | 2020-01-08 17:17:56 -0500 | [diff] [blame] | 253 | {qtigralloc::MetadataType_VTTimestamp, "VT Timestamp", true, true}, |
| 254 | {qtigralloc::MetadataType_ColorMetadata, "Color metadata", true, true}, |
| 255 | {qtigralloc::MetadataType_PPParamInterlaced, "Interlaced", true, true}, |
| 256 | {qtigralloc::MetadataType_VideoPerfMode, "Video perf mode", true, true}, |
| 257 | {qtigralloc::MetadataType_GraphicsMetadata, "Graphics metadata", true, true}, |
| 258 | {qtigralloc::MetadataType_UBWCCRStatsInfo, "UBWC stats", true, true}, |
| 259 | {qtigralloc::MetadataType_RefreshRate, "Refresh rate", true, true}, |
| 260 | {qtigralloc::MetadataType_MapSecureBuffer, "Secure buffer mappable", true, true}, |
| 261 | {qtigralloc::MetadataType_LinearFormat, "Linear format", true, true}, |
| 262 | {qtigralloc::MetadataType_SingleBufferMode, "Single buffer mode flag", true, true}, |
| 263 | {qtigralloc::MetadataType_CVPMetadata, "CVP metadata", true, true}, |
| 264 | {qtigralloc::MetadataType_VideoHistogramStats, "Video histogram stats", true, true}, |
| 265 | {qtigralloc::MetadataType_FD, "fd from private_handle_t", true, false}, |
| 266 | {qtigralloc::MetadataType_PrivateFlags, "Flags in private_handle_t", true, false}, |
| 267 | }; |
| 268 | }; |
| 269 | |
| 270 | } // namespace implementation |
| 271 | } // namespace V4_0 |
| 272 | } // namespace mapper |
| 273 | } // namespace display |
| 274 | } // namespace hardware |
| 275 | } // namespace qti |
| 276 | } // namespace vendor |
| 277 | |
| 278 | #endif // __QTIMAPPER4_H__ |