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