1e9297d28Sopenharmony_ci/* 2e9297d28Sopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd. 3e9297d28Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4e9297d28Sopenharmony_ci * you may not use this file except in compliance with the License. 5e9297d28Sopenharmony_ci * You may obtain a copy of the License at 6e9297d28Sopenharmony_ci * 7e9297d28Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8e9297d28Sopenharmony_ci * 9e9297d28Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10e9297d28Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11e9297d28Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12e9297d28Sopenharmony_ci * See the License for the specific language governing permissions and 13e9297d28Sopenharmony_ci * limitations under the License. 14e9297d28Sopenharmony_ci */ 15e9297d28Sopenharmony_ci 16e9297d28Sopenharmony_ci#ifndef FRAMEWORKS_BOOTANIMATION_INCLUDE_UTIL_H 17e9297d28Sopenharmony_ci#define FRAMEWORKS_BOOTANIMATION_INCLUDE_UTIL_H 18e9297d28Sopenharmony_ci 19e9297d28Sopenharmony_ci#include "boot_animation_config.h" 20e9297d28Sopenharmony_ci#include "cJSON.h" 21e9297d28Sopenharmony_ci#include "contrib/minizip/unzip.h" 22e9297d28Sopenharmony_ci#include "contrib/minizip/zip.h" 23e9297d28Sopenharmony_ci#include <cstdint> 24e9297d28Sopenharmony_ci#include <dirent.h> 25e9297d28Sopenharmony_ci#include <functional> 26e9297d28Sopenharmony_ci#include "log.h" 27e9297d28Sopenharmony_ci#include <platform/ohos/rs_irender_service.h> 28e9297d28Sopenharmony_ci#include <sys/stat.h> 29e9297d28Sopenharmony_ci#include <sys/types.h> 30e9297d28Sopenharmony_ci#include <unistd.h> 31e9297d28Sopenharmony_ci#include "zlib.h" 32e9297d28Sopenharmony_ci 33e9297d28Sopenharmony_cinamespace OHOS { 34e9297d28Sopenharmony_cistatic const int NUMBER_TWO = 2; 35e9297d28Sopenharmony_cistatic const int READ_SIZE = 8192; 36e9297d28Sopenharmony_cistatic const int MAX_FILE_NAME = 512; 37e9297d28Sopenharmony_cistatic const int SLEEP_TIME_US = 30000; 38e9297d28Sopenharmony_ciconstexpr const float MAX_ZORDER = 100000.0f; 39e9297d28Sopenharmony_ci 40e9297d28Sopenharmony_ciconstexpr const char* FILE_PREFIX = "file:/"; 41e9297d28Sopenharmony_ciconst std::string BOOT_PIC_CONFIG_FILE = "config.json"; 42e9297d28Sopenharmony_ciconst std::string BOOT_SOUND_PATH = "file://system/etc/graphic/bootsound.wav"; 43e9297d28Sopenharmony_ciconst std::string BOOT_VIDEO_PATH = "file://system/etc/graphic/bootvideo.mp4"; 44e9297d28Sopenharmony_ciconst std::string TYPE_VIDEO = "video"; 45e9297d28Sopenharmony_ciconst std::string TYPE_SOUND = "sound"; 46e9297d28Sopenharmony_ci 47e9297d28Sopenharmony_ciconstexpr const char* BMS_COMPILE_STATUS = "bms.optimizing_apps.status"; 48e9297d28Sopenharmony_ciconst std::string BMS_COMPILE_STATUS_BEGIN = "0"; 49e9297d28Sopenharmony_ciconst std::string BMS_COMPILE_STATUS_END = "1"; 50e9297d28Sopenharmony_ci 51e9297d28Sopenharmony_ciconstexpr const char* BOOT_ANIMATION_STARTED = "bootevent.bootanimation.started"; 52e9297d28Sopenharmony_ciconstexpr const char* BOOT_ANIMATION_READY = "bootevent.bootanimation.ready"; 53e9297d28Sopenharmony_ciconstexpr const char* BOOT_ANIMATION_FINISHED = "bootevent.bootanimation.finished"; 54e9297d28Sopenharmony_ciconstexpr const char* BOOT_COMPLETED = "bootevent.boot.completed"; 55e9297d28Sopenharmony_ci 56e9297d28Sopenharmony_cienum class BootStrategyType { 57e9297d28Sopenharmony_ci ASSOCIATIVE, 58e9297d28Sopenharmony_ci COMPATIBLE, 59e9297d28Sopenharmony_ci INDEPENDENT, 60e9297d28Sopenharmony_ci}; 61e9297d28Sopenharmony_ci 62e9297d28Sopenharmony_ciusing MemStruct = struct MemStruct { 63e9297d28Sopenharmony_cipublic: 64e9297d28Sopenharmony_ci char* memBuffer = nullptr; 65e9297d28Sopenharmony_ci unsigned long bufsize = 0; 66e9297d28Sopenharmony_ci std::shared_ptr<Rosen::Drawing::Data> data_ = nullptr; 67e9297d28Sopenharmony_ci ~MemStruct() 68e9297d28Sopenharmony_ci { 69e9297d28Sopenharmony_ci if (data_ != nullptr) { 70e9297d28Sopenharmony_ci data_ = nullptr; 71e9297d28Sopenharmony_ci } else { 72e9297d28Sopenharmony_ci free(memBuffer); 73e9297d28Sopenharmony_ci memBuffer = nullptr; 74e9297d28Sopenharmony_ci } 75e9297d28Sopenharmony_ci } 76e9297d28Sopenharmony_ci void setOwnerShip(std::shared_ptr<Rosen::Drawing::Data>& data) 77e9297d28Sopenharmony_ci { 78e9297d28Sopenharmony_ci data_ = data; 79e9297d28Sopenharmony_ci } 80e9297d28Sopenharmony_ci void SetBufferSize(unsigned long ibufsize) 81e9297d28Sopenharmony_ci { 82e9297d28Sopenharmony_ci if (ibufsize == 0) { 83e9297d28Sopenharmony_ci LOGE("MemStruct SetBuffer size is invalid!"); 84e9297d28Sopenharmony_ci return; 85e9297d28Sopenharmony_ci } 86e9297d28Sopenharmony_ci if (memBuffer == nullptr) { 87e9297d28Sopenharmony_ci bufsize = ibufsize + 1; 88e9297d28Sopenharmony_ci memBuffer = static_cast<char *>(malloc(bufsize + 1)); 89e9297d28Sopenharmony_ci } 90e9297d28Sopenharmony_ci if (memBuffer == nullptr) { 91e9297d28Sopenharmony_ci LOGE("MemStruct malloc memBuffer failed!"); 92e9297d28Sopenharmony_ci } 93e9297d28Sopenharmony_ci } 94e9297d28Sopenharmony_ci}; 95e9297d28Sopenharmony_ci 96e9297d28Sopenharmony_ciusing ImageStruct = struct ImageStruct { 97e9297d28Sopenharmony_cipublic: 98e9297d28Sopenharmony_ci std::string fileName = {}; 99e9297d28Sopenharmony_ci std::shared_ptr<Rosen::Drawing::Image> imageData = nullptr; 100e9297d28Sopenharmony_ci MemStruct memPtr; 101e9297d28Sopenharmony_ci ~ImageStruct() 102e9297d28Sopenharmony_ci { 103e9297d28Sopenharmony_ci imageData = nullptr; 104e9297d28Sopenharmony_ci } 105e9297d28Sopenharmony_ci}; 106e9297d28Sopenharmony_ciusing ImageStructVec = std::vector<std::shared_ptr<ImageStruct>>; 107e9297d28Sopenharmony_ci 108e9297d28Sopenharmony_ciusing FrameRateConfig = struct FrameRateConfig { 109e9297d28Sopenharmony_cipublic: 110e9297d28Sopenharmony_ci int32_t frameRate = 30; 111e9297d28Sopenharmony_ci}; 112e9297d28Sopenharmony_ci 113e9297d28Sopenharmony_ciusing VSyncCallback = std::function<void(void*)>; 114e9297d28Sopenharmony_cistruct BootAnimationCallback { 115e9297d28Sopenharmony_ci void *userData; 116e9297d28Sopenharmony_ci VSyncCallback callback; 117e9297d28Sopenharmony_ci}; 118e9297d28Sopenharmony_ci 119e9297d28Sopenharmony_ciusing PlayerParams = struct PlayerParams { 120e9297d28Sopenharmony_ci#ifdef PLAYER_FRAMEWORK_ENABLE 121e9297d28Sopenharmony_ci OHOS::sptr<OHOS::Surface> surface; 122e9297d28Sopenharmony_ci#endif 123e9297d28Sopenharmony_ci std::shared_ptr<OHOS::Rosen::RSSurface> rsSurface; 124e9297d28Sopenharmony_ci Rosen::ScreenId screenId; 125e9297d28Sopenharmony_ci bool soundEnabled = false; 126e9297d28Sopenharmony_ci BootAnimationCallback* callback; 127e9297d28Sopenharmony_ci std::string resPath; 128e9297d28Sopenharmony_ci}; 129e9297d28Sopenharmony_ci 130e9297d28Sopenharmony_civoid PostTask(std::function<void()> func, uint32_t delayTime = 0); 131e9297d28Sopenharmony_ci 132e9297d28Sopenharmony_cibool IsFileExisted(const std::string& filePath); 133e9297d28Sopenharmony_ci 134e9297d28Sopenharmony_cibool ParseBootConfig(const std::string& path, int32_t& duration, bool& isCompatible, 135e9297d28Sopenharmony_ci bool& isMultiDisplay, std::vector<BootAnimationConfig>& configs); 136e9297d28Sopenharmony_ci 137e9297d28Sopenharmony_civoid ParseNewConfigFile(cJSON* data, bool& isMultiDisplay, std::vector<BootAnimationConfig>& configs); 138e9297d28Sopenharmony_ci 139e9297d28Sopenharmony_civoid ParseOldConfigFile(cJSON* data, std::vector<BootAnimationConfig>& configs); 140e9297d28Sopenharmony_ci 141e9297d28Sopenharmony_civoid ParseVideoExtraPath(cJSON* data, BootAnimationConfig& config); 142e9297d28Sopenharmony_ci 143e9297d28Sopenharmony_civoid ParseBootDuration(cJSON* data, int32_t& duration); 144e9297d28Sopenharmony_ci 145e9297d28Sopenharmony_cibool ReadZipFile(const std::string& srcFilePath, ImageStructVec& imgVec, FrameRateConfig& frameConfig); 146e9297d28Sopenharmony_ci 147e9297d28Sopenharmony_civoid SortZipFile(ImageStructVec& imgVec); 148e9297d28Sopenharmony_ci 149e9297d28Sopenharmony_cibool ReadImageFile(const unzFile zipFile, const std::string& fileName, ImageStructVec& imgVec, 150e9297d28Sopenharmony_ci FrameRateConfig& frameConfig, unsigned long fileSize); 151e9297d28Sopenharmony_ci 152e9297d28Sopenharmony_cibool ParseImageConfig(const char* fileBuffer, int totalsize, FrameRateConfig& frameConfig); 153e9297d28Sopenharmony_ci 154e9297d28Sopenharmony_cibool CheckImageData(const std::string& fileName, std::shared_ptr<ImageStruct> imageStruct, 155e9297d28Sopenharmony_ci int32_t bufferLen, ImageStructVec& imgVec); 156e9297d28Sopenharmony_ci 157e9297d28Sopenharmony_cibool CloseZipFile(const unzFile zipFile, bool ret); 158e9297d28Sopenharmony_ci 159e9297d28Sopenharmony_ciint32_t TransalteVp2Pixel(const int32_t sideLen, const int32_t vp); 160e9297d28Sopenharmony_ci} // namespace OHOS 161e9297d28Sopenharmony_ci 162e9297d28Sopenharmony_ci#endif // FRAMEWORKS_BOOTANIMATION_INCLUDE_UTIL_H 163