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
This commit is contained in:
parent
4959b3b21c
commit
2871653bf4
2 changed files with 35 additions and 100 deletions
103
kit/Kit.cpp
103
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());
|
||||
|
|
|
@ -258,15 +258,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 ||
|
||||
|
@ -334,7 +333,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);
|
||||
}
|
||||
|
@ -404,11 +403,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());
|
||||
|
@ -421,9 +415,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;
|
||||
|
@ -475,8 +466,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.
|
||||
|
@ -507,7 +497,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:
|
||||
|
@ -517,7 +518,6 @@ private:
|
|||
int _height;
|
||||
int _tileWidth;
|
||||
int _tileHeight;
|
||||
int _id;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
Loading…
Reference in a new issue