1 /*
2  * Copyright (c) 2021-2023 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_CACHE_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_IMAGE_IMAGE_CACHE_H
18 
19 #include <algorithm>
20 #include <list>
21 #include <mutex>
22 #include <shared_mutex>
23 #include <unordered_map>
24 #include <utility>
25 #include <vector>
26 
27 #include "base/memory/ace_type.h"
28 #include "base/utils/macros.h"
29 #include "base/utils/noncopyable.h"
30 #include "core/common/lru/count_limit_lru.h"
31 
32 namespace OHOS::Ace {
33 
34 struct CachedImage;
35 class ImageObject;
36 
37 namespace NG {
38 class ImageObject;
39 class ImageData;
40 } // namespace NG
41 
42 class ACE_EXPORT ImageCache : public AceType {
43     DECLARE_ACE_TYPE(ImageCache, AceType);
44 
45 public:
46     static RefPtr<ImageCache> Create();
47     ImageCache() = default;
48     ~ImageCache() override = default;
49 
50     void CacheImage(const std::string& key, const std::shared_ptr<CachedImage>& image);
51     std::shared_ptr<CachedImage> GetCacheImage(const std::string& key);
52 
53     void CacheImageData(const std::string& key, const RefPtr<NG::ImageData>& imageData);
54     RefPtr<NG::ImageData> GetCacheImageData(const std::string& key);
55 
56     RefPtr<NG::ImageObject> GetCacheImgObjNG(const std::string& key);
57     void CacheImgObjNG(const std::string& key, const RefPtr<NG::ImageObject>& imgObj);
58 
59     void CacheImgObj(const std::string& key, const RefPtr<ImageObject>& imgObj);
60     RefPtr<ImageObject> GetCacheImgObj(const std::string& key);
61 
SetCapacity(size_t capacity)62     void SetCapacity(size_t capacity)
63     {
64         TAG_LOGI(AceLogTag::ACE_IMAGE, "User Set Capacity : %{public}d", static_cast<int32_t>(capacity));
65         capacity_ = capacity;
66     }
67 
SetDataCacheLimit(size_t sizeLimit)68     void SetDataCacheLimit(size_t sizeLimit)
69     {
70         TAG_LOGI(AceLogTag::ACE_IMAGE, "User Set data size cache limit : %{public}d", static_cast<int32_t>(sizeLimit));
71         dataSizeLimit_ = sizeLimit;
72     }
73 
GetCapacity()74     size_t GetCapacity() const
75     {
76         return capacity_;
77     }
78 
GetCachedImageCount()79     size_t GetCachedImageCount() const
80     {
81         std::lock_guard<std::mutex> lock(imageCacheMutex_);
82         return cacheList_.size();
83     }
84 
85     void Clear();
86     static void Purge();
87 
88     void ClearCacheImage(const std::string& key);
89     void DumpCacheInfo();
90 
91 private:
92     bool ProcessImageDataCacheInner(size_t dataSize, std::vector<CacheNode<RefPtr<NG::ImageData>>>& needErase);
93 
94     std::atomic<size_t> capacity_ = 0; // by default memory cache can store 0 images.
95     mutable std::mutex imageCacheMutex_;
96     std::list<CacheNode<std::shared_ptr<CachedImage>>> cacheList_;
97     std::unordered_map<std::string, std::list<CacheNode<std::shared_ptr<CachedImage>>>::iterator> imageCache_;
98 
99     std::timed_mutex dataCacheMutex_;
100     std::list<CacheNode<RefPtr<NG::ImageData>>> dataCacheList_;
101     std::unordered_map<std::string, std::list<CacheNode<RefPtr<NG::ImageData>>>::iterator> imageDataCache_;
102 
103     std::atomic<size_t> dataSizeLimit_ = 0; // by default, image data before decoded cache is 0 MB.;
104     std::atomic<size_t> curDataSize_ = 0;
105 
106     std::mutex imgObjMutex_;
107     std::atomic<size_t> imgObjCapacity_ = 2000; // imgObj is cached after clear image data.
108 
109     std::list<CacheNode<RefPtr<NG::ImageObject>>> cacheImgObjListNG_;
110     std::unordered_map<std::string, std::list<CacheNode<RefPtr<NG::ImageObject>>>::iterator> imgObjCacheNG_;
111     std::list<CacheNode<RefPtr<ImageObject>>> cacheImgObjList_;
112     std::unordered_map<std::string, std::list<CacheNode<RefPtr<ImageObject>>>::iterator> imgObjCache_;
113 
114     ACE_DISALLOW_COPY_AND_MOVE(ImageCache);
115 };
116 
117 } // namespace OHOS::Ace
118 
119 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_IMAGE_IMAGE_CACHE_H
120