nyan~

Engineering Rocks!

Android Window System

Overview

“ Android SurfaceFlinger drives the composition of surface area and the rendering to the display screen. UI application must obtain a surface to draw graphics from SurfaceFlinger. ”

Android Graphics Components

Android Graphics Components

Graphics Pipeline

Graphics Pipeline

BufferQueue

SurfaceFlinger+BufferQueue

BufferQueue Sync

Understanding Android Internals

Graphics Basics I and II

Surface

“Surface is a rectangle canvas window on the screen with 2d geometrical metrics and particular color scheme.”

Window

“Window is associated a certain count of graphics buffers which are used to contain drawing artifacts. ”

Graphic Buffer

SurfaceFlinger

The Graphic Surface Service Interfaces

Gralloc and HwComposer

SELinux for Android

Reference

Projects

Control

  • Permissive mode
BOARD_KERNEL_CMDLINE
1
androidboot.selinux=permissive

HowTo

1
2
3
4
5
6
# reload policy
adb remount && adb push sepolicy /data/security/
adb shell setprop selinux.reload_policy <0|1>
# toggle enforcing and permissive
adb shell setenforce <0|1|permissive|enforcing>
adb shell getenforce

Policy

The policy rules come in the form: allow domains types:classes permissions;, where:

  • Domain - A label for the process or set of processes.
  • Type - A label for the object (e.g. file, socket) or set of objects.
  • Class - The kind of object (e.g. file, socket) being accessed.
  • Permission - The operation (e.g. read, write) being performed.

And so an example use of this would follow the structure:

1
allow appdomain app_data_file:file rw_file_perms;

Machine Learning Notes

Terminology

  • BLAS = Basic Linear Algebra Subprograms
  • CNNs = Convolutional Neural Networks
  • DNNs = Deep Neural Networks is one of Deep Learning (wiki) architectures
  • ML = Machine Learning
  • SGEMM = Single precision, General Matrix Multiply
  • Convolution: Anatomy. one of the sinuous folds or ridges of the surface of the brain.

Software links

  • Cafe is a deep learning framework. See more in the web image demo
  • Torch is a scientific computing network
  • Theano is a Python library

NVIDIA

Tegra Graphics Debugger

Overview

Tegra Graphics Debugger is a free tool from NVIDIA for debugging and profiling OpenGL ES 2.0/3.0/3.1, OpenGL 4.x and Android Extension Pack. Supported host OS are Windows 7/8, Mac OS X 10.9 and Ubuntu Linux x64 10.10/11.04/12.04. Supported target platform is Tegra K1 Android.

As for PerfHUD ES, it focus on Tegra Android platform before and include Tegra 4.

In GTC 2014, there is a talk “SG4116: Tegra K1 Developer Tools for Android, Sebastien Domine” which gives introudction of Tegra Graphics Debugger. You may watch online video starting from 29:05 and download presentation.

Getting Started

Tegra Graphics Debugger is part of Tegra Android Development Pack which is free for download, but requires NVIDIA GameWorks Registered Developer Program membership. You may check online document here or search below doc after installation.

1
~/NVPACK/Tegra_Graphics_Debugger/Tegra_Graphics_Debugger.pdf
  • Launch Tegra Graphics Debugger
1
~/NVPACK/Tegra_Graphics_Debugger$ ./tegra-gfx-debugger
  • Performance Dashboard will be the first view after established connection to target device and launch application.
  • Frame Debugger: Trigger the Capture Frame button.
  • Frame Profiler: From the View menu, select Profiler.
  • Dynamic Shader Editing: Open the API Inspector, navigate to the Program page.

Reference

GPU Technology Conference On-Demand
Tegra Android Development Pack

Linux Kernel

Terminology

IPC

Optimization

Debugger

  • kgdb

  • gdb on android

Target
1
$gdbserver :1234 --attach <pid>
Host
1
2
3
4
5
6
7
8
9
10
11
12
13
14
$adb forward tcp:1234 tcp:1234
$<path>/arm-eabi-gdb <path>/symbols/system/bin/app_process32
(gdb) set solib-search-path <path to out>/symbols/system/lib:<path to out>/symbols/system/vendor/lib/
(gdb) set solib-absolute-prefix <path to out>/symbols/
(gdb) target remote :1234
(gdb) b <symbol>                # set break point
(gdb) b <address>               # set break point
(gdb) b <symbol> if <condition> # set conditional break point
(gdb) i b                       # list break points
(gdb) d <id>                    # delete break point with <id>
(gdb) bt                        # print backtrace
(gdb) s                         # step into
(gdb) n                         # step over
(gdb) c                         # continue
  • strace
1
$strace -tt -o<log> <executable>
.config
1
2
CONFIG_USB_DEBUG=y
CONFIG_USB_MON=y
kernel/drivers/usb/host/ehci-xxx.c
1
#define DEBUG
check bus id
1
2
cat /sys/kernel/debug/usb/devices
T:  Bus=03 Lev=01 Prnt=01 Port=00 Cnt=01 Dev#=  2 Spd=12  MxCh= 0
get raw text trace from
1
$cat /sys/kernel/debug/usb/usbmon/<bus id>u > <log>
  • lowlevel Debug
.config
1
CONFIG_DEBUG_LL=y

Step1: rebuild/flash kernel

.config
1
CONFIG_DEBUG_KMEMLEAK=y

Step2: Get memory leak information

1
2
3
$cat /sys/kernel/debug/kmemleak          # get log
$echo scan > /sys/kernel/debug/kmemleak  # trigger memory scan
$echo clear > /sys/kernel/debug/kmemleak # clear memory leaks
  • debug_slab

Step1: Revert this commit

1
2
3
4
5
commit 3ff84a7f36554b257cd57325b1a7c1fa4b49fbe3
Author: Pekka Enberg <penberg@kernel.org>
Date:  Mon Feb 14 17:46:21 2011 +0200

   Revert "slab: Fix missing DEBUG_SLAB last user"

Step2: rebuild/flash kernel

.config
1
2
CONFIG_DEBUG_SLAB=y
CONFIG_DEBUG_SLAB_LEAK=y

Step3: Get slab information

1
2
$cat /proc/slab_allocators
$cat /proc/slabinfo
  • irqsoff tracer

Step1: rebuild/flash kernel

.config
1
CONFIG_IRQSOFF_TRACER=y

Step2: testing

1
2
3
4
$echo irqsoff > /d/tracing/current_tracer
$echo 0 > /sys/kernel/debug/tracing/tracing_max_latency
# start testing
$cat /sys/kernel/debug/tracing/tracing_max_latency

Step1: rebuild/flash kernel

.config
1
2
3
CONFIG_DYNAMIC_FTRACE=y
CONFIG_FTRACE=y
CONFIG_FUNCTION_TRACER=y

Step2: testing

1
2
3
4
$echo <pid> > /d/tracing/set_ftrace_pid
$echo function > /d/tracing/current_tracer
# start testing
$cat /d/tracing/trace_pipe

Step1: rebuild/flash kernel

.config
1
2
3
4
5
CONFIG_HAVE_PERF_EVENTS=y
CONFIG_PERF_EVENTS=y
CONFIG_PROFILING=y
CONFIG_OPROFILE=y
CONFIG_HW_PERF_EVENTS=y

Step2: testing

1
2
3
# push library symbol
$perf record -g -f -p <pid>
$perf report --sort dso,symbol >output.txt
.config
1
2
3
CONFIG_TRACING=y
CONFIG_FUNCTION_TRACER=y
CONFIG_TRACEDUMP_PANIC=y
kernel/trace/tracedump.c
1
format_ascii = true
1
2
3
$cd /sys/kernel/debug/tracing
$echo function > current_tracer
$echo 1 > /proc/sys/kernel/ftrace_dump_on_oops

Code Snippet

1
2
#define LIKELY( exp )       (__builtin_expect( (exp) != 0, true  ))
#define UNLIKELY( exp )     (__builtin_expect( (exp) != 0, false ))
  • Kenrel independent build
kernel/kernel/module.c
1
2
3
4
5
6
7
8
9
10
11
12
diff --git a/kernel/module.c b/kernel/module.c
index 3b5a5d6..5b35c69 100644
--- a/kernel/module.c
+++ b/kernel/module.c
@@ -2704,7 +2704,6 @@ static int check_modinfo(struct module *mod, struct load_info *info, int flags)
        } else if (!same_magic(modmagic, vermagic, info->index.vers)) {
                printk(KERN_ERR "%s: version magic '%s' should be '%s'\n",
                       mod->name, modmagic, vermagic);
-               return -ENOEXEC;
        }

        if (!get_modinfo(info, "intree"))

