• 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 <surface_type.h>
27 #include <buffer_manager.h>
28 
29 #include "surface_buffer_impl.h"
30 
31 namespace OHOS {
32 enum BufferState {
33     BUFFER_STATE_RELEASED,
34     BUFFER_STATE_REQUESTED,
35     BUFFER_STATE_FLUSHED,
36     BUFFER_STATE_ACQUIRED,
37     BUFFER_STATE_ATTACHED,
38 };
39 
40 typedef struct {
41     sptr<SurfaceBufferImpl> buffer;
42     BufferState state;
43     bool isDeleting;
44 
45     BufferRequestConfig config;
46     int32_t fence;
47     int64_t timestamp;
48     Rect damage;
49 } BufferElement;
50 
51 class BufferQueue : public RefBase {
52 public:
53     BufferQueue(const std::string &name, bool isShared = false);
54     virtual ~BufferQueue();
55     GSError Init();
56 
57     GSError RequestBuffer(const BufferRequestConfig &config, BufferExtraData &bedata,
58                                struct IBufferProducer::RequestBufferReturnValue &retval);
59 
60     GSError ReuseBuffer(const BufferRequestConfig &config, BufferExtraData &bedata,
61                              struct IBufferProducer::RequestBufferReturnValue &retval);
62 
63     GSError CancelBuffer(int32_t sequence, const BufferExtraData &bedata);
64 
65     GSError FlushBuffer(int32_t sequence, const BufferExtraData &bedata,
66                              int32_t fence, const BufferFlushConfig &config);
67 
68     GSError DoFlushBuffer(int32_t sequence, const BufferExtraData &bedata,
69                                int32_t fence, const BufferFlushConfig &config);
70 
71     GSError AcquireBuffer(sptr<SurfaceBufferImpl>& buffer, int32_t &fence,
72                                int64_t &timestamp, Rect &damage);
73     GSError ReleaseBuffer(sptr<SurfaceBufferImpl>& buffer, int32_t fence);
74 
75     GSError AttachBuffer(sptr<SurfaceBufferImpl>& buffer);
76 
77     GSError DetachBuffer(sptr<SurfaceBufferImpl>& buffer);
78 
79     uint32_t GetQueueSize();
80     GSError SetQueueSize(uint32_t queueSize);
81 
82     GSError GetName(std::string &name);
83 
84     GSError RegisterConsumerListener(sptr<IBufferConsumerListener>& listener);
85     GSError RegisterConsumerListener(IBufferConsumerListenerClazz *listener);
86     GSError RegisterReleaseListener(OnReleaseFunc func);
87     GSError UnregisterConsumerListener();
88 
89     GSError SetDefaultWidthAndHeight(int32_t width, int32_t height);
90     int32_t GetDefaultWidth();
91     int32_t GetDefaultHeight();
92     GSError SetDefaultUsage(uint32_t usage);
93     uint32_t GetDefaultUsage();
94 
95     GSError CleanCache();
96 
97     uint64_t GetUniqueId() const;
98 
99     void Dump(std::string &result);
100 
101 private:
102     GSError AllocBuffer(sptr<SurfaceBufferImpl>& buffer, const BufferRequestConfig &config);
103     GSError FreeBuffer(sptr<SurfaceBufferImpl>& buffer);
104     void DeleteBufferInCache(int sequence);
105     void DumpToFile(int32_t sequence);
106 
107     uint32_t GetUsedSize();
108     void DeleteBuffers(int32_t count);
109 
110     GSError PopFromFreeList(sptr<SurfaceBufferImpl>& buffer, const BufferRequestConfig &config);
111     GSError PopFromDirtyList(sptr<SurfaceBufferImpl>& buffer);
112 
113     GSError CheckRequestConfig(const BufferRequestConfig &config);
114     GSError CheckFlushConfig(const BufferFlushConfig &config);
115     void DumpCache(std::string &result);
116 
117     int32_t defaultWidth = 0;
118     int32_t defaultHeight = 0;
119     uint32_t defaultUsage = 0;
120     uint32_t queueSize_ = SURFACE_DEFAULT_QUEUE_SIZE;
121     std::string name_;
122     std::list<int32_t> freeList_;
123     std::list<int32_t> dirtyList_;
124     std::list<int32_t> deletingList_;
125     std::map<int32_t, BufferElement> bufferQueueCache_;
126     sptr<IBufferConsumerListener> listener_ = nullptr;
127     IBufferConsumerListenerClazz *listenerClazz_ = nullptr;
128     std::mutex mutex_;
129     const uint64_t uniqueId_;
130     sptr<BufferManager> bufferManager_ = nullptr;
131     OnReleaseFunc onBufferRelease = nullptr;
132     bool isShared_ = false;
133     std::condition_variable waitReqCon_;
134 };
135 }; // namespace OHOS
136 
137 #endif // FRAMEWORKS_SURFACE_INCLUDE_BUFFER_QUEUE_H
138