Skbitmap is an important class in Skia, a lot of drawing action involves Skbitmap, it encapsulates a series of operations related to bitmaps, understand its memory management strategy to help us better use it, understand its original intention is to achieve the Skia in the BLiTTER hardware acceleration.
1. Class structure of Skbitmap:
2. Skbitmap Embedded class Allocator
Allocator is a Skbitmap inline class, in fact, there is only one member function: Allocpixelref (), so it is more appropriate to understand it as an interface, SKBITMAP using allocator derived classes- Heapallocator as its default allocator. It is implemented as follows:
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;
}
Of course, you can also define a allocator yourself, using the Skbitmap member function allocpixels (allocator* allocator, skcolortable* CTable), and pass in the custom allocator, If NULL is passed in, the default heapallocator is used.
3. Skpixelref class
Skpixelref and allocator are closely related, allocator allocated memory by SKPIXELREF to handle reference count, each allocator corresponding to a skpixelref, usually after the allocation of memory success, Setpixelref is called by allocator to bind. By default, Skbitmap uses Skmallocpixelref and heapallocator for pairing. So if you want to derive the allocator class, you typically also need to derive a skpixelref class corresponding to it.
4. Examples of Use
Here's a short code that signals how to dynamically assign a skbitmap:
Skbitmap bitmap;
Bitmap.setconfig (Hasalpha?) Skbitmap::kargb_8888_config:
Skbitmap::krgb_565_config, width, height);
if (!bitmap.allocpixels ()) {
Return
}
...//Bitmap paint operation
//......
Draw on the canvas.
Canvas->drawbitmap (Bitmap, Skfloattoscalar (x), Skfloattoscalar (y), paint);