LCOV - code coverage report
Current view: top level - sd/source/ui/slidesorter/cache - SlsCacheCompactor.cxx (source / functions) Hit Total Coverage
Test: commit 0e63ca4fde4e446f346e35849c756a30ca294aab Lines: 30 66 45.5 %
Date: 2014-04-11 Functions: 5 14 35.7 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2             : /*
       3             :  * This file is part of the LibreOffice project.
       4             :  *
       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/.
       8             :  *
       9             :  * This file incorporates work covered by the following license notice:
      10             :  *
      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 .
      18             :  */
      19             : 
      20             : 
      21             : #include "SlsCacheCompactor.hxx"
      22             : 
      23             : #include "SlsBitmapCompressor.hxx"
      24             : #include "SlsBitmapCache.hxx"
      25             : #include "SlsCacheConfiguration.hxx"
      26             : 
      27             : #include <rtl/ustring.hxx>
      28             : #include <com/sun/star/uno/Any.hxx>
      29             : #include <set>
      30             : 
      31             : using namespace ::com::sun::star::uno;
      32             : 
      33             : namespace {
      34             : 
      35             : /** This is a trivial implementation of the CacheCompactor interface class.
      36             :     It ignores calls to RequestCompaction() and thus will never decrease the
      37             :     total size of off-screen preview bitmaps.
      38             : */
      39           0 : class NoCacheCompaction
      40             :     : public ::sd::slidesorter::cache::CacheCompactor
      41             : {
      42             : public:
      43           0 :     NoCacheCompaction (
      44             :         ::sd::slidesorter::cache::BitmapCache& rCache,
      45             :         sal_Int32 nMaximalCacheSize)
      46           0 :         : CacheCompactor(rCache, nMaximalCacheSize)
      47           0 :     {}
      48             : 
      49           0 :     virtual void RequestCompaction (void) SAL_OVERRIDE { /* Ignored */ };
      50             : 
      51             : protected:
      52           0 :     virtual void Run (void) SAL_OVERRIDE { /* Do nothing */ };
      53             : };
      54             : 
      55             : 
      56             : 
      57             : 
      58             : /** This implementation of the CacheCompactor interface class uses one of
      59             :     several bitmap compression algorithms to reduce the number of the bytes
      60             :     of the off-screen previews in the bitmap cache.  See the documentation
      61             :     of CacheCompactor::Create() for more details on configuration properties
      62             :     that control the choice of compression algorithm.
      63             : */
      64         126 : class CacheCompactionByCompression
      65             :     : public ::sd::slidesorter::cache::CacheCompactor
      66             : {
      67             : public:
      68             :     CacheCompactionByCompression (
      69             :         ::sd::slidesorter::cache::BitmapCache& rCache,
      70             :         sal_Int32 nMaximalCacheSize,
      71             :         const ::boost::shared_ptr< ::sd::slidesorter::cache::BitmapCompressor>& rpCompressor);
      72             : 
      73             : protected:
      74             :     virtual void Run (void) SAL_OVERRIDE;
      75             : 
      76             : private:
      77             :     ::boost::shared_ptr< ::sd::slidesorter::cache::BitmapCompressor>  mpCompressor;
      78             : };
      79             : 
      80             : 
      81             : } // end of anonymous namespace
      82             : 
      83             : namespace sd { namespace slidesorter { namespace cache {
      84             : 
      85             : SAL_WNODEPRECATED_DECLARATIONS_PUSH
      86          63 : ::std::auto_ptr<CacheCompactor> CacheCompactor::Create (
      87             :     BitmapCache& rCache,
      88             :     sal_Int32 nMaximalCacheSize)
      89             : {
      90          63 :     static const OUString sNone ("None");
      91          63 :     static const OUString sCompress ("Compress");
      92          63 :     static const OUString sErase ("Erase");
      93          63 :     static const OUString sResolution ("ResolutionReduction");
      94          63 :     static const OUString sPNGCompression ("PNGCompression");
      95             : 
      96          63 :     ::boost::shared_ptr<BitmapCompressor> pCompressor;
      97         126 :     OUString sCompressionPolicy(sPNGCompression);
      98         126 :     Any aCompressionPolicy (CacheConfiguration::Instance()->GetValue("CompressionPolicy"));
      99          63 :     if (aCompressionPolicy.has<OUString>())
     100           0 :         aCompressionPolicy >>= sCompressionPolicy;
     101          63 :     if (sCompressionPolicy == sNone)
     102           0 :         pCompressor.reset(new NoBitmapCompression());
     103          63 :     else if (sCompressionPolicy == sErase)
     104           0 :         pCompressor.reset(new CompressionByDeletion());
     105          63 :     else if (sCompressionPolicy == sResolution)
     106           0 :         pCompressor.reset(new ResolutionReduction());
     107             :     else
     108          63 :         pCompressor.reset(new PngCompression());
     109             : 
     110          63 :     ::std::auto_ptr<CacheCompactor> pCompactor (NULL);
     111         126 :     OUString sCompactionPolicy(sCompress);
     112         126 :     Any aCompactionPolicy (CacheConfiguration::Instance()->GetValue("CompactionPolicy"));
     113          63 :     if (aCompactionPolicy.has<OUString>())
     114           0 :         aCompactionPolicy >>= sCompactionPolicy;
     115          63 :     if (sCompactionPolicy == sNone)
     116           0 :         pCompactor.reset(new NoCacheCompaction(rCache,nMaximalCacheSize));
     117             :     else
     118          63 :         pCompactor.reset(new CacheCompactionByCompression(rCache,nMaximalCacheSize,pCompressor));
     119             : 
     120         126 :     return pCompactor;
     121             : }
     122             : SAL_WNODEPRECATED_DECLARATIONS_POP
     123             : 
     124             : 
     125             : 
     126             : 
     127           0 : void CacheCompactor::RequestCompaction (void)
     128             : {
     129           0 :     if ( ! mbIsCompactionRunning && ! maCompactionTimer.IsActive())
     130           0 :         maCompactionTimer.Start();
     131           0 : }
     132             : 
     133             : 
     134             : 
     135             : 
     136          63 : CacheCompactor::CacheCompactor(
     137             :     BitmapCache& rCache,
     138             :     sal_Int32 nMaximalCacheSize)
     139             :     : mrCache(rCache),
     140             :       mnMaximalCacheSize(nMaximalCacheSize),
     141          63 :       mbIsCompactionRunning(false)
     142             : {
     143          63 :     maCompactionTimer.SetTimeout(100 /*ms*/);
     144          63 :     maCompactionTimer.SetTimeoutHdl(LINK(this,CacheCompactor,CompactionCallback));
     145             : 
     146          63 : }
     147             : 
     148             : 
     149             : 
     150             : 
     151           0 : IMPL_LINK_NOARG(CacheCompactor, CompactionCallback)
     152             : {
     153           0 :     mbIsCompactionRunning = true;
     154             : 
     155             :     try
     156             :     {
     157           0 :         Run();
     158             :     }
     159           0 :     catch (const ::com::sun::star::uno::RuntimeException&)
     160             :     {
     161             :     }
     162           0 :     catch (const ::com::sun::star::uno::Exception&)
     163             :     {
     164             :     }
     165             : 
     166           0 :     mbIsCompactionRunning = false;
     167           0 :     return 1;
     168             : }
     169             : 
     170             : 
     171             : 
     172             : 
     173             : } } } // end of namespace ::sd::slidesorter::cache
     174             : 
     175             : 
     176             : 
     177             : 
     178             : namespace {
     179             : 
     180             : //===== CacheCompactionByCompression ==========================================
     181             : 
     182          63 : CacheCompactionByCompression::CacheCompactionByCompression (
     183             :     ::sd::slidesorter::cache::BitmapCache& rCache,
     184             :     sal_Int32 nMaximalCacheSize,
     185             :     const ::boost::shared_ptr< ::sd::slidesorter::cache::BitmapCompressor>& rpCompressor)
     186             :     : CacheCompactor(rCache,nMaximalCacheSize),
     187          63 :       mpCompressor(rpCompressor)
     188             : {
     189          63 : }
     190             : 
     191             : 
     192             : 
     193             : 
     194           0 : void CacheCompactionByCompression::Run (void)
     195             : {
     196           0 :     if (mrCache.GetSize() > mnMaximalCacheSize)
     197             :     {
     198             :         SAL_INFO("sd.sls", OSL_THIS_FUNC << ": bitmap cache uses to much space: " << mrCache.GetSize() << " > " << mnMaximalCacheSize);
     199             : 
     200             :         SAL_WNODEPRECATED_DECLARATIONS_PUSH
     201             :         ::std::auto_ptr< ::sd::slidesorter::cache::BitmapCache::CacheIndex> pIndex (
     202           0 :             mrCache.GetCacheIndex(false,false));
     203             :         SAL_WNODEPRECATED_DECLARATIONS_POP
     204           0 :         ::sd::slidesorter::cache::BitmapCache::CacheIndex::iterator iIndex;
     205           0 :         for (iIndex=pIndex->begin(); iIndex!=pIndex->end(); ++iIndex)
     206             :         {
     207           0 :             if (*iIndex == NULL)
     208           0 :                 continue;
     209             : 
     210           0 :             mrCache.Compress(*iIndex, mpCompressor);
     211           0 :             if (mrCache.GetSize() < mnMaximalCacheSize)
     212           0 :                 break;
     213             :         }
     214           0 :         mrCache.ReCalculateTotalCacheSize();
     215           0 :         SAL_INFO("sd.sls", OSL_THIS_FUNC << ":    there are now " << mrCache.GetSize() << " bytes occupied");
     216             :     }
     217           0 : }
     218             : 
     219             : 
     220             : } // end of anonymous namespace
     221             : 
     222             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10