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 : #include "SlsCacheCompactor.hxx"
21 :
22 : #include "SlsBitmapCompressor.hxx"
23 : #include "SlsBitmapCache.hxx"
24 : #include "SlsCacheConfiguration.hxx"
25 :
26 : #include <rtl/ustring.hxx>
27 : #include <com/sun/star/uno/Any.hxx>
28 : #include <set>
29 :
30 : using namespace ::com::sun::star::uno;
31 :
32 : namespace {
33 :
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.
37 : */
38 0 : class NoCacheCompaction
39 : : public ::sd::slidesorter::cache::CacheCompactor
40 : {
41 : public:
42 0 : NoCacheCompaction (
43 : ::sd::slidesorter::cache::BitmapCache& rCache,
44 : sal_Int32 nMaximalCacheSize)
45 0 : : CacheCompactor(rCache, nMaximalCacheSize)
46 0 : {}
47 :
48 0 : virtual void RequestCompaction() SAL_OVERRIDE { /* Ignored */ };
49 :
50 : protected:
51 0 : virtual void Run() SAL_OVERRIDE { /* Do nothing */ };
52 : };
53 :
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.
59 : */
60 128 : class CacheCompactionByCompression
61 : : public ::sd::slidesorter::cache::CacheCompactor
62 : {
63 : public:
64 : CacheCompactionByCompression (
65 : ::sd::slidesorter::cache::BitmapCache& rCache,
66 : sal_Int32 nMaximalCacheSize,
67 : const ::boost::shared_ptr< ::sd::slidesorter::cache::BitmapCompressor>& rpCompressor);
68 :
69 : protected:
70 : virtual void Run() SAL_OVERRIDE;
71 :
72 : private:
73 : ::boost::shared_ptr< ::sd::slidesorter::cache::BitmapCompressor> mpCompressor;
74 : };
75 :
76 : } // end of anonymous namespace
77 :
78 : namespace sd { namespace slidesorter { namespace cache {
79 :
80 64 : ::std::unique_ptr<CacheCompactor> CacheCompactor::Create (
81 : BitmapCache& rCache,
82 : sal_Int32 nMaximalCacheSize)
83 : {
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";
89 :
90 64 : ::boost::shared_ptr<BitmapCompressor> pCompressor;
91 128 : OUString sCompressionPolicy(sPNGCompression);
92 128 : Any aCompressionPolicy (CacheConfiguration::Instance()->GetValue("CompressionPolicy"));
93 64 : if (aCompressionPolicy.has<OUString>())
94 0 : aCompressionPolicy >>= sCompressionPolicy;
95 64 : if (sCompressionPolicy == sNone)
96 0 : pCompressor.reset(new NoBitmapCompression());
97 64 : else if (sCompressionPolicy == sErase)
98 0 : pCompressor.reset(new CompressionByDeletion());
99 64 : else if (sCompressionPolicy == sResolution)
100 0 : pCompressor.reset(new ResolutionReduction());
101 : else
102 64 : pCompressor.reset(new PngCompression());
103 :
104 64 : ::std::unique_ptr<CacheCompactor> pCompactor;
105 128 : OUString sCompactionPolicy(sCompress);
106 128 : Any aCompactionPolicy (CacheConfiguration::Instance()->GetValue("CompactionPolicy"));
107 64 : if (aCompactionPolicy.has<OUString>())
108 0 : aCompactionPolicy >>= sCompactionPolicy;
109 64 : if (sCompactionPolicy == sNone)
110 0 : pCompactor.reset(new NoCacheCompaction(rCache,nMaximalCacheSize));
111 : else
112 64 : pCompactor.reset(new CacheCompactionByCompression(rCache,nMaximalCacheSize,pCompressor));
113 :
114 128 : return pCompactor;
115 : }
116 :
117 0 : void CacheCompactor::RequestCompaction()
118 : {
119 0 : if ( ! mbIsCompactionRunning && ! maCompactionTimer.IsActive())
120 0 : maCompactionTimer.Start();
121 0 : }
122 :
123 64 : CacheCompactor::CacheCompactor(
124 : BitmapCache& rCache,
125 : sal_Int32 nMaximalCacheSize)
126 : : mrCache(rCache),
127 : mnMaximalCacheSize(nMaximalCacheSize),
128 64 : mbIsCompactionRunning(false)
129 : {
130 64 : maCompactionTimer.SetTimeout(100);
131 64 : maCompactionTimer.SetTimeoutHdl(LINK(this,CacheCompactor,CompactionCallback));
132 :
133 64 : }
134 :
135 0 : IMPL_LINK_NOARG_TYPED(CacheCompactor, CompactionCallback, Timer *, void)
136 : {
137 0 : mbIsCompactionRunning = true;
138 :
139 : try
140 : {
141 0 : Run();
142 : }
143 0 : catch (const ::com::sun::star::uno::RuntimeException&)
144 : {
145 : }
146 0 : catch (const ::com::sun::star::uno::Exception&)
147 : {
148 : }
149 :
150 0 : mbIsCompactionRunning = false;
151 0 : }
152 :
153 : } } } // end of namespace ::sd::slidesorter::cache
154 :
155 : namespace {
156 :
157 : //===== CacheCompactionByCompression ==========================================
158 :
159 64 : 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 64 : mpCompressor(rpCompressor)
165 : {
166 64 : }
167 :
168 0 : void CacheCompactionByCompression::Run()
169 : {
170 0 : if (mrCache.GetSize() > mnMaximalCacheSize)
171 : {
172 : SAL_INFO("sd.sls", OSL_THIS_FUNC << ": bitmap cache uses to much space: " << mrCache.GetSize() << " > " << mnMaximalCacheSize);
173 :
174 : ::std::unique_ptr< ::sd::slidesorter::cache::BitmapCache::CacheIndex> pIndex (
175 0 : mrCache.GetCacheIndex(false,false));
176 0 : ::sd::slidesorter::cache::BitmapCache::CacheIndex::iterator iIndex;
177 0 : for (iIndex=pIndex->begin(); iIndex!=pIndex->end(); ++iIndex)
178 : {
179 0 : if (*iIndex == NULL)
180 0 : continue;
181 :
182 0 : mrCache.Compress(*iIndex, mpCompressor);
183 0 : if (mrCache.GetSize() < mnMaximalCacheSize)
184 0 : break;
185 : }
186 0 : mrCache.ReCalculateTotalCacheSize();
187 0 : SAL_INFO("sd.sls", OSL_THIS_FUNC << ": there are now " << mrCache.GetSize() << " bytes occupied");
188 : }
189 0 : }
190 :
191 : } // end of anonymous namespace
192 :
193 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|