[Libreoffice-commits] core.git: Branch 'feature/drawinglayercore' - 2 commits - include/vcl sfx2/source vcl/qa vcl/source

Tomaž Vajngerl (via logerrit) logerrit at kemper.freedesktop.org
Mon Dec 28 09:49:06 UTC 2020


 include/vcl/BinaryDataContainer.hxx        |    1 
 include/vcl/gfxlink.hxx                    |    5 ++++
 include/vcl/vectorgraphicdata.hxx          |    4 ---
 sfx2/source/appl/appmisc.cxx               |   10 +++-----
 vcl/qa/cppunit/BinaryDataContainerTest.cxx |   21 +++++++++++++++---
 vcl/source/filter/graphicfilter.cxx        |   13 +++++++----
 vcl/source/filter/ipdf/pdfread.cxx         |   33 ++++++++++++-----------------
 vcl/source/filter/wmf/wmf.cxx              |   14 +++++-------
 vcl/source/gdi/TypeSerializer.cxx          |    8 +++----
 vcl/source/gdi/impgraph.cxx                |   14 ++++++------
 vcl/source/gdi/vectorgraphicdata.cxx       |   21 ++----------------
 vcl/source/graphic/BinaryDataContainer.cxx |    5 ++++
 12 files changed, 75 insertions(+), 74 deletions(-)

New commits:
commit 976ba6fd659f3e4297795346dee5dc33db5708b3
Author:     Tomaž Vajngerl <tomaz.vajngerl at collabora.co.uk>
AuthorDate: Mon Dec 28 18:45:13 2020 +0900
Commit:     Tomaž Vajngerl <tomaz.vajngerl at collabora.co.uk>
CommitDate: Mon Dec 28 18:45:13 2020 +0900

    vcl: remove VectorGraphicDataArray, use BinaryDataContainer instead
    
    This change removes the contructor taking VectorGraphicDataArray
    for constructing VectorGraphicData, and instead of this, use the
    BinaryDataContainer instead. This further removes duplication and
    copying of the binary source data for various vector graphics
    formats (most importantly PDF and SVG).
    
    Change-Id: I1b9abf1662e1f30965c9068d04cb0afb82b3edaf

diff --git a/include/vcl/gfxlink.hxx b/include/vcl/gfxlink.hxx
index 85bb85cb53f2..1acb51d05c0a 100644
--- a/include/vcl/gfxlink.hxx
+++ b/include/vcl/gfxlink.hxx
@@ -86,6 +86,11 @@ public:
     sal_uInt32          GetDataSize() const { return maDataContainer.getSize(); }
     const sal_uInt8*    GetData() const;
 
+    const BinaryDataContainer& getDataContainer()
+    {
+        return maDataContainer;
+    }
+
     const Size&         GetPrefSize() const { return maPrefSize;}
     void                SetPrefSize( const Size& rPrefSize );
     bool                IsPrefSizeValid() const { return mbPrefSizeValid;}
diff --git a/include/vcl/vectorgraphicdata.hxx b/include/vcl/vectorgraphicdata.hxx
index 8d55a9487d26..02511dd7909c 100644
--- a/include/vcl/vectorgraphicdata.hxx
+++ b/include/vcl/vectorgraphicdata.hxx
@@ -86,10 +86,6 @@ private:
     VectorGraphicData& operator=(const VectorGraphicData&) = delete;
 
 public:
