diff options
author | Tomaž Vajngerl <tomaz.vajngerl@collabora.co.uk> | 2018-04-04 17:51:26 +0900 |
---|---|---|
committer | Tomaž Vajngerl <quikee@gmail.com> | 2018-04-12 14:35:14 +0200 |
commit | a2b53fece14f745853bcfe1a300c3dceb580e148 (patch) | |
tree | c4d287f97140363fe9cec7740b5189032e8f114e /vcl/source/graphic/Manager.cxx | |
parent | 92ac45b1920fcce8259c1eab94357415e8f50d82 (diff) |
vcl: Add a internal (memory) manager for Graphic objects
This adds vcl::graphic::Manager which is a manager singleton that
tracks all the creation fo graphic objects and swaps them out on
a time and allocation basis.
Time based - every number of seconds it looks for Graphics that
weren't used for a time.
Allocation based - when creating a new Graphic and the total of
Graphic uses more than the total amount of memory for Graphics
defined in configuration, it tries to release the Graphics that
weren't used for a time.
Change-Id: I5dbf74db4a6455d32c4abcbad7be21c7f0534642
Reviewed-on: https://gerrit.libreoffice.org/52396
Tested-by: Jenkins <ci@libreoffice.org>
Reviewed-by: Tomaž Vajngerl <quikee@gmail.com>
Diffstat (limited to 'vcl/source/graphic/Manager.cxx')
-rw-r--r-- | vcl/source/graphic/Manager.cxx | 200 |
1 files changed, 200 insertions, 0 deletions
diff --git a/vcl/source/graphic/Manager.cxx b/vcl/source/graphic/Manager.cxx new file mode 100644 index 000000000000..ee161c23d878 --- /dev/null +++ b/vcl/source/graphic/Manager.cxx @@ -0,0 +1,200 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#include <graphic/Manager.hxx> +#include <impgraph.hxx> +#include <vcl/lazydelete.hxx> +#include <o3tl/make_unique.hxx> + +using namespace css; + +namespace vcl +{ +namespace graphic +{ +namespace +{ +void setTotalCacheSizeFromConfigIfPossible(sal_Int64& nTotalCacheSize) +{ + if (utl::ConfigManager::IsFuzzing()) + return; + + try + { + nTotalCacheSize = officecfg::Office::Common::Cache::GraphicManager::TotalCacheSize::get(); + } + catch (...) + { + } +} +} + +Manager& Manager::get() +{ + static std::unique_ptr<Manager> gStaticManager(new Manager); + return *gStaticManager; +} + +Manager::Manager() + : mnTotalCacheSize(50000000) + , mnUsedSize(0) + , maSwapOutTimer("graphic::Manager maSwapOutTimer") +{ + setTotalCacheSizeFromConfigIfPossible(mnTotalCacheSize); + + maSwapOutTimer.SetInvokeHandler(LINK(this, Manager, SwapOutTimerHandler)); + maSwapOutTimer.SetTimeout(10000); + maSwapOutTimer.SetDebugName("graphic::Manager maSwapOutTimer"); + maSwapOutTimer.Start(); +} + +void Manager::reduceGraphicMemory() +{ + for (ImpGraphic* pEachImpGraphic : m_pImpGraphicList) + { + if (mnUsedSize < mnTotalCacheSize * 0.7) + return; + + sal_Int64 nCurrentGraphicSize = pEachImpGraphic->ImplGetSizeBytes(); + if (!pEachImpGraphic->ImplIsSwapOut() && nCurrentGraphicSize > 1000000) + { + if (!pEachImpGraphic->mpContext) + { + auto aCurrent = std::chrono::high_resolution_clock::now(); + auto aDeltaTime = aCurrent - pEachImpGraphic->maLastUsed; + auto aSeconds = std::chrono::duration_cast<std::chrono::seconds>(aDeltaTime); + double nSeconds = aSeconds.count(); + if (nSeconds > 10) + pEachImpGraphic->ImplSwapOut(); + } + } + } +} + +IMPL_LINK(Manager, SwapOutTimerHandler, Timer*, pTimer, void) +{ + pTimer->Stop(); + reduceGraphicMemory(); + pTimer->Start(); +} + +void Manager::registerGraphic(std::shared_ptr<ImpGraphic>& pImpGraphic, + OUString const& /*rsContext*/) +{ + // make some space first + if (mnUsedSize > mnTotalCacheSize) + reduceGraphicMemory(); + + // Insert and update the used size (bytes) + mnUsedSize += pImpGraphic->ImplGetSizeBytes(); + m_pImpGraphicList.insert(pImpGraphic.get()); + + // calculate size of the graphic set + sal_Int64 calculatedSize = 0; + for (ImpGraphic* pEachImpGraphic : m_pImpGraphicList) + { + if (!pEachImpGraphic->ImplIsSwapOut()) + { + calculatedSize += pEachImpGraphic->ImplGetSizeBytes(); + } + } + + if (calculatedSize != mnUsedSize) + { + SAL_WARN_IF(calculatedSize != mnUsedSize, "vcl.gdi", + "Calculated size mismatch. Variable size is '" + << mnUsedSize << "' but calculated size is '" << calculatedSize << "'"); + mnUsedSize = calculatedSize; + } +} + +void Manager::unregisterGraphic(ImpGraphic* pImpGraphic) +{ + mnUsedSize -= pImpGraphic->ImplGetSizeBytes(); + m_pImpGraphicList.erase(pImpGraphic); +} + +std::shared_ptr<ImpGraphic> Manager::copy(std::shared_ptr<ImpGraphic> const& rImpGraphicPtr) +{ + auto pReturn = std::make_shared<ImpGraphic>(*rImpGraphicPtr.get()); + registerGraphic(pReturn, "Copy"); + return pReturn; +} + +std::shared_ptr<ImpGraphic> Manager::newInstance() +{ + auto pReturn = std::make_shared<ImpGraphic>(); + registerGraphic(pReturn, "Empty"); + return pReturn; +} + +std::shared_ptr<ImpGraphic> Manager::newInstance(const Bitmap& rBitmap) +{ + auto pReturn = std::make_shared<ImpGraphic>(rBitmap); + registerGraphic(pReturn, "Bitmap"); + return pReturn; +} + +std::shared_ptr<ImpGraphic> Manager::newInstance(const BitmapEx& rBitmapEx) +{ + auto pReturn = std::make_shared<ImpGraphic>(rBitmapEx); + registerGraphic(pReturn, "BitmapEx"); + return pReturn; +} + +std::shared_ptr<ImpGraphic> Manager::newInstance(const Animation& rAnimation) +{ + auto pReturn = std::make_shared<ImpGraphic>(rAnimation); + registerGraphic(pReturn, "Animation"); + return pReturn; +} + +std::shared_ptr<ImpGraphic> Manager::newInstance(const VectorGraphicDataPtr& rVectorGraphicDataPtr) +{ + auto pReturn = std::make_shared<ImpGraphic>(rVectorGraphicDataPtr); + registerGraphic(pReturn, "VectorGraphic"); + return pReturn; +} + +std::shared_ptr<ImpGraphic> Manager::newInstance(const GDIMetaFile& rMetaFile) +{ + auto pReturn = std::make_shared<ImpGraphic>(rMetaFile); + registerGraphic(pReturn, "Metafile"); + return pReturn; +} + +void Manager::swappedIn(const ImpGraphic* pImpGraphic) +{ + mnUsedSize += pImpGraphic->ImplGetSizeBytes(); +} + +void Manager::swappedOut(const ImpGraphic* pImpGraphic) +{ + mnUsedSize -= pImpGraphic->ImplGetSizeBytes(); +} + +void Manager::changeExisting(const ImpGraphic* pImpGraphic, sal_Int64 nOldSizeBytes) +{ + mnUsedSize -= nOldSizeBytes; + mnUsedSize += pImpGraphic->ImplGetSizeBytes(); +} +} +} // end vcl::graphic + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |