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 #include <parameters.h> 33 34 namespace OHOS { 35 static const int NUMBER_TWO = 2; 36 static const int READ_SIZE = 8192; 37 static const int MAX_FILE_NAME = 512; 38 static const int SLEEP_TIME_US = 30000; 39 static const int SLEEP_TIME_US_10 = 10000; 40 static const int INVALID_VOLUME = -1; 41 static const int MIN_VOLUME = 0; 42 static const int MAX_VOLUME = 15; 43 constexpr const float MAX_ZORDER = 100000.0f; 44 constexpr const float HALF = 2.0f; 45 constexpr const float RATIO_PHONE = 360.0f; 46 constexpr const float RATIO_OTHER = 800.0f; 47 constexpr const float RATIO_PHONE_HEIGHT = 780.0f; 48 49 constexpr const char* FILE_PREFIX = "file:/"; 50 const std::string BOOT_PIC_CONFIG_FILE = "config.json"; 51 const std::string BOOT_SOUND_PATH = "file://system/etc/graphic/bootsound.wav"; 52 const std::string BOOT_VIDEO_PATH = "file://system/etc/graphic/bootvideo.mp4"; 53 const std::string TYPE_VIDEO = "video"; 54 const std::string TYPE_SOUND = "sound"; 55 56 const std::string HING_STATUS_INFO_PATH = "/sys/class/sensors/hinge_sensor/hinge_status_info"; 57 58 constexpr const char* BOOT_ANIMATION_STARTED = "bootevent.bootanimation.started"; 59 constexpr const char* BOOT_ANIMATION_READY = "bootevent.bootanimation.ready"; 60 constexpr const char* BOOT_ANIMATION_FINISHED = "bootevent.bootanimation.finished"; 61 constexpr const char* BOOT_COMPLETED = "bootevent.boot.completed"; 62 constexpr const char* BOOT_SOUND = "const.bootanimation.bootsound"; 63 const std::string DEVICE_TYPE_PHONE = "phone"; 64 const std::string DEVICE_TYPE_WEARABLE = "wearable"; 65 66 const std::string FOLD_SCREEN_TYPE = system::GetParameter("const.window.foldscreen.type", ""); 67 68 enum class BootStrategyType { 69 ASSOCIATIVE, 70 COMPATIBLE, 71 INDEPENDENT, 72 }; 73 74 using MemStruct = struct MemStruct { 75 public: 76 char* memBuffer = nullptr; 77 unsigned long bufsize = 0; 78 std::shared_ptr<Rosen::Drawing::Data> data_ = nullptr; ~MemStructMemStruct79 ~MemStruct() 80 { 81 if (data_ != nullptr) { 82 data_ = nullptr; 83 } else { 84 free(memBuffer); 85 memBuffer = nullptr; 86 } 87 } setOwnerShipMemStruct88 void setOwnerShip(std::shared_ptr<Rosen::Drawing::Data>& data) 89 { 90 data_ = data; 91 } SetBufferSizeMemStruct92 void SetBufferSize(unsigned long ibufsize) 93 { 94 if (ibufsize == 0) { 95 LOGE("MemStruct SetBuffer size is invalid!"); 96 return; 97 } 98 if (memBuffer == nullptr) { 99 bufsize = ibufsize + 1; 100 memBuffer = static_cast<char *>(malloc(bufsize + 1)); 101 } 102 if (memBuffer == nullptr) { 103 LOGE("MemStruct malloc memBuffer failed!"); 104 } 105 } 106 }; 107 108 using ImageStruct = struct ImageStruct { 109 public: 110 std::string fileName = {}; 111 std::shared_ptr<Rosen::Drawing::Image> imageData = nullptr; 112 MemStruct memPtr; ~ImageStructImageStruct113 ~ImageStruct() 114 { 115 imageData = nullptr; 116 } 117 }; 118 using ImageStructVec = std::vector<std::shared_ptr<ImageStruct>>; 119 120 using FrameRateConfig = struct FrameRateConfig { 121 public: 122 int32_t frameRate = 30; 123 }; 124 125 using VSyncCallback = std::function<void(void*)>; 126 struct BootAnimationCallback { 127 void *userData; 128 VSyncCallback callback; 129 }; 130 131 using PlayerParams = struct PlayerParams { 132 #ifdef PLAYER_FRAMEWORK_ENABLE 133 OHOS::sptr<OHOS::Surface> surface; 134 #endif 135 std::shared_ptr<OHOS::Rosen::RSSurface> rsSurface; 136 Rosen::ScreenId screenId; 137 bool soundEnabled = false; 138 BootAnimationCallback* callback; 139 std::string resPath; 140 }; 141 142 void PostTask(std::function<void()> func, uint32_t delayTime = 0); 143 144 bool IsFileExisted(const std::string& filePath); 145 146 bool ParseBootConfig(const std::string& path, int32_t& duration, bool& isCompatible, 147 bool& isMultiDisplay, std::vector<BootAnimationConfig>& configs); 148 149 void ParseNewConfigFile(cJSON* data, bool& isMultiDisplay, std::vector<BootAnimationConfig>& configs); 150 151 void ParseOldConfigFile(cJSON* data, std::vector<BootAnimationConfig>& configs); 152 153 void ParseVideoExtraPath(cJSON* data, BootAnimationConfig& config); 154 155 void ParseBootDuration(cJSON* data, int32_t& duration); 156 157 bool ReadZipFile(const std::string& srcFilePath, ImageStructVec& imgVec, FrameRateConfig& frameConfig); 158 159 void SortZipFile(ImageStructVec& imgVec); 160 161 bool ReadImageFile(const unzFile zipFile, const std::string& fileName, ImageStructVec& imgVec, 162 FrameRateConfig& frameConfig, unsigned long fileSize); 163 164 bool ParseImageConfig(const char* fileBuffer, int totalsize, FrameRateConfig& frameConfig); 165 166 bool CheckImageData(const std::string& fileName, std::shared_ptr<ImageStruct> imageStruct, 167 int32_t bufferLen, ImageStructVec& imgVec); 168 169 bool CloseZipFile(const unzFile zipFile, bool ret); 170 171 int32_t TranslateVp2Pixel(const int32_t sideLen, const int32_t vp); 172 173 int32_t TranslateVp2Pixel(const int32_t sideLen, const int32_t vp, const float ratio); 174 175 std::string ReadFile(const std::string &filePath); 176 177 std::string GetHingeStatus(); 178 179 int64_t GetSystemCurrentTime(); 180 181 std::string GetDeviceType(); 182 183 void ParseProgressConfig(const std::string& path, std::map<int32_t, BootAnimationProgressConfig>& configs); 184 185 void ParseProgressData(cJSON* data, std::map<int32_t, BootAnimationProgressConfig>& configs); 186 187 cJSON* ParseFileConfig(const std::string& path); 188 } // namespace OHOS 189 190 #endif // FRAMEWORKS_BOOTANIMATION_INCLUDE_UTIL_H 191