1 /* 2 * Copyright (C) 2024 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_SLR_H 17 #define FRAMEWORKS_INNERKITSIMPL_CONVERTER_INCLUDE_POST_PROC_SLR_H 18 19 #include "image_type.h" 20 #include "include/private/SkMutex.h" 21 #include "src/core/SkLRUCache.h" 22 23 namespace OHOS { 24 namespace Media { 25 26 class SLRMat { 27 public: 28 SLRMat() = default; SLRMat(Size size,PixelFormat format,void * data,int32_t rowStride)29 SLRMat(Size size, PixelFormat format, void *data, int32_t rowStride) 30 :size_(size), format_(format), data_(data), rowStride_(rowStride) {} 31 ~SLRMat() = default; 32 Size size_; 33 PixelFormat format_; 34 void *data_; 35 int32_t rowStride_; 36 }; 37 38 class SLRWeightKey { 39 public: SLRWeightKey(Size src,Size dst)40 SLRWeightKey(Size src, Size dst): src_(src), dst_(dst) 41 { 42 fKey = HashKey(); 43 } 44 HashKey()45 uint32_t HashKey() 46 { 47 uint32_t hash = 0; 48 hash = mix(hash, SkGoodHash()(relax(src_.width))); 49 hash = mix(hash, SkGoodHash()(relax(src_.height))); 50 hash = mix(hash, SkGoodHash()(relax(dst_.width))); 51 hash = mix(hash, SkGoodHash()(relax(dst_.height))); 52 return hash; 53 } 54 mix(uint32_t hash,uint32_t data)55 uint32_t mix(uint32_t hash, uint32_t data) 56 { 57 hash += data; 58 hash += (hash << 10); // 10 hash value 59 hash ^= (hash >> 6); // 6 hash value 60 return hash; 61 } 62 relax(SkScalar a)63 int32_t relax(SkScalar a) 64 { 65 if (SkScalarIsFinite(a)) { 66 auto threshold = SkIntToScalar(1 << 12); 67 return SkFloat2Bits(SkScalarRoundToScalar(a * threshold) / threshold); 68 } else { 69 return SkFloat2Bits(a); 70 } 71 } 72 Size src_; 73 Size dst_; 74 uint32_t fKey; 75 }; 76 77 using SLRWeightVec = std::vector<std::vector<float>>; 78 using SLRWeightMat = std::shared_ptr<SLRWeightVec>; 79 using SLRWeightTuple = std::tuple<SLRWeightMat, SLRWeightMat, SLRWeightKey>; 80 using SLRLRUCache = SkLRUCache<uint32_t, std::shared_ptr<SLRWeightTuple>>; 81 class SkSLRCacheMgr { 82 public: SkSLRCacheMgr(SLRLRUCache & slrCache,SkMutex & mutex)83 SkSLRCacheMgr(SLRLRUCache& slrCache, SkMutex& mutex) 84 :fSLRCache(slrCache), fMutex(mutex) 85 { 86 fMutex.acquire(); 87 } 88 SkSLRCacheMgr(SkSLRCacheMgr&&) = delete; 89 SkSLRCacheMgr(const SkSLRCacheMgr&) = delete; 90 SkSLRCacheMgr& operator=(const SkSLRCacheMgr&) = delete; 91 SkSLRCacheMgr& operator=(SkSLRCacheMgr&&) = delete; 92 ~SkSLRCacheMgr()93 ~SkSLRCacheMgr() 94 { 95 fMutex.release(); 96 } 97 find(uint32_t key)98 std::shared_ptr<SLRWeightTuple> find(uint32_t key) 99 { 100 auto weight = fSLRCache.find(key); 101 return weight == nullptr ? nullptr : *weight; 102 } 103 insert(uint32_t key,std::shared_ptr<SLRWeightTuple> weightTuple)104 std::shared_ptr<SLRWeightTuple> insert(uint32_t key, std::shared_ptr<SLRWeightTuple> weightTuple) 105 { 106 auto weight = fSLRCache.insert(key, std::move(weightTuple)); 107 return weight == nullptr ? nullptr : *weight; 108 } 109 Count()110 int Count() 111 { 112 return fSLRCache.count(); 113 } 114 Reset()115 void Reset() 116 { 117 fSLRCache.reset(); 118 } 119 120 private: 121 SLRLRUCache& fSLRCache; 122 SkMutex& fMutex; 123 }; 124 125 class SLRProc { 126 public: 127 static SLRWeightMat GetWeights(float coeff, int n); 128 static void Serial(const SLRMat &src, SLRMat &dst, const SLRWeightMat &x, const SLRWeightMat &y); 129 static void Parallel(const SLRMat &src, SLRMat &dst, const SLRWeightMat &x, const SLRWeightMat &y); 130 static void Laplacian(SLRMat &srcMat, void* data, float alpha); 131 }; 132 } // namespace Media 133 } // namespace OHOS 134 135 #endif // FRAMEWORKS_INNERKITSIMPL_CONVERTER_INCLUDE_POST_PROC_SLR_H