nss: upgrade to release 3.73
[LibreOffice.git] / basegfx / source / tools / systemdependentdata.cxx
blobe333ce473437d715f521c2e1675db463adf83be5
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 <math.h>
13 namespace basegfx
15 SystemDependentDataManager::SystemDependentDataManager()
19 SystemDependentDataManager::~SystemDependentDataManager()
22 } // namespace basegfx
24 namespace basegfx
26 SystemDependentData::SystemDependentData(
27 SystemDependentDataManager& rSystemDependentDataManager)
28 : mrSystemDependentDataManager(rSystemDependentDataManager),
29 mnCalculatedCycles(0)
33 SystemDependentData::~SystemDependentData()
37 sal_uInt32 SystemDependentData::calculateCombinedHoldCyclesInSeconds() const
39 if(0 == mnCalculatedCycles)
41 const sal_Int64 nBytes(estimateUsageInBytes());
43 // tdf#129845 as indicator for no need to buffer trivial data, stay at and
44 // return zero. As border, use 450 bytes. For polygons, this means to buffer
45 // starting with ca. 50 points (GDIPLUS uses 9 bytes per coordinate). For
46 // Bitmap data this means to more or less always buffer (as it was before).
47 // For the future, a more sophisticated differentiation may be added
48 if(nBytes > 450)
50 const sal_uInt32 nSeconds = 60; // HoldCyclesInSeconds
52 // default is Seconds (minimal is one)
53 sal_uInt32 nResult(0 == nSeconds ? 1 : nSeconds);
55 if(0 != nBytes)
57 // use sqrt to get some curved shape. With a default of 60s we get
58 // a single second at 3600 byte. To get close to 10mb, multiply by
59 // a corresponding scaling factor
60 const double fScaleToMB(3600.0 / (1024.0 * 1024.0 * 10.0));
62 // also use a multiplier to move the start point higher
63 const double fMultiplierSeconds(10.0);
65 // calculate
66 nResult = static_cast<sal_uInt32>((fMultiplierSeconds * nSeconds) / sqrt(nBytes * fScaleToMB));
68 // minimal value is 1
69 if(nResult < 1)
71 nResult = 1;
74 // maximal value is nSeconds
75 if(nResult > nSeconds)
77 nResult = nSeconds;
81 // set locally (once, on-demand created, non-zero)
82 const_cast<SystemDependentData*>(this)->mnCalculatedCycles = nResult;
86 return mnCalculatedCycles;
89 sal_Int64 SystemDependentData::estimateUsageInBytes() const
91 // default implementation has no idea
92 return 0;
94 } // namespace basegfx
96 namespace basegfx
98 SystemDependentDataHolder::SystemDependentDataHolder()
99 : maSystemDependentReferences()
103 SystemDependentDataHolder::~SystemDependentDataHolder()
105 for(const auto& candidate : maSystemDependentReferences)
107 basegfx::SystemDependentData_SharedPtr aData(candidate.second.lock());
109 if(aData)
111 aData->getSystemDependentDataManager().endUsage(aData);
116 void SystemDependentDataHolder::addOrReplaceSystemDependentData(basegfx::SystemDependentData_SharedPtr& rData)
118 const size_t hash_code(typeid(*rData).hash_code());
119 auto result(maSystemDependentReferences.find(hash_code));
121 if(result != maSystemDependentReferences.end())
123 basegfx::SystemDependentData_SharedPtr aData(result->second.lock());
125 if(aData)
127 aData->getSystemDependentDataManager().endUsage(aData);
130 maSystemDependentReferences.erase(result);
131 result = maSystemDependentReferences.end();
134 maSystemDependentReferences[hash_code] = rData;
135 rData->getSystemDependentDataManager().startUsage(rData);
138 SystemDependentData_SharedPtr SystemDependentDataHolder::getSystemDependentData(size_t hash_code) const
140 basegfx::SystemDependentData_SharedPtr aRetval;
141 auto result(maSystemDependentReferences.find(hash_code));
143 if(result != maSystemDependentReferences.end())
145 aRetval = result->second.lock();
147 if(aRetval)
149 aRetval->getSystemDependentDataManager().touchUsage(aRetval);
151 else
153 const_cast< SystemDependentDataHolder* >(this)->maSystemDependentReferences.erase(result);
157 return aRetval;
159 } // namespace basegfx
161 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */