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_IDISPLAY_BUFFER_VDI_H
17#define OHOS_HDI_DISPLAY_V1_0_IDISPLAY_BUFFER_VDI_H
18
19#include <vector>
20#include "buffer_handle.h"
21#include "v1_2/display_buffer_type.h"
22
23namespace OHOS {
24namespace HDI {
25namespace Display {
26namespace Buffer {
27namespace V1_0 {
28
29#ifdef BUFFER_VDI_DEFAULT_LIBRARY_ENABLE
30#define DISPLAY_BUFFER_VDI_DEFAULT_LIBRARY "libdisplay_buffer_vdi_impl_default.z.so"
31#endif // BUFFER_VDI_DEFAULT_LIBRARY_ENABLE
32#define DISPLAY_BUFFER_VDI_LIBRARY "libdisplay_buffer_vdi_impl.z.so"
33
34class IDisplayBufferVdi {
35public:
36    virtual ~IDisplayBufferVdi() = default;
37
38    /**
39     * @brief Allocates memory based on the parameters passed by the GUI.
40     *
41     * @param info Indicates the description of the memory to allocate.
42     *
43     * @param handle Indicates the pointer to the buffer of the memory to allocate.
44     *
45     * @return Returns <b>0</b> if the operation is successful; returns an error code defined in {@link DispErrCode}
46     * otherwise.
47     * @since 1.0
48     * @version 1.0
49     */
50    virtual int32_t AllocMem(const AllocInfo& info, BufferHandle*& handle) const = 0;
51
52    /**
53     * @brief Releases memory.
54     *
55     * @param handle Indicates the reference to the buffer of the memory to release.
56     *
57     * @since 1.0
58     * @version 1.0
59     */
60    virtual void FreeMem(const BufferHandle& handle) const = 0;
61
62    /**
63     * @brief Maps memory to memory without cache in the process's address space.
64     *
65     * @param handle Indicates the reference to the buffer of the memory to map.
66     *
67     * @return Returns the pointer to a valid address if the operation is successful; returns <b>NULL</b> otherwise.
68     * @since 1.0
69     * @version 1.0
70     */
71    virtual void *Mmap(const BufferHandle& handle) const = 0;
72
73    /**
74     * @brief Unmaps memory, that is, removes mappings from the process's address space.
75     *
76     * @param handle Indicates the reference to the buffer of the memory to unmap.
77     *
78     * @return Returns <b>0</b> if the operation is successful; returns an error code defined in {@link DispErrCode}
79     * otherwise.
80     * @since 1.0
81     * @version 1.0
82     */
83    virtual int32_t Unmap(const BufferHandle& handle) const = 0;
84
85    /**
86     * @brief Flushes data from the cache to memory and invalidates the data in the cache.
87     *
88     * @param handle Indicates the reference to the buffer of the cache to flush.
89     *
90     * @return Returns <b>0</b> if the operation is successful; returns an error code defined in {@link DispErrCode}
91     * otherwise.
92     * @since 1.0
93     * @version 1.0
94     */
95    virtual int32_t FlushCache(const BufferHandle& handle) const = 0;
96
97    /**
98     * @brief Invalidates the cache to update it from memory.
99     *
100     * @param handle Indicates the reference to the buffer of the cache, which will be invalidated.
101     *
102     * @return Returns <b>0</b> if the operation is successful; returns an error code defined in {@link DispErrCode}
103     * otherwise.
104     * @since 1.0
105     * @version 1.0
106     */
107    virtual int32_t InvalidateCache(const BufferHandle& handle) const = 0;
108
109    /**
110     * @brief Checks whether the given VerifyAllocInfo array is allocatable.
111     *
112     * @param infos Indicates the VerifyAllocInfo array.
113     * @param supporteds Indicates whether the array is allocatable.
114     *
115     * @return Returns <b>0</b> if the operation is successful; returns an error code defined in {@link DispErrCode}
116     * otherwise.
117     * @since 1.0
118     * @version 1.0
119     */
120    virtual int32_t IsSupportedAlloc(
121        const std::vector<VerifyAllocInfo>& infos, std::vector<bool>& supporteds) const = 0;
122    virtual int32_t RegisterBuffer(const BufferHandle& handle) = 0;
123
124    virtual int32_t SetMetadata(const BufferHandle& handle, uint32_t key, const std::vector<uint8_t>& value) = 0;
125
126    virtual int32_t GetMetadata(const BufferHandle& handle, uint32_t key, std::vector<uint8_t>& value) = 0;
127
128    virtual int32_t ListMetadataKeys(const BufferHandle& handle, std::vector<uint32_t>& keys) = 0;
129
130    virtual int32_t EraseMetadataKey(const BufferHandle& handle, uint32_t key) = 0;
131
132    virtual int32_t GetImageLayout(const BufferHandle& handle, V1_2::ImageLayout& layout) const = 0;
133};
134
135using CreateDisplayBufferVdiFunc = IDisplayBufferVdi* (*)();
136using DestroyDisplayBufferVdiFunc = void (*)(IDisplayBufferVdi* vdi);
137extern "C" IDisplayBufferVdi* CreateDisplayBufferVdi();
138extern "C" void DestroyDisplayBufferVdi(IDisplayBufferVdi* vdi);
139} // namespace V1_0
140} // namespace Buffer
141} // namespace Display
142} // namespace HDI
143} // namespace OHOS
144
145#endif // OHOS_HDI_DISPLAY_V1_0_IDISPLAY_BUFFER_VDI_H
146