1/*
2 * Copyright (c) 2023 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 VIRTUALSREENIMPL_H
17#define VIRTUALSREENIMPL_H
18
19#include "engines/gfx/soft_engine.h"
20#include "gfx_utils/color.h"
21#include "input_device.h"
22
23#include "EndianUtil.h"
24#include "LocalSocket.h"
25#include "VirtualScreen.h"
26
27class VirtualScreenImpl : public OHOS::SoftEngine, public VirtualScreen {
28public:
29    enum class MouseStatus { INDEV_STATE_RELEASE = 0, INDEV_STATE_PRESS };
30
31    static VirtualScreenImpl& GetInstance();
32    static void CheckBufferSend();
33    VirtualScreenImpl(const VirtualScreenImpl&) = delete;
34    VirtualScreenImpl& operator=(const VirtualScreenImpl&) = delete;
35    void Flush(const OHOS::Rect& flushRect) override;
36    OHOS::BufferInfo* GetFBBufferInfo() override;
37    uint16_t GetScreenWidth() override;
38    uint16_t GetScreenHeight() override;
39    void InitBuffer();
40    void InitAll(std::string pipeName, std::string pipePort);
41
42private:
43    VirtualScreenImpl();
44    ~VirtualScreenImpl();
45    bool IsRectValid(int32_t x1, int32_t y1, int32_t x2, int32_t y2) const;
46    uint8_t* wholeBuffer;
47    uint8_t* regionWholeBuffer;
48    uint8_t* screenBuffer;
49    uint8_t* regionBuffer;
50    uint8_t* osBuffer;
51    bool isChanged;
52    void ScheduleBufferSend();
53    void Send(unsigned char* data, int32_t width, int32_t height);
54    void SendFullBuffer();
55    void SendRegionBuffer();
56    void FreeJpgMemory();
57
58    template <class T, class = typename std::enable_if<std::is_integral<T>::value>::type>
59    void WriteBuffer(const T data)
60    {
61        T dataToSend = EndianUtil::ToNetworkEndian<T>(data);
62        char* startPos = reinterpret_cast<char*>(&dataToSend);
63        std::copy(startPos, startPos + sizeof(dataToSend), screenBuffer + currentPos);
64        currentPos += sizeof(dataToSend);
65    }
66
67    void WriteRefreshRegion();
68    void UpdateRegion(int32_t x1, int32_t y1, int32_t x2, int32_t y2);
69
70    unsigned long long currentPos;
71    uint64_t bufferSize;
72    bool isFirstRender;
73    bool isFirstSend;
74    const uint16_t VERSION_POS = 20;
75    int16_t regionX1;
76    int16_t regionY1;
77    int16_t regionX2;
78    int16_t regionY2;
79    int16_t regionWidth;
80    int16_t regionHeight;
81    int32_t extendPix = 15;
82    OHOS::BufferInfo* bufferInfo;
83    static constexpr int SEND_IMG_DURATION_MS = 300;
84};
85
86#endif // VIRTUALSREENIMPL_H
87