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 
10 #ifndef INCLUDED_VCL_INC_GRAPHIC_MANAGER_HXX
11 #define INCLUDED_VCL_INC_GRAPHIC_MANAGER_HXX
12 
13 #include <sal/types.h>
14 #include <rtl/ustring.hxx>
15 #include <vcl/bitmapex.hxx>
16 #include <vcl/animate/Animation.hxx>
17 #include <vcl/vectorgraphicdata.hxx>
18 #include <vcl/timer.hxx>
19 #include <vcl/GraphicExternalLink.hxx>
20 
21 #include <memory>
22 #include <mutex>
23 #include <chrono>
24 #include <unordered_set>
25 
26 class ImpGraphic;
27 
28 namespace vcl
29 {
30 namespace graphic
31 {
32 class Manager final
33 {
34 private:
35     std::recursive_mutex maMutex; // instead of SolarMutex because graphics can live past vcl main
36     std::unordered_set<ImpGraphic*> m_pImpGraphicList;
37     std::chrono::seconds mnAllowedIdleTime;
38     bool mbSwapEnabled;
39     sal_Int64 mnMemoryLimit;
40     sal_Int64 mnUsedSize;
41     Timer maSwapOutTimer;
42 
43     Manager();
44 
45     void registerGraphic(const std::shared_ptr<ImpGraphic>& rImpGraphic, OUString const& rsContext);
46 
47     DECL_LINK(SwapOutTimerHandler, Timer*, void);
48 
49     static sal_Int64 getGraphicSizeBytes(const ImpGraphic* pImpGraphic);
50 
51 public:
52     static Manager& get();
53 
54     void swappedIn(const ImpGraphic* pImpGraphic);
55     void swappedOut(const ImpGraphic* pImpGraphic);
56 
57     void reduceGraphicMemory();
58     void changeExisting(const ImpGraphic* pImpGraphic, sal_Int64 nOldSize);
59     void unregisterGraphic(ImpGraphic* pImpGraphic);
60 
61     std::shared_ptr<ImpGraphic> copy(std::shared_ptr<ImpGraphic> const& pImpGraphic);
62     std::shared_ptr<ImpGraphic> newInstance();
63     std::shared_ptr<ImpGraphic> newInstance(const Bitmap& rBitmap);
64     std::shared_ptr<ImpGraphic> newInstance(const BitmapEx& rBitmapEx);
65     std::shared_ptr<ImpGraphic> newInstance(const VectorGraphicDataPtr& rVectorGraphicDataPtr);
66     std::shared_ptr<ImpGraphic> newInstance(const Animation& rAnimation);
67     std::shared_ptr<ImpGraphic> newInstance(const GDIMetaFile& rMtf);
68     std::shared_ptr<ImpGraphic> newInstance(const GraphicExternalLink& rGraphicLink);
69 };
70 }
71 } // end namespace vcl::graphic
72 
73 #endif // INCLUDED_VCL_INC_GRAPHIC_MANAGER_HXX
74 
75 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
76