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_SURFACE_IMPL_H
17#define GRAPHIC_LITE_SURFACE_IMPL_H
18
19#include "buffer_producer.h"
20#include "buffer_queue_consumer.h"
21#include "ibuffer_consumer_listener.h"
22#include "surface.h"
23#include "surface_buffer.h"
24#include "surface_type.h"
25
26namespace OHOS {
27/**
28 * @brief SurfaceImpl Object. Provide the shared memory ability.
29 *        Supported allocate physical and virtual memory.
30 *        Using in multi media and graphic for multi process.
31 */
32class SurfaceImpl : public Surface {
33public:
34    /**
35     * @brief Generic Surface from ipc io, used for producer on multi process.
36     * @param [in] IpcIo io. Consumer surface sid, ipc sending request param.
37     * @returns Surface pointer.
38     */
39    static Surface* GenericSurfaceByIpcIo(IpcIo& io);
40
41    /**
42     * @brief Surface Constructor, used for consumer.
43     * @returns Surface pointer.
44     */
45    SurfaceImpl();
46
47    /**
48     * @brief Lite Surface Destructor. Free all buffers.
49     */
50    ~SurfaceImpl();
51
52    /**
53     * @brief Set queue size, the surface could alloc max buffer count.
54     *        Default is 1. Max count is 10.
55     * @param [in] queueSize. Could alloc buffer count.
56     */
57    void SetQueueSize(uint8_t queueSize) override;
58
59    /**
60     * @brief Get queue size, the surface could alloc max buffer count.
61     * @returns queue size.
62     */
63    uint8_t GetQueueSize() override;
64
65    /**
66     * @brief Set width and height to calculate the buffer size.
67     * @param [in] width, Buffer width.
68     * @param [in] height, Buffer height.
69     */
70    void SetWidthAndHeight(uint32_t width, uint32_t height) override;
71
72    /**
73     * @brief Get width, buffer width to calculate the buffer size..
74     * @returns width, Buffer width.
75     */
76    uint32_t GetWidth() override;
77
78    /**
79     * @brief Get height, buffer height to calculate the buffer size..
80     * @returns height, Buffer height.
81     */
82    uint32_t GetHeight() override;
83
84    /**
85     * @brief Set format, to calculate the buffer size.
86     *        Default is IMAGE_PIXEL_FORMAT_RGB565. See all formats in OHOS::ImageFormat
87     * @param [in] format, Buffer format.
88     */
89    void SetFormat(uint32_t format) override;
90
91    /**
92     * @brief Get format, buffer format to calculate the buffer size..
93     * @returns format, Buffer format.
94     */
95    uint32_t GetFormat() override;
96
97    /**
98     * @brief Set stride alignment bytes. Default alignment is 4 bytes.
99     * @param [in] strideAlignment, Buffer stride alignment
100     */
101    void SetStrideAlignment(uint32_t strideAlignment) override;
102
103    /**
104     * @brief Get stride alignment bytes. Default alignment is 4 bytes.
105     * @returns strideAlignment, Buffer stride alignment.
106     */
107    uint32_t GetStrideAlignment() override;
108
109    /**
110     * @brief Get bytes of one stride which calculate by width, format and stride alignment.
111     * @returns The stride
112     */
113    uint32_t GetStride() override;
114
115    /**
116     * @brief Set buffer size. Surface alloc buffer size, no need to calculate by width, height, format...
117     * @param [in] The buffer size
118     */
119    void SetSize(uint32_t size) override;
120
121    /**
122     * @brief Get buffer size. Surface alloc buffer size.
123     *        The size is setted by SetSize() or calculated by width, height, format...
124     * @returns The buffer size.
125     */
126    uint32_t GetSize() override;
127
128    /**
129     * @brief Set buffer usage. Surface alloc physical or virtual memory buffer.
130     *        Support usage see detail in OHOS::BUFFER_CONSUMER_USAGE.
131     *        Default is BUFFER_CONSUMER_USAGE_SORTWARE, which will alloc virtual memory buffer.
132     * @param [in] The buffer usage.
133     */
134    void SetUsage(uint32_t usage) override;
135
136    /**
137     * @brief Get buffer usage. Surface alloc physical or virtual memory buffer.
138     *        All usage sees detail in OHOS::BUFFER_CONSUMER_USAGE.
139     * @returns The buffer usage.
140     */
141    uint32_t GetUsage() override;
142
143    /**
144     * @brief Set user data. Surface would construct a local map to store all the user-data.
145     * @param [in] key.
146     * @param [in] value.
147     */
148    void SetUserData(const std::string& key, const std::string& value) override;
149
150    /**
151     * @brief Get user data. Surface get the value from local map.
152     * @returns value refers to the key.
153     */
154    std::string GetUserData(const std::string& key) override;
155
156    /**
157     * @brief Request buffer. Surface producer requests buffer.
158     *        Waiting until some buffer could used. Default wait = 0;
159     * @param [in] whether waiting or not.
160     *        wait = 1. waiting util get surface buffer.
161     *        wait = 0. No wait to get surface buffer.
162     * @returns buffer pointer.
163     */
164    SurfaceBuffer* RequestBuffer(uint8_t wait = 0) override;
165
166    /**
167     * @brief Flush buffer for consumer acquire. When producer flush buffer, buffer
168     *        whill push to dirty list, and call back to consumer that buffer is available to acquire.
169     * @param [in] SurfaceBuffer pointer, Which buffer could acquire for consumer.
170     * @returns Flush buffer succeed or not.
171     *        0 is succeed; other is failed.
172     */
173    int32_t FlushBuffer(SurfaceBuffer* buffer) override;
174
175    /**
176     * @brief Acquire buffer. Consumer acquire buffer, which producer has flush and push to free list.
177     * @returns buffer pointer.
178     */
179    SurfaceBuffer* AcquireBuffer() override;
180
181    /**
182     * @brief Release buffer. Consumer release buffer, which will push to free list for producer request it.
183     * @param [in] SurfaceBuffer, Which buffer need to release.
184     * @returns Whether Release buffer succeed or not.
185     */
186    bool ReleaseBuffer(SurfaceBuffer* buffer) override;
187
188    /**
189     * @brief Cancel buffer. Producer cancel this buffer, buffer will push to free list for request it.
190     * @param [in] SurfaceBuffer pointer, Which buffer will push back to free list for request it.
191     */
192    void CancelBuffer(SurfaceBuffer* buffer) override;
193
194    /**
195     * @brief Register consumer listener, when some buffer is available for acquired.
196     *        One surface only has one consumer listener.
197     * @param [in], IBufferConsumerListener listener.
198     */
199    void RegisterConsumerListener(IBufferConsumerListener& listener) override;
200
201    /**
202     * @brief Unregister consumer listener, remove the consumer listener.
203     *        One surface only has one consumer listener, So when invoking this method,
204     *        there will have no listener.
205     */
206    void UnregisterConsumerListener() override;
207    /**
208     * @brief Serialize Surface attr to IpcIo.
209     * @param [out], IpcIo.
210     */
211    void WriteIoIpcIo(IpcIo& io);
212
213    /**
214     * @brief Deal with the ipc msg. When multi process, producer will send request. This is to deal with the request.
215     * @param [in] ipcMsg, ipc msg, constains request code...
216     * @param [in] IpcIo pointer, ipc msg object, constains request attr...
217     * @returns 0 is succeed; other is failed.
218     */
219    int32_t DoIpcMsg(uint32_t code, IpcIo* data, IpcIo* reply, MessageOption option);
220
221    /**
222     * @brief Surface init succeed or not
223     * @returns Init succeed return true, else return false.
224     */
225    bool Init();
226
227private:
228    /**
229     * @brief SurfaceImpl Constructor, used for producer on multi process.
230     * @param [in] SvcIdentity. Consumer sid, ipc sending request param.
231     * @returns Surface pointer.
232     */
233    SurfaceImpl(const SvcIdentity& sid);
234    static int32_t IpcRequestHandler(uint32_t code, IpcIo* data, IpcIo* reply, MessageOption option);
235    SvcIdentity sid_;
236    IpcObjectStub objectStub_;
237    BufferQueueConsumer* consumer_;
238    BufferProducer* producer_;
239    bool IsConsumer_;
240};
241} // end namespace
242#endif
243