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 AV_ALLOCATOR_H 17#define AV_ALLOCATOR_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 "buffer/avbuffer_common.h" 32#include "refbase.h" 33 34namespace OHOS { 35struct BufferRequestConfig; 36} // namespace OHOS 37namespace OHOS { 38namespace Media { 39/** 40 * @brief AVBuffer's allocator. 41 */ 42class __attribute__((visibility("default"))) AVAllocator { 43public: 44 virtual ~AVAllocator() = default; 45 /** 46 * @brief Get the memory's type set by the creator, refer to {@link MemoryType} 47 * @return the memory's type. 48 * @since 4.1 49 */ 50 virtual MemoryType GetMemoryType() = 0; 51 52 /** 53 * @brief Allocate a memory. 54 * @param capacity The capacity of the memory to be allocated. 55 * @return The pointer of the allocated buffer. When memory's type is {@link SHARED_MEMORY} returns the file 56 * descriptor of allocated memory. 57 * @since 4.1 58 */ 59 virtual void *Alloc(int32_t capacity) = 0; 60 61 /** 62 * @brief Free a memory. 63 * @param ptr The pointer of the allocated buffer. When memory's type is {@link SHARED_MEMORY} the parameter is the 64 * file descriptor of allocated memory. 65 * @return Whether the free was successful. 66 * @since 4.1 67 */ 68 virtual bool Free(void *ptr) = 0; 69 70protected: 71 AVAllocator(){}; 72}; 73 74class __attribute__((visibility("default"))) AVAllocatorFactory { 75public: 76 /** 77 * @brief Create the allocator of CPU buffer. 78 * @return The allocator that allocate CPU buffer. 79 * @since 4.1 80 */ 81 static std::shared_ptr<AVAllocator> CreateVirtualAllocator(); 82 83 /** 84 * @brief Create the allocator of shared memory. 85 * @param memFlag Set the memory's flags, refer to {@link MemoryFlag}. 86 * @return The allocator that allocate shared memory. 87 * @since 4.1 88 */ 89 static std::shared_ptr<AVAllocator> CreateSharedAllocator(MemoryFlag memFlag); 90 91 /** 92 * @brief Create the allocator of surface buffer, refer to {@link SurfaceBuffer}. 93 * @param config Set the config of the surface buffer, refer to {@link BufferRequestConfig}. 94 * @return The allocator that allocate surface buffer. 95 * @since 4.1 96 */ 97 static std::shared_ptr<AVAllocator> CreateSurfaceAllocator(const struct BufferRequestConfig &configs); 98 99 /** 100 * @brief Create the allocator of DMA buffer. 101 * @param fd The file descriptor obtained from allocated DMA buffer. 102 * @param capacity The capacity obtained from allocated DMA buffer. 103 * @param memFlag Set the memory's flags, refer to {@link MemoryFlag}. 104 * @return The allocator that allocate DMA buffer. 105 * @since 4.1 106 */ 107 static std::shared_ptr<AVAllocator> CreateHardwareAllocator(int32_t fd, int32_t capacity, MemoryFlag memFlag, 108 bool isSecure = false); 109 110private: 111 AVAllocatorFactory() = default; 112 ~AVAllocatorFactory() = default; 113}; 114} // namespace Media 115} // namespace OHOS 116#endif // AV_ALLOCATOR_H