#include "ServerBitmap.h"
#include <new>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "BitmapManager.h"
#include "ClientMemoryAllocator.h"
#include "ColorConversion.h"
#include "HWInterface.h"
#include "InterfacePrivate.h"
#include "Overlay.h"
#include "ServerApp.h"
using std::nothrow;
using namespace BPrivate;
ServerBitmap::ServerBitmap(BRect rect, color_space space, uint32 flags,
int32 bytesPerRow, screen_id screen)
:
fMemory(NULL),
fOverlay(NULL),
fBuffer(NULL),
fWidth(rect.IntegerWidth() + 1),
fHeight(rect.IntegerHeight() + 1),
fBytesPerRow(0),
fSpace(space),
fFlags(flags),
fOwner(NULL)
{
int32 minBytesPerRow = get_bytes_per_row(space, fWidth);
fBytesPerRow = max_c(bytesPerRow, minBytesPerRow);
}
ServerBitmap::ServerBitmap(const ServerBitmap* bitmap)
:
fMemory(NULL),
fOverlay(NULL),
fBuffer(NULL),
fOwner(NULL)
{
if (bitmap) {
fWidth = bitmap->fWidth;
fHeight = bitmap->fHeight;
fBytesPerRow = bitmap->fBytesPerRow;
fSpace = bitmap->fSpace;
fFlags = bitmap->fFlags;
} else {
fWidth = 0;
fHeight = 0;
fBytesPerRow = 0;
fSpace = B_NO_COLOR_SPACE;
fFlags = 0;
}
}
ServerBitmap::~ServerBitmap()
{
if (fMemory != NULL) {
if (fMemory != &fClientMemory)
delete fMemory;
} else
delete[] fBuffer;
}
void
ServerBitmap::AllocateBuffer()
{
uint32 length = BitsLength();
if (length > 0) {
delete[] fBuffer;
fBuffer = new(std::nothrow) uint8[length];
}
}
status_t
ServerBitmap::ImportBits(const void *bits, int32 bitsLength, int32 bytesPerRow,
color_space colorSpace)
{
if (!bits || bitsLength < 0 || bytesPerRow <= 0)
return B_BAD_VALUE;
return BPrivate::ConvertBits(bits, fBuffer, bitsLength, BitsLength(),
bytesPerRow, fBytesPerRow, colorSpace, fSpace, fWidth, fHeight);
}
status_t
ServerBitmap::ImportBits(const void *bits, int32 bitsLength, int32 bytesPerRow,
color_space colorSpace, BPoint from, BPoint to, int32 width, int32 height)
{
if (!bits || bitsLength < 0 || bytesPerRow <= 0 || width < 0 || height < 0)
return B_BAD_VALUE;
return BPrivate::ConvertBits(bits, fBuffer, bitsLength, BitsLength(),
bytesPerRow, fBytesPerRow, colorSpace, fSpace, from, to, width,
height);
}
area_id
ServerBitmap::Area() const
{
if (fMemory != NULL)
return fMemory->Area();
return B_ERROR;
}
uint32
ServerBitmap::AreaOffset() const
{
if (fMemory != NULL)
return fMemory->AreaOffset();
return 0;
}
void
ServerBitmap::SetOverlay(::Overlay* overlay)
{
fOverlay.SetTo(overlay);
}
::Overlay*
ServerBitmap::Overlay() const
{
return fOverlay.Get();
}
void
ServerBitmap::SetOwner(ServerApp* owner)
{
fOwner = owner;
}
ServerApp*
ServerBitmap::Owner() const
{
return fOwner;
}
void
ServerBitmap::PrintToStream()
{
printf("Bitmap@%p: (%" B_PRId32 ":%" B_PRId32 "), space %" B_PRId32 ", "
"bpr %" B_PRId32 ", buffer %p\n", this, fWidth, fHeight, (int32)fSpace,
fBytesPerRow, fBuffer);
}
UtilityBitmap::UtilityBitmap(BRect rect, color_space space, uint32 flags,
int32 bytesPerRow, screen_id screen)
:
ServerBitmap(rect, space, flags, bytesPerRow, screen)
{
AllocateBuffer();
}
UtilityBitmap::UtilityBitmap(const ServerBitmap* bitmap)
:
ServerBitmap(bitmap)
{
AllocateBuffer();
if (bitmap->Bits())
memcpy(Bits(), bitmap->Bits(), bitmap->BitsLength());
}
UtilityBitmap::UtilityBitmap(const uint8* alreadyPaddedData, uint32 width,
uint32 height, color_space format)
:
ServerBitmap(BRect(0, 0, width - 1, height - 1), format, 0)
{
AllocateBuffer();
if (Bits())
memcpy(Bits(), alreadyPaddedData, BitsLength());
}
UtilityBitmap::~UtilityBitmap()
{
}