Android中的2D引擎skia之 -- 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作为它的默认分配器。其实现如下:

  1. bool SkBitmap::HeapAllocator::allocPixelRef(SkBitmap* dst,  
  2.                                             SkColorTable* ctable) {  
  3.     Sk64 size = dst->getSize64();  
  4.     if (size.isNeg() || !size.is32()) {  
  5.         return false;  
  6.     }  
  7.     void* addr = sk_malloc_flags(size.get32(), 0);  // returns NULL on failure   
  8.     if (NULL == addr) {  
  9.         return false;  
  10.     }  
  11.     dst->setPixelRef(new SkMallocPixelRef(addr, size.get32(), ctable))->unref();  
  12.     // since we're already allocated, we lockPixels right away   
  13.     dst->lockPixels();  
  14.     return true;  
  15. }  

当然,也可以自己定义一个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:

  1. SkBitmap    bitmap;  
  2.   
  3. bitmap.setConfig(hasAlpha ? SkBitmap::kARGB_8888_Config :  
  4.                  SkBitmap::kRGB_565_Config, width, height);  
  5. if (!bitmap.allocPixels()) {  
  6.     return;  
  7. }  
  8.   
  9. //......   
  10. // 对bitmap进行画图操作   
  11. //......           
  12. // 画到Canvas上   
  13. canvas->drawBitmap(bitmap, SkFloatToScalar(x), SkFloatToScalar(y),  
  14.                    paint);  

相关内容