bump product version to 5.0.4.1
[LibreOffice.git] / sd / source / ui / slidesorter / cache / SlsCacheCompactor.cxx
blobea0dc48d9264f6668cdb80bb7ce1b277a137fce2
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 .
20 #include "SlsCacheCompactor.hxx"
22 #include "SlsBitmapCompressor.hxx"
23 #include "SlsBitmapCache.hxx"
24 #include "SlsCacheConfiguration.hxx"
26 #include <rtl/ustring.hxx>
27 #include <com/sun/star/uno/Any.hxx>
28 #include <set>
30 using namespace ::com::sun::star::uno;
32 namespace {
34 /** This is a trivial implementation of the CacheCompactor interface class.
35 It ignores calls to RequestCompaction() and thus will never decrease the
36 total size of off-screen preview bitmaps.
38 class NoCacheCompaction
39 : public ::sd::slidesorter::cache::CacheCompactor
41 public:
42 NoCacheCompaction (
43 ::sd::slidesorter::cache::BitmapCache& rCache,
44 sal_Int32 nMaximalCacheSize)
45 : CacheCompactor(rCache, nMaximalCacheSize)
48 virtual void RequestCompaction() SAL_OVERRIDE { /* Ignored */ };
50 protected:
51 virtual void Run() SAL_OVERRIDE { /* Do nothing */ };
54 /** This implementation of the CacheCompactor interface class uses one of
55 several bitmap compression algorithms to reduce the number of the bytes
56 of the off-screen previews in the bitmap cache. See the documentation
57 of CacheCompactor::Create() for more details on configuration properties
58 that control the choice of compression algorithm.
60 class CacheCompactionByCompression
61 : public ::sd::slidesorter::cache::CacheCompactor
63 public:
64 CacheCompactionByCompression (
65 ::sd::slidesorter::cache::BitmapCache& rCache,
66 sal_Int32 nMaximalCacheSize,
67 const ::boost::shared_ptr< ::sd::slidesorter::cache::BitmapCompressor>& rpCompressor);
69 protected:
70 virtual void Run() SAL_OVERRIDE;
72 private:
73 ::boost::shared_ptr< ::sd::slidesorter::cache::BitmapCompressor> mpCompressor;
76 } // end of anonymous namespace
78 namespace sd { namespace slidesorter { namespace cache {
80 ::std::unique_ptr<CacheCompactor> CacheCompactor::Create (
81 BitmapCache& rCache,
82 sal_Int32 nMaximalCacheSize)
84 static const char sNone[] = "None";
85 static const char sCompress[] = "Compress";
86 static const char sErase[] = "Erase";
87 static const char sResolution[] = "ResolutionReduction";
88 static const char sPNGCompression[] = "PNGCompression";
90 ::boost::shared_ptr<BitmapCompressor> pCompressor;
91 OUString sCompressionPolicy(sPNGCompression);
92 Any aCompressionPolicy (CacheConfiguration::Instance()->GetValue("CompressionPolicy"));
93 if (aCompressionPolicy.has<OUString>())
94 aCompressionPolicy >>= sCompressionPolicy;
95 if (sCompressionPolicy == sNone)
96 pCompressor.reset(new NoBitmapCompression());
97 else if (sCompressionPolicy == sErase)
98 pCompressor.reset(new CompressionByDeletion());
99 else if (sCompressionPolicy == sResolution)
100 pCompressor.reset(new ResolutionReduction());
101 else
102 pCompressor.reset(new PngCompression());
104 ::std::unique_ptr<CacheCompactor> pCompactor;
105 OUString sCompactionPolicy(sCompress);
106 Any aCompactionPolicy (CacheConfiguration::Instance()->GetValue("CompactionPolicy"));
107 if (aCompactionPolicy.has<OUString>())
108 aCompactionPolicy >>= sCompactionPolicy;
109 if (sCompactionPolicy == sNone)
110 pCompactor.reset(new NoCacheCompaction(rCache,nMaximalCacheSize));
111 else
112 pCompactor.reset(new CacheCompactionByCompression(rCache,nMaximalCacheSize,pCompressor));
114 return pCompactor;
117 void CacheCompactor::RequestCompaction()
119 if ( ! mbIsCompactionRunning && ! maCompactionTimer.IsActive())
120 maCompactionTimer.Start();
123 CacheCompactor::CacheCompactor(
124 BitmapCache& rCache,
125 sal_Int32 nMaximalCacheSize)
126 : mrCache(rCache),
127 mnMaximalCacheSize(nMaximalCacheSize),
128 mbIsCompactionRunning(false)
130 maCompactionTimer.SetTimeout(100);
131 maCompactionTimer.SetTimeoutHdl(LINK(this,CacheCompactor,CompactionCallback));
135 IMPL_LINK_NOARG_TYPED(CacheCompactor, CompactionCallback, Timer *, void)
137 mbIsCompactionRunning = true;
141 Run();
143 catch (const ::com::sun::star::uno::RuntimeException&)
146 catch (const ::com::sun::star::uno::Exception&)
150 mbIsCompactionRunning = false;
153 } } } // end of namespace ::sd::slidesorter::cache
155 namespace {
157 //===== CacheCompactionByCompression ==========================================
159 CacheCompactionByCompression::CacheCompactionByCompression (
160 ::sd::slidesorter::cache::BitmapCache& rCache,
161 sal_Int32 nMaximalCacheSize,
162 const ::boost::shared_ptr< ::sd::slidesorter::cache::BitmapCompressor>& rpCompressor)
163 : CacheCompactor(rCache,nMaximalCacheSize),
164 mpCompressor(rpCompressor)
168 void CacheCompactionByCompression::Run()
170 if (mrCache.GetSize() > mnMaximalCacheSize)
172 SAL_INFO("sd.sls", OSL_THIS_FUNC << ": bitmap cache uses to much space: " << mrCache.GetSize() << " > " << mnMaximalCacheSize);
174 ::std::unique_ptr< ::sd::slidesorter::cache::BitmapCache::CacheIndex> pIndex (
175 mrCache.GetCacheIndex(false,false));
176 ::sd::slidesorter::cache::BitmapCache::CacheIndex::iterator iIndex;
177 for (iIndex=pIndex->begin(); iIndex!=pIndex->end(); ++iIndex)
179 if (*iIndex == NULL)
180 continue;
182 mrCache.Compress(*iIndex, mpCompressor);
183 if (mrCache.GetSize() < mnMaximalCacheSize)
184 break;
186 mrCache.ReCalculateTotalCacheSize();
187 SAL_INFO("sd.sls", OSL_THIS_FUNC << ": there are now " << mrCache.GetSize() << " bytes occupied");
191 } // end of anonymous namespace
193 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */