xref: /haiku/src/servers/app/BitmapManager.cpp (revision 4f00613311d0bd6b70fa82ce19931c41f071ea4e)
1 /*
2  * Copyright 2001-2005, Haiku.
3  * Distributed under the terms of the MIT License.
4  *
5  * Authors:
6  *		DarkWyrm <bpmagic@columbus.rr.com>
7  */
8 
9 /**	Handler for allocating and freeing area memory for BBitmaps
10  *	on the server side. Utilizes the BGET pool allocator.
11  *
12  *	Whenever a ServerBitmap associated with a client-side BBitmap needs to be
13  *	created or destroyed, the BitmapManager needs to handle it. It takes care of
14  *	all memory management related to them.
15  */
16 
17 
18 #include <new>
19 #include <stdio.h>
20 #include <string.h>
21 
22 #include <Autolock.h>
23 
24 #include "BitmapManager.h"
25 #include "ServerBitmap.h"
26 #include "ServerTokenSpace.h"
27 
28 using std::nothrow;
29 
30 
31 //! The bitmap allocator for the server. Memory is allocated/freed by the AppServer class
32 BitmapManager *gBitmapManager = NULL;
33 
34 //! Number of bytes to allocate to each area used for bitmap storage
35 #define BITMAP_AREA_SIZE	B_PAGE_SIZE * 16
36 
37 
38 //! Sets up stuff to be ready to allocate space for bitmaps
39 BitmapManager::BitmapManager()
40 	:
41 	fBitmapList(1024),
42 	fBuffer(NULL),
43 	fLock("BitmapManager Lock"),
44 	fMemPool("bitmap pool", BITMAP_AREA_SIZE)
45 {
46 }
47 
48 
49 //! Deallocates everything associated with the manager
50 BitmapManager::~BitmapManager()
51 {
52 	int32 count = fBitmapList.CountItems();
53 	for (int32 i = 0; i < count; i++) {
54 		if (ServerBitmap* bitmap = (ServerBitmap*)fBitmapList.ItemAt(i)) {
55 			fMemPool.ReleaseBuffer(bitmap->fBuffer);
56 			delete bitmap;
57 		}
58 	}
59 }
60 
61 
62 /*!
63 	\brief Allocates a new ServerBitmap.
64 	\param bounds Size of the bitmap
65 	\param space Color space of the bitmap
66 	\param flags Bitmap flags as defined in Bitmap.h
67 	\param bytesPerRow Number of bytes per row.
68 	\param screen Screen id of the screen associated with it. Unused.
69 	\return A new ServerBitmap or NULL if unable to allocate one.
70 */
71 ServerBitmap*
72 BitmapManager::CreateBitmap(BRect bounds, color_space space, int32 flags,
73 							int32 bytesPerRow, screen_id screen)
74 {
75 	BAutolock locker(fLock);
76 
77 	if (!locker.IsLocked())
78 		return NULL;
79 
80 	ServerBitmap* bitmap = new(nothrow) ServerBitmap(bounds, space, flags, bytesPerRow);
81 	if (bitmap == NULL)
82 		return NULL;
83 
84 	// Server version of this code will also need to handle such things as
85 	// bitmaps which accept child views by checking the flags.
86 	uint8* buffer = (uint8*)fMemPool.GetBuffer(bitmap->BitsLength());
87 
88 	if (buffer && fBitmapList.AddItem(bitmap)) {
89 		bitmap->fArea = area_for(buffer);
90 		bitmap->fBuffer = buffer;
91 		bitmap->fToken = gTokenSpace.NewToken(kBitmapToken, bitmap);
92 		bitmap->fInitialized = true;
93 
94 		// calculate area offset
95 		area_info info;
96 		get_area_info(bitmap->fArea, &info);
97 		bitmap->fOffset = buffer - (uint8*)info.address;
98 	} else {
99 		// Allocation failed for buffer or bitmap list
100 		fMemPool.ReleaseBuffer(buffer);
101 		delete bitmap;
102 		bitmap = NULL;
103 	}
104 
105 	return bitmap;
106 }
107 
108 
109 /*!
110 	\brief Deletes a ServerBitmap.
111 	\param bitmap The bitmap to delete
112 */
113 void
114 BitmapManager::DeleteBitmap(ServerBitmap *bitmap)
115 {
116 	BAutolock locker(fLock);
117 
118 	if (!locker.IsLocked())
119 		return;
120 
121 	if (fBitmapList.RemoveItem(bitmap)) {
122 		// Server code will require a check to ensure bitmap doesn't have its own area
123 		fMemPool.ReleaseBuffer(bitmap->fBuffer);
124 		delete bitmap;
125 	}
126 }
127