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
33namespace OHOS {
34static const int NUMBER_TWO = 2;
35static const int READ_SIZE = 8192;
36static const int MAX_FILE_NAME = 512;
37static const int SLEEP_TIME_US = 30000;
38constexpr const float MAX_ZORDER = 100000.0f;
39
40constexpr const char* FILE_PREFIX = "file:/";
41const std::string BOOT_PIC_CONFIG_FILE = "config.json";
42const std::string BOOT_SOUND_PATH = "file://system/etc/graphic/bootsound.wav";
43const std::string BOOT_VIDEO_PATH = "file://system/etc/graphic/bootvideo.mp4";
44const std::string TYPE_VIDEO = "video";
45const std::string TYPE_SOUND = "sound";
46
47constexpr const char* BMS_COMPILE_STATUS = "bms.optimizing_apps.status";
48const std::string BMS_COMPILE_STATUS_BEGIN = "0";
49const std::string BMS_COMPILE_STATUS_END = "1";
50
51constexpr const char* BOOT_ANIMATION_STARTED = "bootevent.bootanimation.started";
52constexpr const char* BOOT_ANIMATION_READY = "bootevent.bootanimation.ready";
53constexpr const char* BOOT_ANIMATION_FINISHED = "bootevent.bootanimation.finished";
54constexpr const char* BOOT_COMPLETED = "bootevent.boot.completed";
55
56enum class BootStrategyType {
57    ASSOCIATIVE,
58    COMPATIBLE,
59    INDEPENDENT,
60};
61
62using MemStruct = struct MemStruct {
63public:
64    char* memBuffer = nullptr;
65    unsigned long bufsize = 0;
66    std::shared_ptr<Rosen::Drawing::Data> data_ = nullptr;
67    ~MemStruct()
68    {
69        if (data_ != nullptr) {
70            data_ = nullptr;
71        } else {
72            free(memBuffer);
73            memBuffer = nullptr;
74        }
75    }
76    void setOwnerShip(std::shared_ptr<Rosen::Drawing::Data>& data)
77    {
78        data_ = data;
79    }
80    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
96using ImageStruct = struct ImageStruct {
97public:
98    std::string fileName = {};
99    std::shared_ptr<Rosen::Drawing::Image> imageData = nullptr;
100    MemStruct memPtr;
101    ~ImageStruct()
102    {
103        imageData = nullptr;
104    }
105};
106using ImageStructVec = std::vector<std::shared_ptr<ImageStruct>>;
107
108using FrameRateConfig = struct FrameRateConfig {
109public:
110    int32_t frameRate = 30;
111};
112
113using VSyncCallback = std::function<void(void*)>;
114struct BootAnimationCallback {
115    void *userData;
116    VSyncCallback callback;
117};
118
119using PlayerParams = struct PlayerParams {
120#ifdef PLAYER_FRAMEWORK_ENABLE
121    OHOS::sptr<OHOS::Surface> surface;
122#endif
123    std::shared_ptr<OHOS::Rosen::RSSurface> rsSurface;
124    Rosen::ScreenId screenId;
125    bool soundEnabled = false;
126    BootAnimationCallback* callback;
127    std::string resPath;
128};
129
130void PostTask(std::function<void()> func, uint32_t delayTime = 0);
131
132bool IsFileExisted(const std::string& filePath);
133
134bool ParseBootConfig(const std::string& path, int32_t& duration, bool& isCompatible,
135    bool& isMultiDisplay, std::vector<BootAnimationConfig>& configs);
136
137void ParseNewConfigFile(cJSON* data, bool& isMultiDisplay, std::vector<BootAnimationConfig>& configs);
138
139void ParseOldConfigFile(cJSON* data, std::vector<BootAnimationConfig>& configs);
140
141void ParseVideoExtraPath(cJSON* data, BootAnimationConfig& config);
142
143void ParseBootDuration(cJSON* data, int32_t& duration);
144
145bool ReadZipFile(const std::string& srcFilePath, ImageStructVec& imgVec, FrameRateConfig& frameConfig);
146
147void SortZipFile(ImageStructVec& imgVec);
148
149bool ReadImageFile(const unzFile zipFile, const std::string& fileName, ImageStructVec& imgVec,
150    FrameRateConfig& frameConfig, unsigned long fileSize);
151
152bool ParseImageConfig(const char* fileBuffer, int totalsize, FrameRateConfig& frameConfig);
153
154bool CheckImageData(const std::string& fileName, std::shared_ptr<ImageStruct> imageStruct,
155    int32_t bufferLen, ImageStructVec& imgVec);
156
157bool CloseZipFile(const unzFile zipFile, bool ret);
158
159int32_t TransalteVp2Pixel(const int32_t sideLen, const int32_t vp);
160} // namespace OHOS
161
162#endif // FRAMEWORKS_BOOTANIMATION_INCLUDE_UTIL_H
163