• 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 "cJSON.h"
17 #include "util.h"
18 
19 #include <event_handler.h>
20 #include <securec.h>
21 #include <sys/time.h>
22 #include <include/codec/SkCodec.h>
23 
24 namespace OHOS {
GetNowTime()25 int64_t GetNowTime()
26 {
27     struct timeval start = {};
28     gettimeofday(&start, nullptr);
29     constexpr uint32_t secToUsec = 1000 * 1000;
30     return static_cast<int64_t>(start.tv_sec) * secToUsec + start.tv_usec;
31 }
32 
PostTask(std::function<void ()> func,uint32_t delayTime)33 void PostTask(std::function<void()> func, uint32_t delayTime)
34 {
35     auto handler = AppExecFwk::EventHandler::Current();
36     if (handler) {
37         handler->PostTask(func, delayTime);
38     }
39 }
40 
ReadJsonConfig(const char * filebuffer,int totalsize,BootAniConfig & aniconfig)41 bool ReadJsonConfig(const char* filebuffer, int totalsize, BootAniConfig& aniconfig)
42 {
43     std::string JParamsString;
44     JParamsString.assign(filebuffer, totalsize);
45     cJSON* overallData = cJSON_Parse(JParamsString.c_str());
46     if (overallData == nullptr) {
47         LOGE("The config json file fails to compile.");
48         return false;
49     }
50     cJSON* frameRate = cJSON_GetObjectItem(overallData, "FrameRate");
51     if (frameRate != nullptr) {
52         aniconfig.frameRate = frameRate->valueint;
53         LOGI("freq: %{public}d", aniconfig.frameRate);
54     }
55     cJSON_Delete(overallData);
56     return true;
57 }
58 
GenImageData(const std::string & filename,std::shared_ptr<ImageStruct> imagetruct,int32_t bufferlen,ImageStructVec & outBgImgVec)59 bool GenImageData(const std::string& filename, std::shared_ptr<ImageStruct> imagetruct, int32_t bufferlen,
60     ImageStructVec& outBgImgVec)
61 {
62     if (imagetruct->memPtr.memBuffer == nullptr) {
63         LOGE("Json File buffer is null.");
64         return false;
65     }
66     auto skData = SkData::MakeFromMalloc(imagetruct->memPtr.memBuffer, bufferlen);
67     if (skData == nullptr) {
68         LOGE("skdata memory data is null. update data failed");
69         return false;
70     }
71     imagetruct->memPtr.setOwnerShip(skData);
72     auto codec = SkCodec::MakeFromData(skData);
73     imagetruct->fileName = filename;
74     imagetruct->imageData = SkImage::MakeFromEncoded(skData);
75     outBgImgVec.push_back(imagetruct);
76     return true;
77 }
78 
ReadCurrentFile(const unzFile zipfile,const std::string & filename,ImageStructVec & outBgImgVec,BootAniConfig & aniconfig,unsigned long fileSize)79 bool ReadCurrentFile(const unzFile zipfile, const std::string& filename, ImageStructVec& outBgImgVec,
80     BootAniConfig& aniconfig, unsigned long fileSize)
81 {
82     if (zipfile == nullptr) {
83         LOGE("Readzip Json zipfile is null.");
84         return false;
85     }
86     int readlen = UNZ_OK;
87     int totalLen = 0;
88     int ifileSize = static_cast<int>(fileSize);
89     char readBuffer[READ_SIZE] = {0};
90     std::shared_ptr<ImageStruct> imagestrct = std::make_shared<ImageStruct>();
91     imagestrct->memPtr.SetBufferSize(fileSize);
92     do {
93         readlen = unzReadCurrentFile(zipfile, readBuffer, READ_SIZE);
94         if (readlen < 0) {
95             LOGE("Readzip readCurrFile failed");
96             return false;
97         }
98         if (imagestrct->memPtr.memBuffer == nullptr) {
99             LOGE("Readzip memPtr is null.");
100             return false;
101         }
102         if (memcpy_s(imagestrct->memPtr.memBuffer + totalLen, ifileSize - totalLen, \
103             readBuffer, readlen) == EOK) {
104             totalLen += readlen;
105         }
106     } while (readlen > 0);
107 
108     if (totalLen > 0) {
109         LOGD("filename:%{public}s fileSize:%{public}d totalLen:%{public}d", filename.c_str(), ifileSize, totalLen);
110         if (strstr(filename.c_str(), BOOT_PIC_CONFIGFILE.c_str()) != nullptr) {
111             ReadJsonConfig(imagestrct->memPtr.memBuffer, totalLen, aniconfig);
112         } else {
113             GenImageData(filename, imagestrct, totalLen, outBgImgVec);
114         }
115     }
116     return true;
117 }
118 
SortZipFile(ImageStructVec & outBgImgVec)119 void SortZipFile(ImageStructVec& outBgImgVec)
120 {
121     if (outBgImgVec.size() == 0) {
122         return;
123     }
124 
125     sort(outBgImgVec.begin(), outBgImgVec.end(), [](std::shared_ptr<ImageStruct> image1,
126         std::shared_ptr<ImageStruct> image2)
127         -> bool {return image1->fileName < image2->fileName;});
128 }
129 
ReadZipFile(const std::string & srcFilePath,ImageStructVec & outBgImgVec,BootAniConfig & aniconfig)130 bool ReadZipFile(const std::string& srcFilePath, ImageStructVec& outBgImgVec, BootAniConfig& aniconfig)
131 {
132     unzFile zipfile = unzOpen2(srcFilePath.c_str(), nullptr);
133     if (zipfile == nullptr) {
134         return false;
135     }
136     unz_global_info globalInfo;
137     if (unzGetGlobalInfo(zipfile, &globalInfo) != UNZ_OK) {
138         unzClose(zipfile);
139         return false;
140     }
141     LOGD("Readzip zip file num: %{public}ld", globalInfo.number_entry);
142     for (unsigned long i = 0; i < globalInfo.number_entry; ++i) {
143         unz_file_info fileInfo;
144         char filename[MAX_FILE_NAME] = {0};
145         if (unzGetCurrentFileInfo(zipfile, &fileInfo, filename, MAX_FILE_NAME, nullptr, 0, nullptr, 0) != UNZ_OK) {
146             unzClose(zipfile);
147             return false;
148         }
149         if (filename[strlen(filename) - 1] != '/') {
150             if (unzOpenCurrentFile(zipfile) != UNZ_OK) {
151                 unzClose(zipfile);
152                 return false;
153             }
154             std::string strfilename = std::string(filename);
155             size_t npos = strfilename.find_last_of("//");
156             if (npos != std::string::npos) {
157                 strfilename = strfilename.substr(npos + 1, strfilename.length());
158             }
159             if (!ReadCurrentFile(zipfile, strfilename, outBgImgVec, aniconfig, fileInfo.uncompressed_size)) {
160                 LOGE("Readzip deal single file failed");
161                 unzCloseCurrentFile(zipfile);
162                 unzClose(zipfile);
163                 return false;
164             }
165             unzCloseCurrentFile(zipfile);
166         }
167         if (i < (globalInfo.number_entry - 1)) {
168             if (unzGoToNextFile(zipfile) != UNZ_OK) {
169                 unzClose(zipfile);
170                 return false;
171             }
172         }
173     }
174     return true;
175 }
176 
WaitRenderServiceInit()177 void WaitRenderServiceInit()
178 {
179     while (true) {
180         sptr<ISystemAbilityManager> samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
181         if (samgr == nullptr) {
182             LOGI("samgr is null");
183             sleep(1);
184             continue;
185         }
186         sptr<IRemoteObject> remoteObject = samgr->GetSystemAbility(RENDER_SERVICE);
187         if (remoteObject != nullptr) {
188             LOGI("renderService is inited");
189             break;
190         } else {
191             LOGI("renderService is not inited, wait");
192             sleep(1);
193         }
194     }
195 }
196 } // namespace OHOS
197