Home | History | Annotate | Download | only in scripts
      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 if [ -z "$1" ]; then
      9     printf 'Usage:\n    skp-capture.sh PACKAGE_NAME OPTIONAL_FRAME_COUNT\n\n'
     10     printf "Use \`adb shell 'pm list packages'\` to get a listing.\n\n"
     11     exit 1
     12 fi
     13 if ! command -v adb > /dev/null 2>&1; then
     14     if [ -x "${ANDROID_SDK_ROOT}/platform-tools/adb" ]; then
     15         adb() {
     16             "${ANDROID_SDK_ROOT}/platform-tools/adb" "$@"
     17         }
     18     else
     19         echo 'adb missing'
     20         exit 2
     21     fi
     22 fi
     23 phase1_timeout_seconds=15
     24 phase2_timeout_seconds=60
     25 package="$1"
     26 filename="$(date '+%H%M%S').skp"
     27 remote_path="/data/data/${package}/cache/${filename}"
     28 local_path_prefix="$(date '+%Y-%m-%d_%H%M%S')_${package}"
     29 local_path="${local_path_prefix}.skp"
     30 enable_capture_key='debug.hwui.capture_skp_enabled'
     31 enable_capture_value=$(adb shell "getprop '${enable_capture_key}'")
     32 #printf 'captureflag=' "$enable_capture_value" '\n'
     33 if [ -z "$enable_capture_value" ]; then
     34     printf 'Capture SKP property need to be enabled first. Please use\n'
     35     printf "\"adb shell setprop debug.hwui.capture_skp_enabled true\" and then restart\n"
     36     printf "the process.\n\n"
     37     exit 1
     38 fi
     39 if [ ! -z "$2" ]; then
     40     adb shell "setprop 'debug.hwui.capture_skp_frames' $2"
     41 fi
     42 filename_key='debug.hwui.skp_filename'
     43 adb shell "setprop '${filename_key}' '${remote_path}'"
     44 spin() {
     45     case "$spin" in
     46          1) printf '\b|';;
     47          2) printf '\b\\';;
     48          3) printf '\b-';;
     49          *) printf '\b/';;
     50     esac
     51     spin=$(( ( ${spin:-0} + 1 ) % 4 ))
     52     sleep $1
     53 }
     54 
     55 banner() {
     56     printf '\n=====================\n'
     57     printf '   %s' "$*"
     58     printf '\n=====================\n'
     59 }
     60 banner '...WAITING...'
     61 adb_test_exist() {
     62     test '0' = "$(adb shell "test -e \"$1\"; echo \$?")";
     63 }
     64 timeout=$(( $(date +%s) + $phase1_timeout_seconds))
     65 while ! adb_test_exist "$remote_path"; do
     66     spin 0.05
     67     if [ $(date +%s) -gt $timeout ] ; then
     68         printf '\bTimed out.\n'
     69         adb shell "setprop '${filename_key}' ''"
     70         exit 3
     71     fi
     72 done
     73 printf '\b'
     74 
     75 #read -n1 -r -p "Press any key to continue..." key
     76 
     77 banner '...SAVING...'
     78 adb_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 }
     83 #adb_filesize() {
     84 #    adb shell "wc -c \"$1\"" 2> /dev/null | awk '{print $1}'
     85 #}
     86 timeout=$(( $(date +%s) + $phase2_timeout_seconds))
     87 while ! adb_test_file_nonzero "$remote_path"; do
     88     spin 0.05
     89     if [ $(date +%s) -gt $timeout ] ; then
     90         printf '\bTimed out.\n'
     91         adb shell "setprop '${filename_key}' ''"
     92         exit 3
     93     fi
     94 done
     95 printf '\b'
     96 
     97 adb shell "setprop '${filename_key}' ''"
     98 
     99 i=0; while [ $i -lt 10 ]; do spin 0.10; i=$(($i + 1)); done; echo
    100 
    101 adb pull "$remote_path" "$local_path"
    102 if ! [ -f "$local_path" ] ; then
    103     printf "something went wrong with `adb pull`."
    104     exit 4
    105 fi
    106 adb shell rm "$remote_path"
    107 printf '\nSKP saved to file:\n    %s\n\n'  "$local_path"
    108 if [ ! -z "$2" ]; then
    109     bridge="_"
    110     adb shell "setprop 'debug.hwui.capture_skp_frames' ''"
    111     for i in $(seq 2 $2); do
    112         adb pull "${remote_path}_${i}" "${local_path_prefix}_${i}.skp"
    113         adb shell rm "${remote_path}_${i}"
    114     done
    115 fi
    116 
    117