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 (void) SAL_OVERRIDE { /* Ignored */ };
49 :
50 : protected:
51 0 : virtual void Run (void) 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 224 : 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 (void) 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 112 : ::std::unique_ptr<CacheCompactor> CacheCompactor::Create (
81 : BitmapCache& rCache,
82 : sal_Int32 nMaximalCacheSize)
83 : {
84 112 : static const OUString sNone ("None");
85 112 : static const OUString sCompress ("Compress");
86 112 : static const OUString sErase ("Erase");
87 112 : static const OUString sResolution ("ResolutionReduction");
88 112 : static const OUString sPNGCompression ("PNGCompression");
89 :
90 112 : ::boost::shared_ptr<BitmapCompressor> pCompressor;
91 224 : OUString sCompressionPolicy(sPNGCompression);
92 224 : Any aCompressionPolicy (CacheConfiguration::Instance()->GetValue("CompressionPolicy"));
93 112 : if (aCompressionPolicy.has<OUString>())
94 0 : aCompressionPolicy >>= sCompressionPolicy;
95 112 : if (sCompressionPolicy == sNone)
96 0 : pCompressor.reset(new NoBitmapCompression());
97 112 : else if (sCompressionPolicy == sErase)
98 0 : pCompressor.reset(new CompressionByDeletion());
99 112 : else if (sCompressionPolicy == sResolution)
100 0 : pCompressor.reset(new ResolutionReduction());
101 : else
102 112 : pCompressor.reset(new PngCompression());
103 :
104 112 : ::std::unique_ptr<CacheCompactor> pCompactor;
105 224 : OUString sCompactionPolicy(sCompress);
106 224 : Any aCompactionPolicy (CacheConfiguration::Instance()->GetValue("CompactionPolicy"));
107 112 : if (aCompactionPolicy.has<OUString>())
108 0 : aCompactionPolicy >>= sCompactionPolicy;
109 112 : if (sCompactionPolicy == sNone)
110 0 : pCompactor.reset(new NoCacheCompaction(rCache,nMaximalCacheSize));
111 : else
112 112 : pCompactor.reset(new CacheCompactionByCompression(rCache,nMaximalCacheSize,pCompressor));
113 :
114 224 : return pCompactor;
115 : }
116 :
117 0 : void CacheCompactor::RequestCompaction (void)
118 : {
119 0 : if ( ! mbIsCompactionRunning && ! maCompactionTimer.IsActive())
120 0 : maCompactionTimer.Start();
121 0 : }
122 :
123 112 : CacheCompactor::CacheCompactor(
124 : BitmapCache& rCache,
125 : sal_Int32 nMaximalCacheSize)
126 : : mrCache(rCache),
127 : mnMaximalCacheSize(nMaximalCacheSize),
128 112 : mbIsCompactionRunning(false)
129 : {
130 112 : maCompactionTimer.SetTimeout(100 /*ms*/);
131 112 : maCompactionTimer.SetTimeoutHdl(LINK(this,CacheCompactor,CompactionCallback));
132 :
133 112 : }
134 :
135 0 : IMPL_LINK_NOARG(CacheCompactor, CompactionCallback)
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 : return 1;
152 : }
153 :
154 : } } } // end of namespace ::sd::slidesorter::cache
155 :
156 : namespace {
157 :
158 : //===== CacheCompactionByCompression ==========================================
159 :
160 112 : CacheCompactionByCompression::CacheCompactionByCompression (
161 : ::sd::slidesorter::cache::BitmapCache& rCache,
162 : sal_Int32 nMaximalCacheSize,
163 : const ::boost::shared_ptr< ::sd::slidesorter::cache::BitmapCompressor>& rpCompressor)
164 : : CacheCompactor(rCache,nMaximalCacheSize),
165 112 : mpCompressor(rpCompressor)
166 : {
167 112 : }
168 :
169 0 : void CacheCompactionByCompression::Run (void)
170 : {
171 0 : if (mrCache.GetSize() > mnMaximalCacheSize)
172 : {
173 : SAL_INFO("sd.sls", OSL_THIS_FUNC << ": bitmap cache uses to much space: " << mrCache.GetSize() << " > " << mnMaximalCacheSize);
174 :
175 : ::std::unique_ptr< ::sd::slidesorter::cache::BitmapCache::CacheIndex> pIndex (
176 0 : mrCache.GetCacheIndex(false,false));
177 0 : ::sd::slidesorter::cache::BitmapCache::CacheIndex::iterator iIndex;
178 0 : for (iIndex=pIndex->begin(); iIndex!=pIndex->end(); ++iIndex)
179 : {
180 0 : if (*iIndex == NULL)
181 0 : continue;
182 :
183 0 : mrCache.Compress(*iIndex, mpCompressor);
184 0 : if (mrCache.GetSize() < mnMaximalCacheSize)
185 0 : break;
186 : }
187 0 : mrCache.ReCalculateTotalCacheSize();
188 0 : SAL_INFO("sd.sls", OSL_THIS_FUNC << ": there are now " << mrCache.GetSize() << " bytes occupied");
189 : }
190 0 : }
191 :
192 114 : } // end of anonymous namespace
193 :
194 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|