1c5e268c6Sopenharmony_ci/*
2c5e268c6Sopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd.
3c5e268c6Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4c5e268c6Sopenharmony_ci * you may not use this file except in compliance with the License.
5c5e268c6Sopenharmony_ci * You may obtain a copy of the License at
6c5e268c6Sopenharmony_ci *
7c5e268c6Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8c5e268c6Sopenharmony_ci *
9c5e268c6Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10c5e268c6Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11c5e268c6Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12c5e268c6Sopenharmony_ci * See the License for the specific language governing permissions and
13c5e268c6Sopenharmony_ci * limitations under the License.
14c5e268c6Sopenharmony_ci */
15c5e268c6Sopenharmony_ci
16c5e268c6Sopenharmony_ci#include "device_cache_manager.h"
17c5e268c6Sopenharmony_ci
18c5e268c6Sopenharmony_ci#include "common/include/display_interface_utils.h"
19c5e268c6Sopenharmony_ci#include "hdf_base.h"
20c5e268c6Sopenharmony_ci#include "hdf_log.h"
21c5e268c6Sopenharmony_ci
22c5e268c6Sopenharmony_cinamespace OHOS {
23c5e268c6Sopenharmony_cinamespace HDI {
24c5e268c6Sopenharmony_cinamespace Display {
25c5e268c6Sopenharmony_cinamespace Composer {
26c5e268c6Sopenharmony_ci
27c5e268c6Sopenharmony_cistd::shared_ptr<DeviceCacheManager> DeviceCacheManager::GetInstance()
28c5e268c6Sopenharmony_ci{
29c5e268c6Sopenharmony_ci    static std::shared_ptr<DeviceCacheManager> mgr = nullptr;
30c5e268c6Sopenharmony_ci    if (mgr == nullptr) {
31c5e268c6Sopenharmony_ci        mgr = std::make_shared<DeviceCacheManager>();
32c5e268c6Sopenharmony_ci        if (mgr == nullptr) {
33c5e268c6Sopenharmony_ci            HDF_LOGE("%{public}s: DeviceCacheManager construct failed", __func__);
34c5e268c6Sopenharmony_ci            return mgr;
35c5e268c6Sopenharmony_ci        }
36c5e268c6Sopenharmony_ci
37c5e268c6Sopenharmony_ci        int32_t ret = mgr->Init();
38c5e268c6Sopenharmony_ci        if (ret != HDF_SUCCESS) {
39c5e268c6Sopenharmony_ci            mgr = nullptr;
40c5e268c6Sopenharmony_ci            HDF_LOGE("%{public}s: DeviceCacheManager init failed", __func__);
41c5e268c6Sopenharmony_ci        }
42c5e268c6Sopenharmony_ci    }
43c5e268c6Sopenharmony_ci
44c5e268c6Sopenharmony_ci    return mgr;
45c5e268c6Sopenharmony_ci}
46c5e268c6Sopenharmony_ci
47c5e268c6Sopenharmony_ciDeviceCacheManager::DeviceCacheManager()
48c5e268c6Sopenharmony_ci{
49c5e268c6Sopenharmony_ci}
50c5e268c6Sopenharmony_ci
51c5e268c6Sopenharmony_ciDeviceCacheManager::~DeviceCacheManager()
52c5e268c6Sopenharmony_ci{
53c5e268c6Sopenharmony_ci    DestroyCaches();
54c5e268c6Sopenharmony_ci}
55c5e268c6Sopenharmony_ci
56c5e268c6Sopenharmony_ciint32_t DeviceCacheManager::Init()
57c5e268c6Sopenharmony_ci{
58c5e268c6Sopenharmony_ci    deviceCaches_.reset(new CacheManager<uint32_t, DeviceCache>());
59c5e268c6Sopenharmony_ci    DISPLAY_CHK_RETURN(deviceCaches_ == nullptr,
60c5e268c6Sopenharmony_ci        HDF_FAILURE, HDF_LOGE("%{public}s: init deviceCaches failed", __func__));
61c5e268c6Sopenharmony_ci
62c5e268c6Sopenharmony_ci    return HDF_SUCCESS;
63c5e268c6Sopenharmony_ci}
64c5e268c6Sopenharmony_ci
65c5e268c6Sopenharmony_ciint32_t DeviceCacheManager::AddDeviceCache(uint32_t deviceId)
66c5e268c6Sopenharmony_ci{
67c5e268c6Sopenharmony_ci    return AddCacheInternal(deviceId, DeviceCache::DEVICE_TYPE_DEVICE);
68c5e268c6Sopenharmony_ci}
69c5e268c6Sopenharmony_ci
70c5e268c6Sopenharmony_ciint32_t DeviceCacheManager::RemoveDeviceCache(uint32_t deviceId)
71c5e268c6Sopenharmony_ci{
72c5e268c6Sopenharmony_ci    bool ret = deviceCaches_->EraseCache(deviceId);
73c5e268c6Sopenharmony_ci    DISPLAY_CHK_RETURN(ret != true, HDF_FAILURE, HDF_LOGE("%{public}s: Destroy cache failed", __func__));
74c5e268c6Sopenharmony_ci
75c5e268c6Sopenharmony_ci    return HDF_SUCCESS;
76c5e268c6Sopenharmony_ci}
77c5e268c6Sopenharmony_ci
78c5e268c6Sopenharmony_ciint32_t DeviceCacheManager::CreateVirtualDisplayCache(uint32_t deviceId)
79c5e268c6Sopenharmony_ci{
80c5e268c6Sopenharmony_ci    return AddCacheInternal(deviceId, DeviceCache::DEVICE_TYPE_VIRTUAL);
81c5e268c6Sopenharmony_ci}
82c5e268c6Sopenharmony_ci
83c5e268c6Sopenharmony_ciint32_t DeviceCacheManager::DestroyVirtualDisplayCache(uint32_t deviceId)
84c5e268c6Sopenharmony_ci{
85c5e268c6Sopenharmony_ci    auto cache = deviceCaches_->SearchCache(deviceId);
86c5e268c6Sopenharmony_ci    DISPLAY_CHK_RETURN((cache == nullptr) || (cache->CacheType() != DeviceCache::DEVICE_TYPE_VIRTUAL),
87c5e268c6Sopenharmony_ci        HDF_FAILURE, HDF_LOGE("%{public}s: device is not virtual display cache", __func__));
88c5e268c6Sopenharmony_ci
89c5e268c6Sopenharmony_ci    bool ret = deviceCaches_->EraseCache(deviceId);
90c5e268c6Sopenharmony_ci    DISPLAY_CHK_RETURN(ret != true, HDF_FAILURE,
91c5e268c6Sopenharmony_ci        HDF_LOGE("%{public}s: Destroy virtual display cache failed", __func__));
92c5e268c6Sopenharmony_ci
93c5e268c6Sopenharmony_ci    return HDF_SUCCESS;
94c5e268c6Sopenharmony_ci}
95c5e268c6Sopenharmony_ci
96c5e268c6Sopenharmony_ciint32_t DeviceCacheManager::DestroyCaches()
97c5e268c6Sopenharmony_ci{
98c5e268c6Sopenharmony_ci    deviceCaches_.reset();
99c5e268c6Sopenharmony_ci    return HDF_SUCCESS;
100c5e268c6Sopenharmony_ci}
101c5e268c6Sopenharmony_ci
102c5e268c6Sopenharmony_ciDeviceCache* DeviceCacheManager::DeviceCacheInstance(uint32_t deviceId) const
103c5e268c6Sopenharmony_ci{
104c5e268c6Sopenharmony_ci    auto devCache = deviceCaches_->SearchCache(deviceId);
105c5e268c6Sopenharmony_ci    DISPLAY_CHK_RETURN(devCache == nullptr, nullptr, HDF_LOGE("%{public}s: Can't find device Cache", __func__));
106c5e268c6Sopenharmony_ci
107c5e268c6Sopenharmony_ci    return devCache;
108c5e268c6Sopenharmony_ci}
109c5e268c6Sopenharmony_ci
110c5e268c6Sopenharmony_ciLayerCache* DeviceCacheManager::LayerCacheInstance(uint32_t deviceId, uint32_t layerId) const
111c5e268c6Sopenharmony_ci{
112c5e268c6Sopenharmony_ci    auto devCache = deviceCaches_->SearchCache(deviceId);
113c5e268c6Sopenharmony_ci    DISPLAY_CHK_RETURN(devCache == nullptr, nullptr, HDF_LOGE("%{public}s: Can't find device Cache", __func__));
114c5e268c6Sopenharmony_ci
115c5e268c6Sopenharmony_ci    auto layerCache = devCache->LayerCacheInstance(layerId);
116c5e268c6Sopenharmony_ci    DISPLAY_CHK_RETURN(layerCache == nullptr, nullptr, HDF_LOGE("%{public}s: Can't find layer Cache", __func__));
117c5e268c6Sopenharmony_ci
118c5e268c6Sopenharmony_ci    return layerCache;
119c5e268c6Sopenharmony_ci}
120c5e268c6Sopenharmony_ci
121c5e268c6Sopenharmony_civoid DeviceCacheManager::Dump() const
122c5e268c6Sopenharmony_ci{
123c5e268c6Sopenharmony_ci    HDF_LOGE("********************************");
124c5e268c6Sopenharmony_ci    HDF_LOGE(" Devicecache dump start");
125c5e268c6Sopenharmony_ci    HDF_LOGE("--------------------------------");
126c5e268c6Sopenharmony_ci
127c5e268c6Sopenharmony_ci    deviceCaches_->TravelCaches([](int32_t id, const DeviceCache& cache)->void {
128c5e268c6Sopenharmony_ci        cache.Dump();
129c5e268c6Sopenharmony_ci    });
130c5e268c6Sopenharmony_ci
131c5e268c6Sopenharmony_ci    HDF_LOGE("--------------------------------");
132c5e268c6Sopenharmony_ci    HDF_LOGE("  Devicecache dump end");
133c5e268c6Sopenharmony_ci    HDF_LOGE("********************************");
134c5e268c6Sopenharmony_ci}
135c5e268c6Sopenharmony_ci
136c5e268c6Sopenharmony_ciint32_t DeviceCacheManager::AddCacheInternal(uint32_t deviceId, DeviceCache::DeviceType type)
137c5e268c6Sopenharmony_ci{
138c5e268c6Sopenharmony_ci    DeviceCache* device = DeviceCache::Create(deviceId, type);
139c5e268c6Sopenharmony_ci    DISPLAY_CHK_RETURN(device == nullptr, HDF_FAILURE, HDF_LOGE("%{public}s: Create cache failed", __func__));
140c5e268c6Sopenharmony_ci
141c5e268c6Sopenharmony_ci    bool ret = deviceCaches_->InsertCache(deviceId, device);
142c5e268c6Sopenharmony_ci    DISPLAY_CHK_RETURN(ret == false, HDF_FAILURE, HDF_LOGE("%{public}s: insert device cache failed", __func__));
143c5e268c6Sopenharmony_ci
144c5e268c6Sopenharmony_ci    return HDF_SUCCESS;
145c5e268c6Sopenharmony_ci}
146c5e268c6Sopenharmony_ci
147c5e268c6Sopenharmony_cistd::mutex& DeviceCacheManager::GetCacheMgrMutex()
148c5e268c6Sopenharmony_ci{
149c5e268c6Sopenharmony_ci    static std::mutex deviceCacheMgr;
150c5e268c6Sopenharmony_ci    return deviceCacheMgr;
151c5e268c6Sopenharmony_ci}
152c5e268c6Sopenharmony_ci} // namespace Composer
153c5e268c6Sopenharmony_ci} // namespace Display
154c5e268c6Sopenharmony_ci} // namespace HDI
155c5e268c6Sopenharmony_ci} // namespace OHOS
156