1# This script captures MSKP files from RenderEngine in a connected device.
2# this only functions when RenderEngine uses the Skia backend.
3# it triggers code in SkiaCapture.cpp.
4
5# for a newly flashed device, perform first time steps with
6# record.sh rootandsetup
7
8# record all frames that RenderEngine handles over the span of 2 seconds.
9# record.sh 2000
10
11if [ -z "$1" ]; then
12    printf 'Usage:\n    record.sh rootandsetup\n'
13    printf '    record.sh MILLISECONDS\n\n'
14    exit 1
15elif [ "$1" == "rootandsetup" ]; then
16  # first time use requires these changes
17  adb root
18  adb shell setenforce 0
19  adb shell setprop debug.renderengine.backend "skiaglthreaded"
20  adb shell stop
21  adb shell start
22  exit 1;
23fi
24
25check_permission() {
26    adb shell getenforce
27}
28
29mode=$(check_permission)
30
31if [ "$mode" != "Permissive" ]; then
32   echo "Cannot write to disk from RenderEngine. run 'record.sh rootandsetup'"
33   exit 5
34fi
35
36# record frames for some number of milliseconds.
37adb shell setprop debug.renderengine.capture_skia_ms $1
38
39# give the device time to both record, and starting writing the file.
40# Total time needed to write the file depends on how much data was recorded.
41# the loop at the end waits for this.
42sleep $(($1 / 1000 + 4));
43
44# There is no guarantee that at least one frame passed through renderengine during that time
45# but as far as I know it always at least writes a 0-byte file with a new name, unless it crashes
46# the process it is recording.
47# /data/user/re_skiacapture_56204430551705.mskp
48
49spin() {
50    case "$spin" in
51         1) printf '\b|';;
52         2) printf '\b\\';;
53         3) printf '\b-';;
54         *) printf '\b/';;
55    esac
56    spin=$(( ( ${spin:-0} + 1 ) % 4 ))
57    sleep $1
58}
59
60local_path=~/Downloads/
61
62get_filename() {
63    adb shell getprop debug.renderengine.capture_filename
64}
65
66remote_path=""
67counter=0 # used to check only 1/sec though we update spinner 20/sec
68while [ -z $remote_path ] ; do
69    spin 0.05
70    counter=$(( $counter+1 ))
71    if ! (( $counter % 20)) ; then
72        remote_path=$(get_filename)
73    fi
74done
75printf '\b'
76
77printf "MSKP file serialized to: $remote_path\n"
78
79adb_pull_cmd="adb pull $remote_path $local_path"
80echo $adb_pull_cmd
81$adb_pull_cmd
82
83adb shell rm "$remote_path"
84printf 'SKP saved to %s\n\n' "$local_path"
85