Android SkBitmap的内存管理分析


Android使用的2D图形引擎skia,是一个高效的2D矢量图形库,google已经把skia开源:http://code.google.com/p/skia/

SkBitmap是skia中很重要的一个类,很多画图动作涉及到SkBitmap,它封装了与位图相关的一系列操作,了解它的内存管理策略有助于我们更好的使用它,了解它的初衷是要想实现对skia中的blitter进行硬件加速。

1.  SkBitmap的类结构:


2.  SkBitmap的内嵌类Allocator

     Allocator是SkBitmap的内嵌类,其实只有一个成员函数:allocPixelRef(),所以把它理解为一个接口更合适,SkBitmap使用Allocator的派生类--HeapAllocator作为它的默认分配器。其实现如下:

bool SkBitmap::HeapAllocator::allocPixelRef(SkBitmap* dst,
                                            SkColorTable* ctable) {
    Sk64 size = dst->getSize64();
    if (size.isNeg() || !size.is32()) {
        return false;
    }
    void* addr = sk_malloc_flags(size.get32(), 0);  // returns NULL on failure
    if (NULL == addr) {
        return false;
    }
    dst->setPixelRef(new SkMallocPixelRef(addr, size.get32(), ctable))->unref();
    // since we're already allocated, we lockPixels right away
    dst->lockPixels();
    return true;
}

当然,也可以自己定义一个Allocator,使用SkBitmap的成员函数allocPixels(Allocator* allocator, SkColorTable* ctable) ,传入自定义的Allocator即可,如果传入NULL,则使用默认的HeapAllocator

3.  SkPixelRef类

    SkPixelRef和Allocator密切相关,Allocator分配的内存由SkPixelRef来处理引用计数,每个Allocator对应一个SkPixelRef,通常在分配内存成功后,由Allocator调用setPixelRef来进行绑定。默认的情况下,SkBitmap使用SkMallocPixelRef和HeapAllocator进行配对。所以如果你要派生Allocator类,通常也需要派生一个SkPixelRef类与之对应。

4.  使用例子

以下是一段简短的代码,示意如何动态分配一个SkBitmap:

        SkBitmap    bitmap;
        
        bitmap.setConfig(hasAlpha ? SkBitmap::kARGB_8888_Config :
                         SkBitmap::kRGB_565_Config, width, height);
        if (!bitmap.allocPixels()) {
            return;
        }
        
        //......
        // 对bitmap进行画图操作
        //......        
        // 画到Canvas上
        canvas->drawBitmap(bitmap, SkFloatToScalar(x), SkFloatToScalar(y),  paint);

相关内容