[Libreoffice-commits] online.git: Branch 'distro/collabora/collabora-online-4' - 2 commits - kit/Kit.cpp wsd/TileDesc.hpp

Libreoffice Gerrit user logerrit at kemper.freedesktop.org
Wed Apr 24 12:39:58 UTC 2019


 kit/Kit.cpp      |  158 ++++++++++++++-----------------------------------------
 wsd/TileDesc.hpp |   32 +++++------
 2 files changed, 57 insertions(+), 133 deletions(-)

New commits:
commit 1b0c134051d8a88fd4704857ba5e3f2df47ca186
Author:     Michael Meeks <michael.meeks at collabora.com>
AuthorDate: Fri Apr 19 23:56:20 2019 +0100
Commit:     Ashod Nakashian <ashnakash at gmail.com>
CommitDate: Wed Apr 24 14:39:50 2019 +0200

    Inline PngCache lookup to avoid redundant watermark blending.
    
    Change-Id: I6fe5cca81b9268c903e011fe0e77f478befbc836
    Reviewed-on: https://gerrit.libreoffice.org/71130
    Reviewed-by: Ashod Nakashian <ashnakash at gmail.com>
    Tested-by: Ashod Nakashian <ashnakash at gmail.com>

diff --git a/kit/Kit.cpp b/kit/Kit.cpp
index 62f6068cb..8e85ffba7 100644
--- a/kit/Kit.cpp
+++ b/kit/Kit.cpp
@@ -506,9 +506,10 @@ class PngCache
         }
     }
 
+public:
     /// Lookup an entry in the cache and store the data in output.
     /// Returns true on success, otherwise false.
