本文來源電子發(fā)燒友社區(qū),作者:離北況歸, 帖子地址:https://bbs.elecfans.com/jishu_2310076_1_1.html
從3.1release開始使用的開機動畫(視頻演示看作者原帖子文章)
對于筆者這個openharmony高校開發(fā)者來說,開源的樂趣在于折騰。在此記錄下折騰OpenHarmony標準系統(tǒng)開機動畫的過程,ohos的開機動畫從3.1beta之后經(jīng)歷過一次變更。下面展示一下變更前和變更后的開機動畫。
OpenHarmony3.1beta版本使用的舊開機動畫
- 這個開機動畫和HarmonyOS的有異曲同工之妙。
從OpenHarmony3.1release版本開始使用的新開機動畫
帶有開機聲效的視頻如文章開頭。
新版Logo設(shè)計者是劉石老師,劉石老師在《新發(fā)布的 OpenHarmony Logo 竟有這么多的故事!》一文中分享了新版logo背后的故事
兩個版本開機動畫實現(xiàn)方式不一樣
3.2beta3開機動畫模塊源碼分析
- graphic圖形子系統(tǒng)包含了開機動畫模塊,開機動畫模塊在ohos3.2beta3源碼下foundation/graphic/graphic_2d/frameworks/bootanimation,開機動畫模塊bootanimation源碼結(jié)構(gòu)
├── BUILD.gn
├── data
│ ├── bootanimation_tool
│ │ ├── README.md
│ │ ├── raw_maker.py
│ │ └── raw_player.py
│ ├── bootpic.zip # 包括了開機動畫的所有圖片幀和json播放配置文件
│ ├── bootsound.wav # 開機聲效
│ └── generate_raw.sh
├── include # 開機動畫模塊的頭文件
│ ├── boot_animation.h
│ ├── log.h
│ └── util.h
└── src # 開機動畫的源文件
├── boot_animation.cpp
├── main.cpp
└── util.cpp
-
bootpic.zip打開后內(nèi)容如下
-
OH_bootAni compressed內(nèi)容如下,其中包括了150張開機動畫的圖片幀。
- config.json內(nèi)容如下,在這個文件中設(shè)置開機動畫的播放幀率,范圍為30~60幀,此處視頻幀率被設(shè)置為30幀。OH_bootAni compressed文件夾內(nèi)有150張圖片,所以開機動畫的播放時間為5秒。
-
OH_bootAni compressed內(nèi)容如下,其中包括了150張開機動畫的圖片幀。
{
"Remark": "FrameRate Support 30, 60 frame rate configuration",
"FrameRate": 30
}
開機動畫源文件bootpic.zip和bootsound.wav等作為配置文件打包至開發(fā)板/system/etc/init目錄下
閱讀bootanimation目錄下BUILD.gn可以知道bootpic.zip和bootsound.wav等作為配置文件打包至開放板/system/etc/init目錄下。
## Install data/*.jpg to /system/etc/init/ {{{
ohos_prebuilt_etc("bootanimation_pics") {
source = "data/bootpic.zip" ## bootpic.zip在data目錄下
relative_install_dir = "init"
part_name = "graphic_standard" ## 部件名
subsystem_name = "graphic" ## 子系統(tǒng)名
}
ohos_prebuilt_etc("bootanimation_sounds") {
source = "data/bootsound.wav" ## bootsound.wav在data目錄下
relative_install_dir = "init"
part_name = "graphic_standard"
subsystem_name = "graphic"
}
## Install data/*.jpg to /system/etc/init/ }}}
bootpic.zip的解壓縮依賴三方庫zlib
閱讀bootanimation目錄下BUILD.gn可以知道bootpic.zip的解壓縮依賴三方庫zlib
config.json文件在util.h中被聲明為C ++ 的std::string類型
-
閱讀bootanimation目錄下BUILD.gn可以知道config.json的解析依賴三方庫cJSON
開機動畫服務(wù)啟動配置graphic.cfg分別啟動了bootanimation進程
開機動畫服務(wù)啟動配置graphic.cfg在ohosbeta3源碼./foundation/graphic/graphic_2d/graphic.cfg目錄,分別啟動了bootanimation和render_service進程。
.cfg只是一個為開發(fā)及使用方便而"發(fā)明"的一個后綴名。所以,這種文件沒有固定的格式,其實也并不能算作是一種文件類型。
{
"jobs" : [{
"name" : "init",
"cmds" : [
"chmod 666 /dev/mali0",
"chown system graphics /dev/mali0"
]
}, {
"name": "services:restartrender_service",
"cmds": [
"reset foundation",
"reset bootanimation",
"reset gralloc_host",
"reset hwc_host"
]
}
],
"services" : [{
"name" : "render_service", # 渲染服務(wù)端
"path" : ["/system/bin/render_service"],
"critical" : [1, 5, 60],
"importance" : -20,
"uid" : "system",
"gid" : ["system", "shell", "uhid", "root"],
"caps" : ["SYS_NICE"],
"secon" : "u:r:render_service:s0",
"jobs" : {
"on-restart" : "services:restartrender_service"
},
"once" : 0
}, {
"name" : "bootanimation", # 開機啟動進程
"path" : ["/system/bin/bootanimation"],
"bootevents": "bootevent.bootanimation.started",
"importance" : -20,
"once" : 1,
"uid" : "graphics",
"gid" : ["graphics", "system", "shell", "uhid", "root"],
"secon" : "u:r:bootanimation:s0"
}
]
}
-
可以用hdc_std工具將pc端與開發(fā)板連接進入shell界面運行
ps -ef
命令,查看進程信息。
# ps -ef
UID PID PPID C STIME TTY TIME CMD
····
system 565 1 19 09:12:27 ? 00:00:01 bootanimation
system 566 1 6 09:12:27 ? 00:00:00 render_service
····
-
查看beta3源碼foundation/graphic/graphic_2d/BUILD.gn,可以知道graphic.cfg會被打包到開發(fā)板/system/etc/init/目錄下
- 用hdc_std工具連接開發(fā)板后,服務(wù)啟動配置graphic.cfg在開發(fā)板systemetcinit目錄也能找到
find . -name graphic.cfg
boot_animation.cpp中將開發(fā)板目錄/system/etc/init下的bootpic.zip和bootsound.wav聲明為C ++ 的std::string類型
using namespace OHOS;
static const std::string BOOT_PIC_ZIP = "/system/etc/init/bootpic.zip";
static const std::string BOOT_SOUND_URI = "file://system/etc/init/bootsound.wav";
-
C ++中的std :: string類
- C ++在其定義中具有一種將字符序列表示為class對象的方式。此類稱為std ::字符串。
- 它是一個容器類
-
static const
既是只讀的,又是只在當前模塊中可見的。
- const 就是只讀的意思,只在聲明中使用;
- static 一般有2個作用,規(guī)定作用域和存儲方式。
- 對于局部變量, static規(guī)定其為靜態(tài)存儲方式, 每次調(diào)用的初始值為上一次調(diào)用的值,調(diào)用結(jié)束后存儲空間不釋放
- 對于全局變量, 如果以文件劃分作用域的話,此變量只在當前文件可見; 對于static函數(shù)也是在當前模塊內(nèi)函數(shù)可見。
-
using namespace OHOS;
表示使用using指令使用OHOS空間,其在boot_animation.h中定義boot_animation.CPP中要用到boot_animation.h中OHOS空間中的函數(shù)和變量。
namespace OHOS {
class BootAnimation {
public:
void Init(int32_t width, int32_t height, const std::shared_ptr& handler,
std::shared_ptr& runner);
void Draw();
void CheckExitAnimation();
void PlaySound();
bool CheckFrameRateValid(int32_t ratevalue);
~BootAnimation();
private:
void OnVsync();
void OnDraw(SkCanvas* canvas, int32_t curNo);
void InitBootWindow();
void InitRsSurface();
void InitPicCoordinates();
int32_t windowWidth_;
int32_t windowHeight_;
sptr window_;
sptr scene_;
std::unique_ptr framePtr_;
std::shared_ptr rsSurface_;
OHOS::Rosen::RenderContext* rc_;
int32_t freq_ = 30;
int32_t realHeight_ = 0;
int32_t realWidth_ = 0;
int32_t pointX_ = 0;
int32_t pointY_ = 0;
int32_t picCurNo_ = -1;
int32_t imgVecSize_ = 0;
std::shared_ptr receiver_ = nullptr;
std::shared_ptr soundPlayer_ = nullptr;
ImageStructVec imageVector_;
std::shared_ptr mainHandler_ = nullptr;
std::shared_ptr runner_ = nullptr;
bool setBootEvent_ = false;
};
} // namespace OHOS
3.1Beta開機動畫模塊源碼分析
- 開機動畫模塊在社區(qū)gitee倉庫OpenHarmony / graphic_graphic_2d
- bootanimation開機模塊目錄結(jié)構(gòu)如下:
├─data
│ ├── bootanimation-480x960.raw
│ └── generate_raw.sh
├─include
│ ├── raw_parser.h
│ └── util.h
└─src
│ ├── raw_parser.cpp
│ ├── main.cpp
│ └── util.cpp
└─BUILD.gn
開機動畫文件bootanimation.raw作為配置文件打包至開發(fā)板/system/etc目錄下
## Install data/bootanimation-480x960.raw to /system/etc/bootanimation-480x960.raw {{{
ohos_prebuilt_etc("bootanimation-480x960.raw") {
source = "data/bootanimation-480x960.raw"
part_name = "graphic_standard"
subsystem_name = "graphic"
}
## Install data/bootanimation-480x960.raw to /system/etc/bootanimation-480x960.raw }}}
RAW是未經(jīng)處理、也未經(jīng)壓縮的格式,可以把RAW概念化為“原始圖像編碼數(shù)據(jù)”或更形象的稱為“數(shù)字底片”。
3.2beta3開機動畫制作
- 1.開機動畫在3.2beta版本的源文件為bootpic.zip和bootpic.wav,需要利用視頻剪輯軟件制作好一段自己的開機動畫。
- 2.然后利用特殊軟件工具將制作好的視頻變成150張圖片,然后將圖片編號再排好序。
- 3.最后打包為bootpic.zip壓縮包。利用hdc_std工具將自制的bootpic.zip導入開發(fā)板/system/etc/init目錄下。
- 4.重啟開發(fā)板就可以了。
筆者使用的剪映軟件(對筆者來說剪映夠用)制作開機動畫。然后在
https://www.img2go.com/zh/convert-to-image網(wǎng)址將視頻轉(zhuǎn)換成圖片。
最后打包成bootpic.zip(需要和原生的bootpic.zip目錄機構(gòu)一樣),hdc_std工具使用命令如下:
C:UsersjjhDesktop3.2beta1SDKohos-sdkwindowstoolchains-windows-3.2.2.5-Beta1toolchains>hdc_std shell
# cd system/etc/init # 進入system/etc/init目錄
# mount -o remount,rw / # 將系統(tǒng)變成可讀寫
# ls
access_token.cfg locationsa.cfg
accessibility.cfg media_service.cfg
accountmgr.cfg memmgrservice.cfg
appspawn.cfg misc.cfg
audio_policy.cfg mmi_uinput.rc
batterystats.cfg msdp_musl.cfg
bgtaskmgr_service.cfg multimodalinput.cfg
bluetooth_service.cfg netmanager_base.cfg
bootpic.zip netsysnative.cfg
bootsound.wav nwebspawn.cfg
bytrace.cfg param_watcher.cfg
camera_service.cfg pasteboardservice.cfg
config.txt pinauth_sa_profile.cfg
console.cfg pulseaudio.cfg
dcamera.cfg resource_schedule_service.cfg
device_usage_statistics_service.cfg samgr_standard_musl.cfg
deviceauth_service.cfg screenlockservice.cfg
deviceinfoservice.cfg sensors_musl.cfg
dhardware.cfg softbus_server_musl.cfg
distributed_data.cfg storage_daemon.cfg
distributedbms.cfg storage_manager.cfg
distributedfile.cfg telephony.cfg
distributedsched_musl.cfg thermal.cfg
downloadservice.cfg thermal_protector.cfg
dscreen.cfg timeservice.cfg
dslm_service.cfg token_sync.cfg
edm.cfg udevd.rc
faceauth_sa_profile.cfg ueventd.cfg
faultloggerd.cfg ui_service.cfg
fms_service.cfg updater_sa.cfg
foundation.cfg usb_service.cfg
graphic.cfg useriam.cfg
hidumper_service.cfg wallpaperservice.cfg
hilogd.cfg watchdog.cfg
hiview.cfg weston.cfg
huks_service.cfg wifi_hal_service.cfg
init.reboot.cfg wifi_standard.cfg
inputmethodservice.cfg work_scheduler_service.cfg
installs.cfg
# rm bootpic.zip # 刪除原有的開機動畫
# exit # 退出開發(fā)板系統(tǒng)
C:UsersjjhDesktop3.2beta1SDKohos-sdkwindowstoolchains-windows-3.2.2.5-Beta1toolchains>hdc_std file send bootpic.zip /system/etc/init/
FileTransfer finish, File count = 1, Size:2776406 time:389ms rate:7137.29kB/s # 將自制的開機動畫發(fā)送至/system/etc/init目錄下
3.1beta版本開機動畫制作
準備好一段開機動畫的mp4格式動畫,利用以下raw_maker.py腳本,內(nèi)容如下:
# !/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
* Copyright (c) 2022 Shenzhen Kaihong Digital Industry Development Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
"""
import struct
import zlib
import os
import argparse
import re
import pip
def lost_module(module_name):
print("""
need %s module, try install first:
pip install %s""" % (module_name, module_name))
exit()
try:
import cv2
except ImportError:
pip.main(["install", "opencv-python", "-i", "https://pypi.tuna.tsinghua.edu.cn/simple"])
try:
import cv2
except ImportError:
cv2 = None
lost_module("opencv-python")
try:
from PIL import Image
except ImportError:
pip.main(["install", "pillow"])
try:
from PIL import Image
except ImportError:
Image = None
lost_module("pillow")
try:
import numpy as np
except ImportError:
pip.main(["install", "numpy"])
try:
import numpy as np
except ImportError:
np = None
lost_module("numpy")
class RawMaker:
"""
Make a boot video by a MP4 file or some .img files:
"""
def __init__(self, args):
self._mp4 = args.mp4
self._image = args.image
self._out = args.out
self._display = [int(i) for i in re.split(r'[xX* ]+', args.display.strip())]
self._rotate = args.rotate
self._flip = args.flip
self._fnp = 0
self._vdo = None
self._image_files = []
def _iter_img(self):
if self._mp4:
success, frame = self._vdo.read()
if success:
image = Image.fromarray(frame)
return success, image
else:
return False, None
else:
if self._fnp >= len(self._image_files):
return False, None
image = Image.open(os.path.join(self._image, self._image_files[self._fnp]))
self._fnp += 1
return True, image
def make(self):
frame_count, width, height = 0, 0, 0
if self._mp4:
if not os.path.exists(self._mp4):
print("mp4 file %s is not exist" % self._mp4)
exit()
self._vdo = cv2.VideoCapture(self._mp4)
fps = int(self._vdo.get(cv2.CAP_PROP_FPS))
w = int(self._vdo.get(cv2.CAP_PROP_FRAME_WIDTH))
h = int(self._vdo.get(cv2.CAP_PROP_FRAME_HEIGHT))
frame_count = int(self._vdo.get(cv2.CAP_PROP_FRAME_COUNT))
if fps != 30:
print("video fps :", fps, ", width :", w, ", height :", h, ", frame count :", frame_count)
if frame_count <= 0:
exit()
elif self._image:
for fn in os.listdir(self._image):
self._image_files.append(fn)
frame_count = len(self._image_files)
if frame_count <= 0:
exit()
self._image_files.sort()
else:
exit()
output_bytes = bytearray(b"RAW.diff")
offset = 8
screen_old_bytes = None
num = 0
while True:
ret, img = self._iter_img()
if not ret:
break
num += 1
img = img.convert("RGBA")
if self._flip:
img = img.transpose(Image.FLIP_LEFT_RIGHT)
if self._rotate == 90:
img = img.transpose(Image.ROTATE_90)
elif self._rotate == 180:
img = img.transpose(Image.ROTATE_180)
elif self._rotate == 270:
img = img.transpose(Image.ROTATE_270)
if self._display[0] != 0:
img = img.resize((self._display[0], self._display[1]))
img = np.array(img)
height, width = img.shape[0], img.shape[1]
img[img < 20] = 0
img = img.reshape(-1)
screen_now_bytes = img.tobytes()
if screen_old_bytes is None:
screen_old_bytes = screen_now_bytes
start_pos = 0
end_pos = width * height * 4
else:
start_pos, end_pos = 3, 6
for i in range(width * height * 4):
if screen_now_bytes[i] != screen_old_bytes[i]:
start_pos = i
break
for i in range(width * height * 4 - 1, start_pos, -1):
if screen_now_bytes[i] != screen_old_bytes[i]:
end_pos = i + 1
break
screen_old_bytes = screen_now_bytes
print("r|%s%s|" % ("=" * int(num / frame_count * 30), " " * (30 - int(num / frame_count * 30))),
"%.2f%%" % (num / frame_count * 100), end="", flush=True)
if start_pos == 3 or end_pos == 6:
output_bytes[offset:offset + 16] = struct.pack("IIII", 0, 0, 0, 0)
offset += 16
continue
compressed_bytes = zlib.compress(screen_old_bytes[start_pos:end_pos])
raw_len = end_pos - start_pos
new_len = len(compressed_bytes)
output_bytes[offset:offset + 16] = struct.pack("IIII", 2, start_pos, raw_len, new_len)
offset += 16
output_bytes[offset:offset + new_len] = compressed_bytes
offset += new_len
while new_len % 4 != 0:
new_len += 1
output_bytes[offset:offset + 1] = b''
offset += 1
if not os.path.exists(self._out):
os.makedirs(self._out)
with open(os.path.join(self._out, "bootanimation-%dx%d.raw" % (width, height)), "wb") as fp:
fp.write(output_bytes)
print("nGenerate successfully!")
def parse_option():
parser = argparse.ArgumentParser(description="Make a boot video by a MP4 file or some .img files",
usage="python raw_maker.py (-m <*.mp4> | -i ) [-o ] "
"[-d ] [-r ] [-f]n"
" eg.: python raw_maker.py -i ./source/png -o ./out -d 640x480n"
" python raw_maker.py -m ./animation.mp4 -o ./out -d 640x480")
exclusive_group = parser.add_mutually_exclusive_group(required=True)
exclusive_group.add_argument("-m", "--mp4", metavar="<*.mp4>", help="The input <*.mp4> file")
exclusive_group.add_argument("-i", "--image", metavar="",
help="The where image files are stored")
parser.add_argument("-o", "--out", metavar="", default=".",
help="Place generated .raw files into the ")
parser.add_argument("-d", "--display", metavar="", default="0x0",
help="Set the boot video display and zoom the image, e.g.:640x480")
parser.add_argument("-r", "--rotate", metavar="", type=int, help="Rotate video , e.g.:90 180 270")
parser.add_argument("-f", "--flip", action="store_true", help="Flip the video", )
return parser.parse_args()
if __name__ == "__main__":
raw_maker = RawMaker(parse_option())
raw_maker.make()
在當前文件夾打開cmd窗口執(zhí)行以下命令
python raw_maker.py -m ./cat.mp4 -d 720x1280
此時會生成cat.raw文件,分辨率為720x1280
播放cat.raw準備好raw_player.py腳本,內(nèi)容如下:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
* Copyright (c) 2022 Shenzhen Kaihong Digital Industry Development Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
"""
import zlib
import struct
import time
import numpy as np
import cv2
import sys
import re
import argparse
class RawPlayer:
"""
Play a boot video file
"""
def __init__(self, args):
self._raw = args.raw
cv2.namedWindow("play", cv2.WINDOW_AUTOSIZE)
pass
def play(self):
screen_size = re.findall("bootanimation-([0-9]+)x([0-9]+).raw", self._raw)
if len(screen_size) != 1:
exit()
width, height = int(screen_size[0][0]), int(screen_size[0][1])
with open(sys.argv[-1], "rb") as fp:
data = fp.read()
off = 8
img = None
while off < len(data):
data_type, offset, length, data_length = struct.unpack("IIII", data[off:off + 16])
off += 16
if data_type == 0:
time.sleep(0.03)
continue
out = zlib.decompress(data[off:off + data_length])
if img is None:
img = np.copy(np.frombuffer(out, dtype=np.uint8))
else:
temp_img = np.frombuffer(out, dtype=np.uint8)
img[offset:offset + length] = temp_img
reshape_img = img.reshape((height, width, 4))
cv2.imshow("play", reshape_img)
if cv2.waitKey(30) & 0xff == 27 or cv2.getWindowProperty("play", cv2.WND_PROP_VISIBLE) < 1:
break
while data_length % 4 != 0:
data_length += 1
off += data_length
def parse_option():
parser = argparse.ArgumentParser(description="Play a boot video file",
usage="python raw_player.py [-h] <*.raw>n"
" eg.: python raw_player.py ./bootanimation-640x480.raw")
parser.add_argument("raw", metavar="<*.raw>", help="file <*.raw> to play")
return parser.parse_args()
if __name__ == "__main__":
raw_player = RawPlayer(parse_option())
raw_player.play()
在當前文件夾打開cmd窗口,執(zhí)行以下命令;
python raw_player.py bootanimation-720x1280.raw
效果如下:
-
dayu200
+關(guān)注
關(guān)注
1文章
26瀏覽量
1185 -
潤和軟件
+關(guān)注
關(guān)注
1文章
254瀏覽量
855
發(fā)布評論請先 登錄
相關(guān)推薦
評論