The 2D graphics engine skia used by Android is an efficient 2D vector graphics library. Google has opened skia Open Source: http://code.google.com/p/skia /.
Skbitmap is a very important class in skia. Many painting operations involve skbitmap. It encapsulates a series of operations related to bitmap. understanding its memory management policy helps us better use it, it was intended to accelerate the hardware of blitter in skia.
1. skbitmap class structure:
2. The nested class allocator of skbitmap
Allocator is an embedded class of skbitmap. In fact, there is only one member function: allocpixelref (). Therefore, it is more suitable to understand it as an interface. skbitmap uses the derived class of Allocator-heapallocator as its default distributor. The implementation is as follows:
Bool skbitmap: heapallocator: allocpixelref (skbitmap * DST, <br/> skcolortable * ctable) {<br/> sk64 size = DST-> getsize64 (); <br/> If (size. isneg () |! Size. is32 () {<br/> return false; <br/>}< br/> void * ADDR = sk_malloc_flags (size. get32 (), 0); // returns NULL on failure <br/> If (null = ADDR) {<br/> return false; <br/>}< br/> DST-> setpixelref (New skmallocpixelref (ADDR, size. get32 (), ctable)-> unref (); <br/> // since we're re already allocated, we lockpixels right away <br/> DST-> lockpixels (); <br/> return true; <br/>}
Of course, you can also define an allocator by yourself. Use the member function allocpixels (Allocator * Allocator, skcolortable * ctable) of skbitmap to pass in the Custom allocator. If the input is null, the default heapallocator is used.
3. skpixelref class
Skpixelref is closely related to allocator. The memory allocated by allocator is handled by skpixelref. Each Allocator corresponds to a skpixelref. Generally, Allocator calls setpixelref to bind the allocated memory. By default, skbitmap uses skmallocpixelref and heapallocator for pairing. Therefore, if you want to derive the allocator class, you usually need to derive a skpixelref class that corresponds to it.
4. Example
The following is a short piece of code indicating how to dynamically allocate a skbitmap:
Skbitmap bitmap; </P> <p> bitmap. setconfig (hasalpha? Skbitmap: kargb_8888_config: <br/> skbitmap: krgb_565_config, width, height); <br/> If (! Bitmap. allocpixels () {<br/> return; <br/>}</P> <p> //...... <br/> // draw bitmap <br/> //...... <br/> // draw to canvas <br/> canvas-> drawbitmap (bitmap, skfloattoscalar (x), skfloattoscalar (Y), <br/> paint );