1094332d3Sopenharmony_ci/*
2094332d3Sopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd.
3094332d3Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4094332d3Sopenharmony_ci * you may not use this file except in compliance with the License.
5094332d3Sopenharmony_ci * You may obtain a copy of the License at
6094332d3Sopenharmony_ci *
7094332d3Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8094332d3Sopenharmony_ci *
9094332d3Sopenharmony_ci * Unless required by law or agreed to in writing, software
10094332d3Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11094332d3Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12094332d3Sopenharmony_ci * See the License for the specific language governing permissions and
13094332d3Sopenharmony_ci * limitations under the License.
14094332d3Sopenharmony_ci */
15094332d3Sopenharmony_ci
16094332d3Sopenharmony_ci#ifndef OHOS_HDI_CODEC_IMAGE_V2_0_BUFFER_HELPER
17094332d3Sopenharmony_ci#define OHOS_HDI_CODEC_IMAGE_V2_0_BUFFER_HELPER
18094332d3Sopenharmony_ci
19094332d3Sopenharmony_ci#include <map>
20094332d3Sopenharmony_ci#include <list>
21094332d3Sopenharmony_ci#include <set>
22094332d3Sopenharmony_ci#include <fstream>
23094332d3Sopenharmony_ci#include <securec.h>
24094332d3Sopenharmony_ci#include "log.h"
25094332d3Sopenharmony_ci#include "v2_0/icodec_image.h"
26094332d3Sopenharmony_ci#include "v1_2/display_composer_type.h"
27094332d3Sopenharmony_ci#include "v1_2/display_buffer_type.h"
28094332d3Sopenharmony_ci#include "v1_2/include/idisplay_buffer.h"
29094332d3Sopenharmony_ci
30094332d3Sopenharmony_cinamespace OHOS::VDI::HEIF {
31094332d3Sopenharmony_ciclass BufferHelper {
32094332d3Sopenharmony_cipublic:
33094332d3Sopenharmony_ci    BufferHelper();
34094332d3Sopenharmony_ci    ~BufferHelper();
35094332d3Sopenharmony_ci    OHOS::sptr<OHOS::HDI::Base::NativeBuffer> CreateImgBuffer(const std::string& imageFile);
36094332d3Sopenharmony_ci    OHOS::HDI::Codec::Image::V2_0::SharedBuffer CreateSharedBuffer(
37094332d3Sopenharmony_ci        std::map<OHOS::HDI::Codec::Image::V2_0::PropertyType, std::string>& metaInfo);
38094332d3Sopenharmony_ci    OHOS::HDI::Codec::Image::V2_0::SharedBuffer CreateSharedBuffer(const std::string& metaFile);
39094332d3Sopenharmony_ci    void DumpBuffer(const std::string& filePath, const OHOS::HDI::Codec::Image::V2_0::SharedBuffer& buffer);
40094332d3Sopenharmony_ciprivate:
41094332d3Sopenharmony_ci    struct PixelFileInfo {
42094332d3Sopenharmony_ci        uint32_t displayWidth;
43094332d3Sopenharmony_ci        uint32_t alignedWidth;
44094332d3Sopenharmony_ci        uint32_t displayHeight;
45094332d3Sopenharmony_ci        uint32_t alignedHeight;
46094332d3Sopenharmony_ci        uint32_t pixFmt;
47094332d3Sopenharmony_ci    };
48094332d3Sopenharmony_ciprivate:
49094332d3Sopenharmony_ci    static bool ExtractPixelInfoFromFilePath(const std::string& filePath, PixelFileInfo& pixelInfo);
50094332d3Sopenharmony_ci    static uint32_t GetPixelFmtFromFileSuffix(const std::string& imageFile);
51094332d3Sopenharmony_ci    bool CopyYuvData(BufferHandle *handle, std::ifstream &ifs, PixelFileInfo& pixelInfo);
52094332d3Sopenharmony_ci    bool CopyRgbaData(BufferHandle *handle, std::ifstream &ifs, PixelFileInfo& pixelInfo);
53094332d3Sopenharmony_ciprivate:
54094332d3Sopenharmony_ci    OHOS::HDI::Display::Buffer::V1_2::IDisplayBuffer* bufferMgr_;
55094332d3Sopenharmony_ci    std::set<int> allocatedFd_;
56094332d3Sopenharmony_ci};
57094332d3Sopenharmony_ci
58094332d3Sopenharmony_ciclass ByteWriter {
59094332d3Sopenharmony_cipublic:
60094332d3Sopenharmony_ci    ByteWriter() = default;
61094332d3Sopenharmony_ci    ~ByteWriter();
62094332d3Sopenharmony_ci    template <typename T>
63094332d3Sopenharmony_ci    bool AddData(OHOS::HDI::Codec::Image::V2_0::PropertyType key, T& value)
64094332d3Sopenharmony_ci    {
65094332d3Sopenharmony_ci        std::size_t keySize = sizeof(key);
66094332d3Sopenharmony_ci        std::size_t valueSize = sizeof(value);
67094332d3Sopenharmony_ci        std::size_t dataSize = keySize + valueSize;
68094332d3Sopenharmony_ci        uint8_t* p = new uint8_t[dataSize];
69094332d3Sopenharmony_ci        IF_TRUE_RETURN_VAL(p == nullptr, false);
70094332d3Sopenharmony_ci        data_.emplace_back(DataBlock {
71094332d3Sopenharmony_ci            .data = p,
72094332d3Sopenharmony_ci            .len = dataSize
73094332d3Sopenharmony_ci        });
74094332d3Sopenharmony_ci        totalSize_ += dataSize;
75094332d3Sopenharmony_ci        HDF_LOGD("key=%{public}d, keySize=%{public}zu, valueSize=%{public}zu, " \
76094332d3Sopenharmony_ci                 "dataSize=%{public}zu, totalSize_=%{public}zu",
77094332d3Sopenharmony_ci                 key, keySize, valueSize, dataSize, totalSize_);
78094332d3Sopenharmony_ci        errno_t ret = memset_s(p, dataSize, 0, dataSize);
79094332d3Sopenharmony_ci        IF_TRUE_RETURN_VAL(ret != EOK, false);
80094332d3Sopenharmony_ci        ret = memcpy_s(p, dataSize, reinterpret_cast<uint8_t*>(&key), keySize);
81094332d3Sopenharmony_ci        IF_TRUE_RETURN_VAL(ret != EOK, false);
82094332d3Sopenharmony_ci        ret = memcpy_s(p + keySize, valueSize, reinterpret_cast<uint8_t*>(&value), valueSize);
83094332d3Sopenharmony_ci        IF_TRUE_RETURN_VAL(ret != EOK, false);
84094332d3Sopenharmony_ci        return true;
85094332d3Sopenharmony_ci    }
86094332d3Sopenharmony_ci    bool Finalize(std::vector<uint8_t>& dst);
87094332d3Sopenharmony_ci    bool AddDataFromFile(OHOS::HDI::Codec::Image::V2_0::PropertyType key, const std::string& filePath);
88094332d3Sopenharmony_ci    bool Finalize(OHOS::HDI::Codec::Image::V2_0::SharedBuffer& buffer);
89094332d3Sopenharmony_ciprivate:
90094332d3Sopenharmony_ci    struct DataBlock {
91094332d3Sopenharmony_ci        uint8_t* data = nullptr;
92094332d3Sopenharmony_ci        std::size_t len = 0;
93094332d3Sopenharmony_ci    };
94094332d3Sopenharmony_ciprivate:
95094332d3Sopenharmony_ci    bool CopyDataTo(uint8_t* dstStart);
96094332d3Sopenharmony_ciprivate:
97094332d3Sopenharmony_ci    std::list<DataBlock> data_;
98094332d3Sopenharmony_ci    std::size_t totalSize_ = 0;
99094332d3Sopenharmony_ci};
100094332d3Sopenharmony_ci} // OHOS::VDI::HEIF
101094332d3Sopenharmony_ci#endif // OHOS_HDI_CODEC_IMAGE_V2_0_BUFFER_HELPER