深入理解深入理解Android Bitmap
Bitmap是Android系统中的图像处理的最重要类之一。这篇文章主要介绍了理解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: