• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
25 #undef LOG_TAG
26 #define LOG_TAG "DISP_CACHE_MGR"
27 #undef LOG_DOMAIN
28 #define LOG_DOMAIN 0xD002500
29 
30 namespace OHOS {
31 namespace HDI {
32 namespace Display {
33 namespace Composer {
34 namespace V1_0 {
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         caches_.clear();
46     }
47 
SetCacheMaxCount(uint32_t count)48     bool SetCacheMaxCount(uint32_t count)
49     {
50         bool ret = true;
51         uint32_t originalMaxCount = cacheCountMax_;
52         if (count >= cacheCountMax_) {
53             cacheCountMax_ = count;
54         } else if (Size() <= count) {
55             cacheCountMax_ = count;
56         } else {
57             HDF_LOGE("%{public}s error: clientCacheCount can't be set, because cacheCountMax_ > count", __func__);
58             ret = false;
59         }
60         HDF_LOGI("%{public}s: set cache max count from %{public}u to %{public}u",
61             __func__, originalMaxCount, cacheCountMax_);
62         return ret;
63     }
64 
Size()65     uint32_t Size() const
66     {
67         return caches_.size();
68     }
69 
InsertCache(IdType id,CacheType * cache)70     bool InsertCache(IdType id, CacheType* cache)
71     {
72         if (SearchCache(id) != nullptr) {
73             HDF_LOGI("%{public}s: intend to insert a existing cache, SeqNo=%{public}d", __func__, id);
74         } else {
75             if (cacheCountMax_ != 0 && Size() >= cacheCountMax_) {
76                 HDF_LOGE("%{public}s: Caches is full, new seqNo:%{public}d can't be inserted", __func__, id);
77                 return false;
78             }
79         }
80         caches_[id] = std::move(*(new std::unique_ptr<CacheType>(cache)));
81 
82         return true;
83     }
84 
EraseCache(IdType id)85     bool EraseCache(IdType id)
86     {
87         bool ret = false;
88         if (SearchCache(id) != nullptr) {
89             caches_.erase(id);
90             ret = true;
91         } else {
92             HDF_LOGE("%{public}s: Cache %{public}d is not existing\n", __func__, id);
93         }
94 
95         return ret;
96     }
97 
SearchCache(IdType id)98     CacheType* SearchCache(IdType id) const
99     {
100         auto cacheItem = caches_.find(id);
101         if (cacheItem == caches_.end()) {
102             return nullptr;
103         }
104 
105         return cacheItem->second.get();
106     }
107 
TravelCaches(std::function<void (IdType id,const CacheType & cache)> func)108     void TravelCaches(std::function<void (IdType id, const CacheType& cache)> func) const
109     {
110         for (auto const& [key, value] : caches_) {
111             func(key, *value.get());
112         }
113     }
114 
115 private:
116     uint32_t cacheCountMax_;
117     std::unordered_map<IdType, std::unique_ptr<CacheType>> caches_;
118 };
119 } // namespace V1_0
120 } // namespace Composer
121 } // namespace Display
122 } // namespace HDI
123 } // namespace OHOS
124 #endif // OHOS_HDI_DISPLAY_V1_0_CACHE_MANAGER_H
125