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 "VirtualScreen.h"
20
21class ScreenInfo {
22public:
23    int32_t orignalResolutionWidth;
24    int32_t orignalResolutionHeight;
25    int32_t compressionResolutionWidth;
26    int32_t compressionResolutionHeight;
27    std::string foldStatus;
28    bool foldable;
29    int32_t foldWidth;
30    int32_t foldHeight;
31};
32
33class VirtualScreenImpl : public VirtualScreen {
34public:
35    VirtualScreenImpl(const VirtualScreenImpl&) = delete;
36    VirtualScreenImpl& operator=(const VirtualScreenImpl&) = delete;
37    static VirtualScreenImpl& GetInstance();
38    static void StartTimer();
39    static bool FlushEmptyFunc(std::chrono::system_clock::time_point endTime, int64_t timePassed);
40    static bool NoFlushEmptyFunc(int64_t timePassed);
41    static void PrintLoadDocFinishedLog(const std::string& logStr);
42    static void SendBufferOnTimer();
43    static bool LoadDocCallback(const void* data, const size_t length,
44                                const int32_t width, const int32_t height, const uint64_t timeStamp);
45    static bool Callback(const void* data, const size_t length, const int32_t width, const int32_t height,
46        const uint64_t timeStamp);
47    static bool FlushEmptyCallback(const uint64_t timeStamp);
48    void InitFlushEmptyTime() override;
49    static bool PageCallback(const std::string currentRouterPath);
50    static bool LoadContentCallback(const std::string currentRouterPath);
51    static void FastPreviewCallback(const std::string& jsonStr);
52    void InitAll(std::string pipeName, std::string pipePort);
53    ScreenInfo GetScreenInfo();
54    void InitFoldParams();
55private:
56    VirtualScreenImpl();
57    ~VirtualScreenImpl();
58    void Send(const void* data, int32_t retWidth, int32_t retHeight);
59    bool SendPixmap(const void* data, size_t length, int32_t retWidth, int32_t retHeight);
60    void FreeJpgMemory();
61    template<class T, class = typename std::enable_if<std::is_integral<T>::value>::type>
62    void WriteBuffer(const T data)
63    {
64        T dataToSend = EndianUtil::ToNetworkEndian<T>(data);
65        unsigned char* startPos = reinterpret_cast<unsigned char*>(&dataToSend);
66        std::copy(startPos, startPos + sizeof(dataToSend), screenBuffer + currentPos);
67        currentPos += sizeof(dataToSend);
68    }
69
70    bool isFirstSend;
71    bool isFirstRender;
72    size_t writed;
73    uint8_t* wholeBuffer;
74    uint8_t* screenBuffer;
75    uint64_t bufferSize;
76    unsigned long long currentPos;
77    static constexpr int SEND_IMG_DURATION_MS = 300;
78    static constexpr int STOP_SEND_CARD_DURATION_MS = 10000;
79
80    uint8_t* loadDocTempBuffer;
81    uint8_t* loadDocCopyBuffer;
82    size_t lengthTemp;
83    int32_t widthTemp;
84    int32_t heightTemp;
85    uint64_t timeStampTemp;
86
87    static constexpr int TIMEOUT_ONRENDER_DURATION_MS = 100;
88    static constexpr int TIMEOUT_NINE_S = 9000;
89    static constexpr int64_t SEC_TO_NANOSEC = 1000000000;
90    bool isFlushEmpty = false;
91    uint64_t loadDocTimeStamp = 0;
92    uint64_t flushEmptyTimeStamp = 0;
93    std::chrono::system_clock::time_point flushEmptyTime = std::chrono::system_clock::time_point::min();
94    std::chrono::system_clock::time_point onRenderTime = std::chrono::system_clock::time_point::min();
95};
96
97#endif // VIRTUALSREENIMPL_H
98