如何复制位图句柄

来源:互联网 发布:品牌策划案例 知乎 编辑:程序博客网 时间:2024/05/16 06:21

 

Using the Handle() and Duplicate() methods of the CFbsBitmap class we can easily duplicate a bitmap object. 

The resulting object points to the same bitmap object but can be independently deleted from the original object.

 

This is due to the fact that CFbsBitmap is really a handle to a Bitmap managed by font and bitmap server. 

The managed Bitmap object is reference counted and only deleted when reference count reaches zero. 

So when handle to the Bitmap is duplicated the resulting CFbsBitmap object refereces the same bitmap, 

only with its reference count incremented.

 

使用CFbsBitmap类的Handle()和Duplicate方法,我们可以容易的复制一个位图对象。复制的对象指向和源相同的位图对象,但是可以被单独的从原始对象删除。

 

这是因为实际上CFbsBitmap是字体位图服务器所管理的一个位图句柄。被管理的位图对象是被引用计数的。

只有当引用计数达到0时才被删除。所以当位图的句柄被复制时,结果是CFbsBitmap对象指向相同的位图,仅仅是引用计数增长了。

 

Usage Scenarios

 

1. Use these API to share CFbsBitmap instances across threads/processes as you cannot share instances of CFbsBitmap directly between threads.

 

2. An object can take an independent ownership a CFbsBitmap instances contained by another object without fearing deletion by the containing object.

 

3. You can also use this technique for making a copy of the bitmap( in the same thread) rather than copying them pixel by pixel for quick,temporary usage.

 

使用场合:

1。线程进程间共享位图实例,因为你不能直接在线程间共享位图实例。

2。一个对象可以得到一个位图实例的独立所有权,而不害怕删除其中包含的对象。

3。你可以使用这个技术拷贝一个位图(相同线程中),而不是按像素来拷贝,为了效率,但临时使用。

 

Code Snippet

 

The following code snippet demonstrates duplicating bitmap handles within a single thread.

 

//Duplicates a bitmap creating and returning a new CFbsBitmap object.

//Ownership of newly created object vest with the caller.

 

CFbsBitmap* DuplicateBitmapL(CFbsBitmap & aSourceBitmap) 

{

        //Target bitmap.

        CFbsBitmap* dstbitmap = new (ELeave) CFbsBitmap;

        CleanupStack::PushL(dstbitmap);

 

        //Get the handle to source bitmap

        TInt srchandle=aSourceBitmap.Handle();

 

        //Duplicate the bitmap handle. Increases the RefCount of bitmap 

        //managed but Fbs Server

        User::LeaveIfError(dstbitmap->Duplicate(srchandle));

 

        //It should be better to track the error returned by Duplicate() 

        //to get the notification about whether the bitmap is copied successfully or not.

 

        CleanupStack::Pop(dstbitmap);

        return dstbitmap;

}

 

Code Snippet

 

The following code snippet demonstrates duplicating CGulIcon.

 

CGulIcon* DuplicateBitmapL(CGulIcon& aIcon) 

{

       CFbsBitmap* pBitmap = new (ELeave) CFbsBitmap();

       CleanupStack::PushL(pBitmap);

       CFbsBitmap* pBitMask = new (ELeave) CFbsBitmap();

       CleanupStack::PushL(pBitMask);

 

       User::LeaveIfError(pBitmap->Duplicate(aIcon.Bitmap()->Handle()));

       User::LeaveIfError(pBitMask->Duplicate(aIcon.Mask()->Handle()));

 

       CGulIcon* Icon = CGulIcon::NewL(pBitmap, pBitMask);

       //Both pBitmap and pBitMask are now owned by Icon

       CleanupStack::Pop(2); 

       return Icon ;

}

 

NOTE: This way we are not copying the bitmap to another, we are just creating a (duplicate) handle for the bitmap, in the font and bitmap server.

 

Also, deleting one bitmap (thus handle) does not affect the other one.

原创粉丝点击