今天在讀關(guān)于圖片解析的源碼,看到這樣的代碼:
/**
* Decode an input stream into a bitmap. If the input stream is null, or
* cannot be used to decode a bitmap, the function returns null.
* The stream's position will be where ever it was after the encoded data
* was read.
*
* @param is The input stream that holds the raw data to be decoded into a
* bitmap.
* @param outPadding If not null, return the padding rect for the bitmap if
* it exists, otherwise set padding to [-1,-1,-1,-1]. If
* no bitmap is returned (null) then padding is
* unchanged.
* @param opts null-ok; Options that control downsampling and whether the
* image should be completely decoded, or just is size returned.
* @return The decoded bitmap, or null if the image data could not be
* decoded, or, if opts is non-null, if opts requested only the
* size be returned (in opts.outWidth and opts.outHeight)
*
* <p class="note">Prior to {@link android.os.Build.VERSION_CODES#KITKAT},
* if {@link InputStream#markSupported is.markSupported()} returns true,
* <code>is.mark(1024)</code> would be called. As of
* {@link android.os.Build.VERSION_CODES#KITKAT}, this is no longer the case.</p>
*/
public static Bitmap decodeStream(InputStream is, Rect outPadding, Options opts) {
// we don't throw in this case, thus allowing the caller to only check
// the cache, and not force the image to be decoded.
if (is == null) {
return null;
}
Bitmap bm = null;
Trace.traceBegin(Trace.TRACE_TAG_GRAPHICS, "decodeBitmap");
try {
if (is instanceof AssetManager.AssetInputStream) {
final long asset = ((AssetManager.AssetInputStream) is).getNativeAsset();
bm = nativeDecodeAsset(asset, outPadding, opts);
} else {
bm = decodeStreamInternal(is, outPadding, opts);
}
if (bm == null && opts != null && opts.inBitmap != null) {
throw new IllegalArgumentException("Problem decoding into existing bitmap");
}
setDensityFromOptions(bm, opts);
} finally {
Trace.traceEnd(Trace.TRACE_TAG_GRAPHICS);
}
return bm;
}
其中有兩句代碼:
Trace.traceBegin(Trace.TRACE_TAG_GRAPHICS, "decodeBitmap");
Trace.traceEnd(Trace.TRACE_TAG_GRAPHICS);
很好奇拗军,這個(gè)Trace是干嘛的?之前接觸到有一個(gè)TraceView!TraceView 是 Android 平臺(tái)配備一個(gè)很好的性能分析的工具再扭。它可以通過圖形化的方式讓我們了解我們要跟蹤的程序的性能,并且能具體到 method夜矗!
跟進(jìn)源碼瞧個(gè)究竟泛范,看到這個(gè)代碼的文件注釋:
/**
* Writes trace events to the system trace buffer. These trace events can be
* collected and visualized using the Systrace tool.
*
* <p>This tracing mechanism is independent of the method tracing mechanism
* offered by {@link Debug#startMethodTracing}. In particular, it enables
* tracing of events that occur across multiple processes.
* <p>For information about using the Systrace tool, read <a
* href="{@docRoot}tools/debugging/systrace.html">Analyzing Display and Performance
* with Systrace</a>.
*/
翻譯過來就是:
將跟蹤事件寫入系統(tǒng)跟蹤緩沖區(qū)。 可以使用Systrace工具收集和可視化這些跟蹤事件紊撕。
此跟蹤機(jī)制獨(dú)立于startMethodTracing()提供的方法跟蹤機(jī)制罢荡。 特別地,它使得能夠跟蹤跨多個(gè)進(jìn)程發(fā)生的事件对扶。
有關(guān)使用Systrace工具的信息区赵,請(qǐng)參閱使用Systrace分析顯示和性能。
哈哈浪南,出來一個(gè)東西—— Systrace惧笛。等我完了研究下這個(gè)Systrace.
接下來看著兩個(gè)方法:
Trace.traceBegin
/**
* Writes a trace message to indicate that a given section of code has
* begun. Must be followed by a call to {@link #traceEnd} using the same
* tag.
*
* @param traceTag The trace tag.
* @param methodName The method name to appear in the trace.
*
* @hide
*/
public static void traceBegin(long traceTag, String methodName) {
if (isTagEnabled(traceTag)) {
nativeTraceBegin(traceTag, methodName);
}
}
寫入跟蹤信息這標(biāo)志著要追蹤的代碼已經(jīng)開始運(yùn)行。必須在后面調(diào)用traceEnd逞泄,并且使用相同的tag.
參數(shù)一:根據(jù)的標(biāo)記tag;
參數(shù)二:在追蹤路徑中顯示的方法名患整。
Trace.traceEnd
/**
* Writes a trace message to indicate that the current method has ended.
* Must be called exactly once for each call to {@link #traceBegin} using the same tag.
*
* @param traceTag The trace tag.
*
* @hide
*/
public static void traceEnd(long traceTag) {
if (isTagEnabled(traceTag)) {
nativeTraceEnd(traceTag);
}
}
寫入追蹤信息標(biāo)志著當(dāng)前方法已經(jīng)結(jié)束拜效。必須和traceBegin成對(duì)的調(diào)用并且使用同一個(gè)tag.
參數(shù):追蹤tag和traceBegin傳入的要一致。
至此為止各谚,明白了圖片解析里的這兩句代碼是干嘛的:
/* * Writes trace events to the kernel trace buffer. These trace events can be * collected using the "atrace" program for offline analysis. */
將這些追蹤時(shí)間寫入內(nèi)部追蹤緩存中紧憾。這些追蹤事件可以被“atrace”程序收集起來,進(jìn)行離線分析昌渤。
但是赴穗,再往里面走的話,發(fā)現(xiàn)了這東西竟然也走到了Native層:
private static native void nativeTraceBegin(long tag, String name);
private static native void nativeTraceEnd(long tag);
這兩個(gè)方法是在哪兒呢膀息?仔細(xì)看看了文件發(fā)現(xiàn)了有在各種tag上有兩句注釋:
// These tags must be kept in sync with system/core/include/cutils/trace.h.
// They should also be added to frameworks/native/cmds/atrace/atrace.cpp.
好吧般眉,原來這東西到最后也給C++去做了!
那么問題了潜支,我有什么理由不去好好學(xué)下C++,NDK 呢甸赃?