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#ifndef OHOS_HDI_DISPLAY_V1_0_LAYER_CACHE_H
17#define OHOS_HDI_DISPLAY_V1_0_LAYER_CACHE_H
18
19#include <functional>
20#include <memory>
21#include <unordered_map>
22#include "base/native_buffer.h"
23#include "cache_manager.h"
24#include "nocopyable.h"
25#include "v1_2/mapper_stub.h"
26#include "v1_1/imetadata.h"
27
28namespace OHOS {
29namespace HDI {
30namespace Display {
31namespace Composer {
32using namespace OHOS::HDI::Base;
33class LayerCache : public NoCopyable {
34public:
35    ~LayerCache();
36    static LayerCache* Create(uint32_t id);
37    int32_t SetLayerBuffer(BufferHandle*& buffer, uint32_t seqNo, bool &needFreeBuffer,
38        const std::vector<uint32_t>& deletingList, std::function<int32_t (const BufferHandle&)> realFunc);
39    int32_t SetBufferCacheMaxCount(uint32_t cacheCount);
40    int32_t ResetLayerBuffer();
41    void Dump() const;
42    static void NativeBufferInit(sptr<NativeBuffer>& buffer);
43    static void NativeBufferCleanUp(sptr<NativeBuffer>& buffer);
44private:
45    explicit LayerCache(uint32_t id);
46    int32_t Init();
47    static sptr<Buffer::V1_2::IMapper> GetMapperService();
48    static sptr<Buffer::V1_1::IMetadata> GetMetaService();
49    static int32_t Mmap(sptr<NativeBuffer>& buffer);
50    static int32_t Unmap(sptr<NativeBuffer>& buffer);
51    static int32_t FreeMem(sptr<NativeBuffer>& buffer);
52    static int32_t RegisterBuffer(sptr<NativeBuffer>& buffer);
53    uint32_t layerId_;
54    std::shared_ptr<CacheManager<uint32_t, NativeBuffer>> bufferCaches_;
55};
56} // namespace Composer
57} // namespace Display
58} // namespace HDI
59} // namespace OHOS
60#endif // OHOS_HDI_DISPLAY_V1_0_LAYER_CACHE_H
61