1#!/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. 7# 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 13 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 29phase1_timeout_seconds=60 30phase2_timeout_seconds=300 31package="$1" 32extension="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}" 37remote_path="/data/data/${package}/cache/${filename}" 38local_path_prefix="$(date '+%Y-%m-%d_%H%M%S')_${package}" 39local_path="${local_path_prefix}.${extension}" 40enable_capture_key='debug.hwui.capture_skp_enabled' 41enable_capture_value=$(adb shell "getprop '${enable_capture_key}'") 42 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 46if [ -z "$enable_capture_value" ]; then 47 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" 50 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} 73banner '...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. 78adb_test_file_nonzero() { 79 # grab first byte of `wc -c` output 80 X="$(adb shell "wc -c \"$1\" 2> /dev/null | dd bs=1 count=1 2> /dev/null")" 81 test "$X" && test "$X" -ne 0 82} 83timeout=$(( $(date +%s) + $phase1_timeout_seconds)) 84while ! 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 94# Disable further capturing 95adb shell "setprop '${filename_key}' ''" 96 97banner '...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 125i=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" 134 135