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);
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中的2D引擎skia之 -- SkBitmap的内存管理分析
- Android SkBitmap的内存管理分析
- 安卓开发之SkBitmap的内存管理分析【转】
- android 2D图形系统 skia框架基本分析(一)--以后整个skia调用关系图出来
- Android 2d物理引擎Box2d的使用示例
- Android 关于Bitmap对象于内存管理
- Skia引擎API整理介绍(skia in Android 2.3 trunk)
- Android 内存管理 &Memory Leak & OOM 分析
- Skia引擎API整理介绍(skia in Android 2.3 trunk)
- android 2D游戏开发,引擎设计(三) 实验脚本编写与纸娃娃系统
- Android Bitmap 全面解析(三)开源图片框架分析1-UIL(下)
- Android进程的内存管理分析
- Android内存管理及Memory Leak、OOM分析
- Android 内存管理分析(四)
- android 2D游戏开发,引擎设计(六) 画面平移晃动与平移边界问题
- Android Bitmap 全面解析(三)开源图片框架分析1-UIL(下) ...
- Android系统2D/3D硬件加速分析报告
- Android/Ophone平台2D游戏引擎实现系列文章
- android 2D游戏开发,引擎设计(一) 基本构思
- Skia往SkBitmap上绘图时画不出来的问题