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 #ifndef FRAMEWORKS_BOOTANIMATION_INCLUDE_UTIL_H
17 #define FRAMEWORKS_BOOTANIMATION_INCLUDE_UTIL_H
18 
19 #include "boot_animation_config.h"
20 #include "cJSON.h"
21 #include "contrib/minizip/unzip.h"
22 #include "contrib/minizip/zip.h"
23 #include <cstdint>
24 #include <dirent.h>
25 #include <functional>
26 #include "log.h"
27 #include <platform/ohos/rs_irender_service.h>
28 #include <sys/stat.h>
29 #include <sys/types.h>
30 #include <unistd.h>
31 #include "zlib.h"
32 
33 namespace OHOS {
34 static const int NUMBER_TWO = 2;
35 static const int READ_SIZE = 8192;
36 static const int MAX_FILE_NAME = 512;
37 static const int SLEEP_TIME_US = 30000;
38 constexpr const float MAX_ZORDER = 100000.0f;
39 
40 constexpr const char* FILE_PREFIX = "file:/";
41 const std::string BOOT_PIC_CONFIG_FILE = "config.json";
42 const std::string BOOT_SOUND_PATH = "file://system/etc/graphic/bootsound.wav";
43 const std::string BOOT_VIDEO_PATH = "file://system/etc/graphic/bootvideo.mp4";
44 const std::string TYPE_VIDEO = "video";
45 const std::string TYPE_SOUND = "sound";
46 
47 constexpr const char* BMS_COMPILE_STATUS = "bms.optimizing_apps.status";
48 const std::string BMS_COMPILE_STATUS_BEGIN = "0";
49 const std::string BMS_COMPILE_STATUS_END = "1";
50 
51 constexpr const char* BOOT_ANIMATION_STARTED = "bootevent.bootanimation.started";
52 constexpr const char* BOOT_ANIMATION_READY = "bootevent.bootanimation.ready";
53 constexpr const char* BOOT_ANIMATION_FINISHED = "bootevent.bootanimation.finished";
54 constexpr const char* BOOT_COMPLETED = "bootevent.boot.completed";
55 
56 enum class BootStrategyType {
57     ASSOCIATIVE,
58     COMPATIBLE,
59     INDEPENDENT,
60 };
61 
62 using MemStruct = struct MemStruct {
63 public:
64     char* memBuffer = nullptr;
65     unsigned long bufsize = 0;
66     std::shared_ptr<Rosen::Drawing::Data> data_ = nullptr;
~MemStructMemStruct67     ~MemStruct()
68     {
69         if (data_ != nullptr) {
70             data_ = nullptr;
71         } else {
72             free(memBuffer);
73             memBuffer = nullptr;
74         }
75     }
setOwnerShipMemStruct76     void setOwnerShip(std::shared_ptr<Rosen::Drawing::Data>& data)
77     {
78         data_ = data;
79     }
SetBufferSizeMemStruct80     void SetBufferSize(unsigned long ibufsize)
81     {
82         if (ibufsize == 0) {
83             LOGE("MemStruct SetBuffer size is invalid!");
84             return;
85         }
86         if (memBuffer == nullptr) {
87             bufsize = ibufsize + 1;
88             memBuffer = static_cast<char *>(malloc(bufsize + 1));
89         }
90         if (memBuffer == nullptr) {
91             LOGE("MemStruct malloc memBuffer failed!");
92         }
93     }
94 };
95 
96 using ImageStruct = struct ImageStruct {
97 public:
98     std::string fileName = {};
99     std::shared_ptr<Rosen::Drawing::Image> imageData = nullptr;
100     MemStruct memPtr;
~ImageStructImageStruct101     ~ImageStruct()
102     {
103         imageData = nullptr;
104     }
105 };
106 using ImageStructVec = std::vector<std::shared_ptr<ImageStruct>>;
107 
108 using FrameRateConfig = struct FrameRateConfig {
109 public:
110     int32_t frameRate = 30;
111 };
112 
113 using VSyncCallback = std::function<void(void*)>;
114 struct BootAnimationCallback {
115     void *userData;
116     VSyncCallback callback;
117 };
118 
119 using PlayerParams = struct PlayerParams {
120 #ifdef PLAYER_FRAMEWORK_ENABLE
121     OHOS::sptr<OHOS::Surface> surface;
122 #endif
123 #ifdef NEW_RENDER_CONTEXT
124     std::shared_ptr<OHOS::Rosen::RSRenderSurface> rsSurface;
125 #else
126     std::shared_ptr<OHOS::Rosen::RSSurface> rsSurface;
127 #endif
128     Rosen::ScreenId screenId;
129     bool soundEnabled = false;
130     BootAnimationCallback* callback;
131     std::string resPath;
132 };
133 
134 void PostTask(std::function<void()> func, uint32_t delayTime = 0);
135 
136 bool IsFileExisted(const std::string& filePath);
137 
138 bool ParseBootConfig(const std::string& path, int32_t& duration, bool& isCompatible,
139     bool& isMultiDisplay, std::vector<BootAnimationConfig>& configs);
140 
141 void ParseNewConfigFile(cJSON* data, bool& isMultiDisplay, std::vector<BootAnimationConfig>& configs);
142 
143 void ParseOldConfigFile(cJSON* data, std::vector<BootAnimationConfig>& configs);
144 
145 void ParseVideoExtraPath(cJSON* data, BootAnimationConfig& config);
146 
147 void ParseBootDuration(cJSON* data, int32_t& duration);
148 
149 bool ReadZipFile(const std::string& srcFilePath, ImageStructVec& imgVec, FrameRateConfig& frameConfig);
150 
151 void SortZipFile(ImageStructVec& imgVec);
152 
153 bool ReadImageFile(const unzFile zipFile, const std::string& fileName, ImageStructVec& imgVec,
154     FrameRateConfig& frameConfig, unsigned long fileSize);
155 
156 bool ParseImageConfig(const char* fileBuffer, int totalsize, FrameRateConfig& frameConfig);
157 
158 bool CheckImageData(const std::string& fileName, std::shared_ptr<ImageStruct> imageStruct,
159     int32_t bufferLen, ImageStructVec& imgVec);
160 
161 bool CloseZipFile(const unzFile zipFile, bool ret);
162 
163 int32_t TransalteVp2Pixel(const int32_t sideLen, const int32_t vp);
164 } // namespace OHOS
165 
166 #endif // FRAMEWORKS_BOOTANIMATION_INCLUDE_UTIL_H
167