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 "layer_cache.h"
17c5e268c6Sopenharmony_ci
18c5e268c6Sopenharmony_ci#include "buffer_cache_utils.h"
19c5e268c6Sopenharmony_ci#include "common/include/display_interface_utils.h"
20c5e268c6Sopenharmony_ci#include "hdf_base.h"
21c5e268c6Sopenharmony_ci#include "hdf_log.h"
22c5e268c6Sopenharmony_ci
23c5e268c6Sopenharmony_cinamespace OHOS {
24c5e268c6Sopenharmony_cinamespace HDI {
25c5e268c6Sopenharmony_cinamespace Display {
26c5e268c6Sopenharmony_cinamespace Composer {
27c5e268c6Sopenharmony_ci
28c5e268c6Sopenharmony_ciLayerCache* LayerCache::Create(uint32_t id)
29c5e268c6Sopenharmony_ci{
30c5e268c6Sopenharmony_ci    LayerCache* layer = new LayerCache(id);
31c5e268c6Sopenharmony_ci    DISPLAY_CHK_RETURN(layer == nullptr, nullptr, HDF_LOGE("%{public}s: create layer cache failed", __func__));
32c5e268c6Sopenharmony_ci
33c5e268c6Sopenharmony_ci    int32_t ret = layer->Init();
34c5e268c6Sopenharmony_ci    if (ret != HDF_SUCCESS) {
35c5e268c6Sopenharmony_ci        delete layer;
36c5e268c6Sopenharmony_ci        layer = nullptr;
37c5e268c6Sopenharmony_ci        HDF_LOGE("%{public}s: layer cache init failed", __func__);
38c5e268c6Sopenharmony_ci    }
39c5e268c6Sopenharmony_ci
40c5e268c6Sopenharmony_ci    return layer;
41c5e268c6Sopenharmony_ci}
42c5e268c6Sopenharmony_ci
43c5e268c6Sopenharmony_ciLayerCache::LayerCache(uint32_t id) : layerId_(id)
44c5e268c6Sopenharmony_ci{
45c5e268c6Sopenharmony_ci}
46c5e268c6Sopenharmony_ci
47c5e268c6Sopenharmony_ciLayerCache::~LayerCache()
48c5e268c6Sopenharmony_ci{
49c5e268c6Sopenharmony_ci}
50c5e268c6Sopenharmony_ci
51c5e268c6Sopenharmony_ciint32_t LayerCache::Init()
52c5e268c6Sopenharmony_ci{
53c5e268c6Sopenharmony_ci    bufferCaches_.reset(new CacheManager<uint32_t, NativeBuffer>());
54c5e268c6Sopenharmony_ci    DISPLAY_CHK_RETURN(bufferCaches_ == nullptr, HDF_FAILURE,
55c5e268c6Sopenharmony_ci        HDF_LOGE("%{public}s: create buffer caches failed", __func__));
56c5e268c6Sopenharmony_ci
57c5e268c6Sopenharmony_ci    bufferCaches_->SetInitFunc(NativeBufferInit);
58c5e268c6Sopenharmony_ci    bufferCaches_->SetCleanUpFunc(NativeBufferCleanUp);
59c5e268c6Sopenharmony_ci    return HDF_SUCCESS;
60c5e268c6Sopenharmony_ci}
61c5e268c6Sopenharmony_ci
62c5e268c6Sopenharmony_ciint32_t LayerCache::SetBufferCacheMaxCount(uint32_t cacheCount)
63c5e268c6Sopenharmony_ci{
64c5e268c6Sopenharmony_ci    bool ret = bufferCaches_->SetCacheMaxCount(cacheCount);
65c5e268c6Sopenharmony_ci    DISPLAY_CHK_RETURN(ret == false, HDF_FAILURE, HDF_LOGE("%{public}s: failed", __func__));
66c5e268c6Sopenharmony_ci    return HDF_SUCCESS;
67c5e268c6Sopenharmony_ci}
68c5e268c6Sopenharmony_ci
69c5e268c6Sopenharmony_ciint32_t LayerCache::SetLayerBuffer(BufferHandle*& buffer, uint32_t seqNo, bool &needFreeBuffer,
70c5e268c6Sopenharmony_ci    const std::vector<uint32_t>& deletingList, std::function<int32_t (const BufferHandle&)> realFunc)
71c5e268c6Sopenharmony_ci{
72c5e268c6Sopenharmony_ci    if (buffer != nullptr) {
73c5e268c6Sopenharmony_ci        HDF_LOGI("%{public}s, seqNo %{public}u, fd %{public}d, size %{public}d", __func__, seqNo, buffer->fd,
74c5e268c6Sopenharmony_ci                 buffer->size);
75c5e268c6Sopenharmony_ci    }
76c5e268c6Sopenharmony_ci    for (auto num : deletingList) {
77c5e268c6Sopenharmony_ci        (void)bufferCaches_->EraseCache(num);
78c5e268c6Sopenharmony_ci    }
79c5e268c6Sopenharmony_ci
80c5e268c6Sopenharmony_ci    BufferHandle* handle = BufferCacheUtils::NativeBufferCache(bufferCaches_, buffer, seqNo, layerId_, needFreeBuffer);
81c5e268c6Sopenharmony_ci    DISPLAY_CHK_RETURN(handle == nullptr, HDF_FAILURE,
82c5e268c6Sopenharmony_ci        HDF_LOGE("%{public}s: call NativeBufferCache fail", __func__));
83c5e268c6Sopenharmony_ci    int32_t ret = realFunc(*handle);
84c5e268c6Sopenharmony_ci    if (ret != HDF_SUCCESS) {
85c5e268c6Sopenharmony_ci        bufferCaches_->EraseCache(seqNo);
86c5e268c6Sopenharmony_ci        buffer = nullptr;
87c5e268c6Sopenharmony_ci        HDF_LOGE("%{public}s: call realFunc fail", __func__);
88c5e268c6Sopenharmony_ci    }
89c5e268c6Sopenharmony_ci
90c5e268c6Sopenharmony_ci    return HDF_SUCCESS;
91c5e268c6Sopenharmony_ci}
92c5e268c6Sopenharmony_ci
93c5e268c6Sopenharmony_ciint32_t LayerCache::ResetLayerBuffer()
94c5e268c6Sopenharmony_ci{
95c5e268c6Sopenharmony_ci    HDF_LOGI("%{public}s", __func__);
96c5e268c6Sopenharmony_ci    return Init();
97c5e268c6Sopenharmony_ci}
98c5e268c6Sopenharmony_ci
99c5e268c6Sopenharmony_civoid LayerCache::NativeBufferInit(sptr<NativeBuffer>& buffer)
100c5e268c6Sopenharmony_ci{
101c5e268c6Sopenharmony_ci    if (buffer == nullptr) {
102c5e268c6Sopenharmony_ci        HDF_LOGW("NativeBufferInit buffer nullptr!");
103c5e268c6Sopenharmony_ci        return;
104c5e268c6Sopenharmony_ci    }
105c5e268c6Sopenharmony_ci    int32_t ret = RegisterBuffer(buffer);
106c5e268c6Sopenharmony_ci    if (ret != HDF_SUCCESS) {
107c5e268c6Sopenharmony_ci        HDF_LOGE("%{public}s: RegisterBuffer failed with %{public}d!", __func__, ret);
108c5e268c6Sopenharmony_ci    }
109c5e268c6Sopenharmony_ci}
110c5e268c6Sopenharmony_ci
111c5e268c6Sopenharmony_civoid LayerCache::NativeBufferCleanUp(sptr<NativeBuffer>& buffer)
112c5e268c6Sopenharmony_ci{
113c5e268c6Sopenharmony_ci    if (buffer == nullptr) {
114c5e268c6Sopenharmony_ci        HDF_LOGW("NativeBufferCleanUp buffer nullptr!");
115c5e268c6Sopenharmony_ci        return;
116c5e268c6Sopenharmony_ci    }
117c5e268c6Sopenharmony_ci    int32_t ret = FreeMem(buffer);
118c5e268c6Sopenharmony_ci    if (ret != HDF_SUCCESS) {
119c5e268c6Sopenharmony_ci        HDF_LOGE("%{public}s: FreeMem failed with %{public}d!", __func__, ret);
120c5e268c6Sopenharmony_ci    }
121c5e268c6Sopenharmony_ci}
122c5e268c6Sopenharmony_ci
123c5e268c6Sopenharmony_cisptr<Buffer::V1_1::IMetadata> LayerCache::GetMetaService()
124c5e268c6Sopenharmony_ci{
125c5e268c6Sopenharmony_ci    static sptr<Buffer::V1_1::IMetadata> metaService = nullptr;
126c5e268c6Sopenharmony_ci    if (metaService == nullptr) {
127c5e268c6Sopenharmony_ci        metaService = Buffer::V1_1::IMetadata::Get(true);
128c5e268c6Sopenharmony_ci    }
129c5e268c6Sopenharmony_ci    return metaService;
130c5e268c6Sopenharmony_ci}
131c5e268c6Sopenharmony_ci
132c5e268c6Sopenharmony_cisptr<Buffer::V1_2::IMapper> LayerCache::GetMapperService()
133c5e268c6Sopenharmony_ci{
134c5e268c6Sopenharmony_ci    static sptr<Buffer::V1_2::IMapper> mapperService = nullptr;
135c5e268c6Sopenharmony_ci    if (mapperService == nullptr) {
136c5e268c6Sopenharmony_ci        mapperService = Buffer::V1_2::IMapper::Get(true);
137c5e268c6Sopenharmony_ci    }
138c5e268c6Sopenharmony_ci    return mapperService;
139c5e268c6Sopenharmony_ci}
140c5e268c6Sopenharmony_ci
141c5e268c6Sopenharmony_ciint32_t LayerCache::Mmap(sptr<NativeBuffer>& buffer)
142c5e268c6Sopenharmony_ci{
143c5e268c6Sopenharmony_ci    auto mapperService = GetMapperService();
144c5e268c6Sopenharmony_ci    if (mapperService == nullptr) {
145c5e268c6Sopenharmony_ci        HDF_LOGE("GetMapperService failed!");
146c5e268c6Sopenharmony_ci        return HDF_FAILURE;
147c5e268c6Sopenharmony_ci    }
148c5e268c6Sopenharmony_ci    return mapperService->Mmap(buffer);
149c5e268c6Sopenharmony_ci}
150c5e268c6Sopenharmony_ci
151c5e268c6Sopenharmony_ciint32_t LayerCache::Unmap(sptr<NativeBuffer>& buffer)
152c5e268c6Sopenharmony_ci{
153c5e268c6Sopenharmony_ci    auto mapperService = GetMapperService();
154c5e268c6Sopenharmony_ci    if (mapperService == nullptr) {
155c5e268c6Sopenharmony_ci        HDF_LOGE("GetMapperService failed!");
156c5e268c6Sopenharmony_ci        return HDF_FAILURE;
157c5e268c6Sopenharmony_ci    }
158c5e268c6Sopenharmony_ci    return mapperService->Unmap(buffer);
159c5e268c6Sopenharmony_ci}
160c5e268c6Sopenharmony_ci
161c5e268c6Sopenharmony_ciint32_t LayerCache::FreeMem(sptr<NativeBuffer>& buffer)
162c5e268c6Sopenharmony_ci{
163c5e268c6Sopenharmony_ci    auto mapperService = GetMapperService();
164c5e268c6Sopenharmony_ci    if (mapperService == nullptr) {
165c5e268c6Sopenharmony_ci        HDF_LOGE("GetMapperService failed!");
166c5e268c6Sopenharmony_ci        return HDF_FAILURE;
167c5e268c6Sopenharmony_ci    }
168c5e268c6Sopenharmony_ci    int32_t ret = Unmap(buffer);
169c5e268c6Sopenharmony_ci    if (ret != HDF_SUCCESS) {
170c5e268c6Sopenharmony_ci        HDF_LOGE("Unmap failed!");
171c5e268c6Sopenharmony_ci    }
172c5e268c6Sopenharmony_ci    return mapperService->FreeMem(buffer);
173c5e268c6Sopenharmony_ci}
174c5e268c6Sopenharmony_ci
175c5e268c6Sopenharmony_ciint32_t LayerCache::RegisterBuffer(sptr<NativeBuffer>& buffer)
176c5e268c6Sopenharmony_ci{
177c5e268c6Sopenharmony_ci    auto metaService = GetMetaService();
178c5e268c6Sopenharmony_ci    if (metaService == nullptr) {
179c5e268c6Sopenharmony_ci        return HDF_FAILURE;
180c5e268c6Sopenharmony_ci    }
181c5e268c6Sopenharmony_ci    int32_t ret = metaService->RegisterBuffer(buffer);
182c5e268c6Sopenharmony_ci    if (ret != HDF_SUCCESS) {
183c5e268c6Sopenharmony_ci        HDF_LOGE("Register Buffer failed!");
184c5e268c6Sopenharmony_ci        return ret;
185c5e268c6Sopenharmony_ci    }
186c5e268c6Sopenharmony_ci    ret = Mmap(buffer);
187c5e268c6Sopenharmony_ci    if (ret != HDF_SUCCESS) {
188c5e268c6Sopenharmony_ci        HDF_LOGE("Mmap failed!");
189c5e268c6Sopenharmony_ci    }
190c5e268c6Sopenharmony_ci    return HDF_SUCCESS;
191c5e268c6Sopenharmony_ci}
192c5e268c6Sopenharmony_ci
193c5e268c6Sopenharmony_civoid LayerCache::Dump() const
194c5e268c6Sopenharmony_ci{
195c5e268c6Sopenharmony_ci    bufferCaches_->TravelCaches([this](const int32_t id, const NativeBuffer& buffer)->void {
196c5e268c6Sopenharmony_ci        auto info = buffer.Dump();
197c5e268c6Sopenharmony_ci        HDF_LOGE("layerId-%{public}d, buffer[%{public}d]: %{public}s", layerId_, id, info.c_str());
198c5e268c6Sopenharmony_ci    });
199c5e268c6Sopenharmony_ci}
200c5e268c6Sopenharmony_ci} // namespace Composer
201c5e268c6Sopenharmony_ci} // namespace Display
202c5e268c6Sopenharmony_ci} // namespace HDI
203c5e268c6Sopenharmony_ci} // namespace OHOS
204