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