diff options
author | Tomaž Vajngerl <tomaz.vajngerl@collabora.co.uk> | 2020-12-21 21:47:39 +0900 |
---|---|---|
committer | Tomaž Vajngerl <quikee@gmail.com> | 2020-12-28 10:51:41 +0100 |
commit | bd51df8e4e267cdad369af402bb243cb3ed222f1 (patch) | |
tree | 2a254aaab40d43f3d0c483fdc54ea6dc2fa769e5 | |
parent | 4c6ee7ed3d97c3a36b071336a910159fae7820e3 (diff) |
vcl: add BinaryDataContainer and change GfxLink to use it
Add a wrapper for shared_ptr<sal_uInt8> called BinaryDataContainer,
which is used to easily manage the binary data - mainly for a
better reuse, control and to prevent duplication.
Change-Id: I68140ec379dba4a5ab1b624a334129bba2401998
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/108255
Tested-by: Jenkins
Reviewed-by: Tomaž Vajngerl <quikee@gmail.com>
-rw-r--r-- | include/vcl/BinaryDataContainer.hxx | 49 | ||||
-rw-r--r-- | include/vcl/gfxlink.hxx | 14 | ||||
-rw-r--r-- | vcl/Library_vcl.mk | 1 | ||||
-rw-r--r-- | vcl/source/gdi/gfxlink.cxx | 64 | ||||
-rw-r--r-- | vcl/source/graphic/BinaryDataContainer.cxx | 36 |
5 files changed, 120 insertions, 44 deletions
diff --git a/include/vcl/BinaryDataContainer.hxx b/include/vcl/BinaryDataContainer.hxx new file mode 100644 index 000000000000..5eb5f4486b9e --- /dev/null +++ b/include/vcl/BinaryDataContainer.hxx @@ -0,0 +1,49 @@ +/* -*- 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/. + * + */ + +#pragma once + +#include <vcl/dllapi.h> +#include <vector> +#include <memory> + +/** Container for the binary data, which responsiility is to manage the + * make it as simple as possible to manage the binary data. The binary + * data can be anything, but typically it is a in-memory data from + * files (i.e. files of graphic formats). + */ +class VCL_DLLPUBLIC BinaryDataContainer final +{ +private: + // the binary data + std::shared_ptr<std::vector<sal_uInt8>> mpData; + +public: + explicit BinaryDataContainer(); + explicit BinaryDataContainer(size_t nSize); + explicit BinaryDataContainer(const sal_uInt8* pData, size_t nSize); + + explicit BinaryDataContainer(const BinaryDataContainer& rBinaryDataContainer) = default; + explicit BinaryDataContainer(BinaryDataContainer&& rBinaryDataContainer) = default; + BinaryDataContainer& operator=(const BinaryDataContainer& rBinaryDataContainer) = default; + BinaryDataContainer& operator=(BinaryDataContainer&& rBinaryDataContainer) = default; + + size_t getSize() const { return mpData ? mpData->size() : 0; } + bool isEmpty() const { return mpData && mpData->empty(); } + const sal_uInt8* getData() const { return mpData ? mpData->data() : nullptr; } + + size_t calculateHash() const; + + auto cbegin() { return mpData->cbegin(); } + + auto cend() { return mpData->cend(); } +}; + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/include/vcl/gfxlink.hxx b/include/vcl/gfxlink.hxx index 72352f6902a1..85bb85cb53f2 100644 --- a/include/vcl/gfxlink.hxx +++ b/include/vcl/gfxlink.hxx @@ -23,6 +23,7 @@ #include <tools/gen.hxx> #include <vcl/dllapi.h> #include <vcl/mapmod.hxx> +#include <vcl/BinaryDataContainer.hxx> #include <memory> class SvStream; @@ -61,20 +62,17 @@ class VCL_DLLPUBLIC GfxLink private: GfxLinkType meType; sal_uInt32 mnUserId; - mutable std::shared_ptr<sal_uInt8> mpSwapInData; + BinaryDataContainer maDataContainer; mutable size_t maHash; - sal_uInt32 mnSwapInDataSize; MapMode maPrefMapMode; Size maPrefSize; bool mbPrefMapModeValid; bool mbPrefSizeValid; - SAL_DLLPRIVATE std::shared_ptr<sal_uInt8> GetSwapInData() const; public: - GfxLink(); - - // pBuff = The Graphic data. This class takes ownership of this - GfxLink( std::unique_ptr<sal_uInt8[]> pBuf, sal_uInt32 nBufSize, GfxLinkType nType ); + GfxLink(); + explicit GfxLink(std::unique_ptr<sal_uInt8[]> pBuf, sal_uInt32 nBufSize, GfxLinkType nType); + explicit GfxLink(BinaryDataContainer const & rDataConainer, GfxLinkType nType); bool operator==( const GfxLink& ) const; @@ -85,7 +83,7 @@ public: void SetUserId( sal_uInt32 nUserId ) { mnUserId = nUserId; } sal_uInt32 GetUserId() const { return mnUserId; } - sal_uInt32 GetDataSize() const { return mnSwapInDataSize;} + sal_uInt32 GetDataSize() const { return maDataContainer.getSize(); } const sal_uInt8* GetData() const; const Size& GetPrefSize() const { return maPrefSize;} diff --git a/vcl/Library_vcl.mk b/vcl/Library_vcl.mk index ecbf79d2cf4d..f258b0279a64 100644 --- a/vcl/Library_vcl.mk +++ b/vcl/Library_vcl.mk @@ -320,6 +320,7 @@ $(eval $(call gb_Library_add_exception_objects,vcl,\ vcl/source/pdf/XmpMetadata \ vcl/source/pdf/PDFiumLibrary \ vcl/source/pdf/ExternalPDFStreams \ + vcl/source/graphic/BinaryDataContainer \ vcl/source/graphic/GraphicID \ vcl/source/graphic/GraphicLoader \ vcl/source/graphic/GraphicObject \ diff --git a/vcl/source/gdi/gfxlink.cxx b/vcl/source/gdi/gfxlink.cxx index 83936c277ead..82b37fe4e668 100644 --- a/vcl/source/gdi/gfxlink.cxx +++ b/vcl/source/gdi/gfxlink.cxx @@ -30,60 +30,61 @@ GfxLink::GfxLink() : meType(GfxLinkType::NONE) , mnUserId(0) , maHash(0) - , mnSwapInDataSize(0) , mbPrefMapModeValid(false) , mbPrefSizeValid(false) { } - - GfxLink::GfxLink(std::unique_ptr<sal_uInt8[]> pBuf, sal_uInt32 nSize, GfxLinkType nType) : meType(nType) , mnUserId(0) - , mpSwapInData(std::shared_ptr<sal_uInt8>(pBuf.release(), pBuf.get_deleter())) // std::move(pBuf) does not compile on Jenkins MacOSX (24 May 2016) + , maDataContainer(pBuf.get(), nSize) + , maHash(0) + , mbPrefMapModeValid(false) + , mbPrefSizeValid(false) +{ +} + +GfxLink::GfxLink(BinaryDataContainer const & rDataConainer, GfxLinkType nType) + : meType(nType) + , mnUserId(0) + , maDataContainer(rDataConainer) , maHash(0) - , mnSwapInDataSize(nSize) , mbPrefMapModeValid(false) , mbPrefSizeValid(false) { - SAL_WARN_IF(mpSwapInData == nullptr || mnSwapInDataSize <= 0, "vcl", - "GfxLink::GfxLink(): empty/NULL buffer given"); } size_t GfxLink::GetHash() const { if (!maHash) { - std::size_t seed = 0; - boost::hash_combine(seed, mnSwapInDataSize); + std::size_t seed = maDataContainer.calculateHash(); boost::hash_combine(seed, meType); - const sal_uInt8* pData = GetData(); - if (pData) - seed += boost::hash_range(pData, pData + GetDataSize()); maHash = seed; - } return maHash; } bool GfxLink::operator==( const GfxLink& rGfxLink ) const { - if (GetHash() != rGfxLink.GetHash()) - return false; - - if ( mnSwapInDataSize != rGfxLink.mnSwapInDataSize || - meType != rGfxLink.meType ) + if (GetDataSize() != rGfxLink.GetDataSize() + || meType != rGfxLink.meType + || GetHash() != rGfxLink.GetHash()) return false; const sal_uInt8* pSource = GetData(); - const sal_uInt8* pDest = rGfxLink.GetData(); - if ( pSource == pDest ) + const sal_uInt8* pDestination = rGfxLink.GetData(); + + if (pSource == pDestination) return true; + sal_uInt32 nSourceSize = GetDataSize(); sal_uInt32 nDestSize = rGfxLink.GetDataSize(); - if ( pSource && pDest && ( nSourceSize == nDestSize ) ) - return (memcmp( pSource, pDest, nSourceSize ) == 0); + + if (pSource && pDestination && (nSourceSize == nDestSize)) + return memcmp(pSource, pDestination, nSourceSize) == 0; + return false; } @@ -92,37 +93,33 @@ bool GfxLink::IsNative() const return meType >= GfxLinkType::NativeFirst && meType <= GfxLinkType::NativeLast; } - const sal_uInt8* GfxLink::GetData() const { - return mpSwapInData.get(); + return maDataContainer.getData(); } - void GfxLink::SetPrefSize( const Size& rPrefSize ) { maPrefSize = rPrefSize; mbPrefSizeValid = true; } - void GfxLink::SetPrefMapMode( const MapMode& rPrefMapMode ) { maPrefMapMode = rPrefMapMode; mbPrefMapModeValid = true; } - bool GfxLink::LoadNative( Graphic& rGraphic ) { bool bRet = false; - if( IsNative() && mnSwapInDataSize ) + if (IsNative() && !maDataContainer.isEmpty()) { const sal_uInt8* pData = GetData(); if (pData) { - SvMemoryStream aMemoryStream(const_cast<sal_uInt8*>(pData), mnSwapInDataSize, StreamMode::READ | StreamMode::WRITE); + SvMemoryStream aMemoryStream(const_cast<sal_uInt8*>(pData), GetDataSize(), StreamMode::READ | StreamMode::WRITE); OUString aShortName; switch (meType) @@ -157,19 +154,14 @@ bool GfxLink::ExportNative( SvStream& rOStream ) const { if( GetDataSize() ) { - auto pData = GetSwapInData(); + auto pData = GetData(); if (pData) - rOStream.WriteBytes( pData.get(), mnSwapInDataSize ); + rOStream.WriteBytes(pData, GetDataSize()); } return ( rOStream.GetError() == ERRCODE_NONE ); } -std::shared_ptr<sal_uInt8> GfxLink::GetSwapInData() const -{ - return mpSwapInData; -} - bool GfxLink::IsEMF() const { const sal_uInt8* pGraphicAry = GetData(); diff --git a/vcl/source/graphic/BinaryDataContainer.cxx b/vcl/source/graphic/BinaryDataContainer.cxx new file mode 100644 index 000000000000..4c556195fe6a --- /dev/null +++ b/vcl/source/graphic/BinaryDataContainer.cxx @@ -0,0 +1,36 @@ +/* -*- 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/. + * + */ + +#include <vcl/BinaryDataContainer.hxx> +#include <boost/functional/hash.hpp> + +BinaryDataContainer::BinaryDataContainer() = default; + +BinaryDataContainer::BinaryDataContainer(size_t nSize) + : mpData(std::make_shared<std::vector<sal_uInt8>>(nSize)) +{ +} + +BinaryDataContainer::BinaryDataContainer(const sal_uInt8* pData, size_t nSize) + : mpData(std::make_shared<std::vector<sal_uInt8>>(nSize)) +{ + std::copy(pData, pData + nSize, mpData->data()); +} + +size_t BinaryDataContainer::calculateHash() const +{ + size_t nSeed = 0; + boost::hash_combine(nSeed, getSize()); + for (sal_uInt8 const& rByte : *mpData) + boost::hash_combine(nSeed, rByte); + return nSeed; +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |