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