-    VectorGraphicData(
-        const VectorGraphicDataArray& rVectorGraphicDataArray,
-        VectorGraphicDataType eVectorDataType,
-        sal_Int32 nPageIndex = -1);
     VectorGraphicData(
         const BinaryDataContainer& rDataContainer,
         VectorGraphicDataType eVectorDataType,
diff --git a/sfx2/source/appl/appmisc.cxx b/sfx2/source/appl/appmisc.cxx
index c62fcd47a1e5..52906069e483 100644
--- a/sfx2/source/appl/appmisc.cxx
+++ b/sfx2/source/appl/appmisc.cxx
@@ -164,15 +164,13 @@ bool SfxApplication::loadBrandSvg(const char *pName, BitmapEx &rBitmap, int nWid
     if ( !FileExists(aObj) )
         return false;
 
-    std::vector<sal_uInt8> aBinaryData;
+    auto aBinaryData = std::make_unique<std::vector<sal_uInt8>>();
     OUString aPath = aObj.PathToFileName();
-    if (!loadDataFromFile(aPath, aBinaryData))
+    if (!loadDataFromFile(aPath, *aBinaryData))
         return false;
 
-    VectorGraphicDataArray aVectorGraphicDataArray;
-    std::copy(aBinaryData.cbegin(), aBinaryData.cend(), aVectorGraphicDataArray.begin());
-
-    VectorGraphicData aVectorGraphicData(aVectorGraphicDataArray, VectorGraphicDataType::Svg);
+    BinaryDataContainer aDataContainer(aBinaryData);
+    VectorGraphicData aVectorGraphicData(aDataContainer, VectorGraphicDataType::Svg);
 
     // transform into [0,0,width,width*aspect] std dimensions
 
diff --git a/vcl/source/filter/graphicfilter.cxx b/vcl/source/filter/graphicfilter.cxx
index 65f3b8bb450f..35b060409d42 100644
--- a/vcl/source/filter/graphicfilter.cxx
+++ b/vcl/source/filter/graphicfilter.cxx
@@ -1620,7 +1620,8 @@ ErrCode GraphicFilter::ImportGraphic( Graphic& rGraphic, const OUString& rPath,
 
                         if(!aMemStream.GetError() )
                         {
-                            auto aVectorGraphicDataPtr = std::make_shared<VectorGraphicData>(aNewData, VectorGraphicDataType::Svg);
+                            BinaryDataContainer aDataContainer(reinterpret_cast<const sal_uInt8*>(aNewData.getConstArray()), aNewData.getLength());
+                            auto aVectorGraphicDataPtr = std::make_shared<VectorGraphicData>(aDataContainer, VectorGraphicDataType::Svg);
                             rGraphic = Graphic(aVectorGraphicDataPtr);
                             bOkay = true;
                         }
@@ -1633,7 +1634,8 @@ ErrCode GraphicFilter::ImportGraphic( Graphic& rGraphic, const OUString& rPath,
 
                     if(!rIStream.GetError())
                     {
-                        auto aVectorGraphicDataPtr = std::make_shared<VectorGraphicData>(aNewData, VectorGraphicDataType::Svg);
+                        BinaryDataContainer aDataContainer(reinterpret_cast<const sal_uInt8*>(aNewData.getConstArray()), aNewData.getLength());
+                        auto aVectorGraphicDataPtr = std::make_shared<VectorGraphicData>(aDataContainer, VectorGraphicDataType::Svg);
                         rGraphic = Graphic(aVectorGraphicDataPtr);
                         bOkay = true;
                     }
@@ -1707,10 +1709,11 @@ ErrCode GraphicFilter::ImportGraphic( Graphic& rGraphic, const OUString& rPath,
             {
                 const bool bIsWmf(aFilterName.equalsIgnoreAsciiCase(IMP_WMF));
                 const VectorGraphicDataType aDataType(bIsWmf ? VectorGraphicDataType::Wmf : VectorGraphicDataType::Emf);
+
+                BinaryDataContainer aDataContainer(reinterpret_cast<const sal_uInt8*>(aNewData.getConstArray()), aNewData.getLength());
+
                 auto aVectorGraphicDataPtr =
-                    std::make_shared<VectorGraphicData>(
-                        aNewData,
-                        aDataType);
+                    std::make_shared<VectorGraphicData>(aDataContainer, aDataType);
 
                 if (pExtHeader)
                 {
diff --git a/vcl/source/filter/ipdf/pdfread.cxx b/vcl/source/filter/ipdf/pdfread.cxx
index 2c2c8526e779..1b64f1ed398c 100644
--- a/vcl/source/filter/ipdf/pdfread.cxx
+++ b/vcl/source/filter/ipdf/pdfread.cxx
@@ -107,23 +107,23 @@ bool getCompatibleStream(SvStream& rInStream, SvStream& rOutStream)
 }
 #endif // HAVE_FEATURE_PDFIUM
 
-VectorGraphicDataArray createVectorGraphicDataArray(SvStream& rStream)
+BinaryDataContainer createBinaryDataContainer(SvStream& rStream)
 {
     // Save the original PDF stream for later use.
     SvMemoryStream aMemoryStream;
     if (!getCompatibleStream(rStream, aMemoryStream))
-        return VectorGraphicDataArray();
+        return BinaryDataContainer();
 
     const sal_uInt32 nStreamLength = aMemoryStream.TellEnd();
 
-    VectorGraphicDataArray aPdfData(nStreamLength);
+    auto aPdfData = std::make_unique<std::vector<sal_uInt8>>(nStreamLength);
 
     aMemoryStream.Seek(STREAM_SEEK_TO_BEGIN);
-    aMemoryStream.ReadBytes(aPdfData.begin(), nStreamLength);
+    aMemoryStream.ReadBytes(aPdfData->data(), aPdfData->size());
     if (aMemoryStream.GetError())
-        return VectorGraphicDataArray();
+        return BinaryDataContainer();
 
-    return aPdfData;
+    return BinaryDataContainer(aPdfData);
 }
 
 } // end anonymous namespace
@@ -233,15 +233,15 @@ size_t RenderPDFBitmaps(const void* pBuffer, int nSize, std::vector<BitmapEx>& r
 bool importPdfVectorGraphicData(SvStream& rStream,
                                 std::shared_ptr<VectorGraphicData>& rVectorGraphicData)
 {
-    VectorGraphicDataArray aPdfDataArray = createVectorGraphicDataArray(rStream);
-    if (!aPdfDataArray.hasElements())
+    BinaryDataContainer aDataContainer = createBinaryDataContainer(rStream);
+    if (aDataContainer.isEmpty())
     {
         SAL_WARN("vcl.filter", "ImportPDF: empty PDF data array");
         return false;
     }
 
     rVectorGraphicData
-        = std::make_shared<VectorGraphicData>(aPdfDataArray, VectorGraphicDataType::Pdf);
+        = std::make_shared<VectorGraphicData>(aDataContainer, VectorGraphicDataType::Pdf);
 
     return true;
 }
@@ -442,17 +442,12 @@ size_t ImportPDFUnloaded(const OUString& rURL, std::vector<PDFGraphicResult>& rG
         ::utl::UcbStreamHelper::CreateStream(rURL, StreamMode::READ | StreamMode::SHARE_DENYNONE));
 
     // Save the original PDF stream for later use.
-    BinaryDataContainer aBinaryDataContainer;
-    {
-        VectorGraphicDataArray aPdfDataArray = createVectorGraphicDataArray(*xStream);
-        if (!aPdfDataArray.hasElements())
-            return 0;
-        const sal_uInt8* pData = reinterpret_cast<const sal_uInt8*>(aPdfDataArray.getConstArray());
-        aBinaryDataContainer = BinaryDataContainer(pData, aPdfDataArray.getLength());
-    }
+    BinaryDataContainer aDataContainer = createBinaryDataContainer(*xStream);
+    if (aDataContainer.isEmpty())
+        return 0;
 
     // Prepare the link with the PDF stream.
-    auto pGfxLink = std::make_shared<GfxLink>(aBinaryDataContainer, GfxLinkType::NativePdf);
+    auto pGfxLink = std::make_shared<GfxLink>(aDataContainer, GfxLinkType::NativePdf);
 
     auto pPdfium = vcl::pdf::PDFiumLibrary::get();
 
@@ -480,7 +475,7 @@ size_t ImportPDFUnloaded(const OUString& rURL, std::vector<PDFGraphicResult>& rG
         tools::Long nPageHeight = convertTwipToMm100(aPageSize.getY() * pointToTwipconversionRatio);
 
         auto aVectorGraphicDataPtr = std::make_shared<VectorGraphicData>(
-            aBinaryDataContainer, VectorGraphicDataType::Pdf, nPageIndex);
+            aDataContainer, VectorGraphicDataType::Pdf, nPageIndex);
 
         // Create the Graphic with the VectorGraphicDataPtr and link the original PDF stream.
         // We swap out this Graphic as soon as possible, and a later swap in
diff --git a/vcl/source/filter/wmf/wmf.cxx b/vcl/source/filter/wmf/wmf.cxx
index 70ff37f7473f..2394c66d217d 100644
--- a/vcl/source/filter/wmf/wmf.cxx
+++ b/vcl/source/filter/wmf/wmf.cxx
@@ -42,8 +42,9 @@ bool ReadWindowMetafile( SvStream& rStream, GDIMetaFile& rMTF )
 
     // Read binary data to mem array
     const sal_uInt32 nStreamLength(nStreamEnd - nStreamStart);
-    VectorGraphicDataArray aNewData(nStreamLength);
-    rStream.ReadBytes(aNewData.begin(), nStreamLength);
+    auto rData = std::make_unique<std::vector<sal_uInt8>>(nStreamLength);
+    rStream.ReadBytes(rData->data(), rData->size());
+    BinaryDataContainer aDataContainer(rData);
     rStream.Seek(nStreamStart);
 
     if (rStream.good())
@@ -52,9 +53,7 @@ bool ReadWindowMetafile( SvStream& rStream, GDIMetaFile& rMTF )
         // too much for type, this will be checked there. Also no path
         // needed, it is a temporary object
         auto aVectorGraphicDataPtr =
-            std::make_shared<VectorGraphicData>(
-                aNewData,
-                VectorGraphicDataType::Emf);
+            std::make_shared<VectorGraphicData>(aDataContainer, VectorGraphicDataType::Emf);
 
         // create a Graphic and grep Metafile from it
         const Graphic aGraphic(aVectorGraphicDataPtr);
@@ -93,10 +92,9 @@ bool ConvertGraphicToWMF(const Graphic& rGraphic, SvStream& rTargetStream,
     {
         // This may be an EMF+ file, converting that to WMF is better done by re-parsing EMF+ as EMF
         // and converting that to WMF.
-        uno::Sequence<sal_Int8> aData(reinterpret_cast<const sal_Int8*>(aLink.GetData()),
-                                      aLink.GetDataSize());
+        auto & rDataContainer = aLink.getDataContainer();
         auto aVectorGraphicData
-            = std::make_shared<VectorGraphicData>(aData, VectorGraphicDataType::Emf);
+            = std::make_shared<VectorGraphicData>(rDataContainer, VectorGraphicDataType::Emf);
         aVectorGraphicData->setEnableEMFPlus(false);
         Graphic aGraphic(aVectorGraphicData);
         bool bRet = ConvertGDIMetaFileToWMF(aGraphic.GetGDIMetaFile(), rTargetStream, pConfigItem,
diff --git a/vcl/source/gdi/TypeSerializer.cxx b/vcl/source/gdi/TypeSerializer.cxx
index b1cdf4790c01..c1525786fc56 100644
--- a/vcl/source/gdi/TypeSerializer.cxx
+++ b/vcl/source/gdi/TypeSerializer.cxx
@@ -278,9 +278,9 @@ void TypeSerializer::readGraphic(Graphic& rGraphic)
 
                     if (nLength)
                     {
-                        VectorGraphicDataArray aData(nLength);
-
-                        mrStream.ReadBytes(aData.getArray(), nLength);
+                        auto rData = std::make_unique<std::vector<sal_uInt8>>(nLength);
+                        mrStream.ReadBytes(rData->data(), rData->size());
+                        BinaryDataContainer aDataContainer(rData);
 
                         if (!mrStream.GetError())
                         {
@@ -300,7 +300,7 @@ void TypeSerializer::readGraphic(Graphic& rGraphic)
                             }
 
                             auto aVectorGraphicDataPtr
-                                = std::make_shared<VectorGraphicData>(aData, aDataType);
+                                = std::make_shared<VectorGraphicData>(aDataContainer, aDataType);
                             rGraphic = Graphic(aVectorGraphicDataPtr);
                         }
                     }
diff --git a/vcl/source/gdi/impgraph.cxx b/vcl/source/gdi/impgraph.cxx
index aeb471852ec8..83ae4c849b24 100644
--- a/vcl/source/gdi/impgraph.cxx
+++ b/vcl/source/gdi/impgraph.cxx
@@ -1546,14 +1546,14 @@ bool ImpGraphic::swapInGraphic(SvStream& rStream)
 
                 if (constSvgMagic == nMagic || constWmfMagic == nMagic || constEmfMagic == nMagic || constPdfMagic == nMagic)
                 {
-                    sal_uInt32 nVectorGraphicDataArrayLength(0);
-                    rStream.ReadUInt32(nVectorGraphicDataArrayLength);
+                    sal_uInt32 nVectorGraphicDataSize(0);
+                    rStream.ReadUInt32(nVectorGraphicDataSize);
 
-                    if (nVectorGraphicDataArrayLength)
+                    if (nVectorGraphicDataSize)
                     {
-                        VectorGraphicDataArray aNewData(nVectorGraphicDataArrayLength);
-
-                        rStream.ReadBytes(aNewData.getArray(), nVectorGraphicDataArrayLength);
+                        auto rData = std::make_unique<std::vector<sal_uInt8>>(nVectorGraphicDataSize);
+                        rStream.ReadBytes(rData->data(), nVectorGraphicDataSize);
+                        BinaryDataContainer aDataContainer(rData);
 
                         if (rStream.GetError())
                             return false;
@@ -1578,7 +1578,7 @@ bool ImpGraphic::swapInGraphic(SvStream& rStream)
                                 return false;
                         }
 
-                        auto aVectorGraphicDataPtr = std::make_shared<VectorGraphicData>(aNewData, aDataType);
+                        auto aVectorGraphicDataPtr = std::make_shared<VectorGraphicData>(aDataContainer, aDataType);
 
                         if (!rStream.GetError())
                         {
diff --git a/vcl/source/gdi/vectorgraphicdata.cxx b/vcl/source/gdi/vectorgraphicdata.cxx
index 609040f44129..7f11dc454753 100644
--- a/vcl/source/gdi/vectorgraphicdata.cxx
+++ b/vcl/source/gdi/vectorgraphicdata.cxx
@@ -252,9 +252,9 @@ void VectorGraphicData::ensureSequenceAndRange()
                 {"PageIndex", uno::makeAny<sal_Int32>(mnPageIndex)},
             });
             // TODO: change xPdfDecomposer to use BinaryDataContainer directly
-            VectorGraphicDataArray aVectorGraphicDataArray(maDataContainer.getSize());
-            std::copy(maDataContainer.cbegin(), maDataContainer.cend(), aVectorGraphicDataArray.begin());
-            auto xPrimitive2D = xPdfDecomposer->getDecomposition(aVectorGraphicDataArray, aDecompositionParameters);
+            css::uno::Sequence<sal_Int8> aDataSequence(maDataContainer.getSize());
+            std::copy(maDataContainer.cbegin(), maDataContainer.cend(), aDataSequence.begin());
+            auto xPrimitive2D = xPdfDecomposer->getDecomposition(aDataSequence, aDecompositionParameters);
             maSequence = comphelper::sequenceToContainer<std::deque<uno::Reference<graphic::XPrimitive2D>>>(xPrimitive2D);
 
             break;
@@ -301,21 +301,6 @@ std::pair<VectorGraphicData::State, size_t> VectorGraphicData::getSizeBytes() co
     }
 }
 
-VectorGraphicData::VectorGraphicData(
-    const VectorGraphicDataArray& rVectorGraphicDataArray,
-    VectorGraphicDataType eVectorDataType,
-    sal_Int32 nPageIndex)
-:   maDataContainer(reinterpret_cast<const sal_uInt8*>(rVectorGraphicDataArray.begin()), rVectorGraphicDataArray.getLength()),
-    mbSequenceCreated(false),
-    maRange(),
-    maSequence(),
-    maReplacement(),
-    mNestedBitmapSize(0),
-    meVectorGraphicDataType(eVectorDataType),
-    mnPageIndex(nPageIndex)
-{
-}
-
 VectorGraphicData::VectorGraphicData(
     const BinaryDataContainer& rDataContainer,
     VectorGraphicDataType eVectorDataType,
commit 36ba103f6592d5c3b840a7919ab0cc0e6a2df8e1
Author:     Tomaž Vajngerl <tomaz.vajngerl at collabora.co.uk>
AuthorDate: Mon Dec 28 18:34:39 2020 +0900
Commit:     Tomaž Vajngerl <tomaz.vajngerl at collabora.co.uk>
CommitDate: Mon Dec 28 18:34:39 2020 +0900

    vcl: a way to create data for BinaryDataContainer without copying
    
    Construstor for BinaryDataContainer taking unique_ptr<sal_uInt8>
    represents a way how to create and fill the data for the container
    and make sure that the container is the only one that is managing
    the data after construction.
    Generally we don't want to give the access to the internal
    shared_ptr to the outside, so with the unique_ptr we make sure
    that there is no outside references present (which would be the
    case if the we would take shared_ptr as the constructor) and the
    unique_ptr is cleared (moved) after construction.
    
    Change-Id: I123114c8bee92e342740d94a784b2c16e2564528

diff --git a/include/vcl/BinaryDataContainer.hxx b/include/vcl/BinaryDataContainer.hxx
index da0607cdfc97..48a4f201ec0d 100644
--- a/include/vcl/BinaryDataContainer.hxx
+++ b/include/vcl/BinaryDataContainer.hxx
@@ -28,6 +28,7 @@ private:
 public:
     explicit BinaryDataContainer();
     explicit BinaryDataContainer(const sal_uInt8* pData, size_t nSize);
+    explicit BinaryDataContainer(std::unique_ptr<std::vector<sal_uInt8>>& rData);
 
     BinaryDataContainer(const BinaryDataContainer& rBinaryDataContainer)
         : mpData(rBinaryDataContainer.mpData)
diff --git a/vcl/qa/cppunit/BinaryDataContainerTest.cxx b/vcl/qa/cppunit/BinaryDataContainerTest.cxx
index 597e7d4bc3aa..3f965e8bd3fd 100644
--- a/vcl/qa/cppunit/BinaryDataContainerTest.cxx
+++ b/vcl/qa/cppunit/BinaryDataContainerTest.cxx
@@ -19,14 +19,14 @@ namespace
 {
 class BinaryDataContainerTest : public CppUnit::TestFixture
 {
-    void test();
+    void testConstruct();
 
     CPPUNIT_TEST_SUITE(BinaryDataContainerTest);
-    CPPUNIT_TEST(test);
+    CPPUNIT_TEST(testConstruct);
     CPPUNIT_TEST_SUITE_END();
 };
 
-void BinaryDataContainerTest::test()
+void BinaryDataContainerTest::testConstruct()
 {
     {
         BinaryDataContainer aContainer;
@@ -54,6 +54,21 @@ void BinaryDataContainerTest::test()
         CPPUNIT_ASSERT_EQUAL(true, bool(aCopyOfContainer.isEmpty()));
         CPPUNIT_ASSERT_EQUAL(size_t(0), aCopyOfContainer.getSize());
     }
+    {
+        // construct a unique_ptr data array
+        std::vector<sal_uInt8> aTestByteArray = { 1, 2, 3, 4 };
+        auto aConstructionByteArray = std::make_unique<std::vector<sal_uInt8>>(aTestByteArray);
+
+        // remember for later to compare
+        const sal_uInt8* pInternal = aConstructionByteArray->data();
+
+        BinaryDataContainer aContainer(aConstructionByteArray);
+
+        // make sure the unique_ptr was moved into BinaryDataContainer
+        CPPUNIT_ASSERT_EQUAL(false, bool(aConstructionByteArray));
+        // make sure we didn't copy data into BinaryDataContainer (pointers match)
+        CPPUNIT_ASSERT_EQUAL(pInternal, aContainer.getData());
+    }
 }
 
 } // namespace
diff --git a/vcl/source/graphic/BinaryDataContainer.cxx b/vcl/source/graphic/BinaryDataContainer.cxx
index 7576852215b1..4490fb78339b 100644
--- a/vcl/source/graphic/BinaryDataContainer.cxx
+++ b/vcl/source/graphic/BinaryDataContainer.cxx
@@ -19,6 +19,11 @@ BinaryDataContainer::BinaryDataContainer(const sal_uInt8* pData, size_t nSize)
     std::copy(pData, pData + nSize, mpData->data());
 }
 
+BinaryDataContainer::BinaryDataContainer(std::unique_ptr<std::vector<sal_uInt8>>& rData)
+    : mpData(std::move(rData))
+{
+}
+
 size_t BinaryDataContainer::calculateHash() const
 {
     size_t nSeed = 0;


More information about the Libreoffice-commits mailing list