1 /* 2 * Copyright (c) 2021 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 INTERFACES_INNERKITS_SURFACE_SURFACE_BUFFER_H 17 #define INTERFACES_INNERKITS_SURFACE_SURFACE_BUFFER_H 18 19 #include <functional> 20 21 #include <memory> 22 #include <refbase.h> 23 24 #include "buffer_handle_utils.h" 25 #include "surface_type.h" 26 #include "egl_data.h" 27 #include "buffer_extra_data.h" 28 #include "native_buffer.h" 29 30 struct BufferWrapper; 31 32 namespace OHOS { 33 class IProducerListener; 34 class MessageParcel; 35 class Parcel; 36 class SyncFence; 37 38 using ProducerInitInfo = struct { 39 uint64_t uniqueId; 40 int32_t width; 41 int32_t height; 42 std::string name; 43 std::string appName; 44 bool isInHebcList; 45 std::string bufferName; 46 uint64_t producerId; 47 sptr<IProducerListener> propertyListener; // register callback in ctor 48 int32_t transformHint; 49 }; 50 51 struct RSBufferInfo { 52 GraphicColorGamut surfaceBufferColorGamut = GraphicColorGamut::GRAPHIC_COLOR_GAMUT_SRGB; 53 GraphicTransformType transform = GraphicTransformType::GRAPHIC_ROTATE_NONE; 54 ScalingMode scalingMode = ScalingMode::SCALING_MODE_SCALE_TO_WINDOW; 55 int32_t surfaceBufferWidth = 0; 56 int32_t surfaceBufferHeight = 0; 57 uint32_t sequence = 0; 58 BufferRequestConfig bufferRequestConfig; 59 }; 60 61 class SurfaceBuffer : public RefBase { 62 public: 63 virtual BufferHandle *GetBufferHandle() const = 0; 64 virtual int32_t GetWidth() const = 0; 65 virtual int32_t GetHeight() const = 0; 66 virtual int32_t GetStride() const = 0; 67 virtual int32_t GetFormat() const = 0; 68 virtual uint64_t GetUsage() const = 0; 69 virtual uint64_t GetPhyAddr() const = 0; 70 virtual void* GetVirAddr() = 0; 71 virtual int32_t GetFileDescriptor() const = 0; 72 virtual uint32_t GetSize() const = 0; 73 74 virtual GraphicColorGamut GetSurfaceBufferColorGamut() const = 0; 75 virtual GraphicTransformType GetSurfaceBufferTransform() const = 0; 76 virtual void SetSurfaceBufferColorGamut(const GraphicColorGamut& colorGamut) = 0; 77 virtual void SetSurfaceBufferTransform(const GraphicTransformType& transform) = 0; 78 79 virtual int32_t GetSurfaceBufferWidth() const = 0; 80 virtual int32_t GetSurfaceBufferHeight() const = 0; 81 virtual void SetSurfaceBufferWidth(int32_t width) = 0; 82 virtual void SetSurfaceBufferHeight(int32_t width) = 0; 83 84 virtual uint32_t GetSeqNum() const = 0; 85 86 virtual void SetExtraData(sptr<BufferExtraData> bedata) = 0; 87 virtual sptr<BufferExtraData> GetExtraData() const = 0; 88 virtual GSError WriteToMessageParcel(MessageParcel &parcel) = 0; 89 /* 90 * @Description: ReadFromMessageParcel 91 * @param parcel: A MessageParcel object 92 * @param readSafeFdFunc:Optional parameter, caller can use this callback function to implement 93 * their own way of obtaining Fd from parcel 94 * @return Returns GSERROR_OK if SetMetadata is successful; returns GErrorCode otherwise. 95 */ 96 virtual GSError ReadFromMessageParcel(MessageParcel &parcel, 97 std::function<int(MessageParcel &parcel, 98 std::function<int(Parcel &)>readFdDefaultFunc)>readSafeFdFunc = nullptr) = 0; 99 virtual void SetBufferHandle(BufferHandle *handle) = 0; 100 101 /** 102 * @brief Allocates a surface buffer based on the specified configuration. 103 * 104 * This method allocates a new buffer according to the parameters in the given BufferRequestConfig. 105 * If a previous buffer (`previousBuffer`) is provided, the implementation may attempt to reuse or reallocate 106 * from it to optimize memory usage or performance. If `previousBuffer` is null, 107 * a new buffer is allocated from scratch. 108 * @param config Buffer configuration including size, format, usage, timeout, color gamut, etc. 109 * @param previousBuffer Optional previous buffer to be reused or reallocated. If nullptr, no reuse is attempted. 110 * @return Returns: 111 * - GSERROR_OK on successful allocation or reallocation; 112 * - GSERROR_INVALID_ARGUMENTS if the input config is invalid; 113 * - GSERROR_INTERNAL if the internal display buffer is not initialized; 114 * - GSERROR_HDI_ERROR for lower-level allocation or registration failures. 115 */ 116 virtual GSError Alloc(const BufferRequestConfig &config, const sptr<SurfaceBuffer>& previousBuffer = nullptr) = 0; 117 virtual GSError Map() = 0; 118 virtual GSError Unmap() = 0; 119 virtual GSError FlushCache() = 0; 120 virtual GSError InvalidateCache() = 0; 121 122 // metadata 123 /* 124 * @Description: SetMetadata 125 * @param key:Metadata key 126 * @param value:Metadata value 127 * @param enableCache:If true(default true), enable metaData cache optimization, 128 * when the same metaData is repeatedly set, the instruction count can be reduced. 129 * Prohibit setting different enableCache values for the same surfacebuffer object 130 * @return Returns GSERROR_OK if SetMetadata is successful; returns GErrorCode otherwise. 131 */ 132 virtual GSError SetMetadata(uint32_t key, const std::vector<uint8_t>& value, bool enableCache = true) = 0; 133 virtual GSError GetMetadata(uint32_t key, std::vector<uint8_t>& value) = 0; 134 virtual GSError ListMetadataKeys(std::vector<uint32_t>& keys) = 0; 135 virtual GSError EraseMetadataKey(uint32_t key) = 0; 136 137 virtual void SetCropMetadata(const Rect& crop) = 0; 138 virtual bool GetCropMetadata(Rect& crop) = 0; 139 NativeBufferToSurfaceBuffer(OH_NativeBuffer * buffer)140 static SurfaceBuffer* NativeBufferToSurfaceBuffer(OH_NativeBuffer* buffer) 141 { 142 return reinterpret_cast<SurfaceBuffer *>(buffer); 143 }; 144 NativeBufferToSurfaceBuffer(OH_NativeBuffer const * buffer)145 static const SurfaceBuffer* NativeBufferToSurfaceBuffer(OH_NativeBuffer const* buffer) 146 { 147 return reinterpret_cast<SurfaceBuffer const*>(buffer); 148 }; 149 150 virtual OH_NativeBuffer* SurfaceBufferToNativeBuffer() = 0; 151 152 static sptr<SurfaceBuffer> Create(); 153 WriteBufferRequestConfig(MessageParcel & parcel)154 virtual GSError WriteBufferRequestConfig(MessageParcel &parcel) 155 { 156 (void)parcel; 157 return GSERROR_OK; 158 }; ReadBufferRequestConfig(MessageParcel & parcel)159 virtual GSError ReadBufferRequestConfig(MessageParcel &parcel) 160 { 161 (void)parcel; 162 return GSERROR_OK; 163 }; ReadBufferProperty(MessageParcel & parcel)164 virtual GSError ReadBufferProperty(MessageParcel& parcel) 165 { 166 (void)parcel; 167 return GSERROR_OK; 168 }; WriteBufferProperty(MessageParcel & parcel)169 virtual GSError WriteBufferProperty(MessageParcel& parcel) 170 { 171 (void)parcel; 172 return GSERROR_OK; 173 }; ReadFromBufferInfo(const RSBufferInfo & bufferInfo)174 virtual GSError ReadFromBufferInfo(const RSBufferInfo &bufferInfo) 175 { 176 (void)bufferInfo; 177 return GSERROR_OK; 178 } 179 virtual BufferRequestConfig GetBufferRequestConfig() const = 0; SetBufferRequestConfig(const BufferRequestConfig & config)180 virtual void SetBufferRequestConfig(const BufferRequestConfig &config) 181 { 182 (void)config; 183 }; SetConsumerAttachBufferFlag(bool value)184 virtual void SetConsumerAttachBufferFlag(bool value) 185 { 186 (void)value; 187 }; GetConsumerAttachBufferFlag()188 virtual bool GetConsumerAttachBufferFlag() 189 { 190 return false; 191 }; GetPlanesInfo(void ** planesInfo)192 virtual GSError GetPlanesInfo(void **planesInfo) 193 { 194 (void)planesInfo; 195 return GSERROR_OK; 196 }; SetSurfaceBufferScalingMode(const ScalingMode & scalingMode)197 virtual void SetSurfaceBufferScalingMode(const ScalingMode &scalingMode) 198 { 199 (void) scalingMode; 200 } GetSurfaceBufferScalingMode()201 virtual ScalingMode GetSurfaceBufferScalingMode() const 202 { 203 return SCALING_MODE_SCALE_TO_WINDOW; 204 } SetBufferDeleteFromCacheFlag(const bool & flag)205 virtual void SetBufferDeleteFromCacheFlag(const bool &flag) 206 { 207 (void) flag; 208 } GetBufferDeleteFromCacheFlag()209 virtual bool GetBufferDeleteFromCacheFlag() const 210 { 211 return false; 212 } TryReclaim()213 virtual GSError TryReclaim() 214 { 215 return GSERROR_NOT_SUPPORT; 216 } TryResumeIfNeeded()217 virtual GSError TryResumeIfNeeded() 218 { 219 return GSERROR_NOT_SUPPORT; 220 } IsReclaimed()221 virtual bool IsReclaimed() 222 { 223 return false; 224 } 225 virtual void SetAndMergeSyncFence(const sptr<SyncFence>& syncFence) = 0; 226 virtual sptr<SyncFence> GetSyncFence() const = 0; 227 protected: SurfaceBuffer()228 SurfaceBuffer() {} 229 SurfaceBuffer(const SurfaceBuffer&) = delete; 230 SurfaceBuffer& operator=(const SurfaceBuffer&) = delete; ~SurfaceBuffer()231 virtual ~SurfaceBuffer() {} 232 }; 233 234 struct SurfaceProperty { 235 GraphicTransformType transformHint = GraphicTransformType::GRAPHIC_ROTATE_NONE; 236 }; 237 238 using OnReleaseFunc = std::function<GSError(sptr<SurfaceBuffer> &)>; 239 using OnDeleteBufferFunc = std::function<void(int32_t)>; 240 using OnReleaseFuncWithFence = std::function<GSError(const sptr<SurfaceBuffer>&, const sptr<SyncFence>&)>; 241 using OnUserDataChangeFunc = std::function<void(const std::string& key, const std::string& value)>; 242 using OnPropertyChangeFunc = std::function<GSError(const SurfaceProperty&)>; 243 } // namespace OHOS 244 245 #endif // INTERFACES_INNERKITS_SURFACE_SURFACE_BUFFER_H 246