1 /* 2 * Copyright (c) 2023 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef FRAMEWORKS_SURFACE_INCLUDE_METADATA_MANAGER_H 17 #define FRAMEWORKS_SURFACE_INCLUDE_METADATA_MANAGER_H 18 19 #include <vector> 20 #include <securec.h> 21 22 #include "graphic_common.h" 23 #include "surface_buffer.h" 24 #include "v1_0/cm_color_space.h" 25 #include "v1_0/hdr_static_metadata.h" 26 #include "v1_0/buffer_handle_meta_key_type.h" 27 #ifdef RS_ENABLE_TV_PQ_METADATA 28 #include <functional> 29 #include "tv_pq_metadata.h" 30 #endif 31 32 namespace OHOS { 33 class MetadataHelper { 34 public: 35 // GSERROR_OK for success, GSERROR_API_FAILED for fail 36 template <typename T> ConvertMetadataToVec(const T & metadata,std::vector<uint8_t> & data)37 static GSError ConvertMetadataToVec(const T& metadata, std::vector<uint8_t>& data) 38 { 39 data.resize(sizeof(T)); 40 if (memcpy_s(data.data(), data.size(), &metadata, sizeof(T)) != EOK) { 41 return GSERROR_API_FAILED; 42 } 43 return GSERROR_OK; 44 } 45 46 template <typename T> ConvertVecToMetadata(const std::vector<uint8_t> & data,T & metadata)47 static GSError ConvertVecToMetadata(const std::vector<uint8_t>& data, T& metadata) 48 { 49 if (data.size() != sizeof(T)) { 50 return GSERROR_NOT_SUPPORT; 51 } 52 53 if (memcpy_s(&metadata, sizeof(T), data.data(), data.size()) != EOK) { 54 return GSERROR_API_FAILED; 55 } 56 return GSERROR_OK; 57 } 58 59 static GSError ConvertColorSpaceTypeToInfo( 60 const HDI::Display::Graphic::Common::V1_0::CM_ColorSpaceType& colorSpaceType, 61 HDI::Display::Graphic::Common::V1_0::CM_ColorSpaceInfo& colorSpaceInfo); 62 static GSError ConvertColorSpaceInfoToType( 63 const HDI::Display::Graphic::Common::V1_0::CM_ColorSpaceInfo& colorSpaceInfo, 64 HDI::Display::Graphic::Common::V1_0::CM_ColorSpaceType& colorSpaceType); 65 66 static GSError SetColorSpaceInfo(sptr<SurfaceBuffer>& buffer, 67 const HDI::Display::Graphic::Common::V1_0::CM_ColorSpaceInfo& colorSpaceInfo); 68 static GSError GetColorSpaceInfo(const sptr<SurfaceBuffer>& buffer, 69 HDI::Display::Graphic::Common::V1_0::CM_ColorSpaceInfo& colorSpaceInfo); 70 71 static GSError SetColorSpaceType(sptr<SurfaceBuffer>& buffer, 72 const HDI::Display::Graphic::Common::V1_0::CM_ColorSpaceType& colorSpaceType); 73 static GSError GetColorSpaceType(const sptr<SurfaceBuffer>& buffer, 74 HDI::Display::Graphic::Common::V1_0::CM_ColorSpaceType& colorSpaceType); 75 76 static GSError SetHDRMetadataType(sptr<SurfaceBuffer>& buffer, 77 const HDI::Display::Graphic::Common::V1_0::CM_HDR_Metadata_Type& hdrMetadataType); 78 static GSError GetHDRMetadataType(const sptr<SurfaceBuffer>& buffer, 79 HDI::Display::Graphic::Common::V1_0::CM_HDR_Metadata_Type& hdrMetadataType); 80 81 static GSError SetHDRStaticMetadata(sptr<SurfaceBuffer>& buffer, 82 const HDI::Display::Graphic::Common::V1_0::HdrStaticMetadata& hdrStaticMetadata); 83 static GSError GetHDRStaticMetadata(const sptr<SurfaceBuffer>& buffer, 84 HDI::Display::Graphic::Common::V1_0::HdrStaticMetadata& hdrStaticMetadata); 85 86 static GSError SetHDRDynamicMetadata(sptr<SurfaceBuffer>& buffer, const std::vector<uint8_t>& hdrDynamicMetadata); 87 static GSError GetHDRDynamicMetadata(const sptr<SurfaceBuffer>& buffer, std::vector<uint8_t>& hdrDynamicMetadata); 88 89 static GSError SetHDRStaticMetadata(sptr<SurfaceBuffer>& buffer, const std::vector<uint8_t>& hdrStaticMetadata); 90 static GSError GetHDRStaticMetadata(const sptr<SurfaceBuffer>& buffer, std::vector<uint8_t>& hdrStaticMetadata); 91 92 static GSError GetCropRectMetadata(const sptr<SurfaceBuffer>& buffer, 93 HDI::Display::Graphic::Common::V1_0::BufferHandleMetaRegion& crop); 94 95 static GSError SetImageHDRMetadataType(sptr<SurfaceBuffer>& buffer, uint8_t* metadata); 96 97 static bool IsImageMetadataType(uint8_t* metadata); 98 99 #ifdef RS_ENABLE_TV_PQ_METADATA 100 using OnSetFieldsFunc = std::function<void(TvPQMetadata &tvPQMetadata)>; 101 static GSError SetVideoTVMetadata(sptr<SurfaceBuffer>& buffer, const TvPQMetadata& tvMetadata); 102 static GSError GetVideoTVMetadata(const sptr<SurfaceBuffer>& buffer, TvPQMetadata& tvMetadata); 103 104 static GSError SetSceneTag(sptr<SurfaceBuffer>& buffer, unsigned char value); 105 static GSError SetUIFrameCount(sptr<SurfaceBuffer>& buffer, unsigned char value); 106 static GSError SetVideoFrameCount(sptr<SurfaceBuffer>& buffer, unsigned char value); 107 static GSError SetVideoFrameRate(sptr<SurfaceBuffer>& buffer, unsigned char value); 108 static GSError SetVideoDecoderHigh(sptr<SurfaceBuffer>& buffer, unsigned short vidVdhWidth, 109 unsigned short vidVdhHeight); 110 static GSError SetVideoTVScaleMode(sptr<SurfaceBuffer>& buffer, unsigned char value); 111 static GSError SetVideoTVDpPixelFormat(sptr<SurfaceBuffer>& buffer, unsigned int value); 112 static GSError SetVideoColorimetryHdr(sptr<SurfaceBuffer>& buffer, unsigned char hdr, unsigned char colorimetry); 113 static GSError SetVideoTVInfo(sptr<SurfaceBuffer>& buffer, const TvVideoWindow& tvVideoWindow); 114 static GSError EraseVideoTVInfoKey(sptr<SurfaceBuffer>& buffer); 115 #endif 116 static GSError SetAdaptiveFOVMetadata(sptr<SurfaceBuffer>& buffer, 117 const std::vector<uint8_t>& adaptiveFOVMetadata); 118 static GSError GetAdaptiveFOVMetadata(const sptr<SurfaceBuffer>& buffer, 119 std::vector<uint8_t>& adaptiveFOVMetadata); 120 121 static GSError GetSDRDynamicMetadata(const sptr<SurfaceBuffer>& buffer, 122 std::vector<uint8_t>& sdrDynamicMetadata); 123 private: 124 #ifdef RS_ENABLE_TV_PQ_METADATA 125 static GSError UpdateTVMetadataField(sptr<SurfaceBuffer>& buffer, OnSetFieldsFunc func); 126 #endif 127 static constexpr uint32_t PRIMARIES_MASK = 128 static_cast<uint32_t>(HDI::Display::Graphic::Common::V1_0::CM_PRIMARIES_MASK); 129 static constexpr uint32_t TRANSFUNC_MASK = 130 static_cast<uint32_t>(HDI::Display::Graphic::Common::V1_0::CM_TRANSFUNC_MASK); 131 static constexpr uint32_t MATRIX_MASK = 132 static_cast<uint32_t>(HDI::Display::Graphic::Common::V1_0::CM_MATRIX_MASK); 133 static constexpr uint32_t RANGE_MASK = 134 static_cast<uint32_t>(HDI::Display::Graphic::Common::V1_0::CM_RANGE_MASK); 135 static constexpr uint32_t TRANSFUNC_OFFSET = 8; 136 static constexpr uint32_t MATRIX_OFFSET = 16; 137 static constexpr uint32_t RANGE_OFFSET = 21; 138 }; 139 } // namespace OHOS 140 141 #endif // FRAMEWORKS_SURFACE_INCLUDE_METADATA_MANAGER_H