Bump version to 21.06.18.1
[LibreOffice.git] / sd / source / ui / slidesorter / cache / SlsCacheCompactor.cxx
blobb4a44530ca57bfbe68aadfa777607489389bc3b3
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>
32 using namespace ::com::sun::star::uno;
34 namespace {
36 /** This is a trivial implementation of the CacheCompactor interface class.
37 It ignores calls to RequestCompaction() and thus will never decrease the
38 total size of off-screen preview bitmaps.
40 class NoCacheCompaction
41 : public ::sd::slidesorter::cache::CacheCompactor
43 public:
44 NoCacheCompaction (
45 ::sd::slidesorter::cache::BitmapCache& rCache,
46 sal_Int32 nMaximalCacheSize)
47 : CacheCompactor(rCache, nMaximalCacheSize)
50 virtual void RequestCompaction() override { /* Ignored */ };
52 protected:
53 virtual void Run() override { /* Do nothing */ };
56 /** This implementation of the CacheCompactor interface class uses one of
57 several bitmap compression algorithms to reduce the number of the bytes
58 of the off-screen previews in the bitmap cache. See the documentation
59 of CacheCompactor::Create() for more details on configuration properties
60 that control the choice of compression algorithm.
62 class CacheCompactionByCompression
63 : public ::sd::slidesorter::cache::CacheCompactor
65 public:
66 CacheCompactionByCompression (
67 ::sd::slidesorter::cache::BitmapCache& rCache,
68 sal_Int32 nMaximalCacheSize,
69 const std::shared_ptr< ::sd::slidesorter::cache::BitmapCompressor>& rpCompressor);
71 protected:
72 virtual void Run() override;
74 private:
75 std::shared_ptr< ::sd::slidesorter::cache::BitmapCompressor> mpCompressor;
78 } // end of anonymous namespace
80 namespace sd::slidesorter::cache {
82 ::std::unique_ptr<CacheCompactor> CacheCompactor::Create (
83 BitmapCache& rCache,
84 sal_Int32 nMaximalCacheSize)
86 static const char sNone[] = "None";
88 std::shared_ptr<BitmapCompressor> pCompressor;
89 OUString sCompressionPolicy("PNGCompression");
90 Any aCompressionPolicy (CacheConfiguration::Instance()->GetValue("CompressionPolicy"));
91 if (aCompressionPolicy.has<OUString>())
92 aCompressionPolicy >>= sCompressionPolicy;
93 if (sCompressionPolicy == sNone)
94 pCompressor = std::make_shared<NoBitmapCompression>();
95 else if (sCompressionPolicy == "Erase")
96 pCompressor = std::make_shared<CompressionByDeletion>();
97 else if (sCompressionPolicy == "ResolutionReduction")
98 pCompressor = std::make_shared<ResolutionReduction>();
99 else
100 pCompressor = std::make_shared<PngCompression>();
102 ::std::unique_ptr<CacheCompactor> pCompactor;
103 OUString sCompactionPolicy("Compress");
104 Any aCompactionPolicy (CacheConfiguration::Instance()->GetValue("CompactionPolicy"));
105 if (aCompactionPolicy.has<OUString>())
106 aCompactionPolicy >>= sCompactionPolicy;
107 if (sCompactionPolicy == sNone)
108 pCompactor.reset(new NoCacheCompaction(rCache,nMaximalCacheSize));
109 else
110 pCompactor.reset(new CacheCompactionByCompression(rCache,nMaximalCacheSize,pCompressor));
112 return pCompactor;
115 void CacheCompactor::RequestCompaction()
117 if ( ! mbIsCompactionRunning && ! maCompactionTimer.IsActive())
118 maCompactionTimer.Start();
121 CacheCompactor::CacheCompactor(
122 BitmapCache& rCache,
123 sal_Int32 nMaximalCacheSize)
124 : mrCache(rCache),
125 mnMaximalCacheSize(nMaximalCacheSize),
126 mbIsCompactionRunning(false)
128 maCompactionTimer.SetTimeout(100);
129 maCompactionTimer.SetInvokeHandler(LINK(this,CacheCompactor,CompactionCallback));
133 IMPL_LINK_NOARG(CacheCompactor, CompactionCallback, Timer *, void)
135 mbIsCompactionRunning = true;
139 Run();
141 catch (const css::uno::RuntimeException&)
144 catch (const css::uno::Exception&)
148 mbIsCompactionRunning = false;
151 } // end of namespace ::sd::slidesorter::cache
153 namespace {
155 //===== CacheCompactionByCompression ==========================================
157 CacheCompactionByCompression::CacheCompactionByCompression (
158 ::sd::slidesorter::cache::BitmapCache& rCache,
159 sal_Int32 nMaximalCacheSize,
160 const std::shared_ptr< ::sd::slidesorter::cache::BitmapCompressor>& rpCompressor)
161 : CacheCompactor(rCache,nMaximalCacheSize),
162 mpCompressor(rpCompressor)
166 void CacheCompactionByCompression::Run()
168 if (mrCache.GetSize() <= mnMaximalCacheSize)
169 return;
171 SAL_INFO("sd.sls", __func__ << ": bitmap cache uses too much space: " << mrCache.GetSize() << " > " << mnMaximalCacheSize);
173 ::std::unique_ptr< ::sd::slidesorter::cache::BitmapCache::CacheIndex> pIndex (
174 mrCache.GetCacheIndex());
175 for (const auto& rpIndex : *pIndex)
177 if (rpIndex == nullptr)
178 continue;
180 mrCache.Compress(rpIndex, mpCompressor);
181 if (mrCache.GetSize() < mnMaximalCacheSize)
182 break;
184 mrCache.ReCalculateTotalCacheSize();
185 SAL_INFO("sd.sls", __func__ << ": there are now " << mrCache.GetSize() << " bytes occupied");
188 } // end of anonymous namespace
190 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */