1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 .
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>
33 using namespace ::com::sun::star::uno
;
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
46 ::sd::slidesorter::cache::BitmapCache
& rCache
,
47 sal_Int32 nMaximalCacheSize
)
48 : CacheCompactor(rCache
, nMaximalCacheSize
)
51 virtual void RequestCompaction() override
{ /* Ignored */ };
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
67 CacheCompactionByCompression (
68 ::sd::slidesorter::cache::BitmapCache
& rCache
,
69 sal_Int32 nMaximalCacheSize
,
70 const std::shared_ptr
< ::sd::slidesorter::cache::BitmapCompressor
>& rpCompressor
);
73 virtual void Run() override
;
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 (
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
);
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
));
111 pCompactor
.reset(new CacheCompactionByCompression(rCache
,nMaximalCacheSize
,pCompressor
));
116 void CacheCompactor::RequestCompaction()
118 if ( ! mbIsCompactionRunning
&& ! maCompactionTimer
.IsActive())
119 maCompactionTimer
.Start();
122 CacheCompactor::CacheCompactor(
124 sal_Int32 nMaximalCacheSize
)
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;
142 catch (const css::uno::RuntimeException
&)
145 catch (const css::uno::Exception
&)
149 mbIsCompactionRunning
= false;
152 } } } // end of namespace ::sd::slidesorter::cache
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
)
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)
181 mrCache
.Compress(rpIndex
, mpCompressor
);
182 if (mrCache
.GetSize() < mnMaximalCacheSize
)
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: */