2005-06-24 07:31:41 +04:00
|
|
|
/*
|
|
|
|
* Copyright 2001-2005, Haiku.
|
|
|
|
* Distributed under the terms of the MIT License.
|
|
|
|
*
|
|
|
|
* Authors:
|
|
|
|
* DarkWyrm <bpmagic@columbus.rr.com>
|
|
|
|
*/
|
|
|
|
|
|
|
|
/** Handler for allocating and freeing area memory for BBitmaps
|
|
|
|
* on the server side. Utilizes the BGET pool allocator.
|
2005-11-14 22:46:20 +03:00
|
|
|
*
|
|
|
|
* Whenever a ServerBitmap associated with a client-side BBitmap needs to be
|
|
|
|
* created or destroyed, the BitmapManager needs to handle it. It takes care of
|
|
|
|
* all memory management related to them.
|
2005-06-24 07:31:41 +04:00
|
|
|
*/
|
|
|
|
|
2005-06-24 03:14:40 +04:00
|
|
|
|
|
|
|
#include <new>
|
2003-02-13 03:03:31 +03:00
|
|
|
#include <stdio.h>
|
2005-06-24 03:14:40 +04:00
|
|
|
#include <string.h>
|
|
|
|
|
2005-06-24 07:31:41 +04:00
|
|
|
#include <Autolock.h>
|
2005-06-24 03:14:40 +04:00
|
|
|
|
|
|
|
#include "BitmapManager.h"
|
2005-11-14 22:46:20 +03:00
|
|
|
#include "ServerBitmap.h"
|
|
|
|
#include "ServerTokenSpace.h"
|
2003-02-13 03:03:31 +03:00
|
|
|
|
2005-11-13 02:27:14 +03:00
|
|
|
using std::nothrow;
|
2005-06-24 07:31:41 +04:00
|
|
|
|
2005-11-14 22:46:20 +03:00
|
|
|
|
2003-02-20 23:14:57 +03:00
|
|
|
//! The bitmap allocator for the server. Memory is allocated/freed by the AppServer class
|
2005-06-24 07:31:41 +04:00
|
|
|
BitmapManager *gBitmapManager = NULL;
|
2003-02-20 23:14:57 +03:00
|
|
|
|
2003-02-17 19:24:27 +03:00
|
|
|
//! Number of bytes to allocate to each area used for bitmap storage
|
2005-12-08 15:41:19 +03:00
|
|
|
#define BITMAP_AREA_SIZE B_PAGE_SIZE * 16
|
2003-02-13 03:03:31 +03:00
|
|
|
|
2005-11-14 22:46:20 +03:00
|
|
|
|
2003-02-17 19:24:27 +03:00
|
|
|
//! Sets up stuff to be ready to allocate space for bitmaps
|
2005-06-24 03:14:40 +04:00
|
|
|
BitmapManager::BitmapManager()
|
2005-06-24 08:01:16 +04:00
|
|
|
:
|
|
|
|
fBitmapList(1024),
|
|
|
|
fBuffer(NULL),
|
|
|
|
fLock("BitmapManager Lock"),
|
|
|
|
fMemPool("bitmap pool", BITMAP_AREA_SIZE)
|
2005-06-24 03:14:40 +04:00
|
|
|
{
|
2003-02-13 03:03:31 +03:00
|
|
|
}
|
|
|
|
|
2005-11-14 22:46:20 +03:00
|
|
|
|
2003-02-17 19:24:27 +03:00
|
|
|
//! Deallocates everything associated with the manager
|
2005-06-24 03:14:40 +04:00
|
|
|
BitmapManager::~BitmapManager()
|
2003-02-13 03:03:31 +03:00
|
|
|
{
|
2005-06-24 03:14:40 +04:00
|
|
|
int32 count = fBitmapList.CountItems();
|
|
|
|
for (int32 i = 0; i < count; i++) {
|
|
|
|
if (ServerBitmap* bitmap = (ServerBitmap*)fBitmapList.ItemAt(i)) {
|
|
|
|
fMemPool.ReleaseBuffer(bitmap->fBuffer);
|
|
|
|
delete bitmap;
|
2003-02-13 03:03:31 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-11-14 22:46:20 +03:00
|
|
|
|
2003-02-17 19:24:27 +03:00
|
|
|
/*!
|
|
|
|
\brief Allocates a new ServerBitmap.
|
|
|
|
\param bounds Size of the bitmap
|
|
|
|
\param space Color space of the bitmap
|
|
|
|
\param flags Bitmap flags as defined in Bitmap.h
|
2005-06-24 03:14:40 +04:00
|
|
|
\param bytesPerRow Number of bytes per row.
|
2003-02-17 19:24:27 +03:00
|
|
|
\param screen Screen id of the screen associated with it. Unused.
|
|
|
|
\return A new ServerBitmap or NULL if unable to allocate one.
|
|
|
|
*/
|
2005-06-24 03:14:40 +04:00
|
|
|
ServerBitmap*
|
|
|
|
BitmapManager::CreateBitmap(BRect bounds, color_space space, int32 flags,
|
|
|
|
int32 bytesPerRow, screen_id screen)
|
2003-02-13 03:03:31 +03:00
|
|
|
{
|
2005-06-24 07:31:41 +04:00
|
|
|
BAutolock locker(fLock);
|
2005-06-24 03:14:40 +04:00
|
|
|
|
2005-06-24 07:31:41 +04:00
|
|
|
if (!locker.IsLocked())
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
ServerBitmap* bitmap = new(nothrow) ServerBitmap(bounds, space, flags, bytesPerRow);
|
|
|
|
if (bitmap == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
// Server version of this code will also need to handle such things as
|
|
|
|
// bitmaps which accept child views by checking the flags.
|
|
|
|
uint8* buffer = (uint8*)fMemPool.GetBuffer(bitmap->BitsLength());
|
|
|
|
|
|
|
|
if (buffer && fBitmapList.AddItem(bitmap)) {
|
|
|
|
bitmap->fArea = area_for(buffer);
|
|
|
|
bitmap->fBuffer = buffer;
|
2005-11-14 22:46:20 +03:00
|
|
|
bitmap->fToken = gTokenSpace.NewToken(kBitmapToken, bitmap);
|
2005-06-24 07:31:41 +04:00
|
|
|
bitmap->fInitialized = true;
|
|
|
|
|
2005-06-24 08:01:16 +04:00
|
|
|
// calculate area offset
|
|
|
|
area_info info;
|
|
|
|
get_area_info(bitmap->fArea, &info);
|
|
|
|
bitmap->fOffset = buffer - (uint8*)info.address;
|
2005-06-24 07:31:41 +04:00
|
|
|
} else {
|
|
|
|
// Allocation failed for buffer or bitmap list
|
|
|
|
fMemPool.ReleaseBuffer(buffer);
|
|
|
|
delete bitmap;
|
|
|
|
bitmap = NULL;
|
2003-02-13 03:03:31 +03:00
|
|
|
}
|
2005-06-24 07:31:41 +04:00
|
|
|
|
2005-06-24 03:14:40 +04:00
|
|
|
return bitmap;
|
2003-02-13 03:03:31 +03:00
|
|
|
}
|
|
|
|
|
2005-11-14 22:46:20 +03:00
|
|
|
|
2003-02-17 19:24:27 +03:00
|
|
|
/*!
|
|
|
|
\brief Deletes a ServerBitmap.
|
|
|
|
\param bitmap The bitmap to delete
|
|
|
|
*/
|
2005-06-24 03:14:40 +04:00
|
|
|
void
|
|
|
|
BitmapManager::DeleteBitmap(ServerBitmap *bitmap)
|
2003-02-13 03:03:31 +03:00
|
|
|
{
|
2005-06-24 07:31:41 +04:00
|
|
|
BAutolock locker(fLock);
|
|
|
|
|
|
|
|
if (!locker.IsLocked())
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (fBitmapList.RemoveItem(bitmap)) {
|
|
|
|
// Server code will require a check to ensure bitmap doesn't have its own area
|
|
|
|
fMemPool.ReleaseBuffer(bitmap->fBuffer);
|
|
|
|
delete bitmap;
|
2003-02-13 03:03:31 +03:00
|
|
|
}
|
|
|
|
}
|