• 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 #include "device_cache.h"
17 
18 #include "buffer_cache_utils.h"
19 #include "common/include/display_interface_utils.h"
20 #include "hdf_base.h"
21 #include "hdf_log.h"
22 
23 namespace OHOS {
24 namespace HDI {
25 namespace Display {
26 namespace Composer {
27 namespace V1_0 {
28 
Create(uint32_t id,DeviceType type)29 DeviceCache* DeviceCache::Create(uint32_t id, DeviceType type)
30 {
31     DeviceCache* device = new DeviceCache(id, type);
32     DISPLAY_CHK_RETURN(device == nullptr, nullptr, HDF_LOGE("%{public}s: create device cache failed", __func__));
33 
34     int32_t ret = device->Init();
35     if (ret != HDF_SUCCESS) {
36         delete device;
37         device = nullptr;
38         HDF_LOGE("%{public}s: device cache init failed", __func__);
39     }
40 
41     return device;
42 }
43 
DeviceCache(uint32_t id,DeviceType type)44 DeviceCache::DeviceCache(uint32_t id, DeviceType type) : deviceId_(id), cacheType_(type)
45 {
46 }
47 
~DeviceCache()48 DeviceCache::~DeviceCache()
49 {
50 }
51 
Init()52 int32_t DeviceCache::Init()
53 {
54     layerCaches_.reset(new CacheManager<uint32_t, LayerCache>());
55     DISPLAY_CHK_RETURN(layerCaches_ == nullptr, HDF_FAILURE,
56         HDF_LOGE("%{public}s: create layer caches failed", __func__));
57 
58     clientBufferCaches_.reset(new CacheManager<uint32_t, NativeBuffer>());
59     DISPLAY_CHK_RETURN(clientBufferCaches_ == nullptr, HDF_FAILURE,
60         HDF_LOGE("%{public}s: create client buffer caches failed", __func__));
61 
62     outputBufferCaches_.reset(new CacheManager<uint32_t, NativeBuffer>());
63     DISPLAY_CHK_RETURN(outputBufferCaches_ == nullptr, HDF_FAILURE,
64         HDF_LOGE("%{public}s: create output buffer caches failed", __func__));
65 
66     return HDF_SUCCESS;
67 }
68 
SetClientBufferCacheCount(uint32_t bufferCacheCount)69 int32_t DeviceCache::SetClientBufferCacheCount(uint32_t bufferCacheCount)
70 {
71     return clientBufferCaches_->SetCacheMaxCount(bufferCacheCount) ? HDF_SUCCESS : HDF_FAILURE;
72 }
73 
LayerCacheInstance(uint32_t layerId) const74 LayerCache* DeviceCache::LayerCacheInstance(uint32_t layerId) const
75 {
76     return layerCaches_->SearchCache(layerId);
77 }
78 
AddLayerCache(uint32_t id,uint32_t bufferCacheCount)79 int32_t DeviceCache::AddLayerCache(uint32_t id, uint32_t bufferCacheCount)
80 {
81     LayerCache* layer = LayerCache::Create(id);
82     DISPLAY_CHK_RETURN(layer == nullptr, HDF_FAILURE, HDF_LOGE("%{public}s: Create cache failed", __func__));
83 
84     int32_t retResult = layer->SetBufferCacheMaxCount(bufferCacheCount);
85     DISPLAY_CHK_RETURN(retResult != HDF_SUCCESS, retResult,
86         HDF_LOGE("%{public}s: set buffer cache max count failed", __func__));
87 
88     bool ret = layerCaches_->InsertCache(id, layer);
89     DISPLAY_CHK_RETURN(ret != true, HDF_FAILURE, HDF_LOGE("%{public}s: insert cache failed", __func__));
90     return HDF_SUCCESS;
91 }
92 
RemoveLayerCache(uint32_t id)93 int32_t DeviceCache::RemoveLayerCache(uint32_t id)
94 {
95     bool ret = layerCaches_->EraseCache(id);
96     DISPLAY_CHK_RETURN(ret != true, HDF_FAILURE, HDF_LOGE("%{public}s: Destroy cache failed", __func__));
97 
98     return HDF_SUCCESS;
99 }
100 
SetDisplayClientBuffer(const BufferHandle * buffer,uint32_t seqNo,std::function<int32_t (const BufferHandle &)> realFunc)101 int32_t DeviceCache::SetDisplayClientBuffer(const BufferHandle* buffer, uint32_t seqNo,
102     std::function<int32_t (const BufferHandle&)> realFunc)
103 {
104     BufferHandle* handle = BufferCacheUtils::NativeBufferCache(clientBufferCaches_,
105         const_cast<BufferHandle*>(buffer), seqNo, deviceId_);
106     DISPLAY_CHK_RETURN(handle == nullptr, HDF_FAILURE,
107         HDF_LOGE("%{public}s: call NativeBufferCache fail", __func__));
108     auto ret = realFunc(*handle);
109     DISPLAY_CHK_RETURN(ret != HDF_SUCCESS, ret, HDF_LOGE("%{public}s: call realFunc fail", __func__));
110 
111     return ret;
112 }
113 
SetVirtualDisplayBuffer(const BufferHandle * buffer,uint32_t seqNo,std::function<int32_t (const BufferHandle &)> realFunc)114 int32_t DeviceCache::SetVirtualDisplayBuffer(const BufferHandle* buffer, uint32_t seqNo,
115     std::function<int32_t (const BufferHandle&)> realFunc)
116 {
117     int32_t ret = HDF_FAILURE;
118     if (CacheType() == DEVICE_TYPE_VIRTUAL) {
119         BufferHandle* handle = BufferCacheUtils::NativeBufferCache(outputBufferCaches_,
120             const_cast<BufferHandle*>(buffer), seqNo, deviceId_);
121         DISPLAY_CHK_RETURN(handle == nullptr, HDF_FAILURE,
122             HDF_LOGE("%{public}s: call NativeBufferCache fail", __func__));
123         ret = realFunc(*handle);
124         DISPLAY_CHK_RETURN(ret != HDF_SUCCESS, ret, HDF_LOGE("%{public}s: call realFunc fail", __func__));
125     } else {
126         HDF_LOGE("%{public}s: not a virtual display", __func__);
127     }
128 
129     return ret;
130 }
131 
CacheType() const132 DeviceCache::DeviceType DeviceCache::CacheType() const
133 {
134     return cacheType_;
135 }
136 
Dump() const137 void DeviceCache::Dump() const
138 {
139     clientBufferCaches_->TravelCaches([this](int32_t id, const NativeBuffer& buffer)->void {
140         auto info = buffer.Dump();
141         HDF_LOGE("devId-%{public}d, clientBuffer[%{public}d]: %{public}s", deviceId_, id, info.c_str());
142     });
143     outputBufferCaches_->TravelCaches([this](int32_t id, const NativeBuffer& buffer)->void {
144         auto info = buffer.Dump();
145         HDF_LOGE("devId-%{public}d, outputBuffer[%{public}d]: %{public}s", deviceId_, id, info.c_str());
146     });
147     layerCaches_->TravelCaches([](int32_t id, const LayerCache& cache)->void {
148         cache.Dump();
149     });
150 }
151 } // namespace V1_0
152 } // namespace Composer
153 } // namespace Display
154 } // namespace HDI
155 } // namespace OHOS
156