blob: 4b46fbf868184c0d47b726a3498e840f6fb1c90f [file] [log] [blame]
Stan Ilieve9d00122017-09-19 12:07:10 -04001#!/bin/sh
2
3# Copyright 2015 Google Inc.
4#
5# Use of this source code is governed by a BSD-style license that can be
6# found in the LICENSE file.
Nathaniel Nifongd2e49a22019-06-24 15:07:34 -04007#
8# Before this can be used, the device must be rooted and the filesystem must be writable by Skia
9# - These steps are necessary once after flashing to enable capture -
10# adb root
11# adb remount
12# adb reboot
Stan Ilieve9d00122017-09-19 12:07:10 -040013
14if [ -z "$1" ]; then
15 printf 'Usage:\n skp-capture.sh PACKAGE_NAME OPTIONAL_FRAME_COUNT\n\n'
16 printf "Use \`adb shell 'pm list packages'\` to get a listing.\n\n"
17 exit 1
18fi
19if ! command -v adb > /dev/null 2>&1; then
20 if [ -x "${ANDROID_SDK_ROOT}/platform-tools/adb" ]; then
21 adb() {
22 "${ANDROID_SDK_ROOT}/platform-tools/adb" "$@"
23 }
24 else
25 echo 'adb missing'
26 exit 2
27 fi
28fi
Nathaniel Nifongd2e49a22019-06-24 15:07:34 -040029phase1_timeout_seconds=60
30phase2_timeout_seconds=300
Stan Ilieve9d00122017-09-19 12:07:10 -040031package="$1"
Nathaniel Nifonge2617db2019-07-25 10:44:08 -040032extension="skp"
33if (( "$2" > 1 )); then # 2nd arg is number of frames
34 extension="mskp" # use different extension for multi frame files.
35fi
36filename="$(date '+%H%M%S').${extension}"
Stan Ilieve9d00122017-09-19 12:07:10 -040037remote_path="/data/data/${package}/cache/${filename}"
38local_path_prefix="$(date '+%Y-%m-%d_%H%M%S')_${package}"
Nathaniel Nifonge2617db2019-07-25 10:44:08 -040039local_path="${local_path_prefix}.${extension}"
Stan Ilieve9d00122017-09-19 12:07:10 -040040enable_capture_key='debug.hwui.capture_skp_enabled'
41enable_capture_value=$(adb shell "getprop '${enable_capture_key}'")
Nathaniel Nifongd2e49a22019-06-24 15:07:34 -040042
43# TODO(nifong): check if filesystem is writable here with "avbctl get-verity"
44# result will either start with "verity is disabled" or "verity is enabled"
45
Stan Ilieve9d00122017-09-19 12:07:10 -040046if [ -z "$enable_capture_value" ]; then
Nathaniel Nifongd2e49a22019-06-24 15:07:34 -040047 printf 'debug.hwui.capture_skp_enabled was found to be disabled, enabling it now.\n'
48 printf " restart the process you want to capture on the device, then retry this script.\n\n"
49 adb shell "setprop '${enable_capture_key}' true"
Stan Ilieve9d00122017-09-19 12:07:10 -040050 exit 1
51fi
52if [ ! -z "$2" ]; then
53 adb shell "setprop 'debug.hwui.capture_skp_frames' $2"
54fi
55filename_key='debug.hwui.skp_filename'
56adb shell "setprop '${filename_key}' '${remote_path}'"
57spin() {
58 case "$spin" in
59 1) printf '\b|';;
60 2) printf '\b\\';;
61 3) printf '\b-';;
62 *) printf '\b/';;
63 esac
64 spin=$(( ( ${spin:-0} + 1 ) % 4 ))
65 sleep $1
66}
67
68banner() {
69 printf '\n=====================\n'
70 printf ' %s' "$*"
71 printf '\n=====================\n'
72}
Nathaniel Nifongd2e49a22019-06-24 15:07:34 -040073banner '...WAITING FOR APP INTERACTION...'
74# Waiting for nonzero file is an indication that the pipeline has both opened the file and written
75# the header. With multiple frames this does not occur until the last frame has been recorded,
76# so we continue to show the "waiting for app interaction" message as long as the app still requires
77# interaction to draw more frames.
Stan Ilieve9d00122017-09-19 12:07:10 -040078adb_test_file_nonzero() {
79 # grab first byte of `du` output
80 X="$(adb shell "du \"$1\" 2> /dev/null | dd bs=1 count=1 2> /dev/null")"
81 test "$X" && test "$X" -ne 0
82}
Nathaniel Nifongd2e49a22019-06-24 15:07:34 -040083timeout=$(( $(date +%s) + $phase1_timeout_seconds))
Stan Ilieve9d00122017-09-19 12:07:10 -040084while ! adb_test_file_nonzero "$remote_path"; do
85 spin 0.05
86 if [ $(date +%s) -gt $timeout ] ; then
87 printf '\bTimed out.\n'
88 adb shell "setprop '${filename_key}' ''"
89 exit 3
90 fi
91done
92printf '\b'
93
Nathaniel Nifongd2e49a22019-06-24 15:07:34 -040094# Disable further capturing
Stan Ilieve9d00122017-09-19 12:07:10 -040095adb shell "setprop '${filename_key}' ''"
96
Nathaniel Nifongd2e49a22019-06-24 15:07:34 -040097banner '...SAVING...'
98# return the size of a file in bytes
99adb_filesize() {
100 adb shell "wc -c \"$1\"" 2> /dev/null | awk '{print $1}'
101}
102timeout=$(( $(date +%s) + $phase2_timeout_seconds))
103last_size='0' # output of last size check command
104unstable=true # false once the file size stops changing
105counter=0 # used to perform size check only 1/sec though we update spinner 20/sec
106# loop until the file size is unchanged for 1 second.
107while [ $unstable != 0 ] ; do
108 spin 0.05
109 counter=$(( $counter+1 ))
110 if ! (( $counter % 20)) ; then
111 new_size=$(adb_filesize "$remote_path")
112 unstable=$(($(adb_filesize "$remote_path") != last_size))
113 last_size=$new_size
114 fi
115 if [ $(date +%s) -gt $timeout ] ; then
116 printf '\bTimed out.\n'
117 adb shell "setprop '${filename_key}' ''"
118 exit 3
119 fi
120done
121printf '\b'
122
123printf "SKP file serialized: %s\n" $(echo $last_size | numfmt --to=iec)
124
Stan Ilieve9d00122017-09-19 12:07:10 -0400125i=0; while [ $i -lt 10 ]; do spin 0.10; i=$(($i + 1)); done; echo
126
127adb pull "$remote_path" "$local_path"
128if ! [ -f "$local_path" ] ; then
129 printf "something went wrong with `adb pull`."
130 exit 4
131fi
132adb shell rm "$remote_path"
133printf '\nSKP saved to file:\n %s\n\n' "$local_path"
Stan Ilieve9d00122017-09-19 12:07:10 -0400134