LibreOffice Module sd (master) 1
SlsCacheCompactor.cxx
Go to the documentation of this file.
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 <memory>
21#include "SlsCacheCompactor.hxx"
22
24#include "SlsBitmapCache.hxx"
26
27#include <rtl/ustring.hxx>
28#include <sal/log.hxx>
29#include <com/sun/star/uno/Any.hxx>
30#include <utility>
31
32using namespace ::com::sun::star::uno;
33
34namespace {
35
40class NoCacheCompaction
42{
43public:
44 NoCacheCompaction (
46 sal_Int32 nMaximalCacheSize)
47 : CacheCompactor(rCache, nMaximalCacheSize)
48 {}
49
50 virtual void RequestCompaction() override { /* Ignored */ };
51
52protected:
53 virtual void Run() override { /* Do nothing */ };
54};
55
62class CacheCompactionByCompression
64{
65public:
66 CacheCompactionByCompression (
68 sal_Int32 nMaximalCacheSize,
69 std::shared_ptr< ::sd::slidesorter::cache::BitmapCompressor> pCompressor);
70
71protected:
72 virtual void Run() override;
73
74private:
75 std::shared_ptr< ::sd::slidesorter::cache::BitmapCompressor> mpCompressor;
76};
77
78} // end of anonymous namespace
79
80namespace sd::slidesorter::cache {
81
82::std::unique_ptr<CacheCompactor> CacheCompactor::Create (
83 BitmapCache& rCache,
84 sal_Int32 nMaximalCacheSize)
85{
86 static const char sNone[] = "None";
87
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>();
101
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));
111
112 return pCompactor;
113}
114
116{
119}
120
122 BitmapCache& rCache,
123 sal_Int32 nMaximalCacheSize)
124 : mrCache(rCache),
125 mnMaximalCacheSize(nMaximalCacheSize),
126 maCompactionTimer("sd CacheCompactor maCompactionTimer"),
127 mbIsCompactionRunning(false)
128{
130 maCompactionTimer.SetInvokeHandler(LINK(this,CacheCompactor,CompactionCallback));
131}
132
133IMPL_LINK_NOARG(CacheCompactor, CompactionCallback, Timer *, void)
134{
135 mbIsCompactionRunning = true;
136
137 try
138 {
139 Run();
140 }
141 catch (const css::uno::RuntimeException&)
142 {
143 }
144 catch (const css::uno::Exception&)
145 {
146 }
147
148 mbIsCompactionRunning = false;
149}
150
151} // end of namespace ::sd::slidesorter::cache
152
153namespace {
154
155//===== CacheCompactionByCompression ==========================================
156
157CacheCompactionByCompression::CacheCompactionByCompression (
159 sal_Int32 nMaximalCacheSize,
160 std::shared_ptr< ::sd::slidesorter::cache::BitmapCompressor> pCompressor)
161 : CacheCompactor(rCache,nMaximalCacheSize),
162 mpCompressor(std::move(pCompressor))
163{
164}
165
166void CacheCompactionByCompression::Run()
167{
168 if (mrCache.GetSize() <= mnMaximalCacheSize)
169 return;
170
171 SAL_INFO("sd.sls", __func__ << ": bitmap cache uses too much space: " << mrCache.GetSize() << " > " << mnMaximalCacheSize);
172
174 mrCache.GetCacheIndex());
175 for (const auto& rpIndex : aIndex)
176 {
177 if (rpIndex == nullptr)
178 continue;
179
180 mrCache.Compress(rpIndex, mpCompressor);
181 if (mrCache.GetSize() < mnMaximalCacheSize)
182 break;
183 }
184 mrCache.ReCalculateTotalCacheSize();
185 SAL_INFO("sd.sls", __func__ << ": there are now " << mrCache.GetSize() << " bytes occupied");
186}
187
188} // end of anonymous namespace
189
190/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
bool IsActive() const
void SetTimeout(sal_uInt64 nTimeoutMs)
void SetInvokeHandler(const Link< Timer *, void > &rLink)
virtual void Start(bool bStartTimer=true) override
This low level cache is the actual bitmap container.
::std::vector< CacheKey > CacheIndex
This is an interface class whose implementations are created via the Create() factory method.
virtual void RequestCompaction()
Request a compaction of the off-screen previews in the bitmap cache.
Timer maCompactionTimer
This timer is used to collect calls to RequestCompaction() and eventually call Run().
static ::std::unique_ptr< CacheCompactor > Create(BitmapCache &rCache, sal_Int32 nMaximalCacheSize)
Create a new instance of the CacheCompactor interface class.
virtual void Run()=0
This method actually tries to reduce the total number of bytes used by the off-screen preview bitmaps...
CacheCompactor(BitmapCache &rCache, sal_Int32 nMaximalCacheSize)
static std::shared_ptr< CacheConfiguration > Instance()
Return an instance to this class.
std::deque< AttacherIndex_Impl > aIndex
#define SAL_INFO(area, stream)
IMPL_LINK_NOARG(CacheCompactor, CompactionCallback, Timer *, void)
const char GetValue[]
constexpr OUStringLiteral sNone