• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 FRAMEWORKS_SURFACE_INCLUDE_BUFFER_QUEUE_H
17 #define FRAMEWORKS_SURFACE_INCLUDE_BUFFER_QUEUE_H
18 
19 #include <map>
20 #include <list>
21 #include <vector>
22 #include <mutex>
23 
24 #include <ibuffer_consumer_listener.h>
25 #include <ibuffer_producer.h>
26 #include "iconsumer_surface.h"
27 #include "surface_type.h"
28 #include <surface_tunnel_handle.h>
29 #include "surface_buffer.h"
30 #include "consumer_surface_delegator.h"
31 
32 namespace OHOS {
33 enum BufferState {
34     BUFFER_STATE_RELEASED,
35     BUFFER_STATE_REQUESTED,
36     BUFFER_STATE_FLUSHED,
37     BUFFER_STATE_ACQUIRED,
38     BUFFER_STATE_ATTACHED,
39 };
40 
41 enum InvokerType {
42     PRODUCER_INVOKER,
43     CONSUMER_INVOKER,
44 };
45 
46 using BufferElement = struct BufferElement {
47     sptr<SurfaceBuffer> buffer;
48     BufferState state;
49     bool isDeleting;
50 
51     BufferRequestConfig config;
52     sptr<SyncFence> fence;
53     int64_t timestamp;
54     std::vector<Rect> damages;
55     HDRMetaDataType hdrMetaDataType = HDRMetaDataType::HDR_NOT_USED;
56     std::vector<GraphicHDRMetaData> metaData;
57     GraphicHDRMetadataKey key;
58     std::vector<uint8_t> metaDataSet;
59     GraphicPresentTimestamp presentTimestamp = {GRAPHIC_DISPLAY_PTS_UNSUPPORTED, 0};
60     /**
61      * The desired time to present the buffer in nanoseconds.
62      * The buffer should wait until desiredPresentTimestamp is reached before being consumed and displayed.
63      * If multiple buffers reach desiredPresentTimestamp, the earlier buffer should be dropped.
64      */
65     int64_t desiredPresentTimestamp;
66     /**
67      * The desiredPresentTimestamp is automatically generated by the system, isAutoTimestamp is true.
68      * The desiredPresentTimestamp is manually set by the producer, isAutoTimestamp is false.
69      */
70     bool isAutoTimestamp;
71     /**
72      * lastAcquireTime is the time when this buffer was acquired last time through AcquireBuffer interface.
73      */
74     int64_t lastAcquireTime;
75 };
76 
77 using BufferAndFence = std::pair<sptr<SurfaceBuffer>, sptr<SyncFence>>;
78 
79 class BufferQueue : public RefBase {
80 public:
81     BufferQueue(const std::string &name);
82     virtual ~BufferQueue();
83 
84     GSError GetProducerInitInfo(ProducerInitInfo &info);
85 
86     GSError RequestBuffer(const BufferRequestConfig &config, sptr<BufferExtraData> &bedata,
87                           struct IBufferProducer::RequestBufferReturnValue &retval);
88 
89     GSError ReuseBuffer(const BufferRequestConfig &config, sptr<BufferExtraData> &bedata,
90                         struct IBufferProducer::RequestBufferReturnValue &retval, std::unique_lock<std::mutex> &lock);
91 
92     GSError CancelBuffer(uint32_t sequence, sptr<BufferExtraData> bedata);
93 
94     GSError FlushBuffer(uint32_t sequence, sptr<BufferExtraData> bedata,
95                         sptr<SyncFence> fence, const BufferFlushConfigWithDamages &config);
96 
97     GSError DoFlushBuffer(uint32_t sequence, sptr<BufferExtraData> bedata,
98         sptr<SyncFence> fence, const BufferFlushConfigWithDamages &config);
99 
100     GSError GetLastFlushedBuffer(sptr<SurfaceBuffer>& buffer, sptr<SyncFence>& fence,
101         float matrix[16], uint32_t matrixSize, bool isUseNewMatrix, bool needRecordSequence = false);
102 
103     GSError AcquireBuffer(sptr<SurfaceBuffer>& buffer, sptr<SyncFence>& fence,
104                           int64_t &timestamp, std::vector<Rect> &damages);
105     GSError AcquireBuffer(IConsumerSurface::AcquireBufferReturnValue &returnValue, int64_t expectPresentTimestamp,
106                           bool isUsingAutoTimestamp);
107     GSError ReleaseBuffer(sptr<SurfaceBuffer>& buffer, const sptr<SyncFence>& fence);
108 
109     GSError AttachBuffer(sptr<SurfaceBuffer>& buffer, int32_t timeOut);
110 
111     GSError DetachBuffer(sptr<SurfaceBuffer>& buffer);
112 
113     GSError RegisterSurfaceDelegator(sptr<IRemoteObject> client, sptr<Surface> cSurface);
114 
115     bool QueryIfBufferAvailable();
116 
117     uint32_t GetQueueSize();
118     GSError SetQueueSize(uint32_t queueSize);
119 
120     GSError GetName(std::string &name);
121 
122     GSError RegisterConsumerListener(sptr<IBufferConsumerListener>& listener);
123     GSError RegisterConsumerListener(IBufferConsumerListenerClazz *listener);
124     GSError RegisterReleaseListener(OnReleaseFunc func);
125     GSError RegisterProducerReleaseListener(sptr<IProducerListener> listener);
126     GSError RegisterProducerReleaseListenerBackup(sptr<IProducerListener> listener);
127     GSError UnRegisterProducerReleaseListener();
128     GSError UnRegisterProducerReleaseListenerBackup();
129     GSError RegisterDeleteBufferListener(OnDeleteBufferFunc func, bool isForUniRedraw = false);
130     GSError UnregisterConsumerListener();
131     GSError RegisterProducerPropertyListener(sptr<IProducerListener> listener, uint64_t producerId);
132     GSError UnRegisterProducerPropertyListener(uint64_t producerId);
133 
134     GSError SetDefaultWidthAndHeight(int32_t width, int32_t height);
135     int32_t GetDefaultWidth();
136     int32_t GetDefaultHeight();
137     GSError SetDefaultUsage(uint64_t usage);
138     uint64_t GetDefaultUsage();
139 
140     GSError CleanCache(bool cleanAll, uint32_t *bufSeqNum);
141     GSError GoBackground();
142     GSError OnConsumerDied();
143 
144     uint64_t GetUniqueId() const;
145 
146     void Dump(std::string &result);
147 
148     GSError SetTransform(GraphicTransformType transform);
149     GraphicTransformType GetTransform() const;
150 
151     GSError SetBufferHold(bool hold);
152     GSError SetBufferName(const std::string &bufferName);
IsBufferHold()153     inline bool IsBufferHold()
154     {
155         return isBufferHold_;
156     }
157     GSError SetScalingMode(uint32_t sequence, ScalingMode scalingMode);
158     GSError GetScalingMode(uint32_t sequence, ScalingMode &scalingMode);
159     GSError SetMetaData(uint32_t sequence, const std::vector<GraphicHDRMetaData> &metaData);
160     GSError SetMetaDataSet(uint32_t sequence, GraphicHDRMetadataKey key,
161                            const std::vector<uint8_t> &metaData);
162     GSError QueryMetaDataType(uint32_t sequence, HDRMetaDataType &type);
163     GSError GetMetaData(uint32_t sequence, std::vector<GraphicHDRMetaData> &metaData);
164     GSError GetMetaDataSet(uint32_t sequence, GraphicHDRMetadataKey &key,
165                            std::vector<uint8_t> &metaData);
166     GSError SetTunnelHandle(const sptr<SurfaceTunnelHandle> &handle);
167     sptr<SurfaceTunnelHandle> GetTunnelHandle();
168     GSError SetPresentTimestamp(uint32_t sequence, const GraphicPresentTimestamp &timestamp);
169     GSError GetPresentTimestamp(uint32_t sequence, GraphicPresentTimestampType type, int64_t &time);
170 
171     bool GetStatus() const;
172     void SetStatus(bool status);
173 
174     void SetBatchHandle(bool batch);
175 
176     GSError SetProducerCacheCleanFlag(bool flag);
ConsumerRequestCpuAccess(bool on)177     inline void ConsumerRequestCpuAccess(bool on)
178     {
179         isCpuAccessable_ = on;
180     }
181 
182     GSError AttachBufferToQueue(sptr<SurfaceBuffer> buffer, InvokerType invokerType);
183     GSError DetachBufferFromQueue(sptr<SurfaceBuffer> buffer, InvokerType invokerType, bool isReserveSlot);
184 
185     GSError SetTransformHint(GraphicTransformType transformHint, uint64_t fromId);
186     GSError SetTransformHint(GraphicTransformType transformHint);
187     GraphicTransformType GetTransformHint() const;
188     GSError SetScalingMode(ScalingMode scalingMode);
189 
190     GSError SetSurfaceSourceType(OHSurfaceSource sourceType);
191     OHSurfaceSource GetSurfaceSourceType() const;
192 
193     GSError SetSurfaceAppFrameworkType(std::string appFrameworkType);
194     std::string GetSurfaceAppFrameworkType() const;
195 
196     GSError SetHdrWhitePointBrightness(float brightness);
197     GSError SetSdrWhitePointBrightness(float brightness);
198     float GetHdrWhitePointBrightness() const;
199     float GetSdrWhitePointBrightness() const;
200 
201     GSError IsSurfaceBufferInCache(uint32_t seqNum, bool &isInCache);
202 
203     GSError AcquireLastFlushedBuffer(sptr<SurfaceBuffer> &buffer, sptr<SyncFence> &fence,
204         float matrix[16], uint32_t matrixSize, bool isUseNewMatrix);
205     GSError ReleaseLastFlushedBuffer(uint32_t sequence);
206     GSError SetGlobalAlpha(int32_t alpha);
207     GSError GetGlobalAlpha(int32_t &alpha);
208     uint32_t GetAvailableBufferCount();
209 
210     void SetConnectedPid(int32_t connectedPid);
211     GSError RequestAndDetachBuffer(const BufferRequestConfig& config, sptr<BufferExtraData>& bedata,
212         struct IBufferProducer::RequestBufferReturnValue& retval);
213     GSError AttachAndFlushBuffer(sptr<SurfaceBuffer>& buffer, sptr<BufferExtraData>& bedata,
214         const sptr<SyncFence>& fence, BufferFlushConfigWithDamages& config, bool needMap);
215     GSError GetLastFlushedDesiredPresentTimeStamp(int64_t &lastFlushedDesiredPresentTimeStamp);
216     GSError GetBufferSupportFastCompose(bool &bufferSupportFastCompose);
217     GSError GetBufferCacheConfig(const sptr<SurfaceBuffer>& buffer, BufferRequestConfig& config);
218     GSError GetCycleBuffersNumber(uint32_t& cycleBuffersNumber);
219     GSError SetCycleBuffersNumber(uint32_t cycleBuffersNumber);
220     GSError ConnectStrictly();
221     GSError DisconnectStrictly();
222     GSError GetLastConsumeTime(int64_t &lastConsumeTime);
223 private:
224     GSError AllocBuffer(sptr<SurfaceBuffer>& buffer, const BufferRequestConfig &config,
225         std::unique_lock<std::mutex> &lock);
226     void DeleteBufferInCache(uint32_t sequence, std::unique_lock<std::mutex> &lock);
227 
228     uint32_t GetUsedSize();
229     void DeleteBuffersLocked(int32_t count, std::unique_lock<std::mutex> &lock);
230 
231     GSError PopFromFreeListLocked(sptr<SurfaceBuffer>& buffer, const BufferRequestConfig &config);
232     GSError PopFromDirtyListLocked(sptr<SurfaceBuffer>& buffer);
233 
234     GSError CheckRequestConfig(const BufferRequestConfig &config);
235     GSError CheckFlushConfig(const BufferFlushConfigWithDamages &config);
236     void DumpCache(std::string &result);
237     void DumpMetadata(std::string &result, BufferElement element);
238     void ClearLocked(std::unique_lock<std::mutex> &lock);
239     bool CheckProducerCacheListLocked();
240     GSError SetProducerCacheCleanFlagLocked(bool flag, std::unique_lock<std::mutex> &lock);
241     GSError AttachBufferUpdateStatus(std::unique_lock<std::mutex> &lock, uint32_t sequence, int32_t timeOut);
242     void AttachBufferUpdateBufferInfo(sptr<SurfaceBuffer>& buffer, bool needMap);
243     void ListenerBufferReleasedCb(sptr<SurfaceBuffer> &buffer, const sptr<SyncFence> &fence);
244     void OnBufferDeleteCbForHardwareThreadLocked(const sptr<SurfaceBuffer> &buffer) const;
245     GSError CheckBufferQueueCache(uint32_t sequence);
246     GSError ReallocBufferLocked(const BufferRequestConfig &config,
247         struct IBufferProducer::RequestBufferReturnValue &retval, std::unique_lock<std::mutex> &lock);
248     void SetSurfaceBufferHebcMetaLocked(sptr<SurfaceBuffer> buffer);
249     GSError RequestBufferCheckStatus();
250     GSError DelegatorQueueBuffer(uint32_t sequence, sptr<SyncFence> fence);
251     bool WaitForCondition();
252     void RequestBufferDebugInfoLocked();
253     bool GetStatusLocked() const;
254     void CallConsumerListener();
255     void SetSurfaceBufferGlobalAlphaUnlocked(sptr<SurfaceBuffer> buffer);
256     void LogAndTraceAllBufferInBufferQueueCache();
257     bool IsPresentTimestampReady(int64_t desiredPresentTimestamp, int64_t expectPresentTimestamp);
258     void SetDesiredPresentTimestampAndUiTimestamp(uint32_t sequence, int64_t desiredPresentTimestamp,
259                                                   uint64_t uiTimestamp);
260     void DropFirstDirtyBuffer(BufferElement &frontBufferElement, BufferElement &secondBufferElement,
261                               int64_t &frontDesiredPresentTimestamp, bool &frontIsAutoTimestamp,
262                               std::vector<BufferAndFence> &dropBuffers);
263     void ReleaseDropBuffers(std::vector<BufferAndFence> &dropBuffers);
264     void OnBufferDeleteForRS(uint32_t sequence);
265     void DeleteBufferInCacheNoWaitForAllocatingState(uint32_t sequence);
266     void AddDeletingBuffersLocked(std::vector<uint32_t> &deletingBuffers);
267     GSError DetachBufferFromQueueLocked(uint32_t sequence, InvokerType invokerType,
268         std::unique_lock<std::mutex> &lock, bool isReserveSlot);
269     GSError AttachBufferToQueueLocked(sptr<SurfaceBuffer> buffer, InvokerType invokerType, bool needMap);
270     GSError FlushBufferImprovedLocked(uint32_t sequence, sptr<BufferExtraData> &bedata,
271         const sptr<SyncFence> &fence, const BufferFlushConfigWithDamages &config, std::unique_lock<std::mutex> &lock);
272     GSError CheckBufferQueueCacheLocked(uint32_t sequence);
273     GSError DoFlushBufferLocked(uint32_t sequence, sptr<BufferExtraData> bedata,
274         sptr<SyncFence> fence, const BufferFlushConfigWithDamages &config, std::unique_lock<std::mutex> &lock);
275     GSError RequestBufferLocked(const BufferRequestConfig &config, sptr<BufferExtraData> &bedata,
276         struct IBufferProducer::RequestBufferReturnValue &retval, std::unique_lock<std::mutex> &lock);
277     GSError CancelBufferLocked(uint32_t sequence, sptr<BufferExtraData> bedata);
278     void DumpPropertyListener();
279 
280     int32_t defaultWidth_ = 0;
281     int32_t defaultHeight_ = 0;
282     uint64_t defaultUsage_ = 0;
283     uint32_t bufferQueueSize_ = SURFACE_DEFAULT_QUEUE_SIZE;
284     ScalingMode scalingMode_ = ScalingMode::SCALING_MODE_SCALE_TO_WINDOW;
285     GraphicTransformType transform_ = GraphicTransformType::GRAPHIC_ROTATE_NONE;
286     GraphicTransformType lastFlushedTransform_ = GraphicTransformType::GRAPHIC_ROTATE_NONE;
287     std::string name_;
288     std::list<uint32_t> freeList_;
289     std::list<uint32_t> dirtyList_;
290     std::list<uint32_t> deletingList_;
291     std::list<uint32_t> producerCacheList_;
292     std::map<uint32_t, BufferElement> bufferQueueCache_;
293     sptr<IBufferConsumerListener> listener_ = nullptr;
294     IBufferConsumerListenerClazz *listenerClazz_ = nullptr;
295     mutable std::mutex mutex_;
296     std::mutex listenerMutex_;
297     std::mutex producerListenerMutex_;
298     std::mutex propertyChangeMutex_;
299     const uint64_t uniqueId_;
300     std::string bufferName_ = "";
301     OnReleaseFunc onBufferRelease_ = nullptr;
302     std::mutex onBufferReleaseMutex_;
303     sptr<IProducerListener> producerListener_ = nullptr;
304     sptr<IProducerListener> producerListenerBackup_ = nullptr;
305     const size_t propertyChangeListenerMaxNum_ = 50; // 50 : limit producer num
306     std::map<uint64_t, sptr<IProducerListener>> propertyChangeListeners_;
307     OnDeleteBufferFunc onBufferDeleteForRSMainThread_;
308     OnDeleteBufferFunc onBufferDeleteForRSHardwareThread_;
309     std::condition_variable waitReqCon_;
310     std::condition_variable waitAttachCon_;
311     sptr<SurfaceTunnelHandle> tunnelHandle_ = nullptr;
312     bool isValidStatus_ = true;
313     bool producerCacheClean_ = false;
314     const bool isLocalRender_;
315     uint32_t lastFlusedSequence_ = 0;
316     sptr<SyncFence> lastFlusedFence_;
317     wptr<ConsumerSurfaceDelegator> wpCSurfaceDelegator_;
318     bool isCpuAccessable_ = false;
319     GraphicTransformType transformHint_ = GraphicTransformType::GRAPHIC_ROTATE_NONE;
320     bool isBufferHold_ = false;
321     bool isBatch_ = false;
322     OHSurfaceSource sourceType_ = OHSurfaceSource::OH_SURFACE_SOURCE_DEFAULT;
323     std::string appFrameworkType_ = "";
324     float hdrWhitePointBrightness_ = 0.0;
325     float sdrWhitePointBrightness_ = 0.0;
326     uint32_t acquireLastFlushedBufSequence_;
327     int32_t globalAlpha_ = -1;
328     std::mutex globalAlphaMutex_;
329     std::string requestBufferStateStr_;
330     std::string acquireBufferStateStr_;
331     int32_t connectedPid_ = 0;
332     bool isAllocatingBuffer_ = false;
333     std::condition_variable isAllocatingBufferCon_;
334     int64_t lastFlushedDesiredPresentTimeStamp_ = 0;
335     bool bufferSupportFastCompose_ = false;
336     uint32_t rotatingBufferNumber_ = 0;
337     uint32_t detachReserveSlotNum_ = 0;
338     int64_t lastConsumeTime_ = 0;
339 };
340 }; // namespace OHOS
341 
342 #endif // FRAMEWORKS_SURFACE_INCLUDE_BUFFER_QUEUE_H
343