2003-02-15 18:28:22 +03:00
|
|
|
//------------------------------------------------------------------------------
|
2004-09-21 02:50:02 +04:00
|
|
|
// Copyright (c) 2001-2002, Haiku, Inc.
|
2003-02-15 18:28:22 +03:00
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
//------------------------------------------------------------------------------
|
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>
|
|
|
|
|
|
|
|
#include "ServerBitmap.h"
|
|
|
|
|
|
|
|
#include "BitmapManager.h"
|
2003-02-13 03:03:31 +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 03:14:40 +04:00
|
|
|
BitmapManager *bitmapmanager = 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
|
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
|
2005-06-24 03:14:40 +04:00
|
|
|
BitmapManager::BitmapManager()
|
|
|
|
: fBitmapList(1024),
|
|
|
|
fBitmapArea(B_ERROR),
|
|
|
|
fBuffer(NULL),
|
|
|
|
fTokenizer(),
|
|
|
|
fLock(B_ERROR),
|
|
|
|
fMemPool()
|
2003-02-13 03:03:31 +03:00
|
|
|
|
2005-06-24 03:14:40 +04:00
|
|
|
{
|
2003-02-13 03:03:31 +03:00
|
|
|
// When create_area is passed the B_ANY_ADDRESS flag, the address of the area
|
|
|
|
// is stored in the pointer to a pointer.
|
2005-06-24 03:14:40 +04:00
|
|
|
fBitmapArea = create_area("bitmap_area", (void**)&fBuffer,
|
|
|
|
B_ANY_ADDRESS, BITMAP_AREA_SIZE,
|
|
|
|
B_NO_LOCK, B_READ_AREA | B_WRITE_AREA);
|
|
|
|
|
|
|
|
if (fBitmapArea < B_OK)
|
|
|
|
printf("PANIC: BitmapManager couldn't allocate area: %s\n", strerror(fBitmapArea));
|
2003-02-13 03:03:31 +03:00
|
|
|
|
2005-06-24 03:14:40 +04:00
|
|
|
fLock = create_sem(1,"bmpmanager_fLock");
|
|
|
|
if (fLock < B_OK)
|
|
|
|
printf("PANIC: BitmapManager couldn't allocate fLocking semaphore!!\n");
|
2003-02-13 03:03:31 +03:00
|
|
|
|
2005-06-24 03:14:40 +04:00
|
|
|
fMemPool.AddToPool(fBuffer, BITMAP_AREA_SIZE);
|
2003-02-13 03:03:31 +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-06-24 03:14:40 +04:00
|
|
|
delete_area(fBitmapArea);
|
|
|
|
delete_sem(fLock);
|
2003-02-13 03:03:31 +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 03:14:40 +04:00
|
|
|
ServerBitmap* bitmap = NULL;
|
|
|
|
if (acquire_sem(fLock) >= B_OK) {
|
|
|
|
bitmap = new(nothrow) ServerBitmap(bounds, space, flags, bytesPerRow);
|
2003-02-13 03:03:31 +03:00
|
|
|
|
2005-06-24 03:14:40 +04:00
|
|
|
// 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;
|
|
|
|
bitmap->fToken = fTokenizer.GetToken();
|
|
|
|
bitmap->fInitialized = true;
|
|
|
|
|
|
|
|
// calculate area offset
|
|
|
|
area_info ai;
|
|
|
|
get_area_info(bitmap->fArea, &ai);
|
|
|
|
bitmap->fOffset = buffer - (uint8*)ai.address;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
// Allocation failed for buffer or bitmap list
|
|
|
|
fMemPool.ReleaseBuffer(buffer);
|
|
|
|
delete bitmap;
|
|
|
|
bitmap = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
release_sem(fLock);
|
2003-02-13 03:03:31 +03:00
|
|
|
}
|
2005-06-24 03:14:40 +04:00
|
|
|
return bitmap;
|
2003-02-13 03:03:31 +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 03:14:40 +04:00
|
|
|
if (acquire_sem(fLock) >= B_OK) {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
release_sem(fLock);
|
2003-02-13 03:03:31 +03:00
|
|
|
}
|
|
|
|
}
|