您好,登錄后才能下訂單哦!
這篇文章主要介紹Android中Bitmap有什么用,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!
Bitmap (android.graphics.Bitmap)
Bitmap是Android系統中的圖像處理的最重要類之一。用它可以獲取圖像文件信息,進行圖像剪切、旋轉、縮放等操作,并可以指定格式保存圖像文件。
基于android-6.0.1_r80源代碼分析
通過下面三個章節基本可以掃清 Bitmap 盲區。文章沒有覆蓋到的一方面是Bitmap用法,這部分建議閱讀 Glide 庫源代碼。一些 Color 的概念,例如 premultiplied / Dither ,需要具備一定CG物理基礎,不管怎樣先讀下去。
Bitmap對象創建
Bitmap java 層構造函數是通過 native 層 jni call 過來的,邏輯在 Bitmap_creator 方法中。
// /home/yuxiang/repo_aosp/android-6.0.1_r79/frameworks/base/core/jni/android/graphics/Bitmap.cpp static jobject Bitmap_creator(JNIEnv* env, jobject, jintArray jColors, jint offset, jint stride, jint width, jint height, jint configHandle, jboolean isMutable) { SkColorType colorType = GraphicsJNI::legacyBitmapConfigToColorType(configHandle); if (NULL != jColors) { size_t n = env->GetArrayLength(jColors); if (n < SkAbs32(stride) * (size_t)height) { doThrowAIOOBE(env); return NULL; } } // ARGB_4444 is a deprecated format, convert automatically to 8888 if (colorType == kARGB_4444_SkColorType) { colorType = kN32_SkColorType; } SkBitmap bitmap; bitmap.setInfo(SkImageInfo::Make(width, height, colorType, kPremul_SkAlphaType)); Bitmap* nativeBitmap = GraphicsJNI::allocateJavaPixelRef(env, &bitmap, NULL); if (!nativeBitmap) { return NULL; } if (jColors != NULL) { GraphicsJNI::SetPixels(env, jColors, offset, stride, 0, 0, width, height, bitmap); } return GraphicsJNI::createBitmap(env, nativeBitmap, getPremulBitmapCreateFlags(isMutable)); }
legacyBitmapConfigToColorType 將 Bitmap.Config.ARGB_8888 轉成skia域的顏色類型 kBGRA_8888_SkColorType ,顏色類型定義在 SkImageInfo.h 中, kARGB_4444_SkColorType 會強轉成 kN32_SkColorType ,它就是 kBGRA_8888_SkColorType ,不必糾結。
// /home/yuxiang/repo_aosp/android-6.0.1_r79/external/skia/include/core/SkImageInfo.h enum SkColorType { kUnknown_SkColorType, kAlpha_8_SkColorType, kRGB_565_SkColorType, kARGB_4444_SkColorType, kRGBA_8888_SkColorType, kBGRA_8888_SkColorType, kIndex_8_SkColorType, kGray_8_SkColorType, kLastEnum_SkColorType = kGray_8_SkColorType, #if SK_PMCOLOR_BYTE_ORDER(B,G,R,A) kN32_SkColorType = kBGRA_8888_SkColorType, #elif SK_PMCOLOR_BYTE_ORDER(R,G,B,A) kN32_SkColorType = kRGBA_8888_SkColorType, #else #error "SK_*32_SHFIT values must correspond to BGRA or RGBA byte order" #endif };
接著,根據寬、高、顏色類型等創建 SkBitmap ,注意 kPremul_SkAlphaType 描述是 alpha 采用 premultiplied 處理的方式, CG 處理 alpha 存在 premultiplied和unpremultiplied兩 兩種方式。
public: SkImageInfo() : fWidth(0) , fHeight(0) , fColorType(kUnknown_SkColorType) , fAlphaType(kUnknown_SkAlphaType) , fProfileType(kLinear_SkColorProfileType) {} static SkImageInfo Make(int width, int height, SkColorType ct, SkAlphaType at, SkColorProfileType pt = kLinear_SkColorProfileType) { return SkImageInfo(width, height, ct, at, pt); }
Make 創建 SkImageInfo 對象, fWidth 的賦值是一個關鍵點,后面Java層通過 getAllocationByteCount 獲取 Bitmap 內存占用中會用到它計算一行像素占用空間。 allocateJavaPixelRef 是通過 JNI 調用 VMRuntime 實例的 newNonMovableArray 方法分配內存。
int register_android_graphics_Graphics(JNIEnv* env) { jmethodID m; jclass c; ... gVMRuntime = env->NewGlobalRef(env->CallStaticObjectMethod(gVMRuntime_class, m)); gVMRuntime_newNonMovableArray = env->GetMethodID(gVMRuntime_class, "newNonMovableArray", "(Ljava/lang/Class;I)Ljava/lang/Object;"); ... } env->CallObjectMethod(gVMRuntime, gVMRuntime_newNonMovableArray, gByte_class, size) 拿到虛擬機分配Heap對象, env->CallLongMethod(gVMRuntime, gVMRuntime_addressOf, arrayObj) 拿到分配對象的地址,調用 native 層構造函數 new android::Bitmap(env, arrayObj, (void*) addr, info, rowBytes, ctable) // /home/yuxiang/repo_aosp/android-6.0.1_r79/frameworks/base/core/jni/android/graphics/Bitmap.cpp Bitmap::Bitmap(JNIEnv* env, jbyteArray storageObj, void* address, const SkImageInfo& info, size_t rowBytes, SkColorTable* ctable) : mPixelStorageType(PixelStorageType::Java) { env->GetJavaVM(&mPixelStorage.java.jvm); mPixelStorage.java.jweakRef = env->NewWeakGlobalRef(storageObj); mPixelStorage.java.jstrongRef = nullptr; mPixelRef.reset(new WrappedPixelRef(this, address, info, rowBytes, ctable)); // Note: this will trigger a call to onStrongRefDestroyed(), but // we want the pixel ref to have a ref count of 0 at this point mPixelRef->unref(); } void Bitmap::getSkBitmap(SkBitmap* outBitmap) { assertValid(); android::AutoMutex _lock(mLock); // Safe because mPixelRef is a WrappedPixelRef type, otherwise rowBytes() // would require locking the pixels first. outBitmap->setInfo(mPixelRef->info(), mPixelRef->rowBytes()); outBitmap->setPixelRef(refPixelRefLocked())->unref(); outBitmap->setHasHardwareMipMap(hasHardwareMipMap()); } void Bitmap::pinPixelsLocked() { switch (mPixelStorageType) { case PixelStorageType::Invalid: LOG_ALWAYS_FATAL("Cannot pin invalid pixels!"); break; case PixelStorageType::External: case PixelStorageType::Ashmem: // Nothing to do break; case PixelStorageType::Java: { JNIEnv* env = jniEnv(); if (!mPixelStorage.java.jstrongRef) { mPixelStorage.java.jstrongRef = reinterpret_cast<jbyteArray>( env->NewGlobalRef(mPixelStorage.java.jweakRef)); if (!mPixelStorage.java.jstrongRef) { LOG_ALWAYS_FATAL("Failed to acquire strong reference to pixels"); } } break; } } } // /home/yuxiang/repo_aosp/android-6.0.1_r79/frameworks/base/core/jni/android/graphics/Bitmap.h std::unique_ptr<WrappedPixelRef> mPixelRef; PixelStorageType mPixelStorageType; union { struct { void* address; void* context; FreeFunc freeFunc; } external; struct { void* address; int fd; size_t size; } ashmem; struct { JavaVM* jvm; jweak jweakRef; jbyteArray jstrongRef; } java; } mPixelStorage;
native 層的 Bitmap 構造函數, mPixelStorage 保存前面創建 Heap 對象的弱引用, mPixelRef 指向 WrappedPixelRef 。 outBitmap 拿到 mPixelRef 強引用對象,這里理解為拿到 SkBitmap 對象。 Bitmap* nativeBitmap = GraphicsJNI::allocateJavaPixelRef 完成 Bitmap Heap 分配,創建 native 層 Bitmap , SkBitmap 對象,最后自然是創建 Java 層 Bitmap 對象,把該包的包上。 native 層是通過 JNI 方法,在 Java 層創建一個數組對象的,這個數組是對應在 Java 層的 Bitmap 對象的 buffer 數組,所以 pixels 還是保存在 Java 堆。而在 native 層這里它是通過weak指針來引用的,在需要的時候會轉換為strong指針,用完之后又去掉strong指針,這樣這個數組對象還是能夠被Java堆自動回收。里面jstrongRef一開始是賦值為null的,但是在bitmap的getSkBitmap方法會使用weakRef給他賦值。
// /home/yuxiang/repo_aosp/android-6.0.1_r79/frameworks/base/core/jni/android/graphics/Bitmap.cpp jobject GraphicsJNI::createBitmap(JNIEnv* env, android::Bitmap* bitmap, int bitmapCreateFlags, jbyteArray ninePatchChunk, jobject ninePatchInsets, int density) { bool isMutable = bitmapCreateFlags & kBitmapCreateFlag_Mutable; bool isPremultiplied = bitmapCreateFlags & kBitmapCreateFlag_Premultiplied; // The caller needs to have already set the alpha type properly, so the // native SkBitmap stays in sync with the Java Bitmap. assert_premultiplied(bitmap->info(), isPremultiplied); jobject obj = env->NewObject(gBitmap_class, gBitmap_constructorMethodID, reinterpret_cast<jlong>(bitmap), bitmap->javaByteArray(), bitmap->width(), bitmap->height(), density, isMutable, isPremultiplied, ninePatchChunk, ninePatchInsets); hasException(env); // For the side effect of logging. return obj; }
重點看下這里 env->NewObject(gBitmap_class, gBitmap_constructorMethodID,... ,參數中有一處 bitmap->javaByteArray() ,指向的是Heap對象。所以,實際的像素內存只有一份,被不同對象持有, Java 層的 Bitmap , native 層的 Btimap 。
這里順帶說一下 JNI 生命周期。 JNI Local Reference 的生命期是在 native method 的執行期(從 Java 程序切換到 native code 環境時開始創建,或者在 native method 執行時調用 JNI function 創建),在 native method 執行完畢切換回 Java 程序時,所有 JNI Local Reference 被刪除,生命期結束(調用 JNI function 可以提前結束其生命期)。
JNI 編程中明顯的內存泄漏
Native Code 本身的內存泄漏
JNI 編程首先是一門具體的編程語言,或者 C 語言,或者 C++,或者匯編,或者其它 native 的編程語言。每門編程語言環境都實現了自身的內存管理機制。因此,JNI 程序開發者要遵循 native 語言本身的內存管理機制,避免造成內存泄漏。以 C 語言為例,當用 malloc() 在進程堆中動態分配內存時,JNI 程序在使用完后,應當調用 free() 將內存釋放。總之,所有在 native 語言編程中應當注意的內存泄漏規則,在 JNI 編程中依然適應。
Native 語言本身引入的內存泄漏會造成 native memory 的內存,嚴重情況下會造成 native memory 的 out of memory。
Global Reference 引入的內存泄漏
JNI 編程還要同時遵循 JNI 的規范標準,JVM 附加了 JNI 編程特有的內存管理機制。
JNI 中的 Local Reference 只在 native method 執行時存在,當 native method 執行完后自動失效。這種自動失效,使得對 Local Reference 的使用相對簡單,native method 執行完后,它們所引用的 Java 對象的 reference count 會相應減 1。不會造成 Java Heap 中 Java 對象的內存泄漏。
而 Global Reference 對 Java 對象的引用一直有效,因此它們引用的 Java 對象會一直存在 Java Heap 中。程序員在使用 Global Reference 時,需要仔細維護對 Global Reference 的使用。如果一定要使用 Global Reference,務必確保在不用的時候刪除。就像在 C 語言中,調用 malloc() 動態分配一塊內存之后,調用 free() 釋放一樣。否則,Global Reference 引用的 Java 對象將永遠停留在 Java Heap 中,造成 Java Heap 的內存泄漏。
更多JNI泄露,參考閱讀 JNI 編程中潛在的內存泄漏——對 LocalReference 的深入理解
Bitmap對象釋放
基于前文 JNI Local Reference和Global Reference 泄露,可以看到 nativeRecycle 實際調用native層Bitmap的 freePixels 方法, DeleteWeakGlobalRef 釋放Bitmap native層Gloabl引用。邏輯還是很簡單的。
// /home/yuxiang/repo_aosp/android-6.0.1_r79/frameworks/base/core/jni/android/graphics/Bitmap.cpp void Bitmap::freePixels() { AutoMutex _lock(mLock); if (mPinnedRefCount == 0) { doFreePixels(); mPixelStorageType = PixelStorageType::Invalid; } } void Bitmap::doFreePixels() { switch (mPixelStorageType) { case PixelStorageType::Invalid: // already free'd, nothing to do break; case PixelStorageType::External: mPixelStorage.external.freeFunc(mPixelStorage.external.address, mPixelStorage.external.context); break; case PixelStorageType::Ashmem: munmap(mPixelStorage.ashmem.address, mPixelStorage.ashmem.size); close(mPixelStorage.ashmem.fd); break; case PixelStorageType::Java: JNIEnv* env = jniEnv(); LOG_ALWAYS_FATAL_IF(mPixelStorage.java.jstrongRef, "Deleting a bitmap wrapper while there are outstanding strong " "references! mPinnedRefCount = %d", mPinnedRefCount); env->DeleteWeakGlobalRef(mPixelStorage.java.jweakRef); break; } if (android::uirenderer::Caches::hasInstance()) { android::uirenderer::Caches::getInstance().textureCache.releaseTexture( mPixelRef->getStableID()); } }
需要注意兩點訊息,一是Java層主動call recycle()方法或者Bitmap析構函數都會調用freePixels,移除Global對象引用,這個對象是Heap上存一堆像素的空間。GC時釋放掉。二是,JNI不再持有Global Reference,并native函數執行后釋放掉,但Java層的Bitmap對象還在,只是它的 mBuffer 和 mNativePtr 是無效地址,沒有像素Heap的Bitmap也就幾乎不消耗內存了。至于Java層Bitmap對象什么時候釋放,生命周期結束自然free掉了。
// /home/yuxiang/repo_aosp/android-6.0.1_r79/art/runtime/jni_internal.cc static void DeleteWeakGlobalRef(JNIEnv* env, jweak obj) { JavaVMExt* vm = down_cast<JNIEnvExt*>(env)->vm; Thread* self = down_cast<JNIEnvExt*>(env)->self; vm->DeleteWeakGlobalRef(self, obj); } // /home/yuxiang/repo_aosp/android-6.0.1_r79/art/runtime/java_vm_ext.cc void JavaVMExt::DeleteWeakGlobalRef(Thread* self, jweak obj) { if (obj == nullptr) { return; } MutexLock mu(self, weak_globals_lock_); if (!weak_globals_.Remove(IRT_FIRST_SEGMENT, obj)) { LOG(WARNING) << "JNI WARNING: DeleteWeakGlobalRef(" << obj << ") " << "failed to find entry"; } }
通過BitmapFactory創建Bitmap
Bitmap工廠類提供了多種decodeXXX方法創建Bitmap對象,主要是兼容不同的數據源,包括byte數組、文件、FD、Resource對象、InputStream,最終去到native層方法, 如下:
// BitmapFactory.java private static native Bitmap nativeDecodeStream(InputStream is, byte[] storage, Rect padding, Options opts); private static native Bitmap nativeDecodeFileDescriptor(FileDescriptor fd, Rect padding, Options opts); private static native Bitmap nativeDecodeAsset(long nativeAsset, Rect padding, Options opts); private static native Bitmap nativeDecodeByteArray(byte[] data, int offset, int length, Options opts); private static native boolean nativeIsSeekable(FileDescriptor fd);
來看看 nativeDecodeStream 方法,該方法中先是創建了 bufferedStream 對象,接著 doDecode 返回Bitmap對象。 SkStreamRewindable 定義在 skia 庫中繼承 SkStream ,它聲明了兩個方法 rewind 和 duplicate ,寫過網絡庫的同學一看命名便知是byte操作,前者功能是將文件內部的指針重新指向一個流的開頭,后者是創建共享此緩沖區內容的新的字節緩沖區。
// BitmapFactory.cpp static jobject nativeDecodeStream(JNIEnv* env, jobject clazz, jobject is, jbyteArray storage, jobject padding, jobject options) { jobject bitmap = NULL; SkAutoTDelete<SkStream> stream(CreateJavaInputStreamAdaptor(env, is, storage)); if (stream.get()) { SkAutoTDelete<SkStreamRewindable> bufferedStream( SkFrontBufferedStream::Create(stream.detach(), BYTES_TO_BUFFER)); SkASSERT(bufferedStream.get() != NULL); bitmap = doDecode(env, bufferedStream, padding, options); } return bitmap; }
doDecode 先是通過JNI拿到 Java 層 Options 對象里面的屬性, outWidth、outHeight、inDensity、inTargetDensity 這些。后兩者用來計算Bitmap縮放比例,計算公式 scale = (float) targetDensity / density 。
// BitmapFactory.cpp static jobject doDecode(JNIEnv* env, SkStreamRewindable* stream, jobject padding, jobject options) { int sampleSize = 1; SkImageDecoder::Mode decodeMode = SkImageDecoder::kDecodePixels_Mode; SkColorType prefColorType = kN32_SkColorType; bool doDither = true; bool isMutable = false; float scale = 1.0f; bool preferQualityOverSpeed = false; bool requireUnpremultiplied = false; jobject javaBitmap = NULL; if (options != NULL) { sampleSize = env->GetIntField(options, gOptions_sampleSizeFieldID); if (optionsJustBounds(env, options)) { decodeMode = SkImageDecoder::kDecodeBounds_Mode; } // initialize these, in case we fail later on env->SetIntField(options, gOptions_widthFieldID, -1); env->SetIntField(options, gOptions_heightFieldID, -1); env->SetObjectField(options, gOptions_mimeFieldID, 0); jobject jconfig = env->GetObjectField(options, gOptions_configFieldID); prefColorType = GraphicsJNI::getNativeBitmapColorType(env, jconfig); isMutable = env->GetBooleanField(options, gOptions_mutableFieldID); doDither = env->GetBooleanField(options, gOptions_ditherFieldID); preferQualityOverSpeed = env->GetBooleanField(options, gOptions_preferQualityOverSpeedFieldID); requireUnpremultiplied = !env->GetBooleanField(options, gOptions_premultipliedFieldID); javaBitmap = env->GetObjectField(options, gOptions_bitmapFieldID); if (env->GetBooleanField(options, gOptions_scaledFieldID)) { const int density = env->GetIntField(options, gOptions_densityFieldID); const int targetDensity = env->GetIntField(options, gOptions_targetDensityFieldID); const int screenDensity = env->GetIntField(options, gOptions_screenDensityFieldID); if (density != 0 && targetDensity != 0 && density != screenDensity) { scale = (float) targetDensity / density; } } ... }
這些參數是提供給圖片解碼器 SkImageDecoder 。圖片資源無非是壓縮格式, SkImageDecoder 工廠類根據輸入流同步拿到具體壓縮格式并創建相應解碼器。 GetFormatName 返回支持的圖片格式。
SkImageDecoder 實例將 Options 參數設置下去。如此解壓出來的是根據實際尺寸裁剪后的圖片。
// BitmapFactory.cpp static jobject doDecode(JNIEnv* env, SkStreamRewindable* stream, jobject padding, jobject options) { ... SkImageDecoder* decoder = SkImageDecoder::Factory(stream); if (decoder == NULL) { return nullObjectReturn("SkImageDecoder::Factory returned null"); } decoder->setSampleSize(sampleSize); decoder->setDitherImage(doDither); decoder->setPreferQualityOverSpeed(preferQualityOverSpeed); decoder->setRequireUnpremultipliedColors(requireUnpremultiplied) ... } // SkImageDecoder_FactoryDefault.cpp SkImageDecoder* SkImageDecoder::Factory(SkStreamRewindable* stream) { return image_decoder_from_stream(stream); } // SkImageDecoder_FactoryRegistrar.cpp SkImageDecoder* image_decoder_from_stream(SkStreamRewindable* stream) { SkImageDecoder* codec = NULL; const SkImageDecoder_DecodeReg* curr = SkImageDecoder_DecodeReg::Head(); while (curr) { codec = curr->factory()(stream); // we rewind here, because we promise later when we call "decode", that // the stream will be at its beginning. bool rewindSuceeded = stream->rewind(); // our image decoder's require that rewind is supported so we fail early // if we are given a stream that does not support rewinding. if (!rewindSuceeded) { SkDEBUGF(("Unable to rewind the image stream.")); SkDELETE(codec); return NULL; } if (codec) { return codec; } curr = curr->next(); } return NULL; } // SkImageDecoder.cpp const char* SkImageDecoder::GetFormatName(Format format) { switch (format) { case kUnknown_Format: return "Unknown Format"; case kBMP_Format: return "BMP"; case kGIF_Format: return "GIF"; case kICO_Format: return "ICO"; case kPKM_Format: return "PKM"; case kKTX_Format: return "KTX"; case kASTC_Format: return "ASTC"; case kJPEG_Format: return "JPEG"; case kPNG_Format: return "PNG"; case kWBMP_Format: return "WBMP"; case kWEBP_Format: return "WEBP"; default: SkDEBUGFAIL("Invalid format type!"); } return "Unknown Format"; }
解碼僅僅完成數據的讀取,圖片是經過渲染才能呈現在最終屏幕上,這個步驟在 canvas.drawBitmap 方法中完成。
// BitmapFactory.cpp static jobject doDecode(JNIEnv* env, SkStreamRewindable* stream, jobject padding, jobject options) { ... SkBitmap outputBitmap; if (willScale) { // This is weird so let me explain: we could use the scale parameter // directly, but for historical reasons this is how the corresponding // Dalvik code has always behaved. We simply recreate the behavior here. // The result is slightly different from simply using scale because of // the 0.5f rounding bias applied when computing the target image size const float sx = scaledWidth / float(decodingBitmap.width()); const float sy = scaledHeight / float(decodingBitmap.height()); // TODO: avoid copying when scaled size equals decodingBitmap size SkColorType colorType = colorTypeForScaledOutput(decodingBitmap.colorType()); // FIXME: If the alphaType is kUnpremul and the image has alpha, the // colors may not be correct, since Skia does not yet support drawing // to/from unpremultiplied bitmaps. outputBitmap.setInfo(SkImageInfo::Make(scaledWidth, scaledHeight, colorType, decodingBitmap.alphaType())); if (!outputBitmap.tryAllocPixels(outputAllocator, NULL)) { return nullObjectReturn("allocation failed for scaled bitmap"); } // If outputBitmap's pixels are newly allocated by Java, there is no need // to erase to 0, since the pixels were initialized to 0. if (outputAllocator != &javaAllocator) { outputBitmap.eraseColor(0); } SkPaint paint; paint.setFilterQuality(kLow_SkFilterQuality); SkCanvas canvas(outputBitmap); canvas.scale(sx, sy); canvas.drawARGB(0x00, 0x00, 0x00, 0x00); canvas.drawBitmap(decodingBitmap, 0.0f, 0.0f, &paint); } ... // now create the java bitmap return GraphicsJNI::createBitmap(env, javaAllocator.getStorageObjAndReset(), bitmapCreateFlags, ninePatchChunk, ninePatchInsets, -1); }
最終渲染后的圖片數據包在了 Bitmap 對象中,這部分邏輯重回第一章節 Bitmap對象創建 。
以上是“Android中Bitmap有什么用”這篇文章的所有內容,感謝各位的閱讀!希望分享的內容對大家有幫助,更多相關知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。