您的位置:首页 > 移动开发 > Android开发

Android中的2D引擎skia之 -- SkBitmap的内存管理分析

2013-05-31 16:00 651 查看
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作为它的默认分配器。其实现如下:

[c-sharp] view
plaincopy

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:

 

[c-sharp] view
plaincopy

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);  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android