Ubuntu Tips

bash

Run bash terminal as a login shell

~/.profile
1
2
3
4
5
6
if [ -n "$BASH_VERSION" ]; then
    # include .bashrc if it exists
    if [ -f "$HOME/.bashrc" ]; then
        . "$HOME/.bashrc"
    fi
fi

vim

vim cheatsheet

1
2
3
4
5
6
7
.               # Position of the last change
' or `          # Position before last jump
`. or '.        # Go back to last edit top
:               # command mode
:set paste      # auto indent for code paste
:set number     # display line number
:1,$s/<target string>/<replace string>/ # replace strings

cscope

1
2
3
cscope -bkR     # create tags
ctrl-]          # go to symbol
ctrl-t          # return to last tag stack

tmux

1
2
3
4
5
6
7
tmux ls                             # list tmux sessions
tmux new session -s [session name]  # create new session
tmux attach -t [session name]       # attach session
ctrl-b + c                          # create new window
ctrl-b + ,                          # rename window
ctrl-b + w                          # switch windows
ctrl-b + s                          # switch sessions

shellscript

declare integer
1
2
3
declare -i y=10
declare -i z=0
z=$(( x + y ))
while loop
1
2
3
4
5
6
loop=0
while true;
do
loop=$(($loop+1))
echo $loop
done
rename folder
1
2
3
4
5
for NAME in `ls`;
do
    NEWNAME=`echo $NAME | tr -d 'bug'`;
    mv $NAME $NEWNAME;
done

remote desktop

  • Access Windows OS Desktop
1
rdesktop -u "[domain name]\[account]" -g 1920x1080 -D [ip address]
  • Access Ubuntu Shared Folders

Check link

mount

  • Samba
1
2
3
sudo apt-get install cifs-utils
sudo mkdir /media/test/
sudo mount.cifs //[path to share folder] /media/test/ -o username=[account],password="[password]"
  • ISO Image
1
2
sudo mkdir /media/iso/
sudo mount [file].iso /media/iso/ -t iso9660 -o loop

Useful utility

  • man: on-line reference manuals interface
1
$man -f <utility|function>
  • axel: A light download accelerator for Linux
1
$axel -n <number of connections> <link to file>
  • gqview: read raw file

  • ffmpeg: strip video from file

1
$ffmpeg -i sling60.ts -an -vcodec copy avc.ts
  • date
1
$date +%G%m%d-%H%M%S
  • tcpdump
1
$tcpdump -i any -s 2048 -tt -n -vvv -xX -w <log.txt>

Android

Codenames

Google I/O

Debug

1
2
import java.lang.Thread
Thread.dumpStack();
1
2
3
#include <utils/CallStack.h>
CallStack stack;
stack.dump("[log prefix]");
  • KeyEvent

Find keycode in frameworks/base/core/java/android/view/KeyEvent.java

1
2
3
4
$adb shell input keyevent [key code]
$adb shell input keyevent 27    # camera
$adb shell input keyevent 82    # unlock screen
$adb shell input text "ANDROID" # input string

Optimization

Step1: Installing the bootchart tool

Download bootchart

1
2
3
$tar xjf bootchart-0.9.tar.bz2
$cd bootchart-0.9
$sudo ./install.sh

Step2: Compile init with bootchart enabled and flash images

1
2
3
$touch [mydroid]/system/core/init/init.c
$export INIT_BOOTCHART=true
$m init ramdisk bootimage

Step3: Trigger bootchart

1
2
$adb shell 'echo 20 > /data/bootchart-start'
$adb shell mkdir /data/bootchart

Step4: Get bootchart.tgz

1
$/system/core/init/grab-bootchart.sh

Step5: Generate bootchart image

1
$java -jar [path to bootchart]/bootchart-0.9/bootchart.jar bootchart.tgz

Display

set display timeout and stay awake
1
2
3
4
5
6
7
8
9
10
$adb shell
#sqlite3 /data/data/com.android.providers.settings/databases/settings.db
#.databases
#.tables
#.dump
#select * from <table>;
#delete from <table> where <condition>;
#update system set value="1800000" where name="screen_off_timeout";
#update system set value="3" where name="stay_on_while_plugged_in";
#.exit

Code Snippet

  • Property Permission

system/core/init/property_service.c

  • Device ID
Device ID
1
2
$adb shell cat /sys/class/android_usb/android0/iSerial
$adb shell getprop ro.serialno
out/target/product/[target name]/recovery/root/init.rc
1
write /sys/class/android_usb/android0/iSerial ${ro.serialno}
system/core/init/init.c
1
property_set("ro.serialno")

Others

Open Browser with url
1
$adb shell am start [url]
Start Java App
1
$adb shell am start -n [com.package.name]/[com.package.name.ActivityName]
Playback Video
1
$adb shell am start -a android.intent.action.VIEW -d ////[path to video]/[video file name] -t video/* -n com.android.gallery3d/.app.MovieActivity

SurfaceFlinger

SurfaceFlinger

Dump of SERVICE SurfaceFlinger

Find more information about dumpsys

1
adb shell dumpsys SurfaceFlinger

Frame Latency

Prints information about last 128 frames displayed in specific window
1
adb shell dumpsys SurfaceFlinger --latency [window name]

According to output definitive, The data returned looks like this:

1
2
3
4
5
16954612
7657467895508 7657482691352 7657493499756
7657484466553 7657499645964 7657511077881
7657500793457 7657516600576 7657527404785
(...)

The first line is the refresh period (here 16.95 ms), it is followed by 128 lines w/ 3 timestamps in nanosecond each:

A) when the app started to draw
B) the vsync immediately preceding SF submitting the frame to the h/w
C) timestamp immediately after SF submitted that frame to the h/w

The difference between the 1st and 3rd timestamp is the frame-latency.

Observe composite rate in logcat

1
adb shell service call SurfaceFlinger 1001 i32 1

Force using SurfaceFlinger as compositor

1
adb shell service call SurfaceFlinger 1008 i32 1

HWUI

Disable HWUI

1
2
3
4
5
6
7
8
9
10
11
12
13
diff --git a/core/java/android/view/Window.java b/core/java/android/view/Window.java
index 71ccac6..ec0b7ba 100644
--- a/core/java/android/view/Window.java
+++ b/core/java/android/view/Window.java
@@ -536,7 +536,7 @@ public abstract class Window {
             boolean hardwareAccelerated) {
         mAppToken = appToken;
         mAppName = appName;
-        mHardwareAccelerated = hardwareAccelerated
-                 || (!hardwareAccelerated && mAppProfileService.canForceHwUi(appName))
-                 || SystemProperties.getBoolean(PROPERTY_HARDWARE_UI, false);
+        mHardwareAccelerated = false;
         if (wm == null) {

OpenGLRenderer (libhwui) Properties

http://source.android.com/devices/tuning.html

Google IO 2012: Accelerating Android Rendering

Android 3.0 introduce a new hardware accelerated 2D rendering.
Guy and Haase have presented

  • How is android UI being drawn
  • How to enable/disable hardware acceleration in application/acitivity/window level
  • Limitations for hw acceleration
  • ComposeShader
  • Old and New drawing model
  • DisplayList
  • Set layer type per view
  • Reasons to use different type of layers
  • Tips & tricks for application developers

Google PIC

Git Tips

1
repo forall -c "pwd && git status" 2>&1 | tee status.txt    # check status for each project
  • Find commit list which contains [function]
1
git show `git log --oneline [path-to-file] | cut -c 1-8` | egrep -rs "[function]|commit"
  • Search
1
2
3
git log --oneline --author=[email]
git branch --contains [commit id]
git log -S"keyword" [path-to-file]
  • Change Author
1
git commit --amend --author=[email]
  • Rebase
1
2
3
4
git rebase -i HEAD~[number_of_commits]
edit
git commit --amend
git rebase --continue
  • Reset
1
2
3
4
git reset --hard [commit-id]
git reset --soft @{1}
git reset --hard HEAD
git reset --hard HEAD^
  • Remote
1
2
git branch -r
git remote show origin
  • Diff
1
2
git log --oneline [tag1]..[tag2]
git log --oneline [branch1]..[branch2]
  • Patch
1
2
3
git format-patch <commit id>^!  # generate patch w.r.t. commit id
git format-patch -1             # generate patch w.r.t. top commit
git cherry-pick <commit id>     # cherry pick commit