1 /*
2  * Copyright (c) 2020-2021 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 #include "dock/screen_device_proxy.h"
17 #include "draw/draw_utils.h"
18 #include "gfx_utils/graphic_log.h"
19 #include "securec.h"
20 
21 namespace OHOS {
GetInstance()22 ScreenDeviceProxy* ScreenDeviceProxy::GetInstance()
23 {
24     static ScreenDeviceProxy instance;
25     return &instance;
26 }
27 
Flush()28 void ScreenDeviceProxy::Flush() {}
29 
OnFlushReady()30 void ScreenDeviceProxy::OnFlushReady()
31 {
32     flush_.Notify();
33 }
34 
OnRenderFinish(const Rect & mask)35 void ScreenDeviceProxy::OnRenderFinish(const Rect& mask)
36 {
37     if (device_ != nullptr) {
38         device_->RenderFinish(mask);
39     }
40 }
41 
DrawAnimatorBuffer(const Rect & invalidatedArea)42 void ScreenDeviceProxy::DrawAnimatorBuffer(const Rect& invalidatedArea)
43 {
44 }
45 
SetAnimatorRect(const Rect & rect)46 void ScreenDeviceProxy::SetAnimatorRect(const Rect& rect)
47 {
48     curViewRect_ = rect;
49     uint16_t bufferWidth = (width_ > curViewRect_.GetWidth()) ? curViewRect_.GetWidth() : width_;
50     uint16_t bufferHeight = (height_ > curViewRect_.GetHeight()) ? curViewRect_.GetHeight() : height_;
51 
52     animatorImageInfo_.header.colorMode = animatorBufferMode_;
53     animatorImageInfo_.dataSize = bufferWidth * bufferHeight * DrawUtils::GetByteSizeByColorMode(animatorBufferMode_);
54     animatorImageInfo_.header.width = bufferWidth;
55     animatorImageInfo_.header.height = bufferHeight;
56     animatorImageInfo_.header.reserved = 0;
57     animatorImageInfo_.data = reinterpret_cast<uint8_t*>(GetBuffer());
58     if (animatorImageInfo_.data == nullptr) {
59         return;
60     }
61 
62     SetAnimatorbufferWidth(bufferWidth);
63     if (memset_s(reinterpret_cast<void*>(const_cast<uint8_t*>(animatorImageInfo_.data)), animatorImageInfo_.dataSize, 0,
64         animatorImageInfo_.dataSize) != EOK) {
65         GRAPHIC_LOGE("animator buffer memset failed.");
66     }
67 }
68 
SetScreenSize(uint16_t width,uint16_t height)69 void ScreenDeviceProxy::SetScreenSize(uint16_t width, uint16_t height)
70 {
71     if ((width == 0) || (height == 0)) {
72         GRAPHIC_LOGE("screen size can not be zero.");
73         return;
74     }
75     width_ = width;
76     height_ = height;
77 }
78 
GetBuffer()79 uint8_t* ScreenDeviceProxy::GetBuffer()
80 {
81     if (enableBitmapBuffer_) {
82         return viewBitmapBuffer_;
83     }
84     flush_.Wait();
85     if (useAnimatorBuff_) {
86         if (animatorBufferAddr_ == nullptr) {
87             GRAPHIC_LOGE("Invalid param animatorBufferAddr_.");
88             return nullptr;
89         }
90         return animatorBufferAddr_;
91     }
92     if (frameBufferAddr_ == nullptr) {
93         GRAPHIC_LOGE("Invalid param frameBufferAddr_.");
94         return nullptr;
95     }
96     return frameBufferAddr_;
97 }
98 
GetBufferMode()99 ColorMode ScreenDeviceProxy::GetBufferMode()
100 {
101     if (useAnimatorBuff_) {
102         return animatorBufferMode_;
103     }
104     return frameBufferMode_;
105 }
106 
EnableBitmapBuffer(uint8_t * viewBitmapBuffer)107 void ScreenDeviceProxy::EnableBitmapBuffer(uint8_t* viewBitmapBuffer)
108 {
109     if (viewBitmapBuffer == nullptr) {
110         return;
111     }
112     viewBitmapBuffer_ = viewBitmapBuffer;
113     enableBitmapBuffer_ = true;
114 }
115 
GetScreenBitmapBuffer(uint8_t * dest,uint32_t size)116 bool ScreenDeviceProxy::GetScreenBitmapBuffer(uint8_t* dest, uint32_t size)
117 {
118     if ((dest == nullptr) || (size == 0)) {
119         return false;
120     }
121     uint8_t byteSize = DrawUtils::GetByteSizeByColorMode(frameBufferMode_);
122     uint32_t bufSize = width_ * height_ * byteSize;
123     if (size < bufSize) {
124         return false;
125     }
126     uint8_t* buf = GetBuffer();
127     if (buf == nullptr) {
128         return false;
129     }
130     if (memcpy_s(dest, size, buf, bufSize) != EOK) {
131         return false;
132     }
133     return true;
134 }
135 } // namespace OHOS
136