17c804472Sopenharmony_ci/*
27c804472Sopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd.
37c804472Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
47c804472Sopenharmony_ci * you may not use this file except in compliance with the License.
57c804472Sopenharmony_ci * You may obtain a copy of the License at
67c804472Sopenharmony_ci *
77c804472Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
87c804472Sopenharmony_ci *
97c804472Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
107c804472Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
117c804472Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
127c804472Sopenharmony_ci * See the License for the specific language governing permissions and
137c804472Sopenharmony_ci * limitations under the License.
147c804472Sopenharmony_ci */
157c804472Sopenharmony_ci
167c804472Sopenharmony_ci#ifndef VIRTUALSREENIMPL_H
177c804472Sopenharmony_ci#define VIRTUALSREENIMPL_H
187c804472Sopenharmony_ci
197c804472Sopenharmony_ci#include "engines/gfx/soft_engine.h"
207c804472Sopenharmony_ci#include "gfx_utils/color.h"
217c804472Sopenharmony_ci#include "input_device.h"
227c804472Sopenharmony_ci
237c804472Sopenharmony_ci#include "EndianUtil.h"
247c804472Sopenharmony_ci#include "LocalSocket.h"
257c804472Sopenharmony_ci#include "VirtualScreen.h"
267c804472Sopenharmony_ci
277c804472Sopenharmony_ciclass VirtualScreenImpl : public OHOS::SoftEngine, public VirtualScreen {
287c804472Sopenharmony_cipublic:
297c804472Sopenharmony_ci    enum class MouseStatus { INDEV_STATE_RELEASE = 0, INDEV_STATE_PRESS };
307c804472Sopenharmony_ci
317c804472Sopenharmony_ci    static VirtualScreenImpl& GetInstance();
327c804472Sopenharmony_ci    static void CheckBufferSend();
337c804472Sopenharmony_ci    VirtualScreenImpl(const VirtualScreenImpl&) = delete;
347c804472Sopenharmony_ci    VirtualScreenImpl& operator=(const VirtualScreenImpl&) = delete;
357c804472Sopenharmony_ci    void Flush(const OHOS::Rect& flushRect) override;
367c804472Sopenharmony_ci    OHOS::BufferInfo* GetFBBufferInfo() override;
377c804472Sopenharmony_ci    uint16_t GetScreenWidth() override;
387c804472Sopenharmony_ci    uint16_t GetScreenHeight() override;
397c804472Sopenharmony_ci    void InitBuffer();
407c804472Sopenharmony_ci    void InitAll(std::string pipeName, std::string pipePort);
417c804472Sopenharmony_ci
427c804472Sopenharmony_ciprivate:
437c804472Sopenharmony_ci    VirtualScreenImpl();
447c804472Sopenharmony_ci    ~VirtualScreenImpl();
457c804472Sopenharmony_ci    bool IsRectValid(int32_t x1, int32_t y1, int32_t x2, int32_t y2) const;
467c804472Sopenharmony_ci    uint8_t* wholeBuffer;
477c804472Sopenharmony_ci    uint8_t* regionWholeBuffer;
487c804472Sopenharmony_ci    uint8_t* screenBuffer;
497c804472Sopenharmony_ci    uint8_t* regionBuffer;
507c804472Sopenharmony_ci    uint8_t* osBuffer;
517c804472Sopenharmony_ci    bool isChanged;
527c804472Sopenharmony_ci    void ScheduleBufferSend();
537c804472Sopenharmony_ci    void Send(unsigned char* data, int32_t width, int32_t height);
547c804472Sopenharmony_ci    void SendFullBuffer();
557c804472Sopenharmony_ci    void SendRegionBuffer();
567c804472Sopenharmony_ci    void FreeJpgMemory();
577c804472Sopenharmony_ci
587c804472Sopenharmony_ci    template <class T, class = typename std::enable_if<std::is_integral<T>::value>::type>
597c804472Sopenharmony_ci    void WriteBuffer(const T data)
607c804472Sopenharmony_ci    {
617c804472Sopenharmony_ci        T dataToSend = EndianUtil::ToNetworkEndian<T>(data);
627c804472Sopenharmony_ci        char* startPos = reinterpret_cast<char*>(&dataToSend);
637c804472Sopenharmony_ci        std::copy(startPos, startPos + sizeof(dataToSend), screenBuffer + currentPos);
647c804472Sopenharmony_ci        currentPos += sizeof(dataToSend);
657c804472Sopenharmony_ci    }
667c804472Sopenharmony_ci
677c804472Sopenharmony_ci    void WriteRefreshRegion();
687c804472Sopenharmony_ci    void UpdateRegion(int32_t x1, int32_t y1, int32_t x2, int32_t y2);
697c804472Sopenharmony_ci
707c804472Sopenharmony_ci    unsigned long long currentPos;
717c804472Sopenharmony_ci    uint64_t bufferSize;
727c804472Sopenharmony_ci    bool isFirstRender;
737c804472Sopenharmony_ci    bool isFirstSend;
747c804472Sopenharmony_ci    const uint16_t VERSION_POS = 20;
757c804472Sopenharmony_ci    int16_t regionX1;
767c804472Sopenharmony_ci    int16_t regionY1;
777c804472Sopenharmony_ci    int16_t regionX2;
787c804472Sopenharmony_ci    int16_t regionY2;
797c804472Sopenharmony_ci    int16_t regionWidth;
807c804472Sopenharmony_ci    int16_t regionHeight;
817c804472Sopenharmony_ci    int32_t extendPix = 15;
827c804472Sopenharmony_ci    OHOS::BufferInfo* bufferInfo;
837c804472Sopenharmony_ci    static constexpr int SEND_IMG_DURATION_MS = 300;
847c804472Sopenharmony_ci};
857c804472Sopenharmony_ci
867c804472Sopenharmony_ci#endif // VIRTUALSREENIMPL_H
87