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 AVBUFFER_COMMON_H
17#define AVBUFFER_COMMON_H
18
19#ifndef MEDIA_NO_OHOS
20#ifndef MEDIA_OHOS
21#define MEDIA_OHOS
22#endif
23#else
24#ifdef MEDIA_OHOS
25#undef MEDIA_OHOS
26#endif
27#endif
28
29#include <memory>
30#include <string>
31#include "meta/meta.h"
32
33namespace OHOS {
34class SurfaceBuffer;
35class MessageParcel;
36struct BufferRequestConfig;
37} // namespace OHOS
38
39namespace OHOS {
40namespace Media {
41class AVMemory;
42constexpr int32_t INVALID_POSITION = -1;
43
44/**
45 * @enum MemoryType
46 * @brief For platforms that support multiple processes, this flag bit indicates the types of data storage, refer to
47 * {@link AVAllocator}.
48 * @since 4.1
49 * @version 1.0
50 */
51enum struct MemoryType : uint8_t {
52    /**
53     * If this type is not set, the allocator will be initialized by the current type by default. This type of memory is
54     * created through malloc() and can only be used by the current process.
55     */
56    VIRTUAL_MEMORY = 0,
57    /**
58     * A memory type that implements a convenient memory sharing mechanism. For platforms that do not support
59     * multiprocessing, it may only encapsulate ordinary memory blocks rather than truly multiprocess shared memory
60     */
61    SHARED_MEMORY,
62    /**
63     * A memory type that provides surface buffer for sharing multi process data.
64     */
65    SURFACE_MEMORY,
66    /**
67     * A memory type that provides DMA method for sharing multi process data. If the hardware does not support it, it
68     * will be invalid when initializing AVAlocator.
69     */
70    HARDWARE_MEMORY,
71    /**
72     * The identifier for buffer queue, representing any type of memory can be allocated.
73     */
74    UNKNOWN_MEMORY
75};
76
77/**
78 * @brief Enumerates the flag bits used to create a new shared memory.
79 */
80enum MemoryFlag : uint8_t {
81    /**
82     * For platforms that support multiple processes, this flag bit indicates that the remote process can only read data
83     * in the shared memory. If this flag is not set, the remote process has both read and write permissions by default.
84     * Adding this flag does not affect the process that creates the memory, which always has the read and write
85     * permission on the shared memory. For platforms that do not support multi-processes, the memory read and write
86     * permission control capability may not be available. In this case, this flag is invalid.
87     */
88    MEMORY_READ_ONLY = 0x1 << 0,
89    /**
90     * For platforms that support multiple processes, this flag bit indicates that the remote process can only write
91     * data in the shared memory.
92     */
93    MEMORY_WRITE_ONLY = 0x1 << 1,
94    /**
95     * This flag bit indicates that the remote process is allowed to read and write the shared memory. If no flags are
96     * specified, this is the default memory sharing policy. If the FLAGS_READ_ONLY bit is set, this flag bit is
97     * ignored.
98     */
99    MEMORY_READ_WRITE = MEMORY_READ_ONLY | MEMORY_WRITE_ONLY,
100};
101
102/**
103 * @brief Struct that encapsulates some info of media buffer.
104 */
105using AVBufferConfig = struct AVBufferConfig {
106    int32_t size = 0;
107    int32_t align = 0;
108    MemoryType memoryType = MemoryType::UNKNOWN_MEMORY;
109    MemoryFlag memoryFlag = MemoryFlag::MEMORY_READ_WRITE;
110    std::unique_ptr<struct BufferRequestConfig> surfaceBufferConfig;
111    int32_t dmaFd = -1;   // to create dma buffer
112    int32_t capacity = 0; // get from buffer
113
114    AVBufferConfig();
115    AVBufferConfig(const AVBufferConfig &rhs);
116    AVBufferConfig(AVBufferConfig &&rhs) noexcept;
117    AVBufferConfig &operator=(const AVBufferConfig &rhs);
118    AVBufferConfig &operator=(AVBufferConfig &&rhs) noexcept;
119    bool operator<=(const struct AVBufferConfig &rhs) const;
120};
121} // namespace Media
122} // namespace OHOS
123#endif // AVBUFFER_COMMON_H