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