-    bool cacheTest(const uint64_t hash, std::vector<char>& output)
+    bool copyFromCache(const uint64_t hash, std::vector<char>& output)
     {
         if (hash)
         {
@@ -529,11 +530,11 @@ class PngCache
         return false;
     }
 
-    bool cacheEncodeSubBufferToPNG(unsigned char* pixmap, size_t startX, size_t startY,
-                                   int width, int height,
-                                   int bufferWidth, int bufferHeight,
-                                   std::vector<char>& output, LibreOfficeKitTileMode mode,
-                                   TileBinaryHash hash, TileWireId wid, TileWireId /*oldWid*/)
+    bool encodeSubBufferToPNG(unsigned char* pixmap, size_t startX, size_t startY,
+                              int width, int height,
+                              int bufferWidth, int bufferHeight,
+                              std::vector<char>& output, LibreOfficeKitTileMode mode,
+                              TileBinaryHash hash, TileWireId wid, TileWireId /*oldWid*/)
     {
         LOG_DBG("PNG cache with hash " << hash << " missed.");
 /*
@@ -568,7 +569,6 @@ class PngCache
             return false;
     }
 
-public:
     PngCache()
     {
         clearCache();
@@ -589,20 +589,6 @@ public:
         }
         return wid;
     }
-
-    bool encodeSubBufferToPNG(unsigned char* pixmap, size_t startX, size_t startY,
-                              int width, int height,
-                              int bufferWidth, int bufferHeight,
-                              std::vector<char>& output, LibreOfficeKitTileMode mode,
-                              TileBinaryHash hash, TileWireId wid, TileWireId oldWid)
-    {
-        if (cacheTest(hash, output))
-            return true;
-
-        return cacheEncodeSubBufferToPNG(pixmap, startX, startY, width, height,
-                                         bufferWidth, bufferHeight, output, mode,
-                                         hash, wid, oldWid);
-    }
 };
 
 class Watermark
@@ -1076,21 +1062,24 @@ public:
                 continue;
             }
 
-            if (_docWatermark)
-                _docWatermark->blending(pixmap.data(), offsetX, offsetY,
-                                        pixmapWidth, pixmapHeight,
-                                        pixelWidth, pixelHeight,
-                                        mode);
-
-            if (!_pngCache.encodeSubBufferToPNG(pixmap.data(), offsetX, offsetY,
-                                                pixelWidth, pixelHeight,
-                                                pixmapWidth, pixmapHeight, output, mode,
-                                                hash, wireId, oldWireId))
+            if (!_pngCache.copyFromCache(hash, output))
             {
-                //FIXME: Return error.
-                //sendTextFrame("error: cmd=tile kind=failure");
-                LOG_ERR("Failed to encode tile into PNG.");
-                return;
+                if (_docWatermark)
+                    _docWatermark->blending(pixmap.data(), offsetX, offsetY,
+                                            pixmapWidth, pixmapHeight,
+                                            pixelWidth, pixelHeight,
+                                            mode);
+
+                if (!_pngCache.encodeSubBufferToPNG(pixmap.data(), offsetX, offsetY,
+                                                    pixelWidth, pixelHeight,
+                                                    pixmapWidth, pixmapHeight, output, mode,
+                                                    hash, wireId, oldWireId))
+                {
+                    // FIXME: Return error.
+                    // sendTextFrame("error: cmd=tile kind=failure");
+                    LOG_ERR("Failed to encode tile into PNG.");
+                    return;
+                }
             }
 
             const size_t imgSize = output.size() - oldSize;
commit 8296221eae3f2320c6d18fbd3337f7afb5467027
Author:     Michael Meeks <michael.meeks at collabora.com>
AuthorDate: Fri Apr 19 22:48:41 2019 +0100
Commit:     Ashod Nakashian <ashnakash at gmail.com>
CommitDate: Wed Apr 24 14:39:40 2019 +0200

    Simplify legacy single tile rendering code-path.
    
    Remove redundant _id member from TileCombined, add constructor
    from TileDesc, and use it to shrink the code.
    
    Change-Id: Idc0ded63166ed350ab81b07e191b7a60d4407cd4
    Reviewed-on: https://gerrit.libreoffice.org/71129
    Reviewed-by: Ashod Nakashian <ashnakash at gmail.com>
    Tested-by: Ashod Nakashian <ashnakash at gmail.com>

diff --git a/kit/Kit.cpp b/kit/Kit.cpp
index 0609cb5b2..62f6068cb 100644
--- a/kit/Kit.cpp
+++ b/kit/Kit.cpp
@@ -590,18 +590,6 @@ public:
         return wid;
     }
 
-    bool encodeBufferToPNG(unsigned char* pixmap, int width, int height,
-                           std::vector<char>& output, LibreOfficeKitTileMode mode,
-                           TileBinaryHash hash, TileWireId wid, TileWireId oldWid)
-    {
-        if (cacheTest(hash, output))
-            return true;
-
-        return cacheEncodeSubBufferToPNG(pixmap, 0, 0, width, height,
-                                         width, height, output, mode,
-                                         hash, wid, oldWid);
-    }
-
     bool encodeSubBufferToPNG(unsigned char* pixmap, size_t startX, size_t startY,
                               int width, int height,
                               int bufferWidth, int bufferHeight,
@@ -986,82 +974,21 @@ public:
 
     void renderTile(const std::vector<std::string>& tokens)
     {
-        TileDesc tile = TileDesc::parse(tokens);
-
-        size_t pixmapDataSize = 4 * tile.getWidth() * tile.getHeight();
-        std::vector<unsigned char> pixmap;
-        pixmap.resize(pixmapDataSize);
-
-        std::unique_lock<std::mutex> lock(_documentMutex);
-        if (!_loKitDocument)
-        {
-            LOG_ERR("Tile rendering requested before loading document.");
-            return;
-        }
-
-        if (_loKitDocument->getViewsCount() <= 0)
-        {
-            LOG_ERR("Tile rendering requested without views.");
-            return;
-        }
-
-        const double area = tile.getWidth() * tile.getHeight();
-        Timestamp timestamp;
-        _loKitDocument->paintPartTile(pixmap.data(), tile.getPart(),
-                                      tile.getWidth(), tile.getHeight(),
-                                      tile.getTilePosX(), tile.getTilePosY(),
-                                      tile.getTileWidth(), tile.getTileHeight());
-        const Poco::Timestamp::TimeDiff elapsed = timestamp.elapsed();
-        LOG_TRC("paintTile at (" << tile.getPart() << ',' << tile.getTilePosX() << ',' << tile.getTilePosY() <<
-                ") " << "ver: " << tile.getVersion() << " rendered in " << (elapsed/1000.) <<
-                " ms (" << area / elapsed << " MP/s).");
-        const auto mode = static_cast<LibreOfficeKitTileMode>(_loKitDocument->getTileMode());
-
-        const TileBinaryHash hash = Png::hashBuffer(pixmap.data(), tile.getWidth(), tile.getHeight());
-        TileWireId wid = _pngCache.hashToWireId(hash);
-        TileWireId oldWireId = tile.getOldWireId();
-
-        tile.setWireId(wid);
-
-        if (hash != 0 && oldWireId == wid)
-        {
-            // The tile content is identical to what the client already has, so skip it
-            LOG_TRC("Match oldWireId==wid (" << wid << " for hash " << hash << "); unchanged");
-            return;
-        }
-
-        // Send back the request with all optional parameters given in the request.
-        std::string response = ADD_DEBUG_RENDERID(tile.serialize("tile:")) + "\n";
-
-        int pixelWidth = tile.getWidth();
-        int pixelHeight = tile.getHeight();
-
-        if (_docWatermark)
-            _docWatermark->blending(pixmap.data(), 0, 0, pixelWidth, pixelHeight, pixelWidth, pixelHeight, mode);
-
-        std::shared_ptr<std::vector<char>> output = std::make_shared<std::vector<char>>();
-        output->reserve(response.size() + pixmapDataSize);
-        output->resize(response.size());
-        std::memcpy(output->data(), response.data(), response.size());
-
-        if (!_pngCache.encodeBufferToPNG(pixmap.data(), tile.getWidth(), tile.getHeight(), *output, mode, hash, wid, oldWireId))
-        {
-            //FIXME: Return error.
-            //sendTextFrame("error: cmd=tile kind=failure");
-
-            LOG_ERR("Failed to encode tile into PNG.");
-            return;
-        }
-
-        LOG_TRC("Sending render-tile response (" << output->size() << " bytes) for: " << response);
-        postMessage(output, WSOpCode::Binary);
+        TileCombined tileCombined(TileDesc::parse(tokens));
+        renderTiles(tileCombined, false);
     }
 
     void renderCombinedTiles(const std::vector<std::string>& tokens)
     {
         TileCombined tileCombined = TileCombined::parse(tokens);
+        renderTiles(tileCombined, true);
+    }
+
+    void renderTiles(TileCombined &tileCombined, bool combined)
+    {
         auto& tiles = tileCombined.getTiles();
 
+        // Calculate the area we cover
         Util::Rectangle renderArea;
         std::vector<Util::Rectangle> tileRecs;
         tileRecs.reserve(tiles.size());
@@ -1103,6 +1030,7 @@ public:
             return;
         }
 
+        // Render the whole area
         const double area = pixmapWidth * pixmapHeight;
         Timestamp timestamp;
         LOG_TRC("Calling paintPartTile(" << (void*)pixmap.data() << ")");
@@ -1120,6 +1048,7 @@ public:
         std::vector<char> output;
         output.reserve(pixmapSize);
 
+        // Compress the area as tiles
         size_t tileIndex = 0;
         for (Util::Rectangle& tileRect : tileRecs)
         {
@@ -1154,7 +1083,8 @@ public:
                                         mode);
 
             if (!_pngCache.encodeSubBufferToPNG(pixmap.data(), offsetX, offsetY,
-                                                pixelWidth, pixelHeight, pixmapWidth, pixmapHeight, output, mode,
+                                                pixelWidth, pixelHeight,
+                                                pixmapWidth, pixmapHeight, output, mode,
                                                 hash, wireId, oldWireId))
             {
                 //FIXME: Return error.
@@ -1182,8 +1112,13 @@ public:
             return;
         }
 
-        const auto tileMsg = ADD_DEBUG_RENDERID(tileCombined.serialize("tilecombine:")) + "\n";
-        LOG_TRC("Sending back painted tiles for " << tileMsg);
+        std::string tileMsg;
+        if (combined)
+            tileMsg = ADD_DEBUG_RENDERID(tileCombined.serialize("tilecombine:")) + "\n";
+        else
+            tileMsg = ADD_DEBUG_RENDERID(tiles[0].serialize("tile:")) + "\n";
+
+        LOG_TRC("Sending back painted tiles for " << tileMsg << " of size " << output.size() << " bytes) for: " << tileMsg);
 
         std::shared_ptr<std::vector<char>> response = std::make_shared<std::vector<char>>();
         response->resize(tileMsg.size() + output.size());
diff --git a/wsd/TileDesc.hpp b/wsd/TileDesc.hpp
index fa945df14..e3c12938b 100644
--- a/wsd/TileDesc.hpp
+++ b/wsd/TileDesc.hpp
@@ -250,15 +250,14 @@ private:
     TileCombined(int part, int width, int height,
                  const std::string& tilePositionsX, const std::string& tilePositionsY,
                  int tileWidth, int tileHeight, const std::string& vers,
-                 const std::string& imgSizes, int id,
+                 const std::string& imgSizes,
                  const std::string& oldWireIds,
                  const std::string& wireIds) :
         _part(part),
         _width(width),
         _height(height),
         _tileWidth(tileWidth),
-        _tileHeight(tileHeight),
-        _id(id)
+        _tileHeight(tileHeight)
     {
         if (_part < 0 ||
             _width <= 0 ||
@@ -326,7 +325,7 @@ private:
                 throw BadArgumentException("Invalid tilecombine descriptor.");
             }
 
-            _tiles.emplace_back(_part, _width, _height, x, y, _tileWidth, _tileHeight, ver, imgSize, id, false);
+            _tiles.emplace_back(_part, _width, _height, x, y, _tileWidth, _tileHeight, ver, imgSize, -1, false);
             _tiles.back().setOldWireId(oldWireId);
             _tiles.back().setWireId(wireId);
         }
@@ -396,11 +395,6 @@ public:
         }
         oss.seekp(-1, std::ios_base::cur); // See beow.
 
-        if (_id >= 0)
-        {
-            oss << " id=" << _id;
-        }
-
         // Make sure we don't return a potential trailing comma that
         // we have seeked back over but not overwritten after all.
         return oss.str().substr(0, oss.tellp());
@@ -413,9 +407,6 @@ public:
         // assume all values to be int.
         std::map<std::string, int> pairs;
 
-        // Optional.
-        pairs["id"] = -1;
-
         std::string tilePositionsX;
         std::string tilePositionsY;
         std::string imgSizes;
@@ -467,8 +458,7 @@ public:
         return TileCombined(pairs["part"], pairs["width"], pairs["height"],
                             tilePositionsX, tilePositionsY,
                             pairs["tilewidth"], pairs["tileheight"],
-                            versions,
-                            imgSizes, pairs["id"], oldwireIds, wireIds);
+                            versions, imgSizes, oldwireIds, wireIds);
     }
 
     /// Deserialize a TileDesc from a string format.
@@ -499,7 +489,18 @@ public:
         vers.seekp(-1, std::ios_base::cur); // Remove last comma.
         return TileCombined(tiles[0].getPart(), tiles[0].getWidth(), tiles[0].getHeight(),
                             xs.str(), ys.str(), tiles[0].getTileWidth(), tiles[0].getTileHeight(),
-                            vers.str(), "", -1, oldhs.str(), hs.str());
+                            vers.str(), "", oldhs.str(), hs.str());
+    }
+
+    /// To support legacy / under-used renderTile
+    TileCombined(const TileDesc &desc)
+    {
+        _part = desc.getPart();
+        _width = desc.getWidth();
+        _height = desc.getHeight();
+        _tileWidth = desc.getTileWidth();
+        _tileHeight = desc.getTileHeight();
+        _tiles.push_back(desc);
     }
 
 private:
@@ -509,7 +510,6 @@ private:
     int _height;
     int _tileWidth;
     int _tileHeight;
-    int _id;
 };
 
 #endif


More information about the Libreoffice-commits mailing list