2003-02-15 18:28:22 +03:00
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
// Copyright (c) 2001-2002, OpenBeOS
|
|
|
|
//
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
// copy of this software and associated documentation files (the "Software"),
|
|
|
|
// to deal in the Software without restriction, including without limitation
|
|
|
|
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
|
|
// and/or sell copies of the Software, and to permit persons to whom the
|
|
|
|
// Software is furnished to do so, subject to the following conditions:
|
|
|
|
//
|
|
|
|
// The above copyright notice and this permission notice shall be included in
|
|
|
|
// all copies or substantial portions of the Software.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
|
|
// DEALINGS IN THE SOFTWARE.
|
|
|
|
//
|
|
|
|
// File Name: BitmapManager.cpp
|
|
|
|
// Author: DarkWyrm <bpmagic@columbus.rr.com>
|
|
|
|
// Description: Handler for allocating and freeing area memory for BBitmaps
|
|
|
|
// on the server side. Utilizes the BGET pool allocator.
|
|
|
|
//------------------------------------------------------------------------------
|
2003-02-13 03:03:31 +03:00
|
|
|
#include "BitmapManager.h"
|
|
|
|
#include "ServerBitmap.h"
|
|
|
|
#include <stdio.h>
|
|
|
|
|
2003-02-20 23:14:57 +03:00
|
|
|
//! The bitmap allocator for the server. Memory is allocated/freed by the AppServer class
|
|
|
|
BitmapManager *bitmapmanager=NULL;
|
|
|
|
|
2003-02-17 19:24:27 +03:00
|
|
|
//! BGET function calls - defined here because of C++ name mangling causing link problems
|
2003-02-13 03:03:31 +03:00
|
|
|
typedef long bufsize;
|
|
|
|
extern "C" void bpool(void *buffer, bufsize len);
|
|
|
|
extern "C" void *bget(bufsize size);
|
|
|
|
extern "C" void *bgetz(bufsize size);
|
|
|
|
extern "C" void *bgetr(void *buffer, bufsize newsize);
|
|
|
|
extern "C" void brel(void *buf);
|
|
|
|
extern "C" void bectl(int (*compact)(bufsize sizereq, int sequence),
|
|
|
|
void *(*acquire)(bufsize size),
|
|
|
|
void (*release)(void *buf), bufsize pool_incr);
|
|
|
|
extern "C" void bstats(bufsize *curalloc, bufsize *totfree, bufsize *maxfree,
|
|
|
|
long *nget, long *nrel);
|
|
|
|
extern "C" void bstatse(bufsize *pool_incr, long *npool, long *npget,
|
|
|
|
long *nprel, long *ndget, long *ndrel);
|
|
|
|
extern "C" void bufdump(void *buf);
|
|
|
|
extern "C" void bpoold(void *pool, int dumpalloc, int dumpfree);
|
|
|
|
extern "C" int bpoolv(void *pool);
|
|
|
|
|
2003-02-17 19:24:27 +03:00
|
|
|
/*!
|
|
|
|
\brief This is a call which makes BGET use a couple of home-grown functions which
|
|
|
|
manage the buffer via area functions
|
|
|
|
*/
|
2003-02-13 03:03:31 +03:00
|
|
|
extern "C" void set_area_buffer_management(void);
|
|
|
|
|
2003-02-17 19:24:27 +03:00
|
|
|
//! Number of bytes to allocate to each area used for bitmap storage
|
2003-02-13 03:03:31 +03:00
|
|
|
#define BITMAP_AREA_SIZE B_PAGE_SIZE * 2
|
|
|
|
|
2003-02-17 19:24:27 +03:00
|
|
|
//! Sets up stuff to be ready to allocate space for bitmaps
|
2003-02-13 03:03:31 +03:00
|
|
|
BitmapManager::BitmapManager(void)
|
|
|
|
{
|
|
|
|
bmplist=new BList(0);
|
|
|
|
|
|
|
|
// When create_area is passed the B_ANY_ADDRESS flag, the address of the area
|
|
|
|
// is stored in the pointer to a pointer.
|
|
|
|
bmparea=create_area("bitmap_area",(void**)&buffer,B_ANY_ADDRESS,BITMAP_AREA_SIZE,
|
|
|
|
B_NO_LOCK, B_READ_AREA | B_WRITE_AREA);
|
|
|
|
if(bmparea==B_BAD_VALUE ||
|
|
|
|
bmparea==B_NO_MEMORY ||
|
|
|
|
bmparea==B_ERROR)
|
|
|
|
printf("PANIC: BitmapManager couldn't allocate area!!\n");
|
|
|
|
|
|
|
|
lock=create_sem(1,"bmpmanager_lock");
|
|
|
|
if(lock<0)
|
|
|
|
printf("PANIC: BitmapManager couldn't allocate locking semaphore!!\n");
|
|
|
|
|
|
|
|
set_area_buffer_management();
|
|
|
|
bpool(buffer,BITMAP_AREA_SIZE);
|
|
|
|
}
|
|
|
|
|
2003-02-17 19:24:27 +03:00
|
|
|
//! Deallocates everything associated with the manager
|
2003-02-13 03:03:31 +03:00
|
|
|
BitmapManager::~BitmapManager(void)
|
|
|
|
{
|
|
|
|
if(bmplist->CountItems()>0)
|
|
|
|
{
|
|
|
|
ServerBitmap *tbmp=NULL;
|
|
|
|
int32 itemcount=bmplist->CountItems();
|
|
|
|
for(int32 i=0; i<itemcount; i++)
|
|
|
|
{
|
|
|
|
tbmp=(ServerBitmap*)bmplist->RemoveItem(0L);
|
|
|
|
if(tbmp)
|
|
|
|
{
|
|
|
|
brel(tbmp->_buffer);
|
|
|
|
delete tbmp;
|
|
|
|
tbmp=NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
delete bmplist;
|
|
|
|
delete_area(bmparea);
|
|
|
|
delete_sem(lock);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
\param bytes_per_row Number of bytes per row.
|
|
|
|
\param screen Screen id of the screen associated with it. Unused.
|
|
|
|
\return A new ServerBitmap or NULL if unable to allocate one.
|
|
|
|
*/
|
2003-02-13 03:03:31 +03:00
|
|
|
ServerBitmap * BitmapManager::CreateBitmap(BRect bounds, color_space space, int32 flags,
|
2003-06-23 06:54:52 +04:00
|
|
|
int32 bytes_per_row, screen_id screen)
|
2003-02-13 03:03:31 +03:00
|
|
|
{
|
|
|
|
acquire_sem(lock);
|
|
|
|
ServerBitmap *bmp=new ServerBitmap(bounds, space, flags, bytes_per_row);
|
|
|
|
|
|
|
|
// Server version of this code will also need to handle such things as
|
|
|
|
// bitmaps which accept child views by checking the flags.
|
|
|
|
uint8 *bmpbuffer=(uint8 *)bget(bmp->BitsLength());
|
|
|
|
|
|
|
|
if(!bmpbuffer)
|
|
|
|
{
|
|
|
|
delete bmp;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
bmp->_area=area_for(bmpbuffer);
|
|
|
|
bmp->_buffer=bmpbuffer;
|
2003-03-12 17:29:59 +03:00
|
|
|
bmp->_token=tokenizer.GetToken();
|
2003-10-05 01:53:09 +04:00
|
|
|
bmp->_initialized=true;
|
2003-03-12 17:29:59 +03:00
|
|
|
|
|
|
|
// calculate area offset
|
|
|
|
area_info ai;
|
|
|
|
get_area_info(bmp->_area,&ai);
|
|
|
|
bmp->_offset=bmpbuffer-(uint8*)ai.address;
|
|
|
|
|
2003-02-13 03:03:31 +03:00
|
|
|
bmplist->AddItem(bmp);
|
|
|
|
release_sem(lock);
|
|
|
|
return bmp;
|
|
|
|
}
|
|
|
|
|
2003-02-17 19:24:27 +03:00
|
|
|
/*!
|
|
|
|
\brief Deletes a ServerBitmap.
|
|
|
|
\param bitmap The bitmap to delete
|
|
|
|
*/
|
2003-02-13 03:03:31 +03:00
|
|
|
void BitmapManager::DeleteBitmap(ServerBitmap *bitmap)
|
|
|
|
{
|
|
|
|
acquire_sem(lock);
|
|
|
|
ServerBitmap *tbmp=(ServerBitmap*)bmplist->RemoveItem(bmplist->IndexOf(bitmap));
|
|
|
|
|
|
|
|
if(!tbmp)
|
|
|
|
{
|
|
|
|
release_sem(lock);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Server code will require a check to ensure bitmap doesn't have its own area
|
|
|
|
brel(tbmp->_buffer);
|
|
|
|
delete tbmp;
|
|
|
|
|
|
|
|
release_sem(lock);
|
|
|
|
}
|