1 /*
2 * Copyright (c) 2021-2022 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 "view.h"
17
18 #include "securec.h"
19 #include "battery_log.h"
20
21 namespace OHOS {
22 namespace HDI {
23 namespace Battery {
24 namespace V1_1 {
25 constexpr int32_t RGBA_PIXEL_SIZE = 4;
CreateBuffer(int32_t w,int32_t h,View::PixelFormat pixelFormat)26 void* View::CreateBuffer(int32_t w, int32_t h, View::PixelFormat pixelFormat)
27 {
28 int32_t pixelSize = -1;
29 switch (static_cast<int32_t>(pixelFormat)) {
30 case int32_t(View::PixelFormat::BGRA888):
31 pixelSize = RGBA_PIXEL_SIZE;
32 break;
33 default:
34 BATTERY_HILOGD(FEATURE_CHARGING, "Unsupported pixel_format=%{public}d, Use default BGRA888",
35 static_cast<int32_t>(pixelFormat));
36 pixelSize = RGBA_PIXEL_SIZE;
37 break;
38 }
39 bufferSize_ = w * h * pixelSize;
40 viewBuffer_ = static_cast<char*>(malloc(bufferSize_));
41 if (viewBuffer_ == nullptr) {
42 BATTERY_HILOGE(FEATURE_CHARGING, "Allocate memory for view failed: %{public}d", errno);
43 return nullptr;
44 }
45 shadowBuffer_ = static_cast<char*>(malloc(bufferSize_));
46 if (shadowBuffer_ == nullptr) {
47 BATTERY_HILOGE(FEATURE_CHARGING, "Allocate memory for shadow failed: %{public}d", errno);
48 free(viewBuffer_);
49 viewBuffer_ = nullptr;
50 return nullptr;
51 }
52 if (memset_s(viewBuffer_, bufferSize_, 0, bufferSize_) != EOK) {
53 BATTERY_HILOGE(FEATURE_CHARGING, "Clean view buffer failed.");
54 free(viewBuffer_);
55 viewBuffer_ = nullptr;
56 return nullptr;
57 }
58 viewWidth_ = w;
59 viewHeight_ = h;
60 return viewBuffer_;
61 }
62
SetBackgroundColor(BRGA888Pixel * color)63 void View::SetBackgroundColor(BRGA888Pixel* color)
64 {
65 BRGA888Pixel pixelBuffer[viewWidth_];
66 for (int32_t w = 0; w < viewWidth_; w++) {
67 pixelBuffer[w].r = color->r;
68 pixelBuffer[w].g = color->g;
69 pixelBuffer[w].b = color->b;
70 pixelBuffer[w].a = color->a;
71 }
72 for (int32_t h = 0; h < viewHeight_; h++) {
73 if (memcpy_s(viewBuffer_ + h * viewWidth_ * sizeof(BRGA888Pixel), viewWidth_ * sizeof(BRGA888Pixel) + 1,
74 reinterpret_cast<char*>(pixelBuffer), viewWidth_ * sizeof(BRGA888Pixel)) != EOK) {
75 return;
76 }
77 }
78 if (isVisiable_) {
79 OnDraw();
80 BATTERY_HILOGD(FEATURE_CHARGING, "view---visible");
81 }
82 }
83
DrawSubView(int32_t x,int32_t y,int32_t w,int32_t h,char * buf)84 void View::DrawSubView(int32_t x, int32_t y, int32_t w, int32_t h, char* buf)
85 {
86 int32_t minWidth = ((x + w) <= viewWidth_) ? w : (viewWidth_ - x);
87 int32_t minHeight = ((y + h) <= viewHeight_) ? h : (viewHeight_ - y);
88 BATTERY_HILOGD(FEATURE_CHARGING, "x = %{public}d, y = %{public}d, w = %{public}d, h = %{public}d", x, y, w, h);
89 BATTERY_HILOGD(FEATURE_CHARGING, "minWidth = %{public}d, minHeight = %{public}d", minWidth, minHeight);
90 for (int32_t i = 0; i < minHeight; i++) {
91 char* src = buf + i * w * static_cast<int32_t>(sizeof(BRGA888Pixel));
92 char* dst = shadowBuffer_ + (i + y) * viewWidth_ * static_cast<int32_t>(sizeof(BRGA888Pixel)) +
93 x * static_cast<int32_t>(sizeof(BRGA888Pixel));
94 if (memcpy_s(dst, minWidth * static_cast<int32_t>(sizeof(BRGA888Pixel)) + 1, src,
95 minWidth * static_cast<int32_t>(sizeof(BRGA888Pixel))) != EOK) {
96 return;
97 }
98 }
99 }
100
OnDraw()101 void View::OnDraw()
102 {
103 std::unique_lock<std::mutex> locker(mutex_);
104 SyncBuffer();
105 }
106
Hide()107 void View::Hide()
108 {
109 if (isVisiable_) {
110 isVisiable_ = false;
111 OnDraw();
112 }
113 }
114
Show()115 void View::Show()
116 {
117 if (!isVisiable_) {
118 isVisiable_ = true;
119 OnDraw();
120 }
121 }
122
SyncBuffer()123 void View::SyncBuffer()
124 {
125 if (memcpy_s(shadowBuffer_, bufferSize_, viewBuffer_, bufferSize_) != EOK) {
126 BATTERY_HILOGD(FEATURE_CHARGING, "Sync buffer failed.");
127 }
128 }
129
GetBuffer() const130 char* View::GetBuffer() const
131 {
132 return shadowBuffer_;
133 }
134
GetRawBuffer() const135 void* View::GetRawBuffer() const
136 {
137 return viewBuffer_;
138 }
139
OnFocus(bool foucsed)140 void View::OnFocus(bool foucsed)
141 {
142 isFocused_ = foucsed;
143 OnDraw();
144 }
145
SetViewId(int32_t id)146 void View::SetViewId(int32_t id)
147 {
148 viewId_ = id;
149 }
150
GetViewId() const151 int32_t View::GetViewId() const
152 {
153 return viewId_;
154 }
155
FreeBuffer()156 void View::FreeBuffer()
157 {
158 free(viewBuffer_);
159 free(shadowBuffer_);
160 viewBuffer_ = nullptr;
161 shadowBuffer_ = nullptr;
162 }
163
IsVisiable() const164 bool View::IsVisiable() const
165 {
166 return isVisiable_;
167 }
168
IsSelected() const169 bool View::IsSelected() const
170 {
171 return isFocused_;
172 }
173
IsFocusAble() const174 bool View::IsFocusAble() const
175 {
176 return focusable_;
177 }
178
SetFocusAble(bool focusable)179 void View::SetFocusAble(bool focusable)
180 {
181 focusable_ = focusable;
182 }
183 } // namespace V1_1
184 } // namespace Battery
185 } // namespace HDI
186 } // namespace OHOS
187