Skbitmap Memory management for Android development

Source: Internet
Author: User

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);

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.