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_CLIENT_PRODUCER_H
17#define GRAPHIC_LITE_BUFFER_CLIENT_PRODUCER_H
18
19#include <pthread.h>
20#include "buffer_producer.h"
21#include "buffer_queue.h"
22#include "ipc_skeleton.h"
23#include "serializer.h"
24#include "surface_buffer.h"
25namespace OHOS {
26/**
27 * @brief Surface producer client class in multi process. Surface Client invoke these method to send ipc
28 *        request to BufferQueueProducer for request buffer, flush buffer, cancel buffer and set buffer attr.
29 */
30class BufferClientProducer : public BufferProducer {
31public:
32    /**
33     * @brief Surface Buffer Client Producer Constructor.
34     * @param [in] SvcIdentity sid, Surface consumer sid, for sending ipc request.
35     */
36    explicit BufferClientProducer(const SvcIdentity& sid);
37
38    /**
39     * @brief Surface Buffer Client Producer Destructor.
40     */
41    ~BufferClientProducer();
42
43    /**
44     * @brief Request buffer. Surface client producer sends ipc message(code=REQUEST_BUFFER) to requests buffer.
45     *        BufferQueueProducer does the request and return Buffer handle, then map the handle with virtual address
46     *        to write data.
47     * @param [in] whether waiting or not.
48     *        wait = 1. waiting util get surface buffer.
49     *        wait = 0. No wait to get surface buffer.
50     * @returns buffer pointer.
51     */
52    SurfaceBufferImpl* RequestBuffer(uint8_t wait) override;
53
54    /**
55     * @brief Flush buffer for consumer acquire. Client producer sends request(code=FLUSH_BUFFER) to flush buffer,
56     *        BufferQueueProducer push buffer to dirty list, and call back to consumer that buffer is available to
57     *        acquire.
58     * @param [in] SurfaceBufferImpl pointer, Which buffer could acquire for consumer.
59     * @returns Flush buffer succeed or not.
60     *        0 is succeed; other is failed.
61     */
62    int32_t FlushBuffer(SurfaceBufferImpl* buffer) override;
63
64    /**
65     * @brief Cancel buffer. Client Producer sends request(CANCEL_BUFFER) to cancel this buffer.
66     *        BufferQueueProducer push push buffer to free list for request it again.
67     * @param [in] SurfaceBufferImpl pointer, push it back to free list for request it.
68     */
69    void Cancel(SurfaceBufferImpl* buffer) override;
70
71    /**
72     * @brief Set queue size. Client Producer sends request(SET_QUEUE_SIZE) to set max buffer count.
73     * @param [in] queueSize. Could allocate buffer count.
74     */
75    void SetQueueSize(uint8_t queueSize) override;
76
77    /**
78     * @brief Get queue size. Client Producer sends request(GET_QUEUE_SIZE) to get max buffer count.
79     * @returns queue size.
80     */
81    uint8_t GetQueueSize() override;
82
83    /**
84     * @brief Client Producer sends request(SET_WIDTH_AND_HEIGHT) to set width and height to calculate the buffer size.
85     * @param [in] width, Buffer width.
86     * @param [in] height, Buffer height.
87     */
88    void SetWidthAndHeight(uint32_t width, uint32_t height) override;
89
90    /**
91     * @brief Client Producer sends request(GET_WIDTH) to get width, buffer width to calculate the buffer size..
92     * @returns width, Buffer width.
93     */
94    uint32_t GetWidth() override;
95
96    /**
97     * @brief Client Producer sends request(GET_HEIGHT) to get height, buffer height to calculate the buffer size..
98     * @returns height, Buffer height.
99     */
100    uint32_t GetHeight() override;
101
102    /**
103     * @brief Client Producer sends request(SET_FORMAT) to set format, to calculate the buffer size.
104     *        Default is IMAGE_PIXEL_FORMAT_RGB565. See all formats in OHOS::ImageFormat
105     * @param [in] format, Buffer format.
106     */
107    void SetFormat(uint32_t format) override;
108
109    /**
110     * @brief Client Producer sends request(GET_FORMAT) to get format, buffer format to calculate the buffer size..
111     * @returns format, Buffer format.
112     */
113    uint32_t GetFormat() override;
114
115    /**
116     * @brief Client Producer sends request(SET_STRIDE_ALIGNMENT) to set stride alignment bytes.
117     *        Default alignment is 4 bytes.
118     * @param [in] strideAlignment, Buffer stride alignment
119     */
120    void SetStrideAlignment(uint32_t strideAlignment) override;
121
122    /**
123     * @brief Client Producer sends request(GET_STRIDE_ALIGNMENT) to get stride alignment bytes.
124     *        Default alignment is 4 bytes.
125     * @returns strideAlignment, Buffer stride alignment.
126     */
127    uint32_t GetStrideAlignment() override;
128
129    /**
130     * @brief Client Producer sends request(GET_STRIDE) to get bytes of one stride which calculate by width,
131     *        format and stride alignment.
132     * @returns The stride
133     */
134    uint32_t GetStride() override;
135
136    /**
137     * @brief Client Producer sends request(SET_SIZE) to set buffer size. Surface alloc buffer size, no need
138     *        to calculate by width, height, format...
139     * @param [in] The buffer size
140     */
141    void SetSize(uint32_t size) override;
142
143    /**
144     * @brief Client Producer sends request(GET_SIZE) to get buffer size. Surface alloc buffer size.
145     *        The size is setted by SetSize() or calculated by width, height, format...
146     * @returns The buffer size.
147     */
148    uint32_t GetSize() override;
149
150    /**
151     * @brief Client Producer sends request(SET_USAGE) to set buffer usage. Surface alloc physical or
152     *        virtual memory buffer. Support usage see detail in OHOS::BUFFER_CONSUMER_USAGE.
153     *        Default is BUFFER_CONSUMER_USAGE_SORTWARE, which will alloc virtual memory buffer.
154     * @param [in] The buffer usage.
155     */
156    void SetUsage(uint32_t usage) override;
157
158    /**
159     * @brief Client Producer sends request(GET_USAGE) to get buffer usage. Surface alloc physical or
160     *        virtual memory buffer. All usage sees detail in OHOS::BUFFER_CONSUMER_USAGE.
161     * @returns The buffer usage.
162     */
163    uint32_t GetUsage() override;
164
165    /**
166     * @brief Set user data. Construct a local map to store all the user-data.
167     * @param [in] key.
168     * @param [in] value.
169     */
170    void SetUserData(const std::string& key, const std::string& value) override;
171
172    /**
173     * @brief Get user data. Get the value from local map.
174     * @returns value refers to the key.
175     */
176    std::string GetUserData(const std::string& key) override;
177
178private:
179    uint32_t GetAttr(uint32_t code);
180    void SetAttr(uint32_t code, uint32_t value);
181    SvcIdentity sid_;
182    IpcObjectStub objectStub_;
183};
184} // end namespace
185
186#endif
187