Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
wsd: encapsulate watermark details
The watermark handler member has been fully encapsulated
inside ChildSession, so there is no need to have public
members or circular dependencies (ChildSession owns Watermark
instance, and Watermark takes ChildSession instance to
construct and initialize).

Minor refactoring and const-correctness improvements.

This is a non-functional patch.

Change-Id: I32525c47e35e96fc5314e107639be93ebc49a60e
Signed-off-by: Ashod Nakashian <ashod.nakashian@collabora.co.uk>
  • Loading branch information
Ashod authored and kendy committed Nov 11, 2020
1 parent b592128 commit 4185c5f
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 39 deletions.
23 changes: 19 additions & 4 deletions kit/ChildSession.hpp
Expand Up @@ -17,8 +17,8 @@
#include "Common.hpp"
#include "Kit.hpp"
#include "Session.hpp"
#include "Watermark.hpp"

class Watermark;
class ChildSession;

enum class LokEventTargetEnum
Expand Down Expand Up @@ -215,7 +215,20 @@ class ChildSession final : public Session

void loKitCallback(const int type, const std::string& payload);

std::shared_ptr<Watermark> _docWatermark;
/// Initializes the watermark support, if enabled and required.
/// Returns true if watermark is enabled and initialized.
bool initWatermark()
{
if (hasWatermark())
{
_docWatermark.reset(
new Watermark(getLOKitDocument(), getWatermarkText(), getWatermarkOpacity()));
}

return _docWatermark != nullptr;
}

const std::shared_ptr<Watermark>& watermark() const { return _docWatermark; };

bool sendTextFrame(const char* buffer, int length) override
{
Expand Down Expand Up @@ -297,12 +310,12 @@ class ChildSession final : public Session
virtual void disconnect() override;
virtual bool _handleInput(const char* buffer, int length) override;

std::shared_ptr<lok::Document> getLOKitDocument()
std::shared_ptr<lok::Document> getLOKitDocument() const
{
return _docManager->getLOKitDocument();
}

std::string getLOKitLastError()
std::string getLOKitLastError() const
{
char *lastErr = _docManager->getLOKit()->getError();
std::string ret;
Expand All @@ -326,6 +339,8 @@ class ChildSession final : public Session
const std::string _jailRoot;
DocumentManagerInterface* _docManager;

std::shared_ptr<Watermark> _docWatermark;

std::queue<std::chrono::steady_clock::time_point> _cursorInvalidatedEvent;
const unsigned _eventStorageIntervalMs = 15*1000;

Expand Down
31 changes: 14 additions & 17 deletions kit/Kit.cpp
Expand Up @@ -674,22 +674,20 @@ class Document final : public DocumentManagerInterface
_loKitDocument->setView(session->getViewId());
#endif

const auto blenderFunc = [&](unsigned char* data, int offsetX, int offsetY,
size_t pixmapWidth, size_t pixmapHeight, int pixelWidth,
int pixelHeight, LibreOfficeKitTileMode mode) {
if (session->watermark())
session->watermark()->blending(data, offsetX, offsetY, pixmapWidth, pixmapHeight,
pixelWidth, pixelHeight, mode);
};

const auto postMessageFunc = [&](const char* buffer, size_t length) {
postMessage(buffer, length, WSOpCode::Binary);
};

if (!RenderTiles::doRender(_loKitDocument, tileCombined, _pngCache, _pngPool, combined,
[&](unsigned char *data,
int offsetX, int offsetY,
size_t pixmapWidth, size_t pixmapHeight,
int pixelWidth, int pixelHeight,
LibreOfficeKitTileMode mode) {
if (session->hasWatermark())
session->_docWatermark->blending(data, offsetX, offsetY,
pixmapWidth, pixmapHeight,
pixelWidth, pixelHeight,
mode);
},
[&](const char *buffer, size_t length) {
postMessage(buffer, length, WSOpCode::Binary);
}
))
blenderFunc, postMessageFunc))
{
LOG_DBG("All tiles skipped, not producing empty tilecombine: message");
return;
Expand Down Expand Up @@ -1283,8 +1281,7 @@ class Document final : public DocumentManagerInterface
sessionId << "] loaded view [" << viewId << "]. Have " <<
viewCount << " view" << (viewCount != 1 ? "s." : "."));

if (session->hasWatermark())
session->_docWatermark.reset(new Watermark(_loKitDocument, session));
session->initWatermark();
session->recalcCanonicalViewId(_sessions);

return _loKitDocument;
Expand Down
35 changes: 17 additions & 18 deletions kit/Watermark.hpp
Expand Up @@ -15,24 +15,24 @@
#include <Log.hpp>
#include <cstdlib>
#include <string>
#include "ChildSession.hpp"

class Watermark
class Watermark final
{
public:
Watermark(const std::shared_ptr<lok::Document>& loKitDoc,
const std::shared_ptr<ChildSession> & session)
Watermark(const std::shared_ptr<lok::Document>& loKitDoc, const std::string& text,
double opacity)
: _loKitDoc(loKitDoc)
, _text(session->getWatermarkText())
, _text(text)
, _font("Carlito")
, _alphaLevel(opacity)
, _width(0)
, _height(0)
, _alphaLevel(session->getWatermarkOpacity())
{
}

~Watermark()
{
if (_loKitDoc == nullptr)
{
LOG_ERR("Watermark rendering requested without a valid document. Watermarking will be disabled.");
assert(_loKitDoc && "Valid loKitDoc is required for Watermark.");
}
}

void blending(unsigned char* tilePixmap,
Expand All @@ -42,8 +42,8 @@ class Watermark
LibreOfficeKitTileMode /*mode*/)
{
// set requested watermark size a little bit smaller than tile size
int width = tileWidth * 0.9;
int height = tileHeight * 0.9;
const int width = tileWidth * 0.9;
const int height = tileHeight * 0.9;

const std::vector<unsigned char>* pixmap = getPixmap(width, height);

Expand Down Expand Up @@ -106,9 +106,8 @@ class Watermark
_width = width;
_height = height;

if (!_loKitDoc)
if (_loKitDoc == nullptr)
{
LOG_ERR("Watermark rendering requested without a valid document.");
return nullptr;
}

Expand Down Expand Up @@ -177,12 +176,12 @@ class Watermark
}

private:
std::shared_ptr<lok::Document> _loKitDoc;
std::string _text;
std::string _font;
const std::shared_ptr<lok::Document> _loKitDoc;
const std::string _text;
const std::string _font;
const double _alphaLevel;
int _width;
int _height;
double _alphaLevel;
std::vector<unsigned char> _pixmap;
};

Expand Down

0 comments on commit 4185c5f

Please sign in to comment.