1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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"
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());
44 FragmentSharedPtr
pFragment((*it
)->allocateSpace(rSize
));
47 // the page created a new fragment, since we maybe want
48 // to consolidate sparse pages we keep a reference to
50 maFragments
.push_back(pFragment
);
57 // otherwise try to create a new page and allocate space there...
58 PageSharedPtr
pPage(new Page(mpRenderModule
));
61 maPages
.push_back(pPage
);
62 FragmentSharedPtr
pFragment(pPage
->allocateSpace(rSize
));
64 maFragments
.push_back(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
);
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(
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
)
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()))
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
);
131 if (*it
&& !((*it
)->isNaked()))
133 const ::basegfx::B2ISize
& rCandidateSize((*it
)->getSize());
134 const sal_uInt32
nArea(rCandidateSize
.getX()*rCandidateSize
.getY());
145 // this does not erase the candidate,
146 // but makes it 'naked'...
147 (*candidate
)->free(*candidate
);
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());
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);
185 // PageManager::validatePages
188 void PageManager::validatePages()
190 ::std::for_each( maPages
.begin(),
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: */