Implemented reference counting of ServerBitmaps, made constructor and destructor private,
as only the BitmapManager class is allowed to call them. git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@15718 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
parent
93052717b0
commit
4acb99b60f
|
@ -1,36 +1,19 @@
|
||||||
//------------------------------------------------------------------------------
|
/*
|
||||||
// Copyright (c) 2001-2005, Haiku, Inc.
|
* Copyright 2001-2005, Haiku.
|
||||||
//
|
* Distributed under the terms of the MIT License.
|
||||||
// Permission is hereby granted, free of charge, to any person obtaining a
|
*
|
||||||
// copy of this software and associated documentation files (the "Software"),
|
* Authors:
|
||||||
// to deal in the Software without restriction, including without limitation
|
* DarkWyrm <bpmagic@columbus.rr.com>
|
||||||
// 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: ServerBitmap.h
|
|
||||||
// Author: DarkWyrm <bpmagic@columbus.rr.com>
|
|
||||||
// Description: Bitmap class used inside the server
|
|
||||||
//
|
|
||||||
//------------------------------------------------------------------------------
|
|
||||||
#ifndef _SERVER_BITMAP_H_
|
#ifndef _SERVER_BITMAP_H_
|
||||||
#define _SERVER_BITMAP_H_
|
#define _SERVER_BITMAP_H_
|
||||||
|
|
||||||
|
|
||||||
#include <GraphicsDefs.h>
|
#include <GraphicsDefs.h>
|
||||||
#include <Rect.h>
|
#include <Rect.h>
|
||||||
#include <OS.h>
|
#include <OS.h>
|
||||||
|
|
||||||
|
|
||||||
class BitmapManager;
|
class BitmapManager;
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
|
@ -43,14 +26,7 @@ class BitmapManager;
|
||||||
*/
|
*/
|
||||||
class ServerBitmap {
|
class ServerBitmap {
|
||||||
public:
|
public:
|
||||||
ServerBitmap(BRect rect,
|
void Acquire();
|
||||||
color_space space,
|
|
||||||
int32 flags,
|
|
||||||
int32 bytesperline = -1,
|
|
||||||
screen_id screen = B_MAIN_SCREEN_ID);
|
|
||||||
ServerBitmap(const ServerBitmap* bmp);
|
|
||||||
|
|
||||||
virtual ~ServerBitmap();
|
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
\brief Returns the area in which the buffer resides
|
\brief Returns the area in which the buffer resides
|
||||||
|
@ -107,6 +83,14 @@ protected:
|
||||||
friend class BitmapManager;
|
friend class BitmapManager;
|
||||||
friend class PicturePlayer;
|
friend class PicturePlayer;
|
||||||
|
|
||||||
|
ServerBitmap(BRect rect,
|
||||||
|
color_space space,
|
||||||
|
int32 flags,
|
||||||
|
int32 bytesperline = -1,
|
||||||
|
screen_id screen = B_MAIN_SCREEN_ID);
|
||||||
|
ServerBitmap(const ServerBitmap* bmp);
|
||||||
|
virtual ~ServerBitmap();
|
||||||
|
|
||||||
//! used by the BitmapManager
|
//! used by the BitmapManager
|
||||||
inline void _SetArea(area_id ID)
|
inline void _SetArea(area_id ID)
|
||||||
{ fArea = ID; }
|
{ fArea = ID; }
|
||||||
|
@ -115,6 +99,8 @@ protected:
|
||||||
inline void _SetBuffer(void *ptr)
|
inline void _SetBuffer(void *ptr)
|
||||||
{ fBuffer = (uint8*)ptr; }
|
{ fBuffer = (uint8*)ptr; }
|
||||||
|
|
||||||
|
bool _Release();
|
||||||
|
|
||||||
void _AllocateBuffer();
|
void _AllocateBuffer();
|
||||||
void _FreeBuffer();
|
void _FreeBuffer();
|
||||||
|
|
||||||
|
@ -124,6 +110,7 @@ protected:
|
||||||
bool fInitialized;
|
bool fInitialized;
|
||||||
area_id fArea;
|
area_id fArea;
|
||||||
uint8* fBuffer;
|
uint8* fBuffer;
|
||||||
|
int32 fReferenceCount;
|
||||||
|
|
||||||
int32 fWidth;
|
int32 fWidth;
|
||||||
int32 fHeight;
|
int32 fHeight;
|
||||||
|
|
|
@ -113,8 +113,12 @@ BitmapManager::CreateBitmap(BRect bounds, color_space space, int32 flags,
|
||||||
void
|
void
|
||||||
BitmapManager::DeleteBitmap(ServerBitmap *bitmap)
|
BitmapManager::DeleteBitmap(ServerBitmap *bitmap)
|
||||||
{
|
{
|
||||||
BAutolock locker(fLock);
|
if (!bitmap->_Release()) {
|
||||||
|
// there are other references to this bitmap, we don't have to delete it yet
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
BAutolock locker(fLock);
|
||||||
if (!locker.IsLocked())
|
if (!locker.IsLocked())
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
|
|
@ -1,37 +1,20 @@
|
||||||
//------------------------------------------------------------------------------
|
/*
|
||||||
// Copyright (c) 2001-2005, Haiku, Inc.
|
* Copyright 2001-2005, Haiku.
|
||||||
//
|
* Distributed under the terms of the MIT License.
|
||||||
// Permission is hereby granted, free of charge, to any person obtaining a
|
*
|
||||||
// copy of this software and associated documentation files (the "Software"),
|
* Authors:
|
||||||
// to deal in the Software without restriction, including without limitation
|
* DarkWyrm <bpmagic@columbus.rr.com>
|
||||||
// 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: ServerBitmap.cpp
|
|
||||||
// Author: DarkWyrm <bpmagic@columbus.rr.com>
|
|
||||||
// Description: Bitmap class used by the server
|
|
||||||
//
|
|
||||||
//------------------------------------------------------------------------------
|
|
||||||
#include <new>
|
|
||||||
|
|
||||||
#include <string.h>
|
|
||||||
|
|
||||||
#include "ServerBitmap.h"
|
#include "ServerBitmap.h"
|
||||||
|
|
||||||
|
#include <new>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
using std::nothrow;
|
using std::nothrow;
|
||||||
|
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
\brief Constructor called by the BitmapManager (only).
|
\brief Constructor called by the BitmapManager (only).
|
||||||
\param rect Size of the bitmap.
|
\param rect Size of the bitmap.
|
||||||
|
@ -48,6 +31,7 @@ ServerBitmap::ServerBitmap(BRect rect, color_space space,
|
||||||
: fInitialized(false),
|
: fInitialized(false),
|
||||||
fArea(B_ERROR),
|
fArea(B_ERROR),
|
||||||
fBuffer(NULL),
|
fBuffer(NULL),
|
||||||
|
fReferenceCount(1),
|
||||||
// WARNING: '1' is added to the width and height.
|
// WARNING: '1' is added to the width and height.
|
||||||
// Same is done in FBBitmap subclass, so if you
|
// Same is done in FBBitmap subclass, so if you
|
||||||
// modify here make sure to do the same under
|
// modify here make sure to do the same under
|
||||||
|
@ -59,16 +43,17 @@ ServerBitmap::ServerBitmap(BRect rect, color_space space,
|
||||||
fFlags(flags),
|
fFlags(flags),
|
||||||
fBitsPerPixel(0)
|
fBitsPerPixel(0)
|
||||||
// TODO: what about fToken and fOffset ?!?
|
// TODO: what about fToken and fOffset ?!?
|
||||||
|
|
||||||
{
|
{
|
||||||
_HandleSpace(space, bytesPerLine);
|
_HandleSpace(space, bytesPerLine);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
//! Copy constructor does not copy the buffer.
|
//! Copy constructor does not copy the buffer.
|
||||||
ServerBitmap::ServerBitmap(const ServerBitmap* bmp)
|
ServerBitmap::ServerBitmap(const ServerBitmap* bmp)
|
||||||
: fInitialized(false),
|
: fInitialized(false),
|
||||||
fArea(B_ERROR),
|
fArea(B_ERROR),
|
||||||
fBuffer(NULL)
|
fBuffer(NULL),
|
||||||
|
fReferenceCount(1)
|
||||||
// TODO: what about fToken and fOffset ?!?
|
// TODO: what about fToken and fOffset ?!?
|
||||||
{
|
{
|
||||||
if (bmp) {
|
if (bmp) {
|
||||||
|
@ -89,6 +74,7 @@ ServerBitmap::ServerBitmap(const ServerBitmap* bmp)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
\brief Empty. Defined for subclasses.
|
\brief Empty. Defined for subclasses.
|
||||||
*/
|
*/
|
||||||
|
@ -98,6 +84,24 @@ ServerBitmap::~ServerBitmap()
|
||||||
// instead of do that in every subclass ?
|
// instead of do that in every subclass ?
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void
|
||||||
|
ServerBitmap::Acquire()
|
||||||
|
{
|
||||||
|
atomic_add(&fReferenceCount, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
bool
|
||||||
|
ServerBitmap::_Release()
|
||||||
|
{
|
||||||
|
if (atomic_add(&fReferenceCount, -1) == 1)
|
||||||
|
return true;
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
\brief Internal function used by subclasses
|
\brief Internal function used by subclasses
|
||||||
|
|
||||||
|
@ -115,19 +119,21 @@ ServerBitmap::_AllocateBuffer(void)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
\brief Internal function used by subclasses
|
\brief Internal function used by subclasses
|
||||||
|
|
||||||
Subclasses should call this to free the internal buffer.
|
Subclasses should call this to free the internal buffer.
|
||||||
*/
|
*/
|
||||||
void
|
void
|
||||||
ServerBitmap::_FreeBuffer(void)
|
ServerBitmap::_FreeBuffer()
|
||||||
{
|
{
|
||||||
delete[] fBuffer;
|
delete[] fBuffer;
|
||||||
fBuffer = NULL;
|
fBuffer = NULL;
|
||||||
fInitialized = false;
|
fInitialized = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
\brief Internal function used to translate color space values to appropriate internal
|
\brief Internal function used to translate color space values to appropriate internal
|
||||||
values.
|
values.
|
||||||
|
@ -237,6 +243,10 @@ ServerBitmap::_HandleSpace(color_space space, int32 bytesPerRow)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// #pragma mark -
|
||||||
|
|
||||||
|
|
||||||
UtilityBitmap::UtilityBitmap(BRect rect, color_space space,
|
UtilityBitmap::UtilityBitmap(BRect rect, color_space space,
|
||||||
int32 flags, int32 bytesperline,
|
int32 flags, int32 bytesperline,
|
||||||
screen_id screen)
|
screen_id screen)
|
||||||
|
@ -245,6 +255,7 @@ UtilityBitmap::UtilityBitmap(BRect rect, color_space space,
|
||||||
_AllocateBuffer();
|
_AllocateBuffer();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
UtilityBitmap::UtilityBitmap(const ServerBitmap* bmp)
|
UtilityBitmap::UtilityBitmap(const ServerBitmap* bmp)
|
||||||
: ServerBitmap(bmp)
|
: ServerBitmap(bmp)
|
||||||
{
|
{
|
||||||
|
@ -253,6 +264,7 @@ UtilityBitmap::UtilityBitmap(const ServerBitmap* bmp)
|
||||||
memcpy(Bits(), bmp->Bits(), bmp->BitsLength());
|
memcpy(Bits(), bmp->Bits(), bmp->BitsLength());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
UtilityBitmap::UtilityBitmap(const uint8* alreadyPaddedData,
|
UtilityBitmap::UtilityBitmap(const uint8* alreadyPaddedData,
|
||||||
uint32 width, uint32 height,
|
uint32 width, uint32 height,
|
||||||
color_space format)
|
color_space format)
|
||||||
|
@ -263,6 +275,7 @@ UtilityBitmap::UtilityBitmap(const uint8* alreadyPaddedData,
|
||||||
memcpy(Bits(), alreadyPaddedData, BitsLength());
|
memcpy(Bits(), alreadyPaddedData, BitsLength());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
UtilityBitmap::~UtilityBitmap()
|
UtilityBitmap::~UtilityBitmap()
|
||||||
{
|
{
|
||||||
_FreeBuffer();
|
_FreeBuffer();
|
||||||
|
|
Loading…
Reference in New Issue