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 .
20 #include <graphic/Manager.hxx>
21 #include <impgraph.hxx>
22 #include <sal/log.hxx>
24 #include <officecfg/Office/Common.hxx>
25 #include <unotools/configmgr.hxx>
35 void setupConfigurationValuesIfPossible(sal_Int64
& rMemoryLimit
,
36 std::chrono::seconds
& rAllowedIdleTime
)
38 if (utl::ConfigManager::IsFuzzing())
43 using officecfg::Office::Common::Cache
;
45 rMemoryLimit
= Cache::GraphicManager::GraphicMemoryLimit::get();
47 = std::chrono::seconds(Cache::GraphicManager::GraphicAllowedIdleTime::get());
55 Manager
& Manager::get()
57 static std::unique_ptr
<Manager
> gStaticManager(new Manager
);
58 return *gStaticManager
;
62 : mnAllowedIdleTime(10)
63 , mnMemoryLimit(300000000)
65 , maSwapOutTimer("graphic::Manager maSwapOutTimer")
67 setupConfigurationValuesIfPossible(mnMemoryLimit
, mnAllowedIdleTime
);
69 maSwapOutTimer
.SetInvokeHandler(LINK(this, Manager
, SwapOutTimerHandler
));
70 maSwapOutTimer
.SetTimeout(10000);
71 maSwapOutTimer
.SetDebugName("graphic::Manager maSwapOutTimer");
72 maSwapOutTimer
.Start();
75 void Manager::reduceGraphicMemory()
77 std::scoped_lock
<std::recursive_mutex
> aGuard(maMutex
);
79 for (ImpGraphic
* pEachImpGraphic
: m_pImpGraphicList
)
81 if (mnUsedSize
< mnMemoryLimit
* 0.7)
84 sal_Int64 nCurrentGraphicSize
= getGraphicSizeBytes(pEachImpGraphic
);
85 if (!pEachImpGraphic
->ImplIsSwapOut() && nCurrentGraphicSize
> 1000000)
87 if (!pEachImpGraphic
->mpContext
)
89 auto aCurrent
= std::chrono::high_resolution_clock::now();
90 auto aDeltaTime
= aCurrent
- pEachImpGraphic
->maLastUsed
;
91 auto aSeconds
= std::chrono::duration_cast
<std::chrono::seconds
>(aDeltaTime
);
93 if (aSeconds
> mnAllowedIdleTime
)
94 pEachImpGraphic
->ImplSwapOut();
100 sal_Int64
Manager::getGraphicSizeBytes(const ImpGraphic
* pImpGraphic
)
102 if (!pImpGraphic
->isAvailable())
104 return pImpGraphic
->ImplGetSizeBytes();
107 IMPL_LINK(Manager
, SwapOutTimerHandler
, Timer
*, pTimer
, void)
109 std::scoped_lock
<std::recursive_mutex
> aGuard(maMutex
);
112 reduceGraphicMemory();
116 void Manager::registerGraphic(const std::shared_ptr
<ImpGraphic
>& pImpGraphic
,
117 OUString
const& /*rsContext*/)
119 std::scoped_lock
<std::recursive_mutex
> aGuard(maMutex
);
121 // make some space first
122 if (mnUsedSize
> mnMemoryLimit
)
123 reduceGraphicMemory();
125 // Insert and update the used size (bytes)
126 mnUsedSize
+= getGraphicSizeBytes(pImpGraphic
.get());
127 m_pImpGraphicList
.insert(pImpGraphic
.get());
129 // calculate size of the graphic set
130 sal_Int64 calculatedSize
= 0;
131 for (ImpGraphic
* pEachImpGraphic
: m_pImpGraphicList
)
133 if (!pEachImpGraphic
->ImplIsSwapOut())
135 calculatedSize
+= getGraphicSizeBytes(pEachImpGraphic
);
139 if (calculatedSize
!= mnUsedSize
)
141 SAL_INFO_IF(calculatedSize
!= mnUsedSize
, "vcl.gdi",
142 "Calculated size mismatch. Variable size is '"
143 << mnUsedSize
<< "' but calculated size is '" << calculatedSize
<< "'");
144 mnUsedSize
= calculatedSize
;
148 void Manager::unregisterGraphic(ImpGraphic
* pImpGraphic
)
150 std::scoped_lock
<std::recursive_mutex
> aGuard(maMutex
);
152 mnUsedSize
-= getGraphicSizeBytes(pImpGraphic
);
153 m_pImpGraphicList
.erase(pImpGraphic
);
156 std::shared_ptr
<ImpGraphic
> Manager::copy(std::shared_ptr
<ImpGraphic
> const& rImpGraphicPtr
)
158 auto pReturn
= std::make_shared
<ImpGraphic
>(*rImpGraphicPtr
);
159 registerGraphic(pReturn
, "Copy");
163 std::shared_ptr
<ImpGraphic
> Manager::newInstance()
165 auto pReturn
= std::make_shared
<ImpGraphic
>();
166 registerGraphic(pReturn
, "Empty");
170 std::shared_ptr
<ImpGraphic
> Manager::newInstance(const Bitmap
& rBitmap
)
172 auto pReturn
= std::make_shared
<ImpGraphic
>(rBitmap
);
173 registerGraphic(pReturn
, "Bitmap");
177 std::shared_ptr
<ImpGraphic
> Manager::newInstance(const BitmapEx
& rBitmapEx
)
179 auto pReturn
= std::make_shared
<ImpGraphic
>(rBitmapEx
);
180 registerGraphic(pReturn
, "BitmapEx");
184 std::shared_ptr
<ImpGraphic
> Manager::newInstance(const Animation
& rAnimation
)
186 auto pReturn
= std::make_shared
<ImpGraphic
>(rAnimation
);
187 registerGraphic(pReturn
, "Animation");
191 std::shared_ptr
<ImpGraphic
> Manager::newInstance(const VectorGraphicDataPtr
& rVectorGraphicDataPtr
)
193 auto pReturn
= std::make_shared
<ImpGraphic
>(rVectorGraphicDataPtr
);
194 registerGraphic(pReturn
, "VectorGraphic");
198 std::shared_ptr
<ImpGraphic
> Manager::newInstance(const GDIMetaFile
& rMetaFile
)
200 auto pReturn
= std::make_shared
<ImpGraphic
>(rMetaFile
);
201 registerGraphic(pReturn
, "Metafile");
205 std::shared_ptr
<ImpGraphic
> Manager::newInstance(const GraphicExternalLink
& rGraphicLink
)
207 auto pReturn
= std::make_shared
<ImpGraphic
>(rGraphicLink
);
208 registerGraphic(pReturn
, "GraphicExternalLink");
212 void Manager::swappedIn(const ImpGraphic
* pImpGraphic
)
214 std::scoped_lock
<std::recursive_mutex
> aGuard(maMutex
);
216 mnUsedSize
+= getGraphicSizeBytes(pImpGraphic
);
219 void Manager::swappedOut(const ImpGraphic
* pImpGraphic
)
221 std::scoped_lock
<std::recursive_mutex
> aGuard(maMutex
);
223 mnUsedSize
-= getGraphicSizeBytes(pImpGraphic
);
226 void Manager::changeExisting(const ImpGraphic
* pImpGraphic
, sal_Int64 nOldSizeBytes
)
228 std::scoped_lock
<std::recursive_mutex
> aGuard(maMutex
);
230 mnUsedSize
-= nOldSizeBytes
;
231 mnUsedSize
+= getGraphicSizeBytes(pImpGraphic
);
234 } // end vcl::graphic
236 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */