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