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"
26 #include <rtl/ustring.hxx>
27 #include <sal/log.hxx>
28 #include <officecfg/Office/Impress.hxx>
31 using namespace ::com::sun::star::uno
;
35 /** This is a trivial implementation of the CacheCompactor interface class.
36 It ignores calls to RequestCompaction() and thus will never decrease the
37 total size of off-screen preview bitmaps.
39 class NoCacheCompaction
40 : public ::sd::slidesorter::cache::CacheCompactor
44 ::sd::slidesorter::cache::BitmapCache
& rCache
,
45 sal_Int32 nMaximalCacheSize
)
46 : CacheCompactor(rCache
, nMaximalCacheSize
)
49 virtual void RequestCompaction() override
{ /* Ignored */ };
52 virtual void Run() override
{ /* Do nothing */ };
55 /** This implementation of the CacheCompactor interface class uses one of
56 several bitmap compression algorithms to reduce the number of the bytes
57 of the off-screen previews in the bitmap cache. See the documentation
58 of CacheCompactor::Create() for more details on configuration properties
59 that control the choice of compression algorithm.
61 class CacheCompactionByCompression
62 : public ::sd::slidesorter::cache::CacheCompactor
65 CacheCompactionByCompression (
66 ::sd::slidesorter::cache::BitmapCache
& rCache
,
67 sal_Int32 nMaximalCacheSize
,
68 std::shared_ptr
< ::sd::slidesorter::cache::BitmapCompressor
> pCompressor
);
71 virtual void Run() override
;
74 std::shared_ptr
< ::sd::slidesorter::cache::BitmapCompressor
> mpCompressor
;
77 } // end of anonymous namespace
79 namespace sd::slidesorter::cache
{
81 ::std::unique_ptr
<CacheCompactor
> CacheCompactor::Create (
83 sal_Int32 nMaximalCacheSize
)
85 static const char sNone
[] = "None";
87 std::shared_ptr
<BitmapCompressor
> pCompressor
;
88 OUString sCompressionPolicy
= officecfg::Office::Impress::MultiPaneGUI::SlideSorterBar::PreviewCache::CompressionPolicy::get();
89 if (sCompressionPolicy
== sNone
)
90 pCompressor
= std::make_shared
<NoBitmapCompression
>();
91 else if (sCompressionPolicy
== "Erase")
92 pCompressor
= std::make_shared
<CompressionByDeletion
>();
93 else if (sCompressionPolicy
== "ResolutionReduction")
94 pCompressor
= std::make_shared
<ResolutionReduction
>();
95 else // default: "PNGCompression"
96 pCompressor
= std::make_shared
<PngCompression
>();
98 ::std::unique_ptr
<CacheCompactor
> pCompactor
;
99 OUString sCompactionPolicy
= officecfg::Office::Impress::MultiPaneGUI::SlideSorterBar::PreviewCache::CompactionPolicy::get();
100 if (sCompactionPolicy
== sNone
)
101 pCompactor
.reset(new NoCacheCompaction(rCache
,nMaximalCacheSize
));
102 else // default: "Compress"
103 pCompactor
.reset(new CacheCompactionByCompression(rCache
, nMaximalCacheSize
, std::move(pCompressor
)));
108 void CacheCompactor::RequestCompaction()
110 if ( ! mbIsCompactionRunning
&& ! maCompactionTimer
.IsActive())
111 maCompactionTimer
.Start();
114 CacheCompactor::CacheCompactor(
116 sal_Int32 nMaximalCacheSize
)
118 mnMaximalCacheSize(nMaximalCacheSize
),
119 maCompactionTimer("sd CacheCompactor maCompactionTimer"),
120 mbIsCompactionRunning(false)
122 maCompactionTimer
.SetTimeout(100);
123 maCompactionTimer
.SetInvokeHandler(LINK(this,CacheCompactor
,CompactionCallback
));
126 IMPL_LINK_NOARG(CacheCompactor
, CompactionCallback
, Timer
*, void)
128 mbIsCompactionRunning
= true;
134 catch (const css::uno::RuntimeException
&)
137 catch (const css::uno::Exception
&)
141 mbIsCompactionRunning
= false;
144 } // end of namespace ::sd::slidesorter::cache
148 //===== CacheCompactionByCompression ==========================================
150 CacheCompactionByCompression::CacheCompactionByCompression (
151 ::sd::slidesorter::cache::BitmapCache
& rCache
,
152 sal_Int32 nMaximalCacheSize
,
153 std::shared_ptr
< ::sd::slidesorter::cache::BitmapCompressor
> pCompressor
)
154 : CacheCompactor(rCache
,nMaximalCacheSize
),
155 mpCompressor(std::move(pCompressor
))
159 void CacheCompactionByCompression::Run()
161 if (mrCache
.GetSize() <= mnMaximalCacheSize
)
164 SAL_INFO("sd.sls", __func__
<< ": bitmap cache uses too much space: " << mrCache
.GetSize() << " > " << mnMaximalCacheSize
);
166 ::sd::slidesorter::cache::BitmapCache::CacheIndex
aIndex (
167 mrCache
.GetCacheIndex());
168 for (const auto& rpIndex
: aIndex
)
170 if (rpIndex
== nullptr)
173 mrCache
.Compress(rpIndex
, mpCompressor
);
174 if (mrCache
.GetSize() < mnMaximalCacheSize
)
177 mrCache
.ReCalculateTotalCacheSize();
178 SAL_INFO("sd.sls", __func__
<< ": there are now " << mrCache
.GetSize() << " bytes occupied");
181 } // end of anonymous namespace
183 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */