1 /* 2 * Copyright (c) 2021-2025 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 FRAMEWORKS_SURFACE_INCLUDE_BUFFER_QUEUE_PRODUCER_H 17 #define FRAMEWORKS_SURFACE_INCLUDE_BUFFER_QUEUE_PRODUCER_H 18 19 #include <vector> 20 #include <mutex> 21 #include <refbase.h> 22 #include <iremote_stub.h> 23 #include <message_parcel.h> 24 #include <message_option.h> 25 26 #include "surface_type.h" 27 #include <ibuffer_producer.h> 28 29 #include "buffer_queue.h" 30 31 namespace OHOS { 32 class SURFACE_HIDDEN BufferQueueProducer : public IRemoteStub<IBufferProducer> { 33 public: 34 BufferQueueProducer(sptr<BufferQueue> bufferQueue); 35 virtual ~BufferQueueProducer(); 36 37 virtual int OnRemoteRequest(uint32_t code, MessageParcel &arguments, 38 MessageParcel &reply, MessageOption &option) override; 39 40 virtual GSError RequestBuffer(const BufferRequestConfig &config, sptr<BufferExtraData> &bedata, 41 RequestBufferReturnValue &retval) override; 42 GSError RequestBuffers(const BufferRequestConfig &config, std::vector<sptr<BufferExtraData>> &bedata, 43 std::vector<RequestBufferReturnValue> &retvalues) override; 44 45 GSError GetProducerInitInfo(ProducerInitInfo &info) override; 46 47 GSError CancelBuffer(uint32_t sequence, sptr<BufferExtraData> bedata) override; 48 49 GSError FlushBuffer(uint32_t sequence, sptr<BufferExtraData> bedata, 50 sptr<SyncFence> fence, BufferFlushConfigWithDamages &config) override; 51 GSError SetLppShareFd(int fd, bool state) override; 52 53 GSError FlushBuffers(const std::vector<uint32_t> &sequences, 54 const std::vector<sptr<BufferExtraData>> &bedata, 55 const std::vector<sptr<SyncFence>> &fences, 56 const std::vector<BufferFlushConfigWithDamages> &configs) override; 57 GSError GetLastFlushedBuffer(sptr<SurfaceBuffer>& buffer, sptr<SyncFence>& fence, 58 float matrix[16], bool isUseNewMatrix) override; 59 60 GSError AttachBuffer(sptr<SurfaceBuffer>& buffer) override; 61 GSError AttachBuffer(sptr<SurfaceBuffer>& buffer, int32_t timeOut) override; 62 63 GSError DetachBuffer(sptr<SurfaceBuffer>& buffer) override; 64 65 uint32_t GetQueueSize() override; 66 GSError SetQueueSize(uint32_t queueSize) override; 67 68 GSError GetName(std::string &name) override; 69 uint64_t GetUniqueId() override; 70 GSError GetNameAndUniqueId(std::string& name, uint64_t& uniqueId) override; 71 72 int32_t GetDefaultWidth() override; 73 int32_t GetDefaultHeight() override; 74 GSError SetDefaultUsage(uint64_t usage) override; 75 uint64_t GetDefaultUsage() override; 76 77 GSError CleanCache(bool cleanAll, uint32_t *bufSeqNum) override; 78 GSError GoBackground() override; 79 80 GSError RegisterReleaseListener(sptr<IProducerListener> listener) override; 81 GSError RegisterReleaseListenerBackup(sptr<IProducerListener> listener) override; 82 GSError UnRegisterReleaseListener() override; 83 GSError UnRegisterReleaseListenerBackup() override; 84 GSError RegisterPropertyListener(sptr<IProducerListener> listener, uint64_t producerId) override; 85 GSError UnRegisterPropertyListener(uint64_t producerId) override; 86 87 GSError SetTransform(GraphicTransformType transform) override; 88 GSError GetTransform(GraphicTransformType &transform) override; 89 90 GSError Connect() override; 91 GSError Disconnect(uint32_t *bufSeqNum) override; 92 93 GSError SetScalingMode(uint32_t sequence, ScalingMode scalingMode) override; 94 GSError SetBufferHold(bool hold) override; 95 GSError SetBufferReallocFlag(bool flag) override; 96 GSError SetBufferName(const std::string &bufferName) override; 97 GSError SetMetaData(uint32_t sequence, const std::vector<GraphicHDRMetaData> &metaData) override; 98 GSError SetMetaDataSet(uint32_t sequence, GraphicHDRMetadataKey key, 99 const std::vector<uint8_t> &metaData) override; 100 GSError SetGlobalAlpha(int32_t alpha) override; 101 GSError SetRequestBufferNoblockMode(bool noblock) override; 102 GSError SetTunnelHandle(const GraphicExtDataHandle *handle) override; 103 GSError GetPresentTimestamp(uint32_t sequence, GraphicPresentTimestampType type, int64_t &time) override; 104 105 bool GetStatus() const; 106 void SetStatus(bool status); 107 108 sptr<NativeSurface> GetNativeSurface() override; 109 110 void OnBufferProducerRemoteDied(); 111 GSError AttachBufferToQueue(sptr<SurfaceBuffer> buffer) override; 112 GSError DetachBufferFromQueue(sptr<SurfaceBuffer> buffer) override; 113 114 GSError SetTransformHint(GraphicTransformType transformHint, uint64_t fromId) override; 115 GSError GetTransformHint(GraphicTransformType &transformHint) override; 116 GSError SetScalingMode(ScalingMode scalingMode) override; 117 118 GSError SetSurfaceSourceType(OHSurfaceSource sourceType) override; 119 GSError GetSurfaceSourceType(OHSurfaceSource &sourceType) override; 120 121 GSError SetSurfaceAppFrameworkType(std::string appFrameworkType) override; 122 GSError GetSurfaceAppFrameworkType(std::string &appFrameworkType) override; 123 124 GSError SetHdrWhitePointBrightness(float brightness) override; 125 GSError SetSdrWhitePointBrightness(float brightness) override; 126 127 GSError AcquireLastFlushedBuffer(sptr<SurfaceBuffer> &buffer, sptr<SyncFence> &fence, 128 float matrix[16], uint32_t matrixSize, bool isUseNewMatrix) override; 129 GSError ReleaseLastFlushedBuffer(uint32_t sequence) override; 130 GSError RequestAndDetachBuffer(const BufferRequestConfig& config, sptr<BufferExtraData>& bedata, 131 RequestBufferReturnValue& retval) override; 132 GSError AttachAndFlushBuffer(sptr<SurfaceBuffer>& buffer, sptr<BufferExtraData>& bedata, 133 const sptr<SyncFence>& fence, BufferFlushConfigWithDamages& config, bool needMap) override; 134 GSError GetCycleBuffersNumber(uint32_t& cycleBuffersNumber) override; 135 GSError SetCycleBuffersNumber(uint32_t cycleBuffersNumber) override; 136 GSError SetFrameGravity(int32_t frameGravity) override; 137 GSError SetFixedRotation(int32_t fixedRotation) override; 138 139 GSError ConnectStrictly() override; 140 GSError DisconnectStrictly() override; 141 GSError PreAllocBuffers(const BufferRequestConfig &config, uint32_t allocBufferCount) override; 142 GSError SetAlphaType(GraphicAlphaType alphaType) override; 143 private: 144 GSError CheckConnectLocked(); 145 GSError SetTunnelHandle(const sptr<SurfaceTunnelHandle> &handle); 146 bool HandleDeathRecipient(sptr<IRemoteObject> token); 147 148 int32_t GetProducerInitInfoRemote(MessageParcel &arguments, MessageParcel &reply, MessageOption &option); 149 int32_t RequestBufferRemote(MessageParcel &arguments, MessageParcel &reply, MessageOption &option); 150 int32_t RequestBuffersRemote(MessageParcel &arguments, MessageParcel &reply, MessageOption &option); 151 int32_t CancelBufferRemote(MessageParcel &arguments, MessageParcel &reply, MessageOption &option); 152 int32_t FlushBufferRemote(MessageParcel &arguments, MessageParcel &reply, MessageOption &option); 153 int32_t FlushBuffersRemote(MessageParcel &arguments, MessageParcel &reply, MessageOption &option); 154 int32_t AttachBufferRemote(MessageParcel &arguments, MessageParcel &reply, MessageOption &option); 155 int32_t DetachBufferRemote(MessageParcel &arguments, MessageParcel &reply, MessageOption &option); 156 int32_t GetQueueSizeRemote(MessageParcel &arguments, MessageParcel &reply, MessageOption &option); 157 int32_t SetQueueSizeRemote(MessageParcel &arguments, MessageParcel &reply, MessageOption &option); 158 int32_t GetNameRemote(MessageParcel &arguments, MessageParcel &reply, MessageOption &option); 159 int32_t GetDefaultWidthRemote(MessageParcel &arguments, MessageParcel &reply, MessageOption &option); 160 int32_t GetDefaultHeightRemote(MessageParcel &arguments, MessageParcel &reply, MessageOption &option); 161 int32_t SetDefaultUsageRemote(MessageParcel &arguments, MessageParcel &reply, MessageOption &option); 162 int32_t GetDefaultUsageRemote(MessageParcel &arguments, MessageParcel &reply, MessageOption &option); 163 int32_t GetUniqueIdRemote(MessageParcel &arguments, MessageParcel &reply, MessageOption &option); 164 int32_t CleanCacheRemote(MessageParcel &arguments, MessageParcel &reply, MessageOption &option); 165 int32_t RegisterReleaseListenerRemote(MessageParcel &arguments, MessageParcel &reply, MessageOption &option); 166 int32_t RegisterReleaseListenerBackupRemote(MessageParcel &arguments, MessageParcel &reply, 167 MessageOption &option); 168 int32_t UnRegisterReleaseListenerRemote(MessageParcel &arguments, MessageParcel &reply, MessageOption &option); 169 int32_t UnRegisterReleaseListenerBackupRemote(MessageParcel &arguments, MessageParcel &reply, 170 MessageOption &option); 171 int32_t SetTransformRemote(MessageParcel &arguments, MessageParcel &reply, MessageOption &option); 172 int32_t GetNameAndUniqueIdRemote(MessageParcel &arguments, MessageParcel &reply, MessageOption &option); 173 int32_t DisconnectRemote(MessageParcel &arguments, MessageParcel &reply, MessageOption &option); 174 int32_t ConnectRemote(MessageParcel &arguments, MessageParcel &reply, MessageOption &option); 175 int32_t SetScalingModeRemote(MessageParcel &arguments, MessageParcel &reply, MessageOption &option); 176 int32_t SetMetaDataRemote(MessageParcel &arguments, MessageParcel &reply, MessageOption &option); 177 int32_t SetMetaDataSetRemote(MessageParcel &arguments, MessageParcel &reply, MessageOption &option); 178 int32_t SetTunnelHandleRemote(MessageParcel &arguments, MessageParcel &reply, MessageOption &option); 179 int32_t GoBackgroundRemote(MessageParcel &arguments, MessageParcel &reply, MessageOption &option); 180 int32_t GetPresentTimestampRemote(MessageParcel &arguments, MessageParcel &reply, MessageOption &option); 181 int32_t GetLastFlushedBufferRemote(MessageParcel &arguments, MessageParcel &reply, MessageOption &option); 182 int32_t GetTransformRemote(MessageParcel &arguments, MessageParcel &reply, MessageOption &option); 183 int32_t AttachBufferToQueueRemote(MessageParcel &arguments, MessageParcel &reply, MessageOption &option); 184 int32_t DetachBufferFromQueueRemote(MessageParcel &arguments, MessageParcel &reply, MessageOption &option); 185 int32_t SetTransformHintRemote(MessageParcel &arguments, MessageParcel &reply, MessageOption &option); 186 int32_t GetTransformHintRemote(MessageParcel &arguments, MessageParcel &reply, MessageOption &option); 187 int32_t SetBufferHoldRemote(MessageParcel &arguments, MessageParcel &reply, MessageOption &option); 188 int32_t SetBufferReallocFlagRemote(MessageParcel &arguments, MessageParcel &reply, MessageOption &option); 189 int32_t SetBufferNameRemote(MessageParcel &arguments, MessageParcel &reply, MessageOption &option); 190 int32_t SetSurfaceSourceTypeRemote(MessageParcel &arguments, MessageParcel &reply, MessageOption &option); 191 int32_t GetSurfaceSourceTypeRemote(MessageParcel &arguments, MessageParcel &reply, MessageOption &option); 192 int32_t SetSurfaceAppFrameworkTypeRemote(MessageParcel &arguments, MessageParcel &reply, MessageOption &option); 193 int32_t GetSurfaceAppFrameworkTypeRemote(MessageParcel &arguments, MessageParcel &reply, MessageOption &option); 194 195 int32_t SetScalingModeV2Remote(MessageParcel &arguments, MessageParcel &reply, MessageOption &option); 196 int32_t SetHdrWhitePointBrightnessRemote(MessageParcel &arguments, MessageParcel &reply, MessageOption &option); 197 int32_t SetSdrWhitePointBrightnessRemote(MessageParcel &arguments, MessageParcel &reply, MessageOption &option); 198 int32_t AcquireLastFlushedBufferRemote(MessageParcel &arguments, MessageParcel &reply, MessageOption &option); 199 int32_t ReleaseLastFlushedBufferRemote(MessageParcel &arguments, MessageParcel &reply, MessageOption &option); 200 int32_t SetGlobalAlphaRemote(MessageParcel &arguments, MessageParcel &reply, MessageOption &option); 201 int32_t SetRequestBufferNoblockModeRemote(MessageParcel &arguments, MessageParcel &reply, MessageOption &option); 202 int32_t RequestAndDetachBufferRemote(MessageParcel &arguments, MessageParcel &reply, MessageOption &option); 203 int32_t AttachAndFlushBufferRemote(MessageParcel &arguments, MessageParcel &reply, MessageOption &option); 204 int32_t GetRotatingBuffersNumberRemote(MessageParcel &arguments, MessageParcel &reply, MessageOption &option); 205 int32_t SetRotatingBuffersNumberRemote(MessageParcel &arguments, MessageParcel &reply, MessageOption &option); 206 int32_t SetFrameGravityRemote(MessageParcel &arguments, MessageParcel &reply, MessageOption &option); 207 int32_t SetFixedRotationRemote(MessageParcel &arguments, MessageParcel &reply, MessageOption &option); 208 int32_t DisconnectStrictlyRemote(MessageParcel &arguments, MessageParcel &reply, MessageOption &option); 209 int32_t ConnectStrictlyRemote(MessageParcel &arguments, MessageParcel &reply, MessageOption &option); 210 int32_t RegisterPropertyListenerRemote(MessageParcel &arguments, MessageParcel &reply, MessageOption &option); 211 int32_t UnRegisterPropertyListenerRemote(MessageParcel &arguments, MessageParcel &reply, MessageOption &option); 212 int32_t SetLppShareFdRemote(MessageParcel &arguments, MessageParcel &reply, MessageOption &option); 213 int32_t SetAlphaTypeRemote(MessageParcel &arguments, MessageParcel &reply, MessageOption &option); 214 215 void SetConnectedPidLocked(int32_t connectedPid); 216 int32_t AttachBufferToQueueReadBuffer(MessageParcel &arguments, 217 MessageParcel &reply, MessageOption &option, sptr<SurfaceBuffer> &buffer); 218 bool CheckIsAlive(); 219 int32_t PreAllocBuffersRemote(MessageParcel &arguments, MessageParcel &reply, MessageOption &option); 220 221 static const std::map<uint32_t, std::function<int32_t(BufferQueueProducer *that, MessageParcel &arguments, 222 MessageParcel &reply, MessageOption &option)>> memberFuncMap_; 223 224 class ProducerSurfaceDeathRecipient : public IRemoteObject::DeathRecipient { 225 public: 226 explicit ProducerSurfaceDeathRecipient(wptr<BufferQueueProducer> producer); 227 virtual ~ProducerSurfaceDeathRecipient() = default; 228 229 void OnRemoteDied(const wptr<IRemoteObject>& remoteObject) override; 230 private: 231 wptr<BufferQueueProducer> producer_; 232 std::string name_ = "DeathRecipient"; 233 }; 234 sptr<ProducerSurfaceDeathRecipient> producerSurfaceDeathRecipient_ = nullptr; 235 sptr<IRemoteObject> token_ = nullptr; 236 237 int32_t connectedPid_ = 0; 238 bool isDisconnectStrictly_ = false; 239 sptr<BufferQueue> bufferQueue_ = nullptr; 240 std::string name_ = "not init"; 241 std::mutex mutex_; 242 uint64_t uniqueId_ = 0; 243 static const uint32_t MAGIC_INIT = 0x16273849; 244 uint32_t magicNum_ = MAGIC_INIT; 245 }; 246 }; // namespace OHOS 247 248 #endif // FRAMEWORKS_SURFACE_INCLUDE_BUFFER_QUEUE_PRODUCER_H 249