[Libreoffice-commits] core.git: Branch 'feature/drawinglayercore' - include/vcl vcl/source
Tomaž Vajngerl (via logerrit)
logerrit at kemper.freedesktop.org
Wed Dec 23 02:39:35 UTC 2020
include/vcl/BinaryDataContainer.hxx | 59 +++++++++++++++++++++++++++++++++
include/vcl/gfxlink.hxx | 14 +++----
vcl/source/gdi/gfxlink.cxx | 64 +++++++++++++++---------------------
3 files changed, 93 insertions(+), 44 deletions(-)
New commits:
commit 9c06701a2d588321348ad28dcd427a6f14912334
Author: Tomaž Vajngerl <tomaz.vajngerl at collabora.co.uk>
AuthorDate: Mon Dec 21 21:47:39 2020 +0900
Commit: Tomaž Vajngerl <tomaz.vajngerl at collabora.co.uk>
CommitDate: Wed Dec 23 11:38:42 2020 +0900
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
diff --git a/include/vcl/BinaryDataContainer.hxx b/include/vcl/BinaryDataContainer.hxx
new file mode 100644
index 000000000000..7ec9926b245c
--- /dev/null
+++ b/include/vcl/BinaryDataContainer.hxx
@@ -0,0 +1,59 @@
+/* -*- 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>
+#include <boost/functional/hash.hpp>
+
+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)
+ : mpData(std::make_shared<std::vector<sal_uInt8>>(nSize))
+ {
+ }
+
+ explicit BinaryDataContainer(const sal_uInt8* pData, size_t nSize)
+ : mpData(std::make_shared<std::vector<sal_uInt8>>(nSize))
+ {
+ std::copy(pData, pData + nSize, mpData->data());
+ }
+
+ 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() : true; }
+
+ const sal_uInt8* getData() const { return mpData ? mpData->data() : nullptr; }
+
+ size_t 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: */
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/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();
More information about the Libreoffice-commits
mailing list