• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
21 class ScreenInfo {
22 public:
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 
33 class VirtualScreenImpl : public VirtualScreen {
34 public:
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();
55 private:
56     VirtualScreenImpl();
57     ~VirtualScreenImpl();
58     void Send(const void* data, int32_t retWidth, int32_t retHeight);
59     void SendRgba(const void* data, size_t length);
60     void BackupAndDeleteBuffer(const unsigned long imageBufferSize);
61     bool JudgeBeforeSend(const void* data);
62     bool SendPixmap(const void* data, size_t length, int32_t retWidth, int32_t retHeight);
63     void FreeJpgMemory();
64     template<class T, class = typename std::enable_if<std::is_integral<T>::value>::type>
WriteBuffer(const T data)65     void WriteBuffer(const T data)
66     {
67         T dataToSend = EndianUtil::ToNetworkEndian<T>(data);
68         unsigned char* startPos = reinterpret_cast<unsigned char*>(&dataToSend);
69         std::copy(startPos, startPos + sizeof(dataToSend), screenBuffer + currentPos);
70         currentPos += sizeof(dataToSend);
71     }
72 
73     bool isFirstSend;
74     bool isFirstRender;
75     size_t writed;
76     uint8_t* wholeBuffer;
77     uint8_t* screenBuffer;
78     uint64_t bufferSize;
79     unsigned long long currentPos;
80     static constexpr int SEND_IMG_DURATION_MS = 300;
81     static constexpr int STOP_SEND_CARD_DURATION_MS = 10000;
82 
83     uint8_t* loadDocTempBuffer;
84     uint8_t* loadDocCopyBuffer;
85     size_t lengthTemp;
86     int32_t widthTemp;
87     int32_t heightTemp;
88     uint64_t timeStampTemp;
89 
90     static constexpr int TIMEOUT_ONRENDER_DURATION_MS = 100;
91     static constexpr int TIMEOUT_NINE_S = 9000;
92     static constexpr int64_t SEC_TO_NANOSEC = 1000000000;
93     bool isFlushEmpty = false;
94     uint64_t loadDocTimeStamp = 0;
95     uint64_t flushEmptyTimeStamp = 0;
96     std::chrono::system_clock::time_point flushEmptyTime = std::chrono::system_clock::time_point::min();
97     std::chrono::system_clock::time_point onRenderTime = std::chrono::system_clock::time_point::min();
98 };
99 
100 #endif // VIRTUALSREENIMPL_H
101