• 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 
16 #include "animation_label.h"
17 #include <cerrno>
18 #include <cstdio>
19 #include <string>
20 #include <sys/epoll.h>
21 #include "frame.h"
22 #include "log.h"
23 #include "png.h"
24 #include "securec.h"
25 #include "view.h"
26 #include "utils/hdf_log.h"
27 
28 namespace OHOS {
29 namespace HDI {
30 namespace Battery {
31 namespace V1_0 {
32 constexpr int PNG_HEADER_SIZE = 8;
33 constexpr int MAX_PICTURE_CHANNELS = 3;
34 constexpr int MAX_BIT_DEPTH = 8;
35 constexpr useconds_t SECOND_PER_MS = 1000;
36 bool AnimationLabel::isVisible_ = true;
37 bool AnimationLabel::needStop_ = false;
38 
AnimationLabel(int startX,int startY,int w,int h,Frame * mParent)39 AnimationLabel::AnimationLabel(int startX, int startY, int w, int h, Frame* mParent)
40 {
41     startX_ = startX;
42     startY_ = startY;
43     this->CreateBuffer(w, h, View::PixelFormat::BGRA888);
44     parent_ = mParent;
45     SetFocusAble(false);
46     needStop_ = false;
47     parent_->ViewRegister(this);
48 }
49 
~AnimationLabel()50 AnimationLabel::~AnimationLabel()
51 {
52     needStop_ = true;
53     FreeBuffer();
54     int imgSize = imgList_.size();
55 
56     for (int i = 0; i < imgSize; i++) {
57         free(imgList_[i]);
58     }
59     imgList_.clear();
60 }
61 
SetStaticImg(int picId)62 void AnimationLabel::SetStaticImg(int picId)
63 {
64     staticShowId_ = picId;
65 }
66 
SetIsVisible(const bool visible)67 void AnimationLabel::SetIsVisible(const bool visible)
68 {
69     isVisible_ = visible;
70 }
71 
SetPlayMode(AnimationLabel::PlayMode mode)72 void AnimationLabel::SetPlayMode(AnimationLabel::PlayMode mode)
73 {
74     HDF_LOGD("%{public}s enter", __func__);
75     if (mode == AnimationLabel::PlayMode::ANIMATION_MODE) {
76         showStatic_ = false;
77     } else if (mode == AnimationLabel::PlayMode::STATIC_MODE) {
78         showStatic_ = true;
79     }
80 }
81 
UpdateLoop()82 void AnimationLabel::UpdateLoop()
83 {
84     HDF_LOGD("%{public}s enter", __func__);
85     unsigned int index = 0;
86 
87     while (!needStop_) {
88         if (showStatic_) {
89             usleep(SECOND_PER_MS * SECOND_PER_MS);
90         } else {
91             usleep(intervalMs_ * SECOND_PER_MS);
92         }
93         if (imgList_.size() <= 0) {
94             continue;
95         }
96 
97         HDF_LOGD("%{public}s, isVisible_ = %{public}d", __func__, isVisible_);
98         if (!isVisible_) {
99             continue;
100         }
101 
102         if (imgList_.size() <= index) {
103             index = 0;
104             needStop_ = true;
105         }
106         SyncBuffer();
107         mutex_.lock();
108 
109         if (showStatic_) {
110             if (staticShowId_ < staticImgSize_) {
111                 DrawSubView(0, 0, viewWidth_, viewHeight_, staticImgList_[staticShowId_]);
112             }
113         } else {
114             DrawSubView(0, 0, viewWidth_, viewHeight_, imgList_[index]);
115         }
116         mutex_.unlock();
117         if (parent_ != nullptr) {
118             parent_->OnDraw();
119         }
120         index++;
121     }
122     HDF_LOGD("%{public}s loop end.", __func__);
123 }
124 
AddImg(const std::string & imgFileName)125 void AnimationLabel::AddImg(const std::string& imgFileName)
126 {
127     HDF_LOGD("%{public}s enter", __func__);
128     mutex_.lock();
129     char* buf = static_cast<char*>(LoadPng(imgFileName));
130     imgList_.push_back(buf);
131     mutex_.unlock();
132 }
133 
AddStaticImg(const std::string & imgFileName)134 int AnimationLabel::AddStaticImg(const std::string& imgFileName)
135 {
136     HDF_LOGD("%{public}s enter", __func__);
137     int id = staticImgSize_;
138     mutex_.lock();
139     staticImgList_[id] = static_cast<char*>(LoadPng(imgFileName));
140     staticImgSize_++;
141     mutex_.unlock();
142     return id;
143 }
144 
LoadPngInternalWithFile(FILE * fp,png_structpp pngPtr,png_infopp pngInfoPtr,struct PictureAttr & attr)145 int AnimationLabel::LoadPngInternalWithFile(FILE* fp, png_structpp pngPtr, png_infopp pngInfoPtr,
146     struct PictureAttr& attr)
147 {
148     HDF_LOGD("%{public}s enter", __func__);
149     if (fp == nullptr) {
150         return -1;
151     }
152     uint8_t header[PNG_HEADER_SIZE];
153     size_t bytesRead = fread(header, 1, sizeof(header), fp);
154     if (bytesRead != sizeof(header)) {
155         HDF_LOGE("%{public}s, read header from file failed, errno=%{public}d", __func__, errno);
156         return -1;
157     }
158     if (png_sig_cmp(header, 0, sizeof(header))) {
159         HDF_LOGE("%{public}s, png file header is not valid.", __func__);
160         return -1;
161     }
162 
163     *pngPtr = png_create_read_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr);
164     if (*pngPtr == nullptr) {
165         HDF_LOGE("%{public}s, creat png struct failed.", __func__);
166         return -1;
167     }
168 
169     *pngInfoPtr = png_create_info_struct(*pngPtr);
170     if (*pngInfoPtr == nullptr) {
171         HDF_LOGE("%{public}s, creat png info failed.", __func__);
172         return -1;
173     }
174     png_init_io(*pngPtr, fp);
175     png_set_sig_bytes(*pngPtr, sizeof(header));
176     png_read_info(*pngPtr, *pngInfoPtr);
177     png_get_IHDR(*pngPtr, *pngInfoPtr, &attr.pictureWidth, &attr.pictureHeight, &attr.bitDepth, &attr.colorType,
178         nullptr, nullptr, nullptr);
179     attr.pictureChannels = png_get_channels(*pngPtr, *pngInfoPtr);
180     if (attr.bitDepth <= MAX_BIT_DEPTH && attr.pictureChannels == 1 && attr.colorType == PNG_COLOR_TYPE_PALETTE) {
181         // paletted images: expand to 8-bit RGB.  Note that we DON'T
182         // currently expand the tRNS chunk (if any) to an alpha
183         // channel, because minui doesn't support alpha channels in
184         // general.
185         png_set_palette_to_rgb(*pngPtr);
186         attr.pictureChannels = MAX_PICTURE_CHANNELS;
187     }
188 
189     if (attr.pictureChannels < MAX_PICTURE_CHANNELS) {
190         HDF_LOGE("%{public}s, need rgb format pic.", __func__);
191         return -1;
192     }
193     return 0;
194 }
195 
CopyPictureBuffer(struct PictureAttr & attr,char * pictureBufferTmp,BRGA888Pixel * pictureBuffer) const196 void AnimationLabel::CopyPictureBuffer(struct PictureAttr& attr, char* pictureBufferTmp,
197     BRGA888Pixel* pictureBuffer) const
198 {
199     HDF_LOGD("%{public}s enter", __func__);
200     int copyHeight = (viewHeight_ < static_cast<int>(attr.pictureHeight)) ? viewHeight_ :
201         static_cast<int>(attr.pictureHeight);
202     int copyWidth = (viewWidth_ < static_cast<int>(attr.pictureWidth)) ? viewWidth_ :
203         static_cast<int>(attr.pictureWidth);
204     auto* rgb = reinterpret_cast<RGB888Pixel*>(pictureBufferTmp);
205     for (int y = 0; y < copyHeight; y++) {
206         for (int x = 0; x < copyWidth; x++) {
207             unsigned int colorValue = rgb[x + y * attr.pictureWidth].r +
208             rgb[x + y * attr.pictureWidth].g + rgb[x + y * attr.pictureWidth].b;
209             if (colorValue > 0) {
210                 pictureBuffer[x + y * viewWidth_].r = rgb[x + y * attr.pictureWidth].r;
211                 pictureBuffer[x + y * viewWidth_].g = rgb[x + y * attr.pictureWidth].g;
212                 pictureBuffer[x + y * viewWidth_].b = rgb[x + y * attr.pictureWidth].b;
213                 pictureBuffer[x + y * viewWidth_].a = 0xff;
214             }
215         }
216     }
217 }
218 
LoadPng(const std::string & imgFileName)219 void* AnimationLabel::LoadPng(const std::string& imgFileName)
220 {
221     HDF_LOGD("%{public}s enter", __func__);
222     png_structp pngPtr = nullptr;
223     png_infop pngInfoPtr = nullptr;
224     struct PictureAttr attr {};
225     char* pictureBufferTmp = nullptr;
226     uint8_t* pictureRow = nullptr;
227 
228     FILE* fp = fopen(imgFileName.c_str(), "rb");
229     if (fp == nullptr) {
230         HDF_LOGD("%{public}s: open font file failed.", __func__);
231         return nullptr;
232     }
233     if (LoadPngInternalWithFile(fp, &pngPtr, &pngInfoPtr, attr) < 0) {
234         png_destroy_read_struct(&pngPtr, &pngInfoPtr, 0);
235         fclose(fp);
236         fp = nullptr;
237         return nullptr;
238     }
239     unsigned int pictureRowSize = attr.pictureWidth * attr.pictureChannels;
240     pictureBufferTmp = static_cast<char*>(malloc(pictureRowSize * attr.pictureHeight));
241     if (pictureBufferTmp == nullptr) {
242         HDF_LOGD("%{public}s: Allocate memory failed.", __func__);
243         if (fp != nullptr) {
244             fclose(fp);
245             fp = nullptr;
246         }
247         return nullptr;
248     }
249 
250     for (unsigned int y = 0; y < attr.pictureHeight; y++) {
251         pictureRow = reinterpret_cast<uint8_t*>((pictureBufferTmp) + y * pictureRowSize);
252         png_read_row(pngPtr, pictureRow, nullptr);
253     }
254 
255     BRGA888Pixel* pictureBuffer = HandleLoadPng(&fp, &pictureBufferTmp, attr);
256     return static_cast<void*>(pictureBuffer);
257 }
258 
HandleLoadPng(FILE ** fp,char ** pictureBufferTmp,struct PictureAttr & attr)259 View::BRGA888Pixel* AnimationLabel::HandleLoadPng(FILE** fp, char** pictureBufferTmp, struct PictureAttr& attr)
260 {
261     HDF_LOGD("%{public}s enter", __func__);
262     int pictureBufferSize = viewHeight_ * viewWidth_ * sizeof(BRGA888Pixel);
263     BRGA888Pixel* pictureBuffer = nullptr;
264     char* backgroundBuffer = static_cast<char*>(GetRawBuffer());
265 
266     pictureBuffer = static_cast<BRGA888Pixel*>(malloc(pictureBufferSize));
267     if (pictureBuffer == nullptr) {
268         HDF_LOGD("%{public}s: Allocate memory failed.", __func__);
269         if (*pictureBufferTmp != nullptr) {
270             free(*pictureBufferTmp);
271             *pictureBufferTmp = nullptr;
272         }
273         if (*fp != nullptr) {
274             fclose(*fp);
275             *fp = nullptr;
276         }
277         return nullptr;
278     }
279 
280     if (memcpy_s(reinterpret_cast<char*>(pictureBuffer), pictureBufferSize,
281         backgroundBuffer, pictureBufferSize) != EOK) {
282         if (*pictureBufferTmp != nullptr) {
283             free(*pictureBufferTmp);
284             *pictureBufferTmp = nullptr;
285         }
286         if (pictureBuffer != nullptr) {
287             free(pictureBuffer);
288             pictureBuffer = nullptr;
289         }
290         if (*fp != nullptr) {
291             fclose(*fp);
292             *fp = nullptr;
293         }
294         return nullptr;
295     }
296     CopyPictureBuffer(attr, *pictureBufferTmp, pictureBuffer);
297     free(*pictureBufferTmp);
298     *pictureBufferTmp = nullptr;
299     int ret = fclose(*fp);
300     if (ret < 0) {
301         HDF_LOGD("%{public}s: fp file close failed.", __func__);
302         return nullptr;
303     }
304     *fp = nullptr;
305     return pictureBuffer;
306 }
307 
SetInterval(int ms)308 void AnimationLabel::SetInterval(int ms)
309 {
310     HDF_LOGD("%{public}s enter", __func__);
311     intervalMs_ = static_cast<uint32_t>(ms);
312 }
313 }  // namespace V1_0
314 }  // namespace Battery
315 }  // namespace HDI
316 }  // namespace OHOS
317