Branch libreoffice-5-0-4
[LibreOffice.git] / canvas / source / tools / pagemanager.cxx
blob7f5d7769cc2ce824b1879558e7ba342107a64cde
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
21 #include <boost/mem_fn.hpp>
22 #include "pagemanager.hxx"
24 namespace canvas
28 // PageManager
32 // PageManager::allocateSpace
35 FragmentSharedPtr PageManager::allocateSpace( const ::basegfx::B2ISize& rSize )
37 // we are asked to find a location for the requested size.
38 // first we try to satisfy the request from the
39 // remaining space in the existing pages.
40 const PageContainer_t::iterator aEnd(maPages.end());
41 PageContainer_t::iterator it(maPages.begin());
42 while(it != aEnd)
44 FragmentSharedPtr pFragment((*it)->allocateSpace(rSize));
45 if(pFragment)
47 // the page created a new fragment, since we maybe want
48 // to consolidate sparse pages we keep a reference to
49 // the fragment.
50 maFragments.push_back(pFragment);
51 return pFragment;
54 ++it;
57 // otherwise try to create a new page and allocate space there...
58 PageSharedPtr pPage(new Page(mpRenderModule));
59 if(pPage->isValid())
61 maPages.push_back(pPage);
62 FragmentSharedPtr pFragment(pPage->allocateSpace(rSize));
63 if (pFragment)
64 maFragments.push_back(pFragment);
65 return pFragment;
68 // the rendermodule failed to create a new page [maybe out
69 // of videomemory], and all other pages could not take
70 // the new request. we decide to create a 'naked' fragment
71 // which will receive its location later.
72 FragmentSharedPtr pFragment(new PageFragment(rSize));
73 maFragments.push_back(pFragment);
74 return pFragment;
78 // PageManager::free
81 void PageManager::free( const FragmentSharedPtr& pFragment )
83 // erase the reference to the given fragment from our
84 // internal container.
85 FragmentContainer_t::iterator it(
86 std::remove(
87 maFragments.begin(),maFragments.end(),pFragment));
88 maFragments.erase(it,maFragments.end());
90 // let the fragment itself know about it...
91 // we need to pass 'this' as argument since the fragment
92 // needs to pass this to the page and can't create
93 // shared_ptr from itself...
94 pFragment->free(pFragment);
98 // PageManager::nakedFragment
101 void PageManager::nakedFragment( const FragmentSharedPtr& pFragment )
103 if(maPages.empty())
104 return;
106 // okay, one last chance is left, we try all available
107 // pages again. maybe some other fragment was deleted
108 // and we can exploit the space.
109 while(!(relocate(pFragment)))
111 // no way, we need to free up some space...
112 // TODO(F1): this is a heuristic, could
113 // be designed as a policy.
114 const FragmentContainer_t::const_iterator aEnd(maFragments.end());
115 FragmentContainer_t::const_iterator candidate(maFragments.begin());
116 while(candidate != aEnd)
118 if(*candidate && !((*candidate)->isNaked()))
119 break;
120 ++candidate;
123 if (candidate != aEnd)
125 const ::basegfx::B2ISize& rSize((*candidate)->getSize());
126 sal_uInt32 nMaxArea(rSize.getX()*rSize.getY());
128 FragmentContainer_t::const_iterator it(candidate);
129 while(it != aEnd)
131 if (*it && !((*it)->isNaked()))
133 const ::basegfx::B2ISize& rCandidateSize((*it)->getSize());
134 const sal_uInt32 nArea(rCandidateSize.getX()*rCandidateSize.getY());
135 if(nArea > nMaxArea)
137 candidate=it;
138 nMaxArea=nArea;
142 ++it;
145 // this does not erase the candidate,
146 // but makes it 'naked'...
147 (*candidate)->free(*candidate);
149 else
150 break;
155 // PageManager::relocate
158 bool PageManager::relocate( const FragmentSharedPtr& pFragment )
160 // the fragment passed as argument is assumed to
161 // be naked, that is it is not located on any page.
162 // we try all available pages again, maybe some
163 // other fragment was deleted and we can exploit the space.
164 const PageContainer_t::iterator aEnd(maPages.end());
165 PageContainer_t::iterator it(maPages.begin());
166 while(it != aEnd)
168 // if the page at hand takes the fragment, we immediately
169 // call select() to pull the information from the associated
170 // image to the hardware surface.
171 if((*it)->nakedFragment(pFragment))
173 // dirty, since newly allocated.
174 pFragment->select(true);
175 return true;
178 ++it;
181 return false;
185 // PageManager::validatePages
188 void PageManager::validatePages()
190 ::std::for_each( maPages.begin(),
191 maPages.end(),
192 ::boost::mem_fn(&Page::validate));
196 // PageManager::getPageSize
199 ::basegfx::B2ISize PageManager::getPageSize() const
201 return mpRenderModule->getPageSize();
205 // PageManager::getRenderModule
210 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */