1 /*
2  * Copyright (c) 2022 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 FOUNDATION_ACE_FRAMEWORKS_CORE_IMAGE_IMAGE_COMPRESSOR_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_IMAGE_IMAGE_COMPRESSOR_H
18 
19 #include <atomic>
20 #include <mutex>
21 #include <set>
22 
23 #include "base/geometry/size.h"
24 #ifdef ENABLE_OPENCL
25 #define USE_OPENCL_WRAPPER
26 #include "opencl_wrapper.h"
27 #endif // ENABLE_OPENCL
28 #ifndef USE_ROSEN_DRAWING
29 #include "include/core/SkData.h"
30 #include "include/core/SkGraphics.h"
31 #include "include/core/SkPixmap.h"
32 #else
33 #include "core/components_ng/render/drawing_forward.h"
34 #endif
35 
36 namespace OHOS::Ace {
37 #define MAGIC_FILE_CONSTANT 0x5CA1AB13
38 #define DIM 4
39 typedef struct {
40     uint8_t magic[4];
41     uint8_t blockdimX;
42     uint8_t blockdimY;
43     uint8_t blockdimZ;
44     uint8_t xsize[3];
45     uint8_t ysize[3];
46     uint8_t zsize[3];
47 } AstcHeader;
48 
49 class ImageCompressor {
50 public:
51     static std::shared_ptr<ImageCompressor> GetInstance();
52     static const int32_t releaseTimeMs = 1000;
53 
54     bool CanCompress();
55 #ifndef USE_ROSEN_DRAWING
56     sk_sp<SkData> GpuCompress(std::string key, SkPixmap& pixmap, int32_t width, int32_t height);
57 #else
58     std::shared_ptr<RSData> GpuCompress(std::string key, RSBitmap& bitmap, int32_t width, int32_t height);
59 #endif
60     std::function<void()> ScheduleReleaseTask();
61 #ifndef USE_ROSEN_DRAWING
62     void WriteToFile(std::string key, sk_sp<SkData> compressdImage, Size size);
63     static sk_sp<SkData> StripFileHeader(sk_sp<SkData> fileData);
64 #else
65     void WriteToFile(std::string key, std::shared_ptr<RSData> compressdImage, Size size);
66     static std::shared_ptr<RSData> StripFileHeader(std::shared_ptr<RSData> fileData);
67 #endif
68 #ifdef FUZZTEST
69     void PartDoing();
70 #endif
71 private:
72     static std::shared_ptr<ImageCompressor> instance_;
73     static std::mutex instanceMutex_;
74 
75     bool clOk_;
76     bool switch_;
77     void Init();
78 #ifdef ENABLE_OPENCL
79     static const int32_t maxSize_ = 100000;
80     int32_t maxErr_;
81     int32_t psnr_;
82     const std::string shader_path_ = "/system/bin/astc.bin";
83     std::atomic<int32_t> refCount_;
84     cl_context context_;
85     cl_command_queue queue_;
86     cl_kernel kernel_;
87 
88     cl_program LoadShaderBin(cl_context context, cl_device_id device_id);
89     bool CreateKernel();
90     void ReleaseResource();
91     bool CheckImageQuality(std::string key, uint32_t sumErr, uint32_t maxErr, int32_t width, int32_t height);
92     bool IsFailedImage(std::string key);
93 #endif
94     int32_t partitions_[73] = {
95         2, 5, 9, 14, 16, 17, 20, 24, 25, 28, 36, 39, 43, 48, 49, 50, 51, 53, 55, 61, 72, 78, 107, 113, 116, 149, 156,
96         198, 204, 210, 216, 226, 232, 239, 269, 273, 293, 324, 344, 348, 359, 389, 394, 441, 443, 475, 476, 479, 496,
97         511, 567, 593, 594, 600, 601, 666, 684, 703, 726, 730, 732, 756, 796, 799, 828, 958, 959, 988, 993
98     };
99     struct PartInfo {
100         int32_t partid;
101         uint32_t bitmaps[2];
102     };
103     void InitPartition();
104     bool InitPartitionInfo(PartInfo *partInfo, int32_t partIndex, int32_t partCount);
105     std::vector<PartInfo> parts_;
106     std::string compileOption_;
107 
108     std::mutex recordsMutex_;
109     std::set<std::string> failedRecords_;
110     std::string recordsPath_;
111     void InitRecords();
112 };
113 } // namespace OHOS::Ace
114 
115 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_IMAGE_IMAGE_COMPRESSOR_H
116