1 /*
2  * Copyright (C) 2021 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_INNERKITSIMPL_CONVERTER_INCLUDE_POST_PROC_H
17 #define FRAMEWORKS_INNERKITSIMPL_CONVERTER_INCLUDE_POST_PROC_H
18 
19 #include <vector>
20 #include "basic_transformer.h"
21 #include "image_type.h"
22 #include "pixel_map.h"
23 #include "scan_line_filter.h"
24 #include "post_proc_slr.h"
25 
26 namespace OHOS {
27 namespace Media {
28 enum class CropValue : int32_t { INVALID, VALID, NOCROP };
29 
30 class PostProc {
31 public:
32     uint32_t DecodePostProc(const DecodeOptions &opts, PixelMap &pixelMap,
33                             FinalOutputStep finalOutputStep = FinalOutputStep::NO_CHANGE);
34     uint32_t ConvertProc(const Rect &cropRect, ImageInfo &dstImageInfo, PixelMap &pixelMap, ImageInfo &srcImageInfo);
35     static bool IsHasCrop(const Rect &rect);
36     bool HasPixelConvert(const ImageInfo &srcImageInfo, ImageInfo &dstImageInfo);
37     bool RotatePixelMap(float rotateDegrees, PixelMap &pixelMap);
38     bool ScalePixelMap(const Size &size, PixelMap &pixelMap);
39     bool ScalePixelMap(float scaleX, float scaleY, PixelMap &pixelMap);
40     bool TranslatePixelMap(float tX, float tY, PixelMap &pixelMap);
41     bool CenterScale(const Size &size, PixelMap &pixelMap);
42     static CropValue GetCropValue(const Rect &rect, const Size &size);
43     static CropValue ValidCropValue(Rect &rect, const Size &size);
44     bool ScalePixelMapWithSLR(const Size &desiredSize, PixelMap &pixelMap);
45     bool ScalePixelMapEx(const Size &desiredSize, PixelMap &pixelMap,
46         const AntiAliasingOption &option = AntiAliasingOption::NONE);
47 private:
48     static uint8_t *AllocSharedMemory(const Size &size, const uint64_t bufferSize, int &fd, uint32_t uniqueId);
49     static uint8_t *AllocDmaMemory(ImageInfo info, const uint64_t bufferSize,
50                                    void **nativeBuffer, int &targetRowStride);
51     uint32_t NeedScanlineFilter(const Rect &cropRect, const Size &srcSize, const bool &hasPixelConvert);
52     void GetDstImageInfo(const DecodeOptions &opts, PixelMap &pixelMap,
53                          ImageInfo srcImageInfo, ImageInfo &dstImageInfo);
54     uint32_t PixelConvertProc(ImageInfo &dstImageInfo, PixelMap &pixelMap, ImageInfo &srcImageInfo);
55     uint32_t AllocBuffer(ImageInfo imageInfo, uint8_t **resultData, uint64_t &dataSize, int &fd, uint32_t uniqueId);
56     bool AllocHeapBuffer(uint64_t bufferSize, uint8_t **buffer);
57     void ReleaseBuffer(AllocatorType allocatorType, int fd, uint64_t dataSize,
58                        uint8_t **buffer, void *nativeBuffer = nullptr);
59     bool Transform(BasicTransformer &trans, const PixmapInfo &input, PixelMap &pixelMap);
60     void ConvertPixelMapToPixmapInfo(PixelMap &pixelMap, PixmapInfo &pixmapInfo);
61     void SetScanlineCropAndConvert(const Rect &cropRect, ImageInfo &dstImageInfo, ImageInfo &srcImageInfo,
62                                    ScanlineFilter &scanlineFilter, bool hasPixelConvert);
63     bool CenterDisplay(PixelMap &pixelMap, int32_t srcWidth, int32_t srcHeight, int32_t targetWidth,
64                        int32_t targetHeight);
65     uint32_t CheckScanlineFilter(const Rect &cropRect, ImageInfo &dstImageInfo, PixelMap &pixelMap,
66                                  int32_t pixelBytes, ScanlineFilter &scanlineFilter);
67     bool CopyPixels(PixelMap& pixelMap, uint8_t* dstPixels, const Size& dstSize,
68                     const int32_t srcWidth, const int32_t srcHeight,
69                     int srcRowStride = 0, int targetRowStride = 0);
70     bool ProcessScanlineFilter(ScanlineFilter &scanlineFilter, const Rect &cropRect, PixelMap &pixelMap,
71                                uint8_t *resultData, uint32_t rowBytes);
72     std::shared_ptr<SLRWeightTuple> initSLRFactor(Size srcSize, Size dstSize);
73 private:
74     DecodeOptions decodeOpts_;
75 };
76 } // namespace Media
77 } // namespace OHOS
78 
79 #endif // FRAMEWORKS_INNERKITSIMPL_CONVERTER_INCLUDE_POST_PROC_H
80