1 /* 2 * Copyright (c) 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 OHOS_HDI_DISPLAY_V1_0_CACHE_MANAGER_H 17 #define OHOS_HDI_DISPLAY_V1_0_CACHE_MANAGER_H 18 19 #include <functional> 20 #include <memory> 21 #include <unordered_map> 22 #include "hdf_log.h" 23 #include "nocopyable.h" 24 #include <mutex> 25 26 #undef LOG_TAG 27 #define LOG_TAG "DISP_CACHE_MGR" 28 #undef LOG_DOMAIN 29 #define LOG_DOMAIN 0xD002515 30 31 namespace OHOS { 32 namespace HDI { 33 namespace Display { 34 namespace Composer { 35 36 template <typename IdType, typename CacheType> 37 class CacheManager : public NoCopyable { 38 public: CacheManager()39 CacheManager() : cacheCountMax_(0) 40 { 41 } 42 ~CacheManager()43 virtual ~CacheManager() 44 { 45 std::lock_guard<std::mutex> lock(mutex_); 46 caches_.clear(); 47 } 48 SetCacheMaxCount(uint32_t count)49 bool SetCacheMaxCount(uint32_t count) 50 { 51 bool ret = true; 52 uint32_t originalMaxCount = cacheCountMax_; 53 if (count >= cacheCountMax_) { 54 cacheCountMax_ = count; 55 } else if (Size() <= count) { 56 cacheCountMax_ = count; 57 } else { 58 HDF_LOGE("%{public}s error: clientCacheCount can't be set, because cacheCountMax_ > count", __func__); 59 ret = false; 60 } 61 HDF_LOGI("%{public}s: set cache max count from %{public}u to %{public}u", 62 __func__, originalMaxCount, cacheCountMax_); 63 return ret; 64 } 65 Size()66 uint32_t Size() const 67 { 68 return caches_.size(); 69 } 70 InsertCache(IdType id,CacheType * cache)71 bool InsertCache(IdType id, CacheType* cache) 72 { 73 if (SearchCache(id) != nullptr) { 74 HDF_LOGI("%{public}s: intend to insert a existing cache, SeqNo=%{public}d", __func__, id); 75 } else { 76 if (cacheCountMax_ != 0 && Size() >= cacheCountMax_) { 77 HDF_LOGE("%{public}s: Caches is full, new seqNo:%{public}d can't be inserted", __func__, id); 78 return false; 79 } 80 } 81 std::lock_guard<std::mutex> lock(mutex_); 82 caches_[id] = std::move(*(new std::unique_ptr<CacheType>(cache))); 83 84 return true; 85 } 86 EraseCache(IdType id)87 bool EraseCache(IdType id) 88 { 89 bool ret = false; 90 if (SearchCache(id) != nullptr) { 91 std::lock_guard<std::mutex> lock(mutex_); 92 caches_.erase(id); 93 ret = true; 94 } else { 95 HDF_LOGE("%{public}s: Cache %{public}d is not existing\n", __func__, id); 96 } 97 98 return ret; 99 } 100 SearchCache(IdType id)101 CacheType* SearchCache(IdType id) const 102 { 103 auto cacheItem = caches_.find(id); 104 if (cacheItem == caches_.end()) { 105 return nullptr; 106 } 107 108 return cacheItem->second.get(); 109 } 110 TravelCaches(std::function<void (IdType id,const CacheType & cache)> func)111 void TravelCaches(std::function<void (IdType id, const CacheType& cache)> func) const 112 { 113 for (auto const& [key, value] : caches_) { 114 func(key, *value.get()); 115 } 116 } 117 118 private: 119 uint32_t cacheCountMax_; 120 std::unordered_map<IdType, std::unique_ptr<CacheType>> caches_; 121 std::mutex mutex_; 122 }; 123 } // namespace Composer 124 } // namespace Display 125 } // namespace HDI 126 } // namespace OHOS 127 #endif // OHOS_HDI_DISPLAY_V1_0_CACHE_MANAGER_H 128