blob: aad31fcc8eb92fde2c39a3216baea30dd5f67e77 [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"
32filename="$(date '+%H%M%S').skp"
33remote_path="/data/data/${package}/cache/${filename}"
34local_path_prefix="$(date '+%Y-%m-%d_%H%M%S')_${package}"
35local_path="${local_path_prefix}.skp"
36enable_capture_key='debug.hwui.capture_skp_enabled'
37enable_capture_value=$(adb shell "getprop '${enable_capture_key}'")
Nathaniel Nifongd2e49a22019-06-24 15:07:34 -040038
39# TODO(nifong): check if filesystem is writable here with "avbctl get-verity"
40# result will either start with "verity is disabled" or "verity is enabled"
41
Stan Ilieve9d00122017-09-19 12:07:10 -040042if [ -z "$enable_capture_value" ]; then
Nathaniel Nifongd2e49a22019-06-24 15:07:34 -040043 printf 'debug.hwui.capture_skp_enabled was found to be disabled, enabling it now.\n'
44 printf " restart the process you want to capture on the device, then retry this script.\n\n"
45 adb shell "setprop '${enable_capture_key}' true"
Stan Ilieve9d00122017-09-19 12:07:10 -040046 exit 1
47fi
48if [ ! -z "$2" ]; then
49 adb shell "setprop 'debug.hwui.capture_skp_frames' $2"
50fi
51filename_key='debug.hwui.skp_filename'
52adb shell "setprop '${filename_key}' '${remote_path}'"
53spin() {
54 case "$spin" in
55 1) printf '\b|';;
56 2) printf '\b\\';;
57 3) printf '\b-';;
58 *) printf '\b/';;
59 esac
60 spin=$(( ( ${spin:-0} + 1 ) % 4 ))
61 sleep $1
62}
63
64banner() {
65 printf '\n=====================\n'
66 printf ' %s' "$*"
67 printf '\n=====================\n'
68}
Nathaniel Nifongd2e49a22019-06-24 15:07:34 -040069banner '...WAITING FOR APP INTERACTION...'
70# Waiting for nonzero file is an indication that the pipeline has both opened the file and written
71# the header. With multiple frames this does not occur until the last frame has been recorded,
72# so we continue to show the "waiting for app interaction" message as long as the app still requires
73# interaction to draw more frames.
Stan Ilieve9d00122017-09-19 12:07:10 -040074adb_test_file_nonzero() {
75 # grab first byte of `du` output
76 X="$(adb shell "du \"$1\" 2> /dev/null | dd bs=1 count=1 2> /dev/null")"
77 test "$X" && test "$X" -ne 0
78}
Nathaniel Nifongd2e49a22019-06-24 15:07:34 -040079timeout=$(( $(date +%s) + $phase1_timeout_seconds))
Stan Ilieve9d00122017-09-19 12:07:10 -040080while ! adb_test_file_nonzero "$remote_path"; do
81 spin 0.05
82 if [ $(date +%s) -gt $timeout ] ; then
83 printf '\bTimed out.\n'
84 adb shell "setprop '${filename_key}' ''"
85 exit 3
86 fi
87done
88printf '\b'
89
Nathaniel Nifongd2e49a22019-06-24 15:07:34 -040090# Disable further capturing
Stan Ilieve9d00122017-09-19 12:07:10 -040091adb shell "setprop '${filename_key}' ''"
92
Nathaniel Nifongd2e49a22019-06-24 15:07:34 -040093banner '...SAVING...'
94# return the size of a file in bytes
95adb_filesize() {
96 adb shell "wc -c \"$1\"" 2> /dev/null | awk '{print $1}'
97}
98timeout=$(( $(date +%s) + $phase2_timeout_seconds))
99last_size='0' # output of last size check command
100unstable=true # false once the file size stops changing
101counter=0 # used to perform size check only 1/sec though we update spinner 20/sec
102# loop until the file size is unchanged for 1 second.
103while [ $unstable != 0 ] ; do
104 spin 0.05
105 counter=$(( $counter+1 ))
106 if ! (( $counter % 20)) ; then
107 new_size=$(adb_filesize "$remote_path")
108 unstable=$(($(adb_filesize "$remote_path") != last_size))
109 last_size=$new_size
110 fi
111 if [ $(date +%s) -gt $timeout ] ; then
112 printf '\bTimed out.\n'
113 adb shell "setprop '${filename_key}' ''"
114 exit 3
115 fi
116done
117printf '\b'
118
119printf "SKP file serialized: %s\n" $(echo $last_size | numfmt --to=iec)
120
Stan Ilieve9d00122017-09-19 12:07:10 -0400121i=0; while [ $i -lt 10 ]; do spin 0.10; i=$(($i + 1)); done; echo
122
123adb pull "$remote_path" "$local_path"
124if ! [ -f "$local_path" ] ; then
125 printf "something went wrong with `adb pull`."
126 exit 4
127fi
128adb shell rm "$remote_path"
129printf '\nSKP saved to file:\n %s\n\n' "$local_path"
Stan Ilieve9d00122017-09-19 12:07:10 -0400130