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