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 #include "device_cache_manager.h"
17
18 #include "common/include/display_interface_utils.h"
19 #include "hdf_base.h"
20 #include "hdf_log.h"
21
22 namespace OHOS {
23 namespace HDI {
24 namespace Display {
25 namespace Composer {
26
GetInstance()27 std::shared_ptr<DeviceCacheManager> DeviceCacheManager::GetInstance()
28 {
29 static std::shared_ptr<DeviceCacheManager> mgr = nullptr;
30 if (mgr == nullptr) {
31 mgr = std::make_shared<DeviceCacheManager>();
32 if (mgr == nullptr) {
33 HDF_LOGE("%{public}s: DeviceCacheManager construct failed", __func__);
34 return mgr;
35 }
36
37 int32_t ret = mgr->Init();
38 if (ret != HDF_SUCCESS) {
39 mgr = nullptr;
40 HDF_LOGE("%{public}s: DeviceCacheManager init failed", __func__);
41 }
42 }
43
44 return mgr;
45 }
46
DeviceCacheManager()47 DeviceCacheManager::DeviceCacheManager()
48 {
49 }
50
~DeviceCacheManager()51 DeviceCacheManager::~DeviceCacheManager()
52 {
53 DestroyCaches();
54 }
55
Init()56 int32_t DeviceCacheManager::Init()
57 {
58 deviceCaches_.reset(new CacheManager<uint32_t, DeviceCache>());
59 DISPLAY_CHK_RETURN(deviceCaches_ == nullptr,
60 HDF_FAILURE, HDF_LOGE("%{public}s: init deviceCaches failed", __func__));
61
62 return HDF_SUCCESS;
63 }
64
AddDeviceCache(uint32_t deviceId)65 int32_t DeviceCacheManager::AddDeviceCache(uint32_t deviceId)
66 {
67 return AddCacheInternal(deviceId, DeviceCache::DEVICE_TYPE_DEVICE);
68 }
69
RemoveDeviceCache(uint32_t deviceId)70 int32_t DeviceCacheManager::RemoveDeviceCache(uint32_t deviceId)
71 {
72 bool ret = deviceCaches_->EraseCache(deviceId);
73 DISPLAY_CHK_RETURN(ret != true, HDF_FAILURE, HDF_LOGE("%{public}s: Destroy cache failed", __func__));
74
75 return HDF_SUCCESS;
76 }
77
CreateVirtualDisplayCache(uint32_t deviceId)78 int32_t DeviceCacheManager::CreateVirtualDisplayCache(uint32_t deviceId)
79 {
80 return AddCacheInternal(deviceId, DeviceCache::DEVICE_TYPE_VIRTUAL);
81 }
82
DestroyVirtualDisplayCache(uint32_t deviceId)83 int32_t DeviceCacheManager::DestroyVirtualDisplayCache(uint32_t deviceId)
84 {
85 auto cache = deviceCaches_->SearchCache(deviceId);
86 DISPLAY_CHK_RETURN((cache == nullptr) || (cache->CacheType() != DeviceCache::DEVICE_TYPE_VIRTUAL),
87 HDF_FAILURE, HDF_LOGE("%{public}s: device is not virtual display cache", __func__));
88
89 bool ret = deviceCaches_->EraseCache(deviceId);
90 DISPLAY_CHK_RETURN(ret != true, HDF_FAILURE,
91 HDF_LOGE("%{public}s: Destroy virtual display cache failed", __func__));
92
93 return HDF_SUCCESS;
94 }
95
DestroyCaches()96 int32_t DeviceCacheManager::DestroyCaches()
97 {
98 deviceCaches_.reset();
99 return HDF_SUCCESS;
100 }
101
DeviceCacheInstance(uint32_t deviceId) const102 DeviceCache* DeviceCacheManager::DeviceCacheInstance(uint32_t deviceId) const
103 {
104 auto devCache = deviceCaches_->SearchCache(deviceId);
105 DISPLAY_CHK_RETURN(devCache == nullptr, nullptr, HDF_LOGE("%{public}s: Can't find device Cache", __func__));
106
107 return devCache;
108 }
109
LayerCacheInstance(uint32_t deviceId,uint32_t layerId) const110 LayerCache* DeviceCacheManager::LayerCacheInstance(uint32_t deviceId, uint32_t layerId) const
111 {
112 auto devCache = deviceCaches_->SearchCache(deviceId);
113 DISPLAY_CHK_RETURN(devCache == nullptr, nullptr, HDF_LOGE("%{public}s: Can't find device Cache", __func__));
114
115 auto layerCache = devCache->LayerCacheInstance(layerId);
116 DISPLAY_CHK_RETURN(layerCache == nullptr, nullptr, HDF_LOGE("%{public}s: Can't find layer Cache", __func__));
117
118 return layerCache;
119 }
120
Dump() const121 void DeviceCacheManager::Dump() const
122 {
123 HDF_LOGE("********************************");
124 HDF_LOGE(" Devicecache dump start");
125 HDF_LOGE("--------------------------------");
126
127 deviceCaches_->TravelCaches([](int32_t id, const DeviceCache& cache)->void {
128 cache.Dump();
129 });
130
131 HDF_LOGE("--------------------------------");
132 HDF_LOGE(" Devicecache dump end");
133 HDF_LOGE("********************************");
134 }
135
AddCacheInternal(uint32_t deviceId,DeviceCache::DeviceType type)136 int32_t DeviceCacheManager::AddCacheInternal(uint32_t deviceId, DeviceCache::DeviceType type)
137 {
138 DeviceCache* device = DeviceCache::Create(deviceId, type);
139 DISPLAY_CHK_RETURN(device == nullptr, HDF_FAILURE, HDF_LOGE("%{public}s: Create cache failed", __func__));
140
141 bool ret = deviceCaches_->InsertCache(deviceId, device);
142 DISPLAY_CHK_RETURN(ret == false, HDF_FAILURE, HDF_LOGE("%{public}s: insert device cache failed", __func__));
143
144 return HDF_SUCCESS;
145 }
146
GetCacheMgrMutex()147 std::mutex& DeviceCacheManager::GetCacheMgrMutex()
148 {
149 static std::mutex deviceCacheMgr;
150 return deviceCacheMgr;
151 }
152 } // namespace Composer
153 } // namespace Display
154 } // namespace HDI
155 } // namespace OHOS
156