• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/log/log.h"
28 #include "base/memory/ace_type.h"
29 #include "base/utils/macros.h"
30 #include "base/utils/noncopyable.h"
31 #include "core/common/lru/count_limit_lru.h"
32 #include "core/components_ng/render/drawing_forward.h"
33 
34 namespace OHOS::Ace {
35 
36 struct CachedImage {
CachedImageCachedImage37     explicit CachedImage(const std::shared_ptr<RSImage>& image) : imagePtr(image) {}
38     std::shared_ptr<RSImage> imagePtr;
39     uint32_t uniqueId = 0;
40 };
41 class ImageObject;
42 
43 namespace NG {
44 class ImageObject;
45 class ImageData;
46 } // namespace NG
47 
48 class ACE_FORCE_EXPORT ImageCache : public AceType {
49     DECLARE_ACE_TYPE(ImageCache, AceType);
50 
51 public:
52     static RefPtr<ImageCache> Create();
53     ImageCache();
54     ~ImageCache() override;
55 
56     void CacheImage(const std::string& key, const std::shared_ptr<CachedImage>& image);
57     std::shared_ptr<CachedImage> GetCacheImage(const std::string& key);
58 
59     void CacheImageData(const std::string& key, const RefPtr<NG::ImageData>& imageData);
60     RefPtr<NG::ImageData> GetCacheImageData(const std::string& key);
61 
62     RefPtr<NG::ImageObject> GetCacheImgObjNG(const std::string& key);
63     void CacheImgObjNG(const std::string& key, const RefPtr<NG::ImageObject>& imgObj);
64 
65     void CacheImgObj(const std::string& key, const RefPtr<ImageObject>& imgObj);
66     RefPtr<ImageObject> GetCacheImgObj(const std::string& key);
67     /**
68     @brief Clears the cached image object associated with the specified key.
69     This interface is for internal use only. Exercise caution when calling it.
70     @param key The unique identifier for the cached image object.
71     */
72     void ClearCacheImgObj(const std::string& key);
73 
SetCapacity(size_t capacity)74     void SetCapacity(size_t capacity)
75     {
76         TAG_LOGI(AceLogTag::ACE_IMAGE, "User Set Capacity : %{public}d", static_cast<int32_t>(capacity));
77         capacity_ = capacity;
78     }
79 
SetDataCacheLimit(size_t sizeLimit)80     void SetDataCacheLimit(size_t sizeLimit)
81     {
82         TAG_LOGI(AceLogTag::ACE_IMAGE, "User Set data size cache limit : %{public}d", static_cast<int32_t>(sizeLimit));
83         dataSizeLimit_ = sizeLimit;
84     }
85 
GetCapacity()86     size_t GetCapacity() const
87     {
88         return capacity_;
89     }
90 
GetCachedImageCount()91     size_t GetCachedImageCount() const
92     {
93         std::lock_guard<std::mutex> lock(imageCacheMutex_);
94         return cacheList_.size();
95     }
96 
97     void Clear();
98     static void Purge();
99 
100     void ClearCacheImage(const std::string& key);
101     void DumpCacheInfo();
102 
103 private:
104     bool ProcessImageDataCacheInner(size_t dataSize, std::vector<CacheNode<RefPtr<NG::ImageData>>>& needErase);
105 
106     std::atomic<size_t> capacity_ = 0; // by default memory cache can store 0 images.
107     mutable std::mutex imageCacheMutex_;
108     std::list<CacheNode<std::shared_ptr<CachedImage>>> cacheList_;
109     std::unordered_map<std::string, std::list<CacheNode<std::shared_ptr<CachedImage>>>::iterator> imageCache_;
110 
111     std::timed_mutex dataCacheMutex_;
112     std::list<CacheNode<RefPtr<NG::ImageData>>> dataCacheList_;
113     std::unordered_map<std::string, std::list<CacheNode<RefPtr<NG::ImageData>>>::iterator> imageDataCache_;
114 
115     std::atomic<size_t> dataSizeLimit_ = 0; // by default, image data before decoded cache is 0 MB.;
116     std::atomic<size_t> curDataSize_ = 0;
117 
118     std::mutex imgObjMutex_;
119     std::atomic<size_t> imgObjCapacity_ = 2000; // imgObj is cached after clear image data.
120 
121     std::list<CacheNode<RefPtr<NG::ImageObject>>> cacheImgObjListNG_;
122     std::unordered_map<std::string, std::list<CacheNode<RefPtr<NG::ImageObject>>>::iterator> imgObjCacheNG_;
123     std::list<CacheNode<RefPtr<ImageObject>>> cacheImgObjList_;
124     std::unordered_map<std::string, std::list<CacheNode<RefPtr<ImageObject>>>::iterator> imgObjCache_;
125 
126     ACE_DISALLOW_COPY_AND_MOVE(ImageCache);
127 };
128 
129 } // namespace OHOS::Ace
130 
131 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_IMAGE_IMAGE_CACHE_H
132