1fa7767c5Sopenharmony_ci/*
2fa7767c5Sopenharmony_ci * Copyright (C) 2023 Huawei Device Co., Ltd.
3fa7767c5Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4fa7767c5Sopenharmony_ci * you may not use this file except in compliance with the License.
5fa7767c5Sopenharmony_ci * You may obtain a copy of the License at
6fa7767c5Sopenharmony_ci *
7fa7767c5Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8fa7767c5Sopenharmony_ci *
9fa7767c5Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10fa7767c5Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11fa7767c5Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12fa7767c5Sopenharmony_ci * See the License for the specific language governing permissions and
13fa7767c5Sopenharmony_ci * limitations under the License.
14fa7767c5Sopenharmony_ci */
15fa7767c5Sopenharmony_ci
16fa7767c5Sopenharmony_ci#ifndef AV_ALLOCATOR_H
17fa7767c5Sopenharmony_ci#define AV_ALLOCATOR_H
18fa7767c5Sopenharmony_ci
19fa7767c5Sopenharmony_ci#ifndef MEDIA_NO_OHOS
20fa7767c5Sopenharmony_ci#ifndef MEDIA_OHOS
21fa7767c5Sopenharmony_ci#define MEDIA_OHOS
22fa7767c5Sopenharmony_ci#endif
23fa7767c5Sopenharmony_ci#else
24fa7767c5Sopenharmony_ci#ifdef MEDIA_OHOS
25fa7767c5Sopenharmony_ci#undef MEDIA_OHOS
26fa7767c5Sopenharmony_ci#endif
27fa7767c5Sopenharmony_ci#endif
28fa7767c5Sopenharmony_ci
29fa7767c5Sopenharmony_ci#include <memory>
30fa7767c5Sopenharmony_ci#include <string>
31fa7767c5Sopenharmony_ci#include "buffer/avbuffer_common.h"
32fa7767c5Sopenharmony_ci#include "refbase.h"
33fa7767c5Sopenharmony_ci
34fa7767c5Sopenharmony_cinamespace OHOS {
35fa7767c5Sopenharmony_cistruct BufferRequestConfig;
36fa7767c5Sopenharmony_ci} // namespace OHOS
37fa7767c5Sopenharmony_cinamespace OHOS {
38fa7767c5Sopenharmony_cinamespace Media {
39fa7767c5Sopenharmony_ci/**
40fa7767c5Sopenharmony_ci * @brief AVBuffer's allocator.
41fa7767c5Sopenharmony_ci */
42fa7767c5Sopenharmony_ciclass __attribute__((visibility("default"))) AVAllocator {
43fa7767c5Sopenharmony_cipublic:
44fa7767c5Sopenharmony_ci    virtual ~AVAllocator() = default;
45fa7767c5Sopenharmony_ci    /**
46fa7767c5Sopenharmony_ci     * @brief Get the memory's type set by the creator, refer to {@link MemoryType}
47fa7767c5Sopenharmony_ci     * @return the memory's type.
48fa7767c5Sopenharmony_ci     * @since 4.1
49fa7767c5Sopenharmony_ci     */
50fa7767c5Sopenharmony_ci    virtual MemoryType GetMemoryType() = 0;
51fa7767c5Sopenharmony_ci
52fa7767c5Sopenharmony_ci    /**
53fa7767c5Sopenharmony_ci     * @brief Allocate a memory.
54fa7767c5Sopenharmony_ci     * @param capacity The capacity of the memory to be allocated.
55fa7767c5Sopenharmony_ci     * @return The pointer of the allocated buffer. When memory's type is {@link SHARED_MEMORY} returns the file
56fa7767c5Sopenharmony_ci     * descriptor of allocated memory.
57fa7767c5Sopenharmony_ci     * @since 4.1
58fa7767c5Sopenharmony_ci     */
59fa7767c5Sopenharmony_ci    virtual void *Alloc(int32_t capacity) = 0;
60fa7767c5Sopenharmony_ci
61fa7767c5Sopenharmony_ci    /**
62fa7767c5Sopenharmony_ci     * @brief Free a memory.
63fa7767c5Sopenharmony_ci     * @param ptr The pointer of the allocated buffer. When memory's type is {@link SHARED_MEMORY} the parameter is the
64fa7767c5Sopenharmony_ci     * file descriptor of allocated memory.
65fa7767c5Sopenharmony_ci     * @return Whether the free was successful.
66fa7767c5Sopenharmony_ci     * @since 4.1
67fa7767c5Sopenharmony_ci     */
68fa7767c5Sopenharmony_ci    virtual bool Free(void *ptr) = 0;
69fa7767c5Sopenharmony_ci
70fa7767c5Sopenharmony_ciprotected:
71fa7767c5Sopenharmony_ci    AVAllocator(){};
72fa7767c5Sopenharmony_ci};
73fa7767c5Sopenharmony_ci
74fa7767c5Sopenharmony_ciclass __attribute__((visibility("default"))) AVAllocatorFactory {
75fa7767c5Sopenharmony_cipublic:
76fa7767c5Sopenharmony_ci    /**
77fa7767c5Sopenharmony_ci     * @brief Create the allocator of CPU buffer.
78fa7767c5Sopenharmony_ci     * @return The allocator that allocate CPU buffer.
79fa7767c5Sopenharmony_ci     * @since 4.1
80fa7767c5Sopenharmony_ci     */
81fa7767c5Sopenharmony_ci    static std::shared_ptr<AVAllocator> CreateVirtualAllocator();
82fa7767c5Sopenharmony_ci
83fa7767c5Sopenharmony_ci    /**
84fa7767c5Sopenharmony_ci     * @brief Create the allocator of shared memory.
85fa7767c5Sopenharmony_ci     * @param memFlag Set the memory's flags, refer to {@link MemoryFlag}.
86fa7767c5Sopenharmony_ci     * @return The allocator that allocate shared memory.
87fa7767c5Sopenharmony_ci     * @since 4.1
88fa7767c5Sopenharmony_ci     */
89fa7767c5Sopenharmony_ci    static std::shared_ptr<AVAllocator> CreateSharedAllocator(MemoryFlag memFlag);
90fa7767c5Sopenharmony_ci
91fa7767c5Sopenharmony_ci    /**
92fa7767c5Sopenharmony_ci     * @brief Create the allocator of surface buffer, refer to {@link SurfaceBuffer}.
93fa7767c5Sopenharmony_ci     * @param config Set the config of the surface buffer, refer to {@link BufferRequestConfig}.
94fa7767c5Sopenharmony_ci     * @return The allocator that allocate surface buffer.
95fa7767c5Sopenharmony_ci     * @since 4.1
96fa7767c5Sopenharmony_ci     */
97fa7767c5Sopenharmony_ci    static std::shared_ptr<AVAllocator> CreateSurfaceAllocator(const struct BufferRequestConfig &configs);
98fa7767c5Sopenharmony_ci
99fa7767c5Sopenharmony_ci    /**
100fa7767c5Sopenharmony_ci     * @brief Create the allocator of DMA buffer.
101fa7767c5Sopenharmony_ci     * @param fd The file descriptor obtained from allocated DMA buffer.
102fa7767c5Sopenharmony_ci     * @param capacity The capacity obtained from allocated DMA buffer.
103fa7767c5Sopenharmony_ci     * @param memFlag Set the memory's flags, refer to {@link MemoryFlag}.
104fa7767c5Sopenharmony_ci     * @return The allocator that allocate DMA buffer.
105fa7767c5Sopenharmony_ci     * @since 4.1
106fa7767c5Sopenharmony_ci     */
107fa7767c5Sopenharmony_ci    static std::shared_ptr<AVAllocator> CreateHardwareAllocator(int32_t fd, int32_t capacity, MemoryFlag memFlag,
108fa7767c5Sopenharmony_ci                                                                bool isSecure = false);
109fa7767c5Sopenharmony_ci
110fa7767c5Sopenharmony_ciprivate:
111fa7767c5Sopenharmony_ci    AVAllocatorFactory() = default;
112fa7767c5Sopenharmony_ci    ~AVAllocatorFactory() = default;
113fa7767c5Sopenharmony_ci};
114fa7767c5Sopenharmony_ci} // namespace Media
115fa7767c5Sopenharmony_ci} // namespace OHOS
116fa7767c5Sopenharmony_ci#endif // AV_ALLOCATOR_H