1*25ea7f45SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*25ea7f45SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*25ea7f45SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*25ea7f45SAndrew Rist  * distributed with this work for additional information
6*25ea7f45SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*25ea7f45SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*25ea7f45SAndrew Rist  * "License"); you may not use this file except in compliance
9*25ea7f45SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*25ea7f45SAndrew Rist  *
11*25ea7f45SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*25ea7f45SAndrew Rist  *
13*25ea7f45SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*25ea7f45SAndrew Rist  * software distributed under the License is distributed on an
15*25ea7f45SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*25ea7f45SAndrew Rist  * KIND, either express or implied.  See the License for the
17*25ea7f45SAndrew Rist  * specific language governing permissions and limitations
18*25ea7f45SAndrew Rist  * under the License.
19*25ea7f45SAndrew Rist  *
20*25ea7f45SAndrew Rist  *************************************************************/
21*25ea7f45SAndrew Rist 
22*25ea7f45SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_canvas.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include <boost/bind.hpp>
28cdf0e10cSrcweir #include "pagemanager.hxx"
29cdf0e10cSrcweir 
30cdf0e10cSrcweir namespace canvas
31cdf0e10cSrcweir {
32cdf0e10cSrcweir 
33cdf0e10cSrcweir 	//////////////////////////////////////////////////////////////////////////////////
34cdf0e10cSrcweir 	// PageManager
35cdf0e10cSrcweir 	//////////////////////////////////////////////////////////////////////////////////
36cdf0e10cSrcweir 
37cdf0e10cSrcweir 	//////////////////////////////////////////////////////////////////////////////////
38cdf0e10cSrcweir 	// PageManager::allocateSpace
39cdf0e10cSrcweir 	//////////////////////////////////////////////////////////////////////////////////
40cdf0e10cSrcweir 
allocateSpace(const::basegfx::B2ISize & rSize)41cdf0e10cSrcweir 	FragmentSharedPtr PageManager::allocateSpace( const ::basegfx::B2ISize& rSize )
42cdf0e10cSrcweir 	{
43cdf0e10cSrcweir 		// we are asked to find a location for the requested size.
44cdf0e10cSrcweir 		// first we try to satisfy the request from the
45cdf0e10cSrcweir 		// remaining space in the existing pages.
46cdf0e10cSrcweir 		const PageContainer_t::iterator aEnd(maPages.end());
47cdf0e10cSrcweir 		PageContainer_t::iterator       it(maPages.begin());
48cdf0e10cSrcweir 		while(it != aEnd)
49cdf0e10cSrcweir 		{
50cdf0e10cSrcweir 			FragmentSharedPtr pFragment((*it)->allocateSpace(rSize));
51cdf0e10cSrcweir 			if(pFragment)
52cdf0e10cSrcweir 			{
53cdf0e10cSrcweir 				// the page created a new fragment, since we maybe want
54cdf0e10cSrcweir 				// to consolidate sparse pages we keep a reference to
55cdf0e10cSrcweir 				// the fragment.
56cdf0e10cSrcweir 				maFragments.push_back(pFragment);
57cdf0e10cSrcweir 				return pFragment;
58cdf0e10cSrcweir 			}
59cdf0e10cSrcweir 
60cdf0e10cSrcweir 			++it;
61cdf0e10cSrcweir 		}
62cdf0e10cSrcweir 
63cdf0e10cSrcweir 		// otherwise try to create a new page and allocate space there...
64cdf0e10cSrcweir 		PageSharedPtr pPage(new Page(mpRenderModule));
65cdf0e10cSrcweir 		if(pPage->isValid())
66cdf0e10cSrcweir 		{
67cdf0e10cSrcweir 			maPages.push_back(pPage);
68cdf0e10cSrcweir 			FragmentSharedPtr pFragment(pPage->allocateSpace(rSize));
69cdf0e10cSrcweir             if (pFragment)
70cdf0e10cSrcweir                 maFragments.push_back(pFragment);
71cdf0e10cSrcweir 			return pFragment;
72cdf0e10cSrcweir 		}
73cdf0e10cSrcweir 
74cdf0e10cSrcweir 		// the rendermodule failed to create a new page [maybe out
75cdf0e10cSrcweir 		// of videomemory], and all other pages could not take
76cdf0e10cSrcweir 		// the new request. we decide to create a 'naked' fragment
77cdf0e10cSrcweir 		// which will receive its location later.
78cdf0e10cSrcweir 		FragmentSharedPtr pFragment(new PageFragment(rSize));
79cdf0e10cSrcweir 		maFragments.push_back(pFragment);
80cdf0e10cSrcweir 		return pFragment;
81cdf0e10cSrcweir 	}
82cdf0e10cSrcweir 
83cdf0e10cSrcweir 	//////////////////////////////////////////////////////////////////////////////////
84cdf0e10cSrcweir 	// PageManager::free
85cdf0e10cSrcweir 	//////////////////////////////////////////////////////////////////////////////////
86cdf0e10cSrcweir 
free(const FragmentSharedPtr & pFragment)87cdf0e10cSrcweir 	void PageManager::free( const FragmentSharedPtr& pFragment )
88cdf0e10cSrcweir 	{
89cdf0e10cSrcweir 		// erase the reference to the given fragment from our
90cdf0e10cSrcweir 		// internal container.
91cdf0e10cSrcweir 		FragmentContainer_t::iterator it(
92cdf0e10cSrcweir 			std::remove(
93cdf0e10cSrcweir 				maFragments.begin(),maFragments.end(),pFragment));
94cdf0e10cSrcweir 		maFragments.erase(it,maFragments.end());
95cdf0e10cSrcweir 
96cdf0e10cSrcweir 		// let the fragment itself know about it...
97cdf0e10cSrcweir 		// we need to pass 'this' as argument since the fragment
98cdf0e10cSrcweir 		// needs to pass this to the page and can't create
99cdf0e10cSrcweir 		// shared_ptr from itself...
100cdf0e10cSrcweir 		pFragment->free(pFragment);
101cdf0e10cSrcweir     }
102cdf0e10cSrcweir 
103cdf0e10cSrcweir 	//////////////////////////////////////////////////////////////////////////////////
104cdf0e10cSrcweir 	// PageManager::nakedFragment
105cdf0e10cSrcweir 	//////////////////////////////////////////////////////////////////////////////////
106cdf0e10cSrcweir 
nakedFragment(const FragmentSharedPtr & pFragment)107cdf0e10cSrcweir 	void PageManager::nakedFragment( const FragmentSharedPtr& pFragment )
108cdf0e10cSrcweir 	{
109cdf0e10cSrcweir 		if(maPages.empty())
110cdf0e10cSrcweir 			return;
111cdf0e10cSrcweir 
112cdf0e10cSrcweir 		// okay, one last chance is left, we try all available
113cdf0e10cSrcweir 		// pages again. maybe some other fragment was deleted
114cdf0e10cSrcweir 		// and we can exploit the space.
115cdf0e10cSrcweir 		while(!(relocate(pFragment)))
116cdf0e10cSrcweir 		{
117cdf0e10cSrcweir 			// no way, we need to free up some space...
118cdf0e10cSrcweir 			// TODO(F1): this is a heuristic, could
119cdf0e10cSrcweir 			// be designed as a policy.
120cdf0e10cSrcweir 			const FragmentContainer_t::const_iterator aEnd(maFragments.end());
121cdf0e10cSrcweir 			FragmentContainer_t::const_iterator       candidate(maFragments.begin());
122cdf0e10cSrcweir 			while(candidate != aEnd)
123cdf0e10cSrcweir 			{
124cdf0e10cSrcweir 				if(*candidate && !((*candidate)->isNaked()))
125cdf0e10cSrcweir 					break;
126cdf0e10cSrcweir 				++candidate;
127cdf0e10cSrcweir 			}
128cdf0e10cSrcweir 
129cdf0e10cSrcweir             if (candidate != aEnd)
130cdf0e10cSrcweir             {
131cdf0e10cSrcweir                 const ::basegfx::B2ISize& rSize((*candidate)->getSize());
132cdf0e10cSrcweir                 sal_uInt32                nMaxArea(rSize.getX()*rSize.getY());
133cdf0e10cSrcweir 
134cdf0e10cSrcweir                 FragmentContainer_t::const_iterator it(candidate);
135cdf0e10cSrcweir                 while(it != aEnd)
136cdf0e10cSrcweir                 {
137cdf0e10cSrcweir                     if (*it && !((*it)->isNaked()))
138cdf0e10cSrcweir                     {
139cdf0e10cSrcweir                         const ::basegfx::B2ISize& rCandidateSize((*it)->getSize());
140cdf0e10cSrcweir                         const sal_uInt32 nArea(rCandidateSize.getX()*rCandidateSize.getY());
141cdf0e10cSrcweir                         if(nArea > nMaxArea)
142cdf0e10cSrcweir                         {
143cdf0e10cSrcweir                             candidate=it;
144cdf0e10cSrcweir                             nMaxArea=nArea;
145cdf0e10cSrcweir                         }
146cdf0e10cSrcweir                     }
147cdf0e10cSrcweir 
148cdf0e10cSrcweir                     ++it;
149cdf0e10cSrcweir                 }
150cdf0e10cSrcweir 
151cdf0e10cSrcweir                 // this does not erase the candidate,
152cdf0e10cSrcweir                 // but makes it 'naked'...
153cdf0e10cSrcweir                 (*candidate)->free(*candidate);
154cdf0e10cSrcweir             }
155cdf0e10cSrcweir             else
156cdf0e10cSrcweir                 break;
157cdf0e10cSrcweir 		}
158cdf0e10cSrcweir 	}
159cdf0e10cSrcweir 
160cdf0e10cSrcweir 	//////////////////////////////////////////////////////////////////////////////////
161cdf0e10cSrcweir 	// PageManager::relocate
162cdf0e10cSrcweir 	//////////////////////////////////////////////////////////////////////////////////
163cdf0e10cSrcweir 
relocate(const FragmentSharedPtr & pFragment)164cdf0e10cSrcweir 	bool PageManager::relocate( const FragmentSharedPtr& pFragment )
165cdf0e10cSrcweir 	{
166cdf0e10cSrcweir 		// the fragment passed as argument is assumed to
167cdf0e10cSrcweir 		// be naked, that is it is not located on any page.
168cdf0e10cSrcweir 		// we try all available pages again, maybe some
169cdf0e10cSrcweir 		// other fragment was deleted and we can exploit the space.
170cdf0e10cSrcweir 		const PageContainer_t::iterator aEnd(maPages.end());
171cdf0e10cSrcweir 		PageContainer_t::iterator       it(maPages.begin());
172cdf0e10cSrcweir 		while(it != aEnd)
173cdf0e10cSrcweir 		{
174cdf0e10cSrcweir 			// if the page at hand takes the fragment, we immediatelly
175cdf0e10cSrcweir 			// call select() to pull the information from the associated
176cdf0e10cSrcweir 			// image to the hardware surface.
177cdf0e10cSrcweir 			if((*it)->nakedFragment(pFragment))
178cdf0e10cSrcweir 			{
179cdf0e10cSrcweir 				// dirty, since newly allocated.
180cdf0e10cSrcweir 				pFragment->select(true);
181cdf0e10cSrcweir 				return true;
182cdf0e10cSrcweir 			}
183cdf0e10cSrcweir 
184cdf0e10cSrcweir 			++it;
185cdf0e10cSrcweir 		}
186cdf0e10cSrcweir 
187cdf0e10cSrcweir 		return false;
188cdf0e10cSrcweir 	}
189cdf0e10cSrcweir 
190cdf0e10cSrcweir 	//////////////////////////////////////////////////////////////////////////////////
191cdf0e10cSrcweir 	// PageManager::validatePages
192cdf0e10cSrcweir 	//////////////////////////////////////////////////////////////////////////////////
193cdf0e10cSrcweir 
validatePages()194cdf0e10cSrcweir 	void PageManager::validatePages()
195cdf0e10cSrcweir 	{
196cdf0e10cSrcweir 		::std::for_each( maPages.begin(),
197cdf0e10cSrcweir                          maPages.end(),
198cdf0e10cSrcweir 						 ::boost::mem_fn(&Page::validate));
199cdf0e10cSrcweir 	}
200cdf0e10cSrcweir 
201cdf0e10cSrcweir 	//////////////////////////////////////////////////////////////////////////////////
202cdf0e10cSrcweir 	// PageManager::getPageSize
203cdf0e10cSrcweir 	//////////////////////////////////////////////////////////////////////////////////
204cdf0e10cSrcweir 
getPageSize() const205cdf0e10cSrcweir 	::basegfx::B2ISize PageManager::getPageSize() const
206cdf0e10cSrcweir 	{
207cdf0e10cSrcweir 		return mpRenderModule->getPageSize();
208cdf0e10cSrcweir 	}
209cdf0e10cSrcweir 
210cdf0e10cSrcweir 	//////////////////////////////////////////////////////////////////////////////////
211cdf0e10cSrcweir 	// PageManager::getRenderModule
212cdf0e10cSrcweir 	//////////////////////////////////////////////////////////////////////////////////
213cdf0e10cSrcweir 
getRenderModule() const214cdf0e10cSrcweir 	canvas::IRenderModuleSharedPtr PageManager::getRenderModule() const
215cdf0e10cSrcweir 	{
216cdf0e10cSrcweir 		return mpRenderModule;
217cdf0e10cSrcweir 	}
218cdf0e10cSrcweir }
219