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