2008年6月9日 星期一

SurfaceView 如何使用



import Android.view.*;

SurfaceView sv;
SurfaceHolder holder;

sv = (SurfaceView) findViewById ( R.id.sv1 );
holder = sv.getHolder();

Canvas cv = holder.lockCanvas();
cv.drawBitmap ( bitmap , left , top , new Paint() );
holder.unlockCanvasAndPost(cv);

這一段 code 會比使用 ImageView 觀看 Bitmap 來的更快, 因為我們利用的 Canvas 直接繪圖, 但是 ImageView 在到 android.graphics.drawable.BitmapDrawable 的 onDraw (Canvas canvas); 之前還需要作很多判斷, 所以速度上來說會比直接使用 Canvas 慢上許多.

另外 drawBitmap 其實是呼叫 JNI native_drawBitmap 來作實際上得繪圖動作.
public void drawBitmap(Bitmap bitmap, float left, float top, Paint paint)
{
native_drawBitmap(mNativeCanvas, bitmap.ni(), left, top, paint == null ? 0 : paint.mNativePaint);
}

2008年6月7日 星期六

測量 CLOCK and Times

自己寫的在 linux 下面測量時間的方式 , 跟大家分享一下 。

typedef struct clock_info {

char name[20];
clock_t start;
clock_t end;
clock_t total;
clock_t times;
clock_t max;
clock_t min;
clock_t tmp_dur;
} clock_info;

void clock_info_Init( clock_info *ci , char *name)
{
strncpy(ci->name, name, 20);
ci->start = 0 ; ci->end = 0; ci->total = 0;
ci->times = 0 ; ci->max = 0; ci->min = 0;
ci->tmp_dur = 0;
}
void clock_info_sample_start ( clock_info *ci, clock_t sample )
{
ci->start = sample;
}
void clock_info_sample_end ( clock_info *ci , clock_t sample )
{
ci->end = sample;
ci->tmp_dur = ci->end - ci->start;
ci->total += ci->tmp_dur;
ci->times ++;
if ( ci->times == 1 )
{
ci->max = ci->tmp_dur;
ci->min = ci->tmp_dur;
}
if ( ci->tmp_dur > ci->max )
ci->max = ci->tmp_dur;

if ( ci->tmp_dur <>min )
ci->min = ci->tmp_dur;
}
void clock_info_print ( clock_info *ci )
{
char str[255];
sprintf(str," CLOCK Measurement report CLOCKS_PER_SEC = %d\n", CLOCKS_PER_SEC);
printf(str);

double avg = (double)( (double) ci->total / (double) ci->times ) ;
double max = (double) ci->max;
double min = (double) ci->min;
sprintf(str," <%s>\t max : %7.5f (clk) \t min : %7.5f (clk) \t avg : %7.5f (clk) \n",
ci->name, max , min , avg);
printf(str);

avg = ( avg / (double)CLOCKS_PER_SEC );
max = ( max / (double)CLOCKS_PER_SEC );
min = ( min / (double)CLOCKS_PER_SEC );
sprintf(str," <%s>\t max : %7.5f (s) \t min : %7.5f (s) \t avg : %7.5f (s)\n",
ci->name, max , min , avg );
printf(str);

avg = avg * 1000.0;
max = max * 1000.0;
min = min * 1000.0;
sprintf(str," <%s>\t max : %7.5f (ms) \t min : %7.5f (ms) \t avg : %7.5f (ms) \n",
ci->name, max , min , avg);
prinf(str);
}

Android上面呼叫 ffmpeg 解壓縮的播放程式


這禮拜五完成一支 可以在Android上面呼叫 ffmpeg 解壓縮的播放程式,這支程式的架構是目前計畫的目標,在 Android 上打出一條路徑可以讓硬體 執行解壓縮。

雖然之前就有做出雛形,但是因為 IPC 控制機制設計不好,所以程式容易 Crash ,另外也容易產生 memory leaking。

最後在本週五下班之前修改出來,每 110ms 可以解壓縮完一張 H.264的 Frame,程式不會 Crash。也能夠控制程式的關閉暫停等等,實在非常感動 努力了三個月終於有一支可以上檯面交差的程式了。( 備註一下 110ms 的時間測量方式 我是採用 clock() 這個 system call 測量得知的 )

在這段撰寫程式的這段時間中, 我遇到了 java threading、同步等兩大難題, 要在 java 裡面做 thread 控制,要在 jni 與 ffmpeg decoder 之間做 ipc 控制。

java threading 算是比較簡單且容易撰寫,但需要閱讀的文件也是最多的。不習慣 synchronized 這種 coding 方式。另外 Thread 要呼叫某個 class的方法必須透過 Android Handler,但是 Handler 沒有 message filter 的功能所以必須自己設計。這大概是我覺得重點的地方。

同步通訊的部份,我用 FIFO ( mkfifo ) 的方式,開通兩個 fifo 讓兩支程式做溝通,傳控制訊息。 至於解開後的 raw data ( RGB32 ) 則適用 memory mapping i/o 的方式,讓 jni 與 ffmpeg decoder 共用同一個 page 降低搬移與傳輸資料的成本。

這邊不得不提一下為何要用 RGB32,因為 FFmpeg 解壓縮的單位是 byte,如果你用 RGB16 就是佔兩個 bytes,RGB24 就是三個。但是 Android 的 Bitmap 可讀取資料來源 array 是 int 格式, 在 java 的定義中是佔了 4 bytes ,所以用 RGB32 可以避免掉 Align data 的時間。

如果加上 SetIntArrayRegion(env, iarr, 0, size, tmp); 就可以輕鬆的把解壓縮完後的 data 傳完 java (但是 Bitmap 那邊要選用 ARGB888 的 config )。

另外還有一件事情就是論文被通知 accept 。
最後感謝跟我一起工作的同事們,沒有你們互相切磋 這隻程式沒法完成,謝謝你們。
這一周真的既勞累又充實, 疲勞都被成功的喜悅給淹沒了。
(加班三天~~ 囧!!)