xref: /haiku/src/servers/app/drawing/BitmapDrawingEngine.cpp (revision cbe0a0c436162d78cc3f92a305b64918c839d079)
1 #include "BitmapDrawingEngine.h"
2 #include "BitmapHWInterface.h"
3 #include "ServerBitmap.h"
4 #include <new>
5 
6 
7 BitmapDrawingEngine::BitmapDrawingEngine(color_space colorSpace)
8 	:	DrawingEngine(),
9 		fColorSpace(colorSpace),
10 		fHWInterface(NULL),
11 		fBitmap(NULL)
12 {
13 }
14 
15 
16 BitmapDrawingEngine::~BitmapDrawingEngine()
17 {
18 	SetSize(0, 0);
19 }
20 
21 
22 #if DEBUG
23 bool
24 BitmapDrawingEngine::IsParallelAccessLocked() const
25 {
26 	// We don't share the HWInterface instance that the Painter is
27 	// attached to, so we never need to be locked.
28 	return true;
29 }
30 #endif
31 
32 
33 bool
34 BitmapDrawingEngine::IsExclusiveAccessLocked() const
35 {
36 	// See IsParallelAccessLocked().
37 	return true;
38 }
39 
40 
41 status_t
42 BitmapDrawingEngine::SetSize(int32 newWidth, int32 newHeight)
43 {
44 	if (fBitmap != NULL && newWidth > 0 && newHeight > 0
45 		&& fBitmap->Bounds().IntegerWidth() >= newWidth
46 		&& fBitmap->Bounds().IntegerHeight() >= newHeight) {
47 		return B_OK;
48 	}
49 
50 	SetHWInterface(NULL);
51 	if (fHWInterface.IsSet()) {
52 		fHWInterface->LockExclusiveAccess();
53 		fHWInterface->Shutdown();
54 		fHWInterface->UnlockExclusiveAccess();
55 		fHWInterface.Unset();
56 	}
57 
58 	if (newWidth <= 0 || newHeight <= 0)
59 		return B_OK;
60 
61 	fBitmap.SetTo(new(std::nothrow) UtilityBitmap(BRect(0, 0, newWidth - 1,
62 		newHeight - 1), fColorSpace, 0));
63 	if (!fBitmap.IsSet())
64 		return B_NO_MEMORY;
65 
66 	fHWInterface.SetTo(new(std::nothrow) BitmapHWInterface(fBitmap));
67 	if (!fHWInterface.IsSet())
68 		return B_NO_MEMORY;
69 
70 	status_t result = fHWInterface->Initialize();
71 	if (result != B_OK)
72 		return result;
73 
74 	// we have to set a valid clipping first
75 	fClipping.Set(fBitmap->Bounds());
76 	ConstrainClippingRegion(&fClipping);
77 	SetHWInterface(fHWInterface.Get());
78 	return B_OK;
79 }
80 
81 
82 UtilityBitmap*
83 BitmapDrawingEngine::ExportToBitmap(int32 width, int32 height,
84 	color_space space)
85 {
86 	if (width <= 0 || height <= 0)
87 		return NULL;
88 
89 	UtilityBitmap *result = new(std::nothrow) UtilityBitmap(BRect(0, 0,
90 		width - 1, height - 1), space, 0);
91 	if (result == NULL)
92 		return NULL;
93 
94 	if (result->ImportBits(fBitmap->Bits(), fBitmap->BitsLength(),
95 		fBitmap->BytesPerRow(), fBitmap->ColorSpace()) != B_OK) {
96 		delete result;
97 		return NULL;
98 	}
99 
100 	return result;
101 }
102