bump product version to 6.3.0.0.beta1
[LibreOffice.git] / sd / source / ui / slidesorter / cache / SlsCacheCompactor.cxx
blob48eebfb1b4296dd5963b8931e3f8eb36921cad2d
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 <memory>
21 #include "SlsCacheCompactor.hxx"
23 #include "SlsBitmapCompressor.hxx"
24 #include "SlsBitmapCache.hxx"
25 #include "SlsCacheConfiguration.hxx"
27 #include <rtl/ustring.hxx>
28 #include <sal/log.hxx>
29 #include <osl/diagnose.h>
30 #include <com/sun/star/uno/Any.hxx>
31 #include <set>
33 using namespace ::com::sun::star::uno;
35 namespace {
37 /** This is a trivial implementation of the CacheCompactor interface class.
38 It ignores calls to RequestCompaction() and thus will never decrease the
39 total size of off-screen preview bitmaps.
41 class NoCacheCompaction
42 : public ::sd::slidesorter::cache::CacheCompactor
44 public:
45 NoCacheCompaction (
46 ::sd::slidesorter::cache::BitmapCache& rCache,
47 sal_Int32 nMaximalCacheSize)
48 : CacheCompactor(rCache, nMaximalCacheSize)
51 virtual void RequestCompaction() override { /* Ignored */ };
53 protected:
54 virtual void Run() override { /* Do nothing */ };
57 /** This implementation of the CacheCompactor interface class uses one of
58 several bitmap compression algorithms to reduce the number of the bytes
59 of the off-screen previews in the bitmap cache. See the documentation
60 of CacheCompactor::Create() for more details on configuration properties
61 that control the choice of compression algorithm.
63 class CacheCompactionByCompression
64 : public ::sd::slidesorter::cache::CacheCompactor
66 public:
67 CacheCompactionByCompression (
68 ::sd::slidesorter::cache::BitmapCache& rCache,
69 sal_Int32 nMaximalCacheSize,
70 const std::shared_ptr< ::sd::slidesorter::cache::BitmapCompressor>& rpCompressor);
72 protected:
73 virtual void Run() override;
75 private:
76 std::shared_ptr< ::sd::slidesorter::cache::BitmapCompressor> mpCompressor;
79 } // end of anonymous namespace
81 namespace sd { namespace slidesorter { namespace cache {
83 ::std::unique_ptr<CacheCompactor> CacheCompactor::Create (
84 BitmapCache& rCache,
85 sal_Int32 nMaximalCacheSize)
87 static const char sNone[] = "None";
89 std::shared_ptr<BitmapCompressor> pCompressor;
90 OUString sCompressionPolicy("PNGCompression");
91 Any aCompressionPolicy (CacheConfiguration::Instance()->GetValue("CompressionPolicy"));
92 if (aCompressionPolicy.has<OUString>())
93 aCompressionPolicy >>= sCompressionPolicy;
94 if (sCompressionPolicy == sNone)
95 pCompressor.reset(new NoBitmapCompression);
96 else if (sCompressionPolicy == "Erase")
97 pCompressor.reset(new CompressionByDeletion);
98 else if (sCompressionPolicy == "ResolutionReduction")
99 pCompressor.reset(new ResolutionReduction);
100 else
101 pCompressor.reset(new PngCompression);
103 ::std::unique_ptr<CacheCompactor> pCompactor;
104 OUString sCompactionPolicy("Compress");
105 Any aCompactionPolicy (CacheConfiguration::Instance()->GetValue("CompactionPolicy"));
106 if (aCompactionPolicy.has<OUString>())
107 aCompactionPolicy >>= sCompactionPolicy;
108 if (sCompactionPolicy == sNone)
109 pCompactor.reset(new NoCacheCompaction(rCache,nMaximalCacheSize));
110 else
111 pCompactor.reset(new CacheCompactionByCompression(rCache,nMaximalCacheSize,pCompressor));
113 return pCompactor;
116 void CacheCompactor::RequestCompaction()
118 if ( ! mbIsCompactionRunning && ! maCompactionTimer.IsActive())
119 maCompactionTimer.Start();
122 CacheCompactor::CacheCompactor(
123 BitmapCache& rCache,
124 sal_Int32 nMaximalCacheSize)
125 : mrCache(rCache),
126 mnMaximalCacheSize(nMaximalCacheSize),
127 mbIsCompactionRunning(false)
129 maCompactionTimer.SetTimeout(100);
130 maCompactionTimer.SetInvokeHandler(LINK(this,CacheCompactor,CompactionCallback));
134 IMPL_LINK_NOARG(CacheCompactor, CompactionCallback, Timer *, void)
136 mbIsCompactionRunning = true;
140 Run();
142 catch (const css::uno::RuntimeException&)
145 catch (const css::uno::Exception&)
149 mbIsCompactionRunning = false;
152 } } } // end of namespace ::sd::slidesorter::cache
154 namespace {
156 //===== CacheCompactionByCompression ==========================================
158 CacheCompactionByCompression::CacheCompactionByCompression (
159 ::sd::slidesorter::cache::BitmapCache& rCache,
160 sal_Int32 nMaximalCacheSize,
161 const std::shared_ptr< ::sd::slidesorter::cache::BitmapCompressor>& rpCompressor)
162 : CacheCompactor(rCache,nMaximalCacheSize),
163 mpCompressor(rpCompressor)
167 void CacheCompactionByCompression::Run()
169 if (mrCache.GetSize() <= mnMaximalCacheSize)
170 return;
172 SAL_INFO("sd.sls", OSL_THIS_FUNC << ": bitmap cache uses too much space: " << mrCache.GetSize() << " > " << mnMaximalCacheSize);
174 ::std::unique_ptr< ::sd::slidesorter::cache::BitmapCache::CacheIndex> pIndex (
175 mrCache.GetCacheIndex());
176 for (const auto& rpIndex : *pIndex)
178 if (rpIndex == nullptr)
179 continue;
181 mrCache.Compress(rpIndex, mpCompressor);
182 if (mrCache.GetSize() < mnMaximalCacheSize)
183 break;
185 mrCache.ReCalculateTotalCacheSize();
186 SAL_INFO("sd.sls", OSL_THIS_FUNC << ": there are now " << mrCache.GetSize() << " bytes occupied");
189 } // end of anonymous namespace
191 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */