Version 24.2.2.2, tag libreoffice-24.2.2.2
[LibreOffice.git] / basegfx / source / tools / systemdependentdata.cxx
blob0d64d9982cef28cef449e93d7a38b2a0c54ab27c
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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/.
8 */
10 #include <basegfx/utils/systemdependentdata.hxx>
11 #include <config_fuzzers.h>
12 #include <math.h>
14 namespace basegfx
16 SystemDependentDataManager::SystemDependentDataManager()
20 SystemDependentDataManager::~SystemDependentDataManager()
23 } // namespace basegfx
25 namespace basegfx
27 SystemDependentData::SystemDependentData(
28 SystemDependentDataManager& rSystemDependentDataManager)
29 : mrSystemDependentDataManager(rSystemDependentDataManager),
30 mnCalculatedCycles(0)
34 SystemDependentData::~SystemDependentData()
38 sal_uInt32 SystemDependentData::calculateCombinedHoldCyclesInSeconds() const
40 #if ENABLE_FUZZERS
41 return 0;
42 #endif
44 if(0 == mnCalculatedCycles)
46 const sal_Int64 nBytes(estimateUsageInBytes());
48 // tdf#129845 as indicator for no need to buffer trivial data, stay at and
49 // return zero. As border, use 450 bytes. For polygons, this means to buffer
50 // starting with ca. 50 points (GDIPLUS uses 9 bytes per coordinate). For
51 // Bitmap data this means to more or less always buffer (as it was before).
52 // For the future, a more sophisticated differentiation may be added
53 if(nBytes > 450)
55 // HoldCyclesInSeconds
56 const sal_uInt32 nSeconds = 60;
58 // default is Seconds (minimal is one)
59 sal_uInt32 nResult(0 == nSeconds ? 1 : nSeconds);
61 if(0 != nBytes)
63 // use sqrt to get some curved shape. With a default of 60s we get
64 // a single second at 3600 byte. To get close to 10mb, multiply by
65 // a corresponding scaling factor
66 const double fScaleToMB(3600.0 / (1024.0 * 1024.0 * 10.0));
68 // also use a multiplier to move the start point higher
69 const double fMultiplierSeconds(10.0);
71 // calculate
72 nResult = static_cast<sal_uInt32>((fMultiplierSeconds * nSeconds) / sqrt(nBytes * fScaleToMB));
74 // minimal value is 1
75 if(nResult < 1)
77 nResult = 1;
80 // maximal value is nSeconds
81 if(nResult > nSeconds)
83 nResult = nSeconds;
87 // set locally (once, on-demand created, non-zero)
88 const_cast<SystemDependentData*>(this)->mnCalculatedCycles = nResult;
92 return mnCalculatedCycles;
95 sal_Int64 SystemDependentData::estimateUsageInBytes() const
97 // default implementation has no idea
98 return 0;
100 } // namespace basegfx
102 namespace basegfx
104 SystemDependentDataHolder::SystemDependentDataHolder()
108 SystemDependentDataHolder::~SystemDependentDataHolder()
110 for(const auto& candidate : maSystemDependentReferences)
112 basegfx::SystemDependentData_SharedPtr aData(candidate.second.lock());
114 if(aData)
116 aData->getSystemDependentDataManager().endUsage(aData);
121 void SystemDependentDataHolder::addOrReplaceSystemDependentData(basegfx::SystemDependentData_SharedPtr& rData)
123 const size_t hash_code(typeid(*rData).hash_code());
124 auto result(maSystemDependentReferences.find(hash_code));
126 if(result != maSystemDependentReferences.end())
128 basegfx::SystemDependentData_SharedPtr aData(result->second.lock());
130 if(aData)
132 aData->getSystemDependentDataManager().endUsage(aData);
135 maSystemDependentReferences.erase(result);
136 result = maSystemDependentReferences.end();
139 maSystemDependentReferences[hash_code] = rData;
140 rData->getSystemDependentDataManager().startUsage(rData);
143 SystemDependentData_SharedPtr SystemDependentDataHolder::getSystemDependentData(size_t hash_code) const
145 basegfx::SystemDependentData_SharedPtr aRetval;
146 auto result(maSystemDependentReferences.find(hash_code));
148 if(result != maSystemDependentReferences.end())
150 aRetval = result->second.lock();
152 if(aRetval)
154 aRetval->getSystemDependentDataManager().touchUsage(aRetval);
156 else
158 const_cast< SystemDependentDataHolder* >(this)->maSystemDependentReferences.erase(result);
162 return aRetval;
164 } // namespace basegfx
166 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */