• 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 #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 static const int INVALID_VOLUME = -1;
39 static const int MIN_VOLUME = 0;
40 static const int MAX_VOLUME = 15;
41 constexpr const float MAX_ZORDER = 100000.0f;
42 
43 constexpr const char* FILE_PREFIX = "file:/";
44 const std::string BOOT_PIC_CONFIG_FILE = "config.json";
45 const std::string BOOT_SOUND_PATH = "file://system/etc/graphic/bootsound.wav";
46 const std::string BOOT_VIDEO_PATH = "file://system/etc/graphic/bootvideo.mp4";
47 const std::string TYPE_VIDEO = "video";
48 const std::string TYPE_SOUND = "sound";
49 
50 constexpr const char* BMS_COMPILE_STATUS = "bms.optimizing_apps.status";
51 const std::string BMS_COMPILE_STATUS_BEGIN = "0";
52 const std::string BMS_COMPILE_STATUS_END = "1";
53 const std::string HING_STATUS_INFO_PATH = "/sys/class/sensors/hinge_sensor/hinge_status_info";
54 
55 constexpr const char* BOOT_ANIMATION_STARTED = "bootevent.bootanimation.started";
56 constexpr const char* BOOT_ANIMATION_READY = "bootevent.bootanimation.ready";
57 constexpr const char* BOOT_ANIMATION_FINISHED = "bootevent.bootanimation.finished";
58 constexpr const char* BOOT_COMPLETED = "bootevent.boot.completed";
59 constexpr const char* BOOT_SOUND = "const.bootanimation.bootsound";
60 
61 enum class BootStrategyType {
62     ASSOCIATIVE,
63     COMPATIBLE,
64     INDEPENDENT,
65 };
66 
67 using MemStruct = struct MemStruct {
68 public:
69     char* memBuffer = nullptr;
70     unsigned long bufsize = 0;
71     std::shared_ptr<Rosen::Drawing::Data> data_ = nullptr;
~MemStructMemStruct72     ~MemStruct()
73     {
74         if (data_ != nullptr) {
75             data_ = nullptr;
76         } else {
77             free(memBuffer);
78             memBuffer = nullptr;
79         }
80     }
setOwnerShipMemStruct81     void setOwnerShip(std::shared_ptr<Rosen::Drawing::Data>& data)
82     {
83         data_ = data;
84     }
SetBufferSizeMemStruct85     void SetBufferSize(unsigned long ibufsize)
86     {
87         if (ibufsize == 0) {
88             LOGE("MemStruct SetBuffer size is invalid!");
89             return;
90         }
91         if (memBuffer == nullptr) {
92             bufsize = ibufsize + 1;
93             memBuffer = static_cast<char *>(malloc(bufsize + 1));
94         }
95         if (memBuffer == nullptr) {
96             LOGE("MemStruct malloc memBuffer failed!");
97         }
98     }
99 };
100 
101 using ImageStruct = struct ImageStruct {
102 public:
103     std::string fileName = {};
104     std::shared_ptr<Rosen::Drawing::Image> imageData = nullptr;
105     MemStruct memPtr;
~ImageStructImageStruct106     ~ImageStruct()
107     {
108         imageData = nullptr;
109     }
110 };
111 using ImageStructVec = std::vector<std::shared_ptr<ImageStruct>>;
112 
113 using FrameRateConfig = struct FrameRateConfig {
114 public:
115     int32_t frameRate = 30;
116 };
117 
118 using VSyncCallback = std::function<void(void*)>;
119 struct BootAnimationCallback {
120     void *userData;
121     VSyncCallback callback;
122 };
123 
124 using PlayerParams = struct PlayerParams {
125 #ifdef PLAYER_FRAMEWORK_ENABLE
126     OHOS::sptr<OHOS::Surface> surface;
127 #endif
128     std::shared_ptr<OHOS::Rosen::RSSurface> rsSurface;
129     Rosen::ScreenId screenId;
130     bool soundEnabled = false;
131     BootAnimationCallback* callback;
132     std::string resPath;
133 };
134 
135 void PostTask(std::function<void()> func, uint32_t delayTime = 0);
136 
137 bool IsFileExisted(const std::string& filePath);
138 
139 bool ParseBootConfig(const std::string& path, int32_t& duration, bool& isCompatible,
140     bool& isMultiDisplay, std::vector<BootAnimationConfig>& configs);
141 
142 void ParseNewConfigFile(cJSON* data, bool& isMultiDisplay, std::vector<BootAnimationConfig>& configs);
143 
144 void ParseOldConfigFile(cJSON* data, std::vector<BootAnimationConfig>& configs);
145 
146 void ParseVideoExtraPath(cJSON* data, BootAnimationConfig& config);
147 
148 void ParseBootDuration(cJSON* data, int32_t& duration);
149 
150 bool ReadZipFile(const std::string& srcFilePath, ImageStructVec& imgVec, FrameRateConfig& frameConfig);
151 
152 void SortZipFile(ImageStructVec& imgVec);
153 
154 bool ReadImageFile(const unzFile zipFile, const std::string& fileName, ImageStructVec& imgVec,
155     FrameRateConfig& frameConfig, unsigned long fileSize);
156 
157 bool ParseImageConfig(const char* fileBuffer, int totalsize, FrameRateConfig& frameConfig);
158 
159 bool CheckImageData(const std::string& fileName, std::shared_ptr<ImageStruct> imageStruct,
160     int32_t bufferLen, ImageStructVec& imgVec);
161 
162 bool CloseZipFile(const unzFile zipFile, bool ret);
163 
164 int32_t TransalteVp2Pixel(const int32_t sideLen, const int32_t vp);
165 
166 std::string ReadFile(const std::string &filePath);
167 
168 std::string GetHingeStatus();
169 
170 int64_t GetSystemCurrentTime();
171 } // namespace OHOS
172 
173 #endif // FRAMEWORKS_BOOTANIMATION_INCLUDE_UTIL_H
174