Add explicit API scaling via ImageTree API.

Also add image scale to in-memory cache lookup.

Change-Id: I1e84b922d4a9ab2f1723c5cb8a72f295c6940504
Reviewed-on: https://gerrit.libreoffice.org/64043
Tested-by: Jenkins
Reviewed-by: Michael Meeks <michael.meeks@collabora.com>
This commit is contained in:
Michael Meeks
2018-11-26 13:24:03 +00:00
parent a71ea5ceff
commit bc8e23fc94
4 changed files with 51 additions and 17 deletions

View File

@@ -64,6 +64,12 @@ public:
BitmapEx & bitmap, bool localized,
const ImageLoadFlags eFlags = ImageLoadFlags::NONE);
VCL_DLLPUBLIC bool loadImage(
OUString const & name, OUString const & style,
BitmapEx & bitmap, bool localized,
sal_Int32 nScalePercentage,
const ImageLoadFlags eFlags = ImageLoadFlags::NONE);
VCL_DLLPUBLIC css::uno::Reference<css::container::XNameAccess> const & getNameAccess();

View File

@@ -46,14 +46,17 @@ struct ImageRequestParameters
bool mbLocalized;
ImageLoadFlags const meFlags;
bool mbWriteImageToCache;
sal_Int32 mnScalePercentage;
ImageRequestParameters(const OUString & rName, const OUString & rStyle, BitmapEx& rBitmap, bool bLocalized, ImageLoadFlags eFlags)
ImageRequestParameters(const OUString & rName, const OUString & rStyle, BitmapEx& rBitmap, bool bLocalized,
ImageLoadFlags eFlags, sal_Int32 nScalePercentage)
: msName(rName)
, msStyle(rStyle)
, mrBitmap(rBitmap)
, mbLocalized(bLocalized)
, meFlags(eFlags)
, mbWriteImageToCache(false)
, mnScalePercentage(nScalePercentage)
{}
bool convertToDarkTheme();
@@ -75,7 +78,8 @@ public:
bool loadImage(
OUString const & name, OUString const & style,
BitmapEx & bitmap, bool localized,
const ImageLoadFlags eFlags);
const ImageLoadFlags eFlags,
sal_Int32 nScalePercentage = -1);
/** a crude form of life cycle control (called from DeInitVCL; otherwise,
* if the ImplImageTree singleton were destroyed during exit that would
@@ -88,14 +92,15 @@ private:
ImplImageTree(const ImplImageTree&) = delete;
ImplImageTree& operator=(const ImplImageTree&) = delete;
typedef std::unordered_map<OUString, std::pair<bool, BitmapEx>> IconCache;
typedef std::unordered_map<OUString, std::pair<bool,BitmapEx>> IconCache;
typedef std::unordered_map<sal_Int32, std::unique_ptr<IconCache>> ScaledIconCache;
typedef std::unordered_map<OUString, OUString> IconLinkHash;
struct IconSet
{
OUString maURL;
css::uno::Reference<css::container::XNameAccess> maNameAccess;
IconCache maIconCache;
ScaledIconCache maScaledIconCaches;
IconLinkHash maLinkHash;
IconSet()
@@ -128,6 +133,8 @@ private:
void createStyle();
IconCache &getIconCache(ImageRequestParameters& rParameters);
bool iconCacheLookup(ImageRequestParameters& rParameters);
bool findImage(std::vector<OUString> const & rPaths, ImageRequestParameters& rParameters);

View File

@@ -37,9 +37,17 @@ std::shared_ptr<SvMemoryStream> ImageTree::getImageStream(OUString const & rName
bool ImageTree::loadImage(OUString const & rName, OUString const & rStyle,
BitmapEx & rBitmap, bool bLocalized,
sal_Int32 nScalePercentage,
const ImageLoadFlags eFlags)
{
return mpImplImageTree->loadImage(rName, rStyle, rBitmap, bLocalized, eFlags);
return mpImplImageTree->loadImage(rName, rStyle, rBitmap, bLocalized, eFlags, nScalePercentage);
}
bool ImageTree::loadImage(OUString const & rName, OUString const & rStyle,
BitmapEx & rBitmap, bool bLocalized,
const ImageLoadFlags eFlags)
{
return loadImage(rName, rStyle, rBitmap, bLocalized, -1, eFlags);
}
css::uno::Reference<css::container::XNameAccess> const & ImageTree::getNameAccess()

View File

@@ -69,6 +69,8 @@ sal_Int32 ImageRequestParameters::scalePercentage()
sal_Int32 aScalePercentage = 100;
if (!(meFlags & ImageLoadFlags::IgnoreScalingFactor))
aScalePercentage = Application::GetDefaultDevice()->GetDPIScalePercentage();
else if (mnScalePercentage > 0)
aScalePercentage = mnScalePercentage;
return aScalePercentage;
}
@@ -96,17 +98,17 @@ OUString createPath(OUString const & name, sal_Int32 pos, OUString const & local
return name.copy(0, pos + 1) + locale + name.copy(pos);
}
OUString getIconCacheUrl(OUString const & sStyle, OUString const & sVariant, OUString const & sName)
OUString getIconCacheUrl(OUString const & sVariant, ImageRequestParameters const & rParameters)
{
OUString sUrl("${$BRAND_BASE_DIR/" LIBO_ETC_FOLDER "/" SAL_CONFIGFILE("bootstrap") ":UserInstallation}/cache/");
sUrl += sStyle + "/" + sVariant + "/" + sName;
sUrl += rParameters.msStyle + "/" + sVariant + "/" + rParameters.msName;
rtl::Bootstrap::expandMacros(sUrl);
return sUrl;
}
OUString createIconCacheUrl(OUString const & sStyle, OUString const & sVariant, OUString const & sName)
OUString createIconCacheUrl(OUString const & sVariant, ImageRequestParameters const & rParameters)
{
OUString sUrl(getIconCacheUrl(sStyle, sVariant, sName));
OUString sUrl(getIconCacheUrl(sVariant, rParameters));
OUString sDir = sUrl.copy(0, sUrl.lastIndexOf('/'));
osl::Directory::createPath(sDir);
return sUrl;
@@ -315,14 +317,15 @@ OUString ImplImageTree::fallbackStyle(const OUString& rsStyle)
return sResult;
}
bool ImplImageTree::loadImage(OUString const & rName, OUString const & rStyle, BitmapEx & rBitmap, bool localized, const ImageLoadFlags eFlags)
bool ImplImageTree::loadImage(OUString const & rName, OUString const & rStyle, BitmapEx & rBitmap, bool localized,
const ImageLoadFlags eFlags, sal_Int32 nScalePercentage)
{
OUString aCurrentStyle(rStyle);
while (!aCurrentStyle.isEmpty())
{
try
{
ImageRequestParameters aParameters(rName, aCurrentStyle, rBitmap, localized, eFlags);
ImageRequestParameters aParameters(rName, aCurrentStyle, rBitmap, localized, eFlags, nScalePercentage);
if (doLoadImage(aParameters))
return true;
}
@@ -349,7 +352,7 @@ static OUString createVariant(ImageRequestParameters& rParameters)
static bool loadDiskCachedVersion(OUString const & sVariant, ImageRequestParameters& rParameters)
{
OUString sUrl(getIconCacheUrl(rParameters.msStyle, sVariant, rParameters.msName));
OUString sUrl(getIconCacheUrl(sVariant, rParameters));
if (!urlExists(sUrl))
return false;
SvFileStream aFileStream(sUrl, StreamMode::READ);
@@ -361,7 +364,7 @@ static bool loadDiskCachedVersion(OUString const & sVariant, ImageRequestParamet
static void cacheBitmapToDisk(OUString const & sVariant, ImageRequestParameters const & rParameters)
{
OUString sUrl(createIconCacheUrl(rParameters.msStyle, sVariant, rParameters.msName));
OUString sUrl(createIconCacheUrl(sVariant, rParameters));
vcl::PNGWriter aWriter(rParameters.mrBitmap);
try
{
@@ -382,9 +385,7 @@ bool ImplImageTree::doLoadImage(ImageRequestParameters& rParameters)
OUString aVariant = createVariant(rParameters);
if (loadDiskCachedVersion(aVariant, rParameters))
{
return true;
}
if (!rParameters.mrBitmap.IsEmpty())
rParameters.mrBitmap.SetEmpty();
@@ -414,7 +415,7 @@ bool ImplImageTree::doLoadImage(ImageRequestParameters& rParameters)
{
cacheBitmapToDisk(aVariant, rParameters);
}
getCurrentIconSet().maIconCache[rParameters.msName] = std::make_pair(rParameters.mbLocalized, rParameters.mrBitmap);
getIconCache(rParameters)[rParameters.msName] = std::make_pair(rParameters.mbLocalized, rParameters.mrBitmap);
}
return bFound;
@@ -482,9 +483,20 @@ void ImplImageTree::createStyle()
loadImageLinks();
}
/// Find an icon cache for the right scale factor
ImplImageTree::IconCache &ImplImageTree::getIconCache(ImageRequestParameters& rParameters)
{
IconSet &rSet = getCurrentIconSet();
auto it = rSet.maScaledIconCaches.find(rParameters.mnScalePercentage);
if ( it != rSet.maScaledIconCaches.end() )
return *it->second.get();
rSet.maScaledIconCaches[rParameters.mnScalePercentage] = std::unique_ptr<IconCache>(new IconCache);
return *rSet.maScaledIconCaches[rParameters.mnScalePercentage].get();
}
bool ImplImageTree::iconCacheLookup(ImageRequestParameters& rParameters)
{
IconCache& rIconCache = getCurrentIconSet().maIconCache;
IconCache& rIconCache = getIconCache(rParameters);
IconCache::iterator i(rIconCache.find(getRealImageName(rParameters.msName)));
if (i != rIconCache.end() && i->second.first == rParameters.mbLocalized)
@@ -513,6 +525,7 @@ bool ImplImageTree::findImage(std::vector<OUString> const & rPaths, ImageRequest
(void)ok; // prevent unused warning in release build
loadImageFromStream(wrapStream(aStream), rPath, rParameters);
return true;
}
}