14ed2deddSopenharmony_ci/*
24ed2deddSopenharmony_ci * Copyright (c) 2020-2022 Huawei Device Co., Ltd.
34ed2deddSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
44ed2deddSopenharmony_ci * you may not use this file except in compliance with the License.
54ed2deddSopenharmony_ci * You may obtain a copy of the License at
64ed2deddSopenharmony_ci *
74ed2deddSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
84ed2deddSopenharmony_ci *
94ed2deddSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
104ed2deddSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
114ed2deddSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
124ed2deddSopenharmony_ci * See the License for the specific language governing permissions and
134ed2deddSopenharmony_ci * limitations under the License.
144ed2deddSopenharmony_ci */
154ed2deddSopenharmony_ci
164ed2deddSopenharmony_ci#ifndef GRAPHIC_LITE_SURFACE_IMPL_H
174ed2deddSopenharmony_ci#define GRAPHIC_LITE_SURFACE_IMPL_H
184ed2deddSopenharmony_ci
194ed2deddSopenharmony_ci#include "buffer_producer.h"
204ed2deddSopenharmony_ci#include "buffer_queue_consumer.h"
214ed2deddSopenharmony_ci#include "ibuffer_consumer_listener.h"
224ed2deddSopenharmony_ci#include "surface.h"
234ed2deddSopenharmony_ci#include "surface_buffer.h"
244ed2deddSopenharmony_ci#include "surface_type.h"
254ed2deddSopenharmony_ci
264ed2deddSopenharmony_cinamespace OHOS {
274ed2deddSopenharmony_ci/**
284ed2deddSopenharmony_ci * @brief SurfaceImpl Object. Provide the shared memory ability.
294ed2deddSopenharmony_ci *        Supported allocate physical and virtual memory.
304ed2deddSopenharmony_ci *        Using in multi media and graphic for multi process.
314ed2deddSopenharmony_ci */
324ed2deddSopenharmony_ciclass SurfaceImpl : public Surface {
334ed2deddSopenharmony_cipublic:
344ed2deddSopenharmony_ci    /**
354ed2deddSopenharmony_ci     * @brief Generic Surface from ipc io, used for producer on multi process.
364ed2deddSopenharmony_ci     * @param [in] IpcIo io. Consumer surface sid, ipc sending request param.
374ed2deddSopenharmony_ci     * @returns Surface pointer.
384ed2deddSopenharmony_ci     */
394ed2deddSopenharmony_ci    static Surface* GenericSurfaceByIpcIo(IpcIo& io);
404ed2deddSopenharmony_ci
414ed2deddSopenharmony_ci    /**
424ed2deddSopenharmony_ci     * @brief Surface Constructor, used for consumer.
434ed2deddSopenharmony_ci     * @returns Surface pointer.
444ed2deddSopenharmony_ci     */
454ed2deddSopenharmony_ci    SurfaceImpl();
464ed2deddSopenharmony_ci
474ed2deddSopenharmony_ci    /**
484ed2deddSopenharmony_ci     * @brief Lite Surface Destructor. Free all buffers.
494ed2deddSopenharmony_ci     */
504ed2deddSopenharmony_ci    ~SurfaceImpl();
514ed2deddSopenharmony_ci
524ed2deddSopenharmony_ci    /**
534ed2deddSopenharmony_ci     * @brief Set queue size, the surface could alloc max buffer count.
544ed2deddSopenharmony_ci     *        Default is 1. Max count is 10.
554ed2deddSopenharmony_ci     * @param [in] queueSize. Could alloc buffer count.
564ed2deddSopenharmony_ci     */
574ed2deddSopenharmony_ci    void SetQueueSize(uint8_t queueSize) override;
584ed2deddSopenharmony_ci
594ed2deddSopenharmony_ci    /**
604ed2deddSopenharmony_ci     * @brief Get queue size, the surface could alloc max buffer count.
614ed2deddSopenharmony_ci     * @returns queue size.
624ed2deddSopenharmony_ci     */
634ed2deddSopenharmony_ci    uint8_t GetQueueSize() override;
644ed2deddSopenharmony_ci
654ed2deddSopenharmony_ci    /**
664ed2deddSopenharmony_ci     * @brief Set width and height to calculate the buffer size.
674ed2deddSopenharmony_ci     * @param [in] width, Buffer width.
684ed2deddSopenharmony_ci     * @param [in] height, Buffer height.
694ed2deddSopenharmony_ci     */
704ed2deddSopenharmony_ci    void SetWidthAndHeight(uint32_t width, uint32_t height) override;
714ed2deddSopenharmony_ci
724ed2deddSopenharmony_ci    /**
734ed2deddSopenharmony_ci     * @brief Get width, buffer width to calculate the buffer size..
744ed2deddSopenharmony_ci     * @returns width, Buffer width.
754ed2deddSopenharmony_ci     */
764ed2deddSopenharmony_ci    uint32_t GetWidth() override;
774ed2deddSopenharmony_ci
784ed2deddSopenharmony_ci    /**
794ed2deddSopenharmony_ci     * @brief Get height, buffer height to calculate the buffer size..
804ed2deddSopenharmony_ci     * @returns height, Buffer height.
814ed2deddSopenharmony_ci     */
824ed2deddSopenharmony_ci    uint32_t GetHeight() override;
834ed2deddSopenharmony_ci
844ed2deddSopenharmony_ci    /**
854ed2deddSopenharmony_ci     * @brief Set format, to calculate the buffer size.
864ed2deddSopenharmony_ci     *        Default is IMAGE_PIXEL_FORMAT_RGB565. See all formats in OHOS::ImageFormat
874ed2deddSopenharmony_ci     * @param [in] format, Buffer format.
884ed2deddSopenharmony_ci     */
894ed2deddSopenharmony_ci    void SetFormat(uint32_t format) override;
904ed2deddSopenharmony_ci
914ed2deddSopenharmony_ci    /**
924ed2deddSopenharmony_ci     * @brief Get format, buffer format to calculate the buffer size..
934ed2deddSopenharmony_ci     * @returns format, Buffer format.
944ed2deddSopenharmony_ci     */
954ed2deddSopenharmony_ci    uint32_t GetFormat() override;
964ed2deddSopenharmony_ci
974ed2deddSopenharmony_ci    /**
984ed2deddSopenharmony_ci     * @brief Set stride alignment bytes. Default alignment is 4 bytes.
994ed2deddSopenharmony_ci     * @param [in] strideAlignment, Buffer stride alignment
1004ed2deddSopenharmony_ci     */
1014ed2deddSopenharmony_ci    void SetStrideAlignment(uint32_t strideAlignment) override;
1024ed2deddSopenharmony_ci
1034ed2deddSopenharmony_ci    /**
1044ed2deddSopenharmony_ci     * @brief Get stride alignment bytes. Default alignment is 4 bytes.
1054ed2deddSopenharmony_ci     * @returns strideAlignment, Buffer stride alignment.
1064ed2deddSopenharmony_ci     */
1074ed2deddSopenharmony_ci    uint32_t GetStrideAlignment() override;
1084ed2deddSopenharmony_ci
1094ed2deddSopenharmony_ci    /**
1104ed2deddSopenharmony_ci     * @brief Get bytes of one stride which calculate by width, format and stride alignment.
1114ed2deddSopenharmony_ci     * @returns The stride
1124ed2deddSopenharmony_ci     */
1134ed2deddSopenharmony_ci    uint32_t GetStride() override;
1144ed2deddSopenharmony_ci
1154ed2deddSopenharmony_ci    /**
1164ed2deddSopenharmony_ci     * @brief Set buffer size. Surface alloc buffer size, no need to calculate by width, height, format...
1174ed2deddSopenharmony_ci     * @param [in] The buffer size
1184ed2deddSopenharmony_ci     */
1194ed2deddSopenharmony_ci    void SetSize(uint32_t size) override;
1204ed2deddSopenharmony_ci
1214ed2deddSopenharmony_ci    /**
1224ed2deddSopenharmony_ci     * @brief Get buffer size. Surface alloc buffer size.
1234ed2deddSopenharmony_ci     *        The size is setted by SetSize() or calculated by width, height, format...
1244ed2deddSopenharmony_ci     * @returns The buffer size.
1254ed2deddSopenharmony_ci     */
1264ed2deddSopenharmony_ci    uint32_t GetSize() override;
1274ed2deddSopenharmony_ci
1284ed2deddSopenharmony_ci    /**
1294ed2deddSopenharmony_ci     * @brief Set buffer usage. Surface alloc physical or virtual memory buffer.
1304ed2deddSopenharmony_ci     *        Support usage see detail in OHOS::BUFFER_CONSUMER_USAGE.
1314ed2deddSopenharmony_ci     *        Default is BUFFER_CONSUMER_USAGE_SORTWARE, which will alloc virtual memory buffer.
1324ed2deddSopenharmony_ci     * @param [in] The buffer usage.
1334ed2deddSopenharmony_ci     */
1344ed2deddSopenharmony_ci    void SetUsage(uint32_t usage) override;
1354ed2deddSopenharmony_ci
1364ed2deddSopenharmony_ci    /**
1374ed2deddSopenharmony_ci     * @brief Get buffer usage. Surface alloc physical or virtual memory buffer.
1384ed2deddSopenharmony_ci     *        All usage sees detail in OHOS::BUFFER_CONSUMER_USAGE.
1394ed2deddSopenharmony_ci     * @returns The buffer usage.
1404ed2deddSopenharmony_ci     */
1414ed2deddSopenharmony_ci    uint32_t GetUsage() override;
1424ed2deddSopenharmony_ci
1434ed2deddSopenharmony_ci    /**
1444ed2deddSopenharmony_ci     * @brief Set user data. Surface would construct a local map to store all the user-data.
1454ed2deddSopenharmony_ci     * @param [in] key.
1464ed2deddSopenharmony_ci     * @param [in] value.
1474ed2deddSopenharmony_ci     */
1484ed2deddSopenharmony_ci    void SetUserData(const std::string& key, const std::string& value) override;
1494ed2deddSopenharmony_ci
1504ed2deddSopenharmony_ci    /**
1514ed2deddSopenharmony_ci     * @brief Get user data. Surface get the value from local map.
1524ed2deddSopenharmony_ci     * @returns value refers to the key.
1534ed2deddSopenharmony_ci     */
1544ed2deddSopenharmony_ci    std::string GetUserData(const std::string& key) override;
1554ed2deddSopenharmony_ci
1564ed2deddSopenharmony_ci    /**
1574ed2deddSopenharmony_ci     * @brief Request buffer. Surface producer requests buffer.
1584ed2deddSopenharmony_ci     *        Waiting until some buffer could used. Default wait = 0;
1594ed2deddSopenharmony_ci     * @param [in] whether waiting or not.
1604ed2deddSopenharmony_ci     *        wait = 1. waiting util get surface buffer.
1614ed2deddSopenharmony_ci     *        wait = 0. No wait to get surface buffer.
1624ed2deddSopenharmony_ci     * @returns buffer pointer.
1634ed2deddSopenharmony_ci     */
1644ed2deddSopenharmony_ci    SurfaceBuffer* RequestBuffer(uint8_t wait = 0) override;
1654ed2deddSopenharmony_ci
1664ed2deddSopenharmony_ci    /**
1674ed2deddSopenharmony_ci     * @brief Flush buffer for consumer acquire. When producer flush buffer, buffer
1684ed2deddSopenharmony_ci     *        whill push to dirty list, and call back to consumer that buffer is available to acquire.
1694ed2deddSopenharmony_ci     * @param [in] SurfaceBuffer pointer, Which buffer could acquire for consumer.
1704ed2deddSopenharmony_ci     * @returns Flush buffer succeed or not.
1714ed2deddSopenharmony_ci     *        0 is succeed; other is failed.
1724ed2deddSopenharmony_ci     */
1734ed2deddSopenharmony_ci    int32_t FlushBuffer(SurfaceBuffer* buffer) override;
1744ed2deddSopenharmony_ci
1754ed2deddSopenharmony_ci    /**
1764ed2deddSopenharmony_ci     * @brief Acquire buffer. Consumer acquire buffer, which producer has flush and push to free list.
1774ed2deddSopenharmony_ci     * @returns buffer pointer.
1784ed2deddSopenharmony_ci     */
1794ed2deddSopenharmony_ci    SurfaceBuffer* AcquireBuffer() override;
1804ed2deddSopenharmony_ci
1814ed2deddSopenharmony_ci    /**
1824ed2deddSopenharmony_ci     * @brief Release buffer. Consumer release buffer, which will push to free list for producer request it.
1834ed2deddSopenharmony_ci     * @param [in] SurfaceBuffer, Which buffer need to release.
1844ed2deddSopenharmony_ci     * @returns Whether Release buffer succeed or not.
1854ed2deddSopenharmony_ci     */
1864ed2deddSopenharmony_ci    bool ReleaseBuffer(SurfaceBuffer* buffer) override;
1874ed2deddSopenharmony_ci
1884ed2deddSopenharmony_ci    /**
1894ed2deddSopenharmony_ci     * @brief Cancel buffer. Producer cancel this buffer, buffer will push to free list for request it.
1904ed2deddSopenharmony_ci     * @param [in] SurfaceBuffer pointer, Which buffer will push back to free list for request it.
1914ed2deddSopenharmony_ci     */
1924ed2deddSopenharmony_ci    void CancelBuffer(SurfaceBuffer* buffer) override;
1934ed2deddSopenharmony_ci
1944ed2deddSopenharmony_ci    /**
1954ed2deddSopenharmony_ci     * @brief Register consumer listener, when some buffer is available for acquired.
1964ed2deddSopenharmony_ci     *        One surface only has one consumer listener.
1974ed2deddSopenharmony_ci     * @param [in], IBufferConsumerListener listener.
1984ed2deddSopenharmony_ci     */
1994ed2deddSopenharmony_ci    void RegisterConsumerListener(IBufferConsumerListener& listener) override;
2004ed2deddSopenharmony_ci
2014ed2deddSopenharmony_ci    /**
2024ed2deddSopenharmony_ci     * @brief Unregister consumer listener, remove the consumer listener.
2034ed2deddSopenharmony_ci     *        One surface only has one consumer listener, So when invoking this method,
2044ed2deddSopenharmony_ci     *        there will have no listener.
2054ed2deddSopenharmony_ci     */
2064ed2deddSopenharmony_ci    void UnregisterConsumerListener() override;
2074ed2deddSopenharmony_ci    /**
2084ed2deddSopenharmony_ci     * @brief Serialize Surface attr to IpcIo.
2094ed2deddSopenharmony_ci     * @param [out], IpcIo.
2104ed2deddSopenharmony_ci     */
2114ed2deddSopenharmony_ci    void WriteIoIpcIo(IpcIo& io);
2124ed2deddSopenharmony_ci
2134ed2deddSopenharmony_ci    /**
2144ed2deddSopenharmony_ci     * @brief Deal with the ipc msg. When multi process, producer will send request. This is to deal with the request.
2154ed2deddSopenharmony_ci     * @param [in] ipcMsg, ipc msg, constains request code...
2164ed2deddSopenharmony_ci     * @param [in] IpcIo pointer, ipc msg object, constains request attr...
2174ed2deddSopenharmony_ci     * @returns 0 is succeed; other is failed.
2184ed2deddSopenharmony_ci     */
2194ed2deddSopenharmony_ci    int32_t DoIpcMsg(uint32_t code, IpcIo* data, IpcIo* reply, MessageOption option);
2204ed2deddSopenharmony_ci
2214ed2deddSopenharmony_ci    /**
2224ed2deddSopenharmony_ci     * @brief Surface init succeed or not
2234ed2deddSopenharmony_ci     * @returns Init succeed return true, else return false.
2244ed2deddSopenharmony_ci     */
2254ed2deddSopenharmony_ci    bool Init();
2264ed2deddSopenharmony_ci
2274ed2deddSopenharmony_ciprivate:
2284ed2deddSopenharmony_ci    /**
2294ed2deddSopenharmony_ci     * @brief SurfaceImpl Constructor, used for producer on multi process.
2304ed2deddSopenharmony_ci     * @param [in] SvcIdentity. Consumer sid, ipc sending request param.
2314ed2deddSopenharmony_ci     * @returns Surface pointer.
2324ed2deddSopenharmony_ci     */
2334ed2deddSopenharmony_ci    SurfaceImpl(const SvcIdentity& sid);
2344ed2deddSopenharmony_ci    static int32_t IpcRequestHandler(uint32_t code, IpcIo* data, IpcIo* reply, MessageOption option);
2354ed2deddSopenharmony_ci    SvcIdentity sid_;
2364ed2deddSopenharmony_ci    IpcObjectStub objectStub_;
2374ed2deddSopenharmony_ci    BufferQueueConsumer* consumer_;
2384ed2deddSopenharmony_ci    BufferProducer* producer_;
2394ed2deddSopenharmony_ci    bool IsConsumer_;
2404ed2deddSopenharmony_ci};
2414ed2deddSopenharmony_ci} // end namespace
2424ed2deddSopenharmony_ci#endif
243