2 * Copyright 2001-2010, Haiku.
3 * Distributed under the terms of the MIT License.
6 * DarkWyrm <bpmagic@columbus.rr.com>
7 * Axel Dörfler, axeld@pinc-software.de
11 #include "ServerBitmap.h"
18 #include "BitmapManager.h"
19 #include "ClientMemoryAllocator.h"
20 #include "ColorConversion.h"
21 #include "HWInterface.h"
22 #include "InterfacePrivate.h"
24 #include "ServerApp.h"
28 using namespace BPrivate
;
31 /*! A word about memory housekeeping and why it's implemented this way:
33 The reason why this looks so complicated is to optimize the most common
34 path (bitmap creation from the application), and don't cause any further
35 memory allocations for maintaining memory in that case.
36 If a bitmap was allocated this way, both, the fAllocator and
37 fAllocationCookie members are used.
39 For overlays, the allocator only allocates a small piece of client memory
40 for use with the overlay_client_data structure - the actual buffer will be
41 placed in the graphics frame buffer and is allocated by the graphics driver.
43 If the memory was allocated on the app_server heap, neither fAllocator, nor
44 fAllocationCookie are used, and the buffer is just freed in that case when
45 the bitmap is destructed. This method is mainly used for cursors.
49 /*! \brief Constructor called by the BitmapManager (only).
50 \param rect Size of the bitmap.
51 \param space Color space of the bitmap
52 \param flags Various bitmap flags to tweak the bitmap as defined in Bitmap.h
53 \param bytesperline Number of bytes in each row. -1 implies the default
54 value. Any value less than the the default will less than the default
55 will be overridden, but any value greater than the default will result
56 in the number of bytes specified.
57 \param screen Screen assigned to the bitmap.
59 ServerBitmap::ServerBitmap(BRect rect
, color_space space
, uint32 flags
,
60 int32 bytesPerRow
, screen_id screen
)
65 // WARNING: '1' is added to the width and height.
66 // Same is done in FBBitmap subclass, so if you
67 // modify here make sure to do the same under
68 // FBBitmap::SetSize(...)
69 fWidth(rect
.IntegerWidth() + 1),
70 fHeight(rect
.IntegerHeight() + 1),
75 // fToken is initialized (if used) by the BitmapManager
77 int32 minBytesPerRow
= get_bytes_per_row(space
, fWidth
);
79 fBytesPerRow
= max_c(bytesPerRow
, minBytesPerRow
);
83 //! Copy constructor does not copy the buffer.
84 ServerBitmap::ServerBitmap(const ServerBitmap
* bitmap
)
92 fWidth
= bitmap
->fWidth
;
93 fHeight
= bitmap
->fHeight
;
94 fBytesPerRow
= bitmap
->fBytesPerRow
;
95 fSpace
= bitmap
->fSpace
;
96 fFlags
= bitmap
->fFlags
;
101 fSpace
= B_NO_COLOR_SPACE
;
107 ServerBitmap::~ServerBitmap()
109 if (fMemory
!= NULL
) {
110 if (fMemory
!= &fClientMemory
)
116 // deleting the overlay will also free the overlay buffer
120 /*! \brief Internal function used by subclasses
122 Subclasses should call this so the buffer can automagically
123 be allocated on the heap.
126 ServerBitmap::AllocateBuffer()
128 uint32 length
= BitsLength();
131 fBuffer
= new(std::nothrow
) uint8
[length
];
137 ServerBitmap::ImportBits(const void *bits
, int32 bitsLength
, int32 bytesPerRow
,
138 color_space colorSpace
)
140 if (!bits
|| bitsLength
< 0 || bytesPerRow
<= 0)
143 return BPrivate::ConvertBits(bits
, fBuffer
, bitsLength
, BitsLength(),
144 bytesPerRow
, fBytesPerRow
, colorSpace
, fSpace
, fWidth
, fHeight
);
149 ServerBitmap::ImportBits(const void *bits
, int32 bitsLength
, int32 bytesPerRow
,
150 color_space colorSpace
, BPoint from
, BPoint to
, int32 width
, int32 height
)
152 if (!bits
|| bitsLength
< 0 || bytesPerRow
<= 0 || width
< 0 || height
< 0)
155 return BPrivate::ConvertBits(bits
, fBuffer
, bitsLength
, BitsLength(),
156 bytesPerRow
, fBytesPerRow
, colorSpace
, fSpace
, from
, to
, width
,
162 ServerBitmap::Area() const
165 return fMemory
->Area();
172 ServerBitmap::AreaOffset() const
175 return fMemory
->AreaOffset();
182 ServerBitmap::SetOverlay(::Overlay
* overlay
)
189 ServerBitmap::Overlay() const
196 ServerBitmap::SetOwner(ServerApp
* owner
)
203 ServerBitmap::Owner() const
210 ServerBitmap::PrintToStream()
212 printf("Bitmap@%p: (%" B_PRId32
":%" B_PRId32
"), space %" B_PRId32
", "
213 "bpr %" B_PRId32
", buffer %p\n", this, fWidth
, fHeight
, (int32
)fSpace
,
214 fBytesPerRow
, fBuffer
);
221 UtilityBitmap::UtilityBitmap(BRect rect
, color_space space
, uint32 flags
,
222 int32 bytesPerRow
, screen_id screen
)
224 ServerBitmap(rect
, space
, flags
, bytesPerRow
, screen
)
230 UtilityBitmap::UtilityBitmap(const ServerBitmap
* bitmap
)
237 memcpy(Bits(), bitmap
->Bits(), bitmap
->BitsLength());
241 UtilityBitmap::UtilityBitmap(const uint8
* alreadyPaddedData
, uint32 width
,
242 uint32 height
, color_space format
)
244 ServerBitmap(BRect(0, 0, width
- 1, height
- 1), format
, 0)
248 memcpy(Bits(), alreadyPaddedData
, BitsLength());
252 UtilityBitmap::~UtilityBitmap()