1a3e0fd82Sopenharmony_ci/*
2a3e0fd82Sopenharmony_ci * Copyright (c) 2020-2021 Huawei Device Co., Ltd.
3a3e0fd82Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4a3e0fd82Sopenharmony_ci * you may not use this file except in compliance with the License.
5a3e0fd82Sopenharmony_ci * You may obtain a copy of the License at
6a3e0fd82Sopenharmony_ci *
7a3e0fd82Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8a3e0fd82Sopenharmony_ci *
9a3e0fd82Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10a3e0fd82Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11a3e0fd82Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12a3e0fd82Sopenharmony_ci * See the License for the specific language governing permissions and
13a3e0fd82Sopenharmony_ci * limitations under the License.
14a3e0fd82Sopenharmony_ci */
15a3e0fd82Sopenharmony_ci
16a3e0fd82Sopenharmony_ci#include "dock/screen_device_proxy.h"
17a3e0fd82Sopenharmony_ci#include "draw/draw_utils.h"
18a3e0fd82Sopenharmony_ci#include "gfx_utils/graphic_log.h"
19a3e0fd82Sopenharmony_ci#include "securec.h"
20a3e0fd82Sopenharmony_ci
21a3e0fd82Sopenharmony_cinamespace OHOS {
22a3e0fd82Sopenharmony_ciScreenDeviceProxy* ScreenDeviceProxy::GetInstance()
23a3e0fd82Sopenharmony_ci{
24a3e0fd82Sopenharmony_ci    static ScreenDeviceProxy instance;
25a3e0fd82Sopenharmony_ci    return &instance;
26a3e0fd82Sopenharmony_ci}
27a3e0fd82Sopenharmony_ci
28a3e0fd82Sopenharmony_civoid ScreenDeviceProxy::Flush() {}
29a3e0fd82Sopenharmony_ci
30a3e0fd82Sopenharmony_civoid ScreenDeviceProxy::OnFlushReady()
31a3e0fd82Sopenharmony_ci{
32a3e0fd82Sopenharmony_ci    flush_.Notify();
33a3e0fd82Sopenharmony_ci}
34a3e0fd82Sopenharmony_ci
35a3e0fd82Sopenharmony_civoid ScreenDeviceProxy::OnRenderFinish(const Rect& mask)
36a3e0fd82Sopenharmony_ci{
37a3e0fd82Sopenharmony_ci    if (device_ != nullptr) {
38a3e0fd82Sopenharmony_ci        device_->RenderFinish(mask);
39a3e0fd82Sopenharmony_ci    }
40a3e0fd82Sopenharmony_ci}
41a3e0fd82Sopenharmony_ci
42a3e0fd82Sopenharmony_civoid ScreenDeviceProxy::DrawAnimatorBuffer(const Rect& invalidatedArea)
43a3e0fd82Sopenharmony_ci{
44a3e0fd82Sopenharmony_ci}
45a3e0fd82Sopenharmony_ci
46a3e0fd82Sopenharmony_civoid ScreenDeviceProxy::SetAnimatorRect(const Rect& rect)
47a3e0fd82Sopenharmony_ci{
48a3e0fd82Sopenharmony_ci    curViewRect_ = rect;
49a3e0fd82Sopenharmony_ci    uint16_t bufferWidth = (width_ > curViewRect_.GetWidth()) ? curViewRect_.GetWidth() : width_;
50a3e0fd82Sopenharmony_ci    uint16_t bufferHeight = (height_ > curViewRect_.GetHeight()) ? curViewRect_.GetHeight() : height_;
51a3e0fd82Sopenharmony_ci
52a3e0fd82Sopenharmony_ci    animatorImageInfo_.header.colorMode = animatorBufferMode_;
53a3e0fd82Sopenharmony_ci    animatorImageInfo_.dataSize = bufferWidth * bufferHeight * DrawUtils::GetByteSizeByColorMode(animatorBufferMode_);
54a3e0fd82Sopenharmony_ci    animatorImageInfo_.header.width = bufferWidth;
55a3e0fd82Sopenharmony_ci    animatorImageInfo_.header.height = bufferHeight;
56a3e0fd82Sopenharmony_ci    animatorImageInfo_.header.reserved = 0;
57a3e0fd82Sopenharmony_ci    animatorImageInfo_.data = reinterpret_cast<uint8_t*>(GetBuffer());
58a3e0fd82Sopenharmony_ci    if (animatorImageInfo_.data == nullptr) {
59a3e0fd82Sopenharmony_ci        return;
60a3e0fd82Sopenharmony_ci    }
61a3e0fd82Sopenharmony_ci
62a3e0fd82Sopenharmony_ci    SetAnimatorbufferWidth(bufferWidth);
63a3e0fd82Sopenharmony_ci    if (memset_s(reinterpret_cast<void*>(const_cast<uint8_t*>(animatorImageInfo_.data)), animatorImageInfo_.dataSize, 0,
64a3e0fd82Sopenharmony_ci        animatorImageInfo_.dataSize) != EOK) {
65a3e0fd82Sopenharmony_ci        GRAPHIC_LOGE("animator buffer memset failed.");
66a3e0fd82Sopenharmony_ci    }
67a3e0fd82Sopenharmony_ci}
68a3e0fd82Sopenharmony_ci
69a3e0fd82Sopenharmony_civoid ScreenDeviceProxy::SetScreenSize(uint16_t width, uint16_t height)
70a3e0fd82Sopenharmony_ci{
71a3e0fd82Sopenharmony_ci    if ((width == 0) || (height == 0)) {
72a3e0fd82Sopenharmony_ci        GRAPHIC_LOGE("screen size can not be zero.");
73a3e0fd82Sopenharmony_ci        return;
74a3e0fd82Sopenharmony_ci    }
75a3e0fd82Sopenharmony_ci    width_ = width;
76a3e0fd82Sopenharmony_ci    height_ = height;
77a3e0fd82Sopenharmony_ci}
78a3e0fd82Sopenharmony_ci
79a3e0fd82Sopenharmony_ciuint8_t* ScreenDeviceProxy::GetBuffer()
80a3e0fd82Sopenharmony_ci{
81a3e0fd82Sopenharmony_ci    if (enableBitmapBuffer_) {
82a3e0fd82Sopenharmony_ci        return viewBitmapBuffer_;
83a3e0fd82Sopenharmony_ci    }
84a3e0fd82Sopenharmony_ci    flush_.Wait();
85a3e0fd82Sopenharmony_ci    if (useAnimatorBuff_) {
86a3e0fd82Sopenharmony_ci        if (animatorBufferAddr_ == nullptr) {
87a3e0fd82Sopenharmony_ci            GRAPHIC_LOGE("Invalid param animatorBufferAddr_.");
88a3e0fd82Sopenharmony_ci            return nullptr;
89a3e0fd82Sopenharmony_ci        }
90a3e0fd82Sopenharmony_ci        return animatorBufferAddr_;
91a3e0fd82Sopenharmony_ci    }
92a3e0fd82Sopenharmony_ci    if (frameBufferAddr_ == nullptr) {
93a3e0fd82Sopenharmony_ci        GRAPHIC_LOGE("Invalid param frameBufferAddr_.");
94a3e0fd82Sopenharmony_ci        return nullptr;
95a3e0fd82Sopenharmony_ci    }
96a3e0fd82Sopenharmony_ci    return frameBufferAddr_;
97a3e0fd82Sopenharmony_ci}
98a3e0fd82Sopenharmony_ci
99a3e0fd82Sopenharmony_ciColorMode ScreenDeviceProxy::GetBufferMode()
100a3e0fd82Sopenharmony_ci{
101a3e0fd82Sopenharmony_ci    if (useAnimatorBuff_) {
102a3e0fd82Sopenharmony_ci        return animatorBufferMode_;
103a3e0fd82Sopenharmony_ci    }
104a3e0fd82Sopenharmony_ci    return frameBufferMode_;
105a3e0fd82Sopenharmony_ci}
106a3e0fd82Sopenharmony_ci
107a3e0fd82Sopenharmony_civoid ScreenDeviceProxy::EnableBitmapBuffer(uint8_t* viewBitmapBuffer)
108a3e0fd82Sopenharmony_ci{
109a3e0fd82Sopenharmony_ci    if (viewBitmapBuffer == nullptr) {
110a3e0fd82Sopenharmony_ci        return;
111a3e0fd82Sopenharmony_ci    }
112a3e0fd82Sopenharmony_ci    viewBitmapBuffer_ = viewBitmapBuffer;
113a3e0fd82Sopenharmony_ci    enableBitmapBuffer_ = true;
114a3e0fd82Sopenharmony_ci}
115a3e0fd82Sopenharmony_ci
116a3e0fd82Sopenharmony_cibool ScreenDeviceProxy::GetScreenBitmapBuffer(uint8_t* dest, uint32_t size)
117a3e0fd82Sopenharmony_ci{
118a3e0fd82Sopenharmony_ci    if ((dest == nullptr) || (size == 0)) {
119a3e0fd82Sopenharmony_ci        return false;
120a3e0fd82Sopenharmony_ci    }
121a3e0fd82Sopenharmony_ci    uint8_t byteSize = DrawUtils::GetByteSizeByColorMode(frameBufferMode_);
122a3e0fd82Sopenharmony_ci    uint32_t bufSize = width_ * height_ * byteSize;
123a3e0fd82Sopenharmony_ci    if (size < bufSize) {
124a3e0fd82Sopenharmony_ci        return false;
125a3e0fd82Sopenharmony_ci    }
126a3e0fd82Sopenharmony_ci    uint8_t* buf = GetBuffer();
127a3e0fd82Sopenharmony_ci    if (buf == nullptr) {
128a3e0fd82Sopenharmony_ci        return false;
129a3e0fd82Sopenharmony_ci    }
130a3e0fd82Sopenharmony_ci    if (memcpy_s(dest, size, buf, bufSize) != EOK) {
131a3e0fd82Sopenharmony_ci        return false;
132a3e0fd82Sopenharmony_ci    }
133a3e0fd82Sopenharmony_ci    return true;
134a3e0fd82Sopenharmony_ci}
135a3e0fd82Sopenharmony_ci} // namespace OHOS
136