1 /*
2  * Copyright (c) 2020-2022 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 GRAPHIC_LITE_BUFFER_QUEUE_H
17 #define GRAPHIC_LITE_BUFFER_QUEUE_H
18 
19 #include <list>
20 #include <map>
21 #include "surface_buffer_impl.h"
22 
23 namespace OHOS {
24 const static int8_t SURFACE_MAX_PLANE_NUM = 4;
25 struct PlaneInfo {
26     uint32_t stride;
27     uint32_t offset;
28     uint32_t size;
29 };
30 
31 enum PLANE_COUNT {
32     IMAGE_PIXEL_FORMAT_PLANE_COUNT_RGB = 1,
33     IMAGE_PIXEL_FORMAT_PLANE_COUNT_YUVSPXX,
34     IMAGE_PIXEL_FORMAT_PLANE_COUNT_YUV4XX
35 };
36 
37 class BufferQueue {
38 public:
39     /**
40      * @brief Surface Constructor. Store all buffers and manager all buffers state.
41      */
42     BufferQueue();
43 
44     /**
45      * @brief BufferQueue Destructor. Free all buffers.
46      */
47     ~BufferQueue();
48 
49     /**
50      * @brief Request buffer. BufferQueue deuque buffer, If free list has buffer, pop and return the buffer.
51      *        If no buffer in free list, and attach count less than queue size, allocate new one .
52      *         Surface producer requests buffer.
53      * @param [in] whether waiting or not.
54      *        wait = 1. waiting util free list has buffer, pop and return it.
55      *        wait = 0. No wait, could return null pointer.
56      * @returns buffer pointer.
57      */
58     SurfaceBufferImpl* RequestBuffer(uint8_t wait);
59 
60     /**
61      * @brief Flush buffer to dirty list, for consumer acquire. When producer flush buffer, buffer
62      *        will push to dirty list, and call back to consumer that buffer is available to acquire.
63      * @param [in] SurfaceBufferImpl, Which buffer could acquire for consumer.
64      * @returns Flush buffer succeed or not.
65      *        0 is succeed; other is failed.
66      */
67     int32_t FlushBuffer(SurfaceBufferImpl& buffer);
68 
69     /**
70      * @brief Acquire buffer. Consumer acquire buffer, which producer has flush and push to free list.
71      * @returns buffer pointer.
72      */
73     SurfaceBufferImpl* AcquireBuffer();
74 
75     /**
76      * @brief Release buffer. Consumer release buffer, which will push to free list for producer request it.
77      * @param [in] SurfaceBufferImpl pointer, Which buffer need to release.
78      * @returns Release buffer succeed or not.
79      *        0 is succeed; other is failed.
80      */
81     bool ReleaseBuffer(const SurfaceBufferImpl& buffer);
82 
83     /**
84      * @brief Cancel buffer. Producer cancel this buffer, buffer will push back to free list for request it again.
85      * @param [in] SurfaceBufferImpl, Which buffer will push back to free list for request it.
86      */
87     int32_t CancelBuffer(const SurfaceBufferImpl& buffer);
88 
89     /**
90      * @brief Set queue size, alloc max buffer count.
91      *        Default is 1. Max count is 10.
92      * @param [in] queueSize. Could alloc buffer count.
93      */
94     void SetQueueSize(uint8_t queueSize);
95 
96     /**
97      * @brief Get queue size, alloc max buffer count.
98      * @returns queue size.
99      */
100     uint8_t GetQueueSize();
101 
102     /**
103      * @brief Set width and height to calculate the buffer size.
104      * @param [in] width, Buffer width.
105      * @param [in] height, Buffer height.
106      */
107     void SetWidthAndHeight(uint32_t width, uint32_t height);
108 
109     /**
110      * @brief Get width, buffer width to calculate the buffer size..
111      * @returns width, Buffer width.
112      */
113     int32_t GetWidth();
114 
115     /**
116      * @brief Get height, buffer height to calculate the buffer size..
117      * @returns height, Buffer height.
118      */
119     int32_t GetHeight();
120 
121     /**
122      * @brief Set format, to calculate the buffer size.
123      *        Default is IMAGE_PIXEL_FORMAT_RGB565. See all formats in OHOS::ImageFormat
124      * @param [in] format, Buffer format.
125      */
126     void SetFormat(uint32_t format);
127 
128     /**
129      * @brief Get format, buffer format to calculate the buffer size..
130      * @returns format, Buffer format.
131      */
132     int32_t GetFormat();
133 
134     /**
135      * @brief Set stride alignment bytes. Default alignment is 4 bytes.
136      * @param [in] strideAlignment, Buffer stride alignment
137      */
138     void SetStrideAlignment(uint32_t stride);
139 
140     /**
141      * @brief Get stride alignment bytes. Default alignment is 4 bytes.
142      * @returns strideAlignment, Buffer stride alignment.
143      */
144     int32_t GetStrideAlignment();
145 
146     /**
147      * @brief Get bytes of one stride which calculate by width, format and stride alignment.
148      * @returns The stride
149      */
150     int32_t GetStride();
151 
152     /**
153      * @brief Set buffer size. Alloc buffer size, no need to calculate by width, height, format...
154      * @param [in] The buffer size
155      */
156     void SetSize(uint32_t size);
157 
158     /**
159      * @brief Get buffer size. Alloc buffer size.
160      *        The size is setted by SetSize() or calculated by width, height, format...
161      * @returns The buffer size.
162      */
163     int32_t GetSize();
164 
165     /**
166      * @brief Set buffer usage. Surface alloc physical or virtual memory buffer.
167      *        Support usage see detail in OHOS::BUFFER_CONSUMER_USAGE.
168      *        Default is BUFFER_CONSUMER_USAGE_SORTWARE, which will alloc virtual memory buffer.
169      * @param [in] The buffer usage.
170      */
171     void SetUsage(uint32_t usage);
172 
173     /**
174      * @brief Get buffer usage. Surface alloc physical or virtual memory buffer.
175      *        All usage sees detail in OHOS::BUFFER_CONSUMER_USAGE.
176      * @returns The buffer usage.
177      */
178     int32_t GetUsage();
179 
180     /**
181      * @brief Set user data. Construct a local map to store all the user-data.
182      * @param [in] key.
183      * @param [in] value.
184      */
185     void SetUserData(const std::string& key, const std::string& value);
186 
187     /**
188      * @brief Get user data. Get the value from local map.
189      * @returns value refers to the key.
190      */
191     std::string GetUserData(const std::string& key);
192 
193     /**
194      * @brief Buffer queue init succeed or not.
195      * @returns Whether init or not.
196      */
197     bool Init();
198 
199 private:
200     bool CanRequest(uint8_t wait);
201     int32_t isValidAttr(uint32_t width, uint32_t height, uint32_t format, uint32_t strideAlignment);
202     int32_t Reset(uint32_t size = 0);
203     void NeedAttach();
204     void Detach(SurfaceBufferImpl* buffer);
205     SurfaceBufferImpl* GetBuffer(const SurfaceBufferImpl& buffer);
206     int32_t ReleaseBuffer(const SurfaceBufferImpl& buffer, BufferState state);
207     uint32_t width_;
208     uint32_t height_;
209     uint32_t format_;
210     uint32_t stride_;
211     uint32_t usage_;
212     uint32_t size_;
213     uint8_t queueSize_;
214     uint32_t strideAlignment_;
215     uint8_t attachCount_;
216     bool customSize_;
217     std::list<SurfaceBufferImpl *> freeList_;
218     std::list<SurfaceBufferImpl *> dirtyList_;
219     std::list<SurfaceBufferImpl *> allBuffers_;
220     pthread_mutex_t lock_;
221     pthread_cond_t freeCond_;
222     std::map<std::string, std::string> usrDataMap_;
223 };
224 } // end namespace
225 #endif
226