• 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 
Create(uint32_t id,DeviceType type)28 DeviceCache* DeviceCache::Create(uint32_t id, DeviceType type)
29 {
30     DeviceCache* device = new DeviceCache(id, type);
31     DISPLAY_CHK_RETURN(device == nullptr, nullptr, HDF_LOGE("%{public}s: create device cache failed", __func__));
32 
33     int32_t ret = device->Init();
34     if (ret != HDF_SUCCESS) {
35         delete device;
36         device = nullptr;
37         HDF_LOGE("%{public}s: device cache init failed", __func__);
38     }
39 
40     return device;
41 }
42 
DeviceCache(uint32_t id,DeviceType type)43 DeviceCache::DeviceCache(uint32_t id, DeviceType type) : deviceId_(id), cacheType_(type)
44 {
45 }
46 
~DeviceCache()47 DeviceCache::~DeviceCache()
48 {
49 }
50 
Init()51 int32_t DeviceCache::Init()
52 {
53     layerCaches_.reset(new CacheManager<uint32_t, LayerCache>());
54     DISPLAY_CHK_RETURN(layerCaches_ == nullptr, HDF_FAILURE,
55         HDF_LOGE("%{public}s: create layer caches failed", __func__));
56 
57     clientBufferCaches_.reset(new CacheManager<uint32_t, NativeBuffer>());
58     DISPLAY_CHK_RETURN(clientBufferCaches_ == nullptr, HDF_FAILURE,
59         HDF_LOGE("%{public}s: create client buffer caches failed", __func__));
60 
61     clientBufferCaches_->SetInitFunc(LayerCache::NativeBufferInit);
62     clientBufferCaches_->SetCleanUpFunc(LayerCache::NativeBufferCleanUp);
63 
64     outputBufferCaches_.reset(new CacheManager<uint32_t, NativeBuffer>());
65     DISPLAY_CHK_RETURN(outputBufferCaches_ == nullptr, HDF_FAILURE,
66         HDF_LOGE("%{public}s: create output buffer caches failed", __func__));
67 
68     outputBufferCaches_->SetInitFunc(LayerCache::NativeBufferInit);
69     outputBufferCaches_->SetCleanUpFunc(LayerCache::NativeBufferCleanUp);
70     return HDF_SUCCESS;
71 }
72 
SetClientBufferCacheCount(uint32_t bufferCacheCount)73 int32_t DeviceCache::SetClientBufferCacheCount(uint32_t bufferCacheCount)
74 {
75     return clientBufferCaches_->SetCacheMaxCount(bufferCacheCount) ? HDF_SUCCESS : HDF_FAILURE;
76 }
77 
LayerCacheInstance(uint32_t layerId) const78 LayerCache* DeviceCache::LayerCacheInstance(uint32_t layerId) const
79 {
80     return layerCaches_->SearchCache(layerId);
81 }
82 
AddLayerCache(uint32_t id,uint32_t bufferCacheCount)83 int32_t DeviceCache::AddLayerCache(uint32_t id, uint32_t bufferCacheCount)
84 {
85     LayerCache* layer = LayerCache::Create(id);
86     DISPLAY_CHK_RETURN(layer == nullptr, HDF_FAILURE, HDF_LOGE("%{public}s: Create cache failed", __func__));
87 
88     int32_t retResult = layer->SetBufferCacheMaxCount(bufferCacheCount);
89     if (retResult != HDF_SUCCESS) {
90         delete layer;
91         layer = nullptr;
92         HDF_LOGE("%{public}s: set buffer cache max count failed", __func__);
93         return retResult;
94     }
95 
96     bool ret = layerCaches_->InsertCache(id, layer);
97     if (ret != true) {
98         delete layer;
99         layer = nullptr;
100         HDF_LOGE("%{public}s: insert cache failed", __func__);
101         return HDF_FAILURE;
102     }
103     return HDF_SUCCESS;
104 }
105 
RemoveLayerCache(uint32_t id)106 int32_t DeviceCache::RemoveLayerCache(uint32_t id)
107 {
108     bool ret = layerCaches_->EraseCache(id);
109     DISPLAY_CHK_RETURN(ret != true, HDF_FAILURE, HDF_LOGE("%{public}s: Destroy cache failed", __func__));
110 
111     return HDF_SUCCESS;
112 }
113 
ClearClientCache()114 int32_t DeviceCache::ClearClientCache()
115 {
116     HDF_LOGI("%{public}s", __func__);
117     clientBufferCaches_.reset(new CacheManager<uint32_t, NativeBuffer>());
118     DISPLAY_CHK_RETURN(clientBufferCaches_ == nullptr, HDF_FAILURE,
119                        HDF_LOGE("%{public}s: create client buffer caches failed", __func__));
120 
121     clientBufferCaches_->SetInitFunc(LayerCache::NativeBufferInit);
122     clientBufferCaches_->SetCleanUpFunc(LayerCache::NativeBufferCleanUp);
123     return HDF_SUCCESS;
124 }
125 
ClearLayerBuffer(uint32_t layerId)126 int32_t DeviceCache::ClearLayerBuffer(uint32_t layerId)
127 {
128     HDF_LOGI("%{public}s, layerId %{public}u", __func__, layerId);
129     if (layerCaches_ == nullptr) {
130         return HDF_FAILURE;
131     }
132     LayerCache* layerCache = layerCaches_->SearchCache(layerId);
133     if (layerCache == nullptr) {
134         HDF_LOGE("%{public}s, layerId %{public}u not found", __func__, layerId);
135         return HDF_FAILURE;
136     }
137 
138     return layerCache->ResetLayerBuffer();
139 }
140 
SetDisplayClientBuffer(BufferHandle * & buffer,uint32_t seqNo,bool & needFreeBuffer,std::function<int32_t (const BufferHandle &)> realFunc)141 int32_t DeviceCache::SetDisplayClientBuffer(BufferHandle*& buffer, uint32_t seqNo, bool &needFreeBuffer,
142     std::function<int32_t (const BufferHandle&)> realFunc)
143 {
144     if (buffer != nullptr) {
145         HDF_LOGI("%{public}s, seqNo %{public}u, fd %{public}d, size %{public}d", __func__, seqNo, buffer->fd,
146                  buffer->size);
147     }
148     BufferHandle* handle = BufferCacheUtils::NativeBufferCache(clientBufferCaches_, buffer, seqNo, deviceId_,
149                                                                needFreeBuffer);
150     DISPLAY_CHK_RETURN(handle == nullptr, HDF_FAILURE,
151         HDF_LOGE("%{public}s: call NativeBufferCache fail", __func__));
152     auto ret = realFunc(*handle);
153     if (ret != HDF_SUCCESS) {
154         HDF_LOGE("%{public}s: call realFunc fail", __func__);
155     }
156 
157     return ret;
158 }
159 
SetVirtualDisplayBuffer(BufferHandle * & buffer,uint32_t seqNo,bool & needFreeBuffer,std::function<int32_t (const BufferHandle &)> realFunc)160 int32_t DeviceCache::SetVirtualDisplayBuffer(BufferHandle*& buffer, uint32_t seqNo, bool &needFreeBuffer,
161     std::function<int32_t (const BufferHandle&)> realFunc)
162 {
163     int32_t ret = HDF_FAILURE;
164     if (CacheType() == DEVICE_TYPE_VIRTUAL) {
165         BufferHandle* handle = BufferCacheUtils::NativeBufferCache(outputBufferCaches_, buffer, seqNo, deviceId_,
166                                                                    needFreeBuffer);
167         DISPLAY_CHK_RETURN(handle == nullptr, HDF_FAILURE,
168             HDF_LOGE("%{public}s: call NativeBufferCache fail", __func__));
169         ret = realFunc(*handle);
170         if (ret != HDF_SUCCESS) {
171             HDF_LOGE("%{public}s: call realFunc fail", __func__);
172         }
173     } else {
174         HDF_LOGE("%{public}s: not a virtual display", __func__);
175     }
176 
177     return ret;
178 }
179 
CacheType() const180 DeviceCache::DeviceType DeviceCache::CacheType() const
181 {
182     return cacheType_;
183 }
184 
Dump() const185 void DeviceCache::Dump() const
186 {
187     clientBufferCaches_->TravelCaches([this](int32_t id, const NativeBuffer& buffer)->void {
188         auto info = buffer.Dump();
189         HDF_LOGE("devId-%{public}d, clientBuffer[%{public}d]: %{public}s", deviceId_, id, info.c_str());
190     });
191     outputBufferCaches_->TravelCaches([this](int32_t id, const NativeBuffer& buffer)->void {
192         auto info = buffer.Dump();
193         HDF_LOGE("devId-%{public}d, outputBuffer[%{public}d]: %{public}s", deviceId_, id, info.c_str());
194     });
195     layerCaches_->TravelCaches([](int32_t id, const LayerCache& cache)->void {
196         cache.Dump();
197     });
198 }
199 } // namespace Composer
200 } // namespace Display
201 } // namespace HDI
202 } // namespace OHOS
203