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