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 "layer_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)29 LayerCache* LayerCache::Create(uint32_t id)
30 {
31 LayerCache* layer = new LayerCache(id);
32 DISPLAY_CHK_RETURN(layer == nullptr, nullptr, HDF_LOGE("%{public}s: create layer cache failed", __func__));
33
34 int32_t ret = layer->Init();
35 if (ret != HDF_SUCCESS) {
36 delete layer;
37 layer = nullptr;
38 HDF_LOGE("%{public}s: layer cache init failed", __func__);
39 }
40
41 return layer;
42 }
43
LayerCache(uint32_t id)44 LayerCache::LayerCache(uint32_t id) : layerId_(id)
45 {
46 }
47
~LayerCache()48 LayerCache::~LayerCache()
49 {
50 }
51
Init()52 int32_t LayerCache::Init()
53 {
54 bufferCaches_.reset(new CacheManager<uint32_t, NativeBuffer>());
55 DISPLAY_CHK_RETURN(bufferCaches_ == nullptr, HDF_FAILURE,
56 HDF_LOGE("%{public}s: create buffer caches failed", __func__));
57
58 return HDF_SUCCESS;
59 }
60
SetBufferCacheMaxCount(uint32_t cacheCount)61 int32_t LayerCache::SetBufferCacheMaxCount(uint32_t cacheCount)
62 {
63 bool ret = bufferCaches_->SetCacheMaxCount(cacheCount);
64 DISPLAY_CHK_RETURN(ret == false, HDF_FAILURE, HDF_LOGE("%{public}s: failed", __func__));
65 return HDF_SUCCESS;
66 }
67
SetLayerBuffer(const BufferHandle * buffer,uint32_t seqNo,const std::vector<uint32_t> & deletingList,std::function<int32_t (const BufferHandle &)> realFunc)68 int32_t LayerCache::SetLayerBuffer(const BufferHandle* buffer, uint32_t seqNo,
69 const std::vector<uint32_t>& deletingList, std::function<int32_t (const BufferHandle&)> realFunc)
70 {
71 bool result = false;
72 for (auto num : deletingList) {
73 result = bufferCaches_->EraseCache(num);
74 DISPLAY_CHECK((result != true), HDF_LOGE("%{public}s: warning, erase buffer cache fail, num: %{public}u",
75 __func__, num));
76 }
77
78 BufferHandle* handle = BufferCacheUtils::NativeBufferCache(bufferCaches_,
79 const_cast<BufferHandle*>(buffer), seqNo, layerId_);
80 DISPLAY_CHK_RETURN(handle == nullptr, HDF_FAILURE,
81 HDF_LOGE("%{public}s: call NativeBufferCache fail", __func__));
82 int32_t ret = realFunc(*handle);
83 DISPLAY_CHK_RETURN(ret != HDF_SUCCESS, ret, HDF_LOGE("%{public}s: call realFunc fail", __func__));
84
85 return HDF_SUCCESS;
86 }
87
Dump() const88 void LayerCache::Dump() const
89 {
90 bufferCaches_->TravelCaches([this](const int32_t id, const NativeBuffer& buffer)->void {
91 auto info = buffer.Dump();
92 HDF_LOGE("layerId-%{public}d, buffer[%{public}d]: %{public}s", layerId_, id, info.c_str());
93 });
94 }
95 } // namespace V1_0
96 } // namespace Composer
97 } // namespace Display
98 } // namespace HDI
99 } // namespace OHOS
100