1 /*
2  * Copyright (c) 2024 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 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 OHOS_HDI_CODEC_IMAGE_V2_0_BUFFER_HELPER
17 #define OHOS_HDI_CODEC_IMAGE_V2_0_BUFFER_HELPER
18 
19 #include <map>
20 #include <list>
21 #include <set>
22 #include <fstream>
23 #include <securec.h>
24 #include "log.h"
25 #include "v2_0/icodec_image.h"
26 #include "v1_2/display_composer_type.h"
27 #include "v1_2/display_buffer_type.h"
28 #include "v1_2/include/idisplay_buffer.h"
29 
30 namespace OHOS::VDI::HEIF {
31 class BufferHelper {
32 public:
33     BufferHelper();
34     ~BufferHelper();
35     OHOS::sptr<OHOS::HDI::Base::NativeBuffer> CreateImgBuffer(const std::string& imageFile);
36     OHOS::HDI::Codec::Image::V2_0::SharedBuffer CreateSharedBuffer(
37         std::map<OHOS::HDI::Codec::Image::V2_0::PropertyType, std::string>& metaInfo);
38     OHOS::HDI::Codec::Image::V2_0::SharedBuffer CreateSharedBuffer(const std::string& metaFile);
39     void DumpBuffer(const std::string& filePath, const OHOS::HDI::Codec::Image::V2_0::SharedBuffer& buffer);
40 private:
41     struct PixelFileInfo {
42         uint32_t displayWidth;
43         uint32_t alignedWidth;
44         uint32_t displayHeight;
45         uint32_t alignedHeight;
46         uint32_t pixFmt;
47     };
48 private:
49     static bool ExtractPixelInfoFromFilePath(const std::string& filePath, PixelFileInfo& pixelInfo);
50     static uint32_t GetPixelFmtFromFileSuffix(const std::string& imageFile);
51     bool CopyYuvData(BufferHandle *handle, std::ifstream &ifs, PixelFileInfo& pixelInfo);
52     bool CopyRgbaData(BufferHandle *handle, std::ifstream &ifs, PixelFileInfo& pixelInfo);
53 private:
54     OHOS::HDI::Display::Buffer::V1_2::IDisplayBuffer* bufferMgr_;
55     std::set<int> allocatedFd_;
56 };
57 
58 class ByteWriter {
59 public:
60     ByteWriter() = default;
61     ~ByteWriter();
62     template <typename T>
AddData(OHOS::HDI::Codec::Image::V2_0::PropertyType key,T & value)63     bool AddData(OHOS::HDI::Codec::Image::V2_0::PropertyType key, T& value)
64     {
65         std::size_t keySize = sizeof(key);
66         std::size_t valueSize = sizeof(value);
67         std::size_t dataSize = keySize + valueSize;
68         uint8_t* p = new uint8_t[dataSize];
69         IF_TRUE_RETURN_VAL(p == nullptr, false);
70         data_.emplace_back(DataBlock {
71             .data = p,
72             .len = dataSize
73         });
74         totalSize_ += dataSize;
75         HDF_LOGD("key=%{public}d, keySize=%{public}zu, valueSize=%{public}zu, " \
76                  "dataSize=%{public}zu, totalSize_=%{public}zu",
77                  key, keySize, valueSize, dataSize, totalSize_);
78         errno_t ret = memset_s(p, dataSize, 0, dataSize);
79         IF_TRUE_RETURN_VAL(ret != EOK, false);
80         ret = memcpy_s(p, dataSize, reinterpret_cast<uint8_t*>(&key), keySize);
81         IF_TRUE_RETURN_VAL(ret != EOK, false);
82         ret = memcpy_s(p + keySize, valueSize, reinterpret_cast<uint8_t*>(&value), valueSize);
83         IF_TRUE_RETURN_VAL(ret != EOK, false);
84         return true;
85     }
86     bool Finalize(std::vector<uint8_t>& dst);
87     bool AddDataFromFile(OHOS::HDI::Codec::Image::V2_0::PropertyType key, const std::string& filePath);
88     bool Finalize(OHOS::HDI::Codec::Image::V2_0::SharedBuffer& buffer);
89 private:
90     struct DataBlock {
91         uint8_t* data = nullptr;
92         std::size_t len = 0;
93     };
94 private:
95     bool CopyDataTo(uint8_t* dstStart);
96 private:
97     std::list<DataBlock> data_;
98     std::size_t totalSize_ = 0;
99 };
100 } // OHOS::VDI::HEIF
101 #endif // OHOS_HDI_CODEC_IMAGE_V2_0_BUFFER_HELPER