LOK: tilebench improvements

* Arguments for max number of parts and tiles to render (optional).
  + Automatic estimation of maximum tiles to render based on max parts
    for Writer docs, since there is only 1 part, this caps the number
    of pages to render, similar to other doc types.
* Fixed rendering of Writer documents over and over (as many times as pages).
  + Writer has a single part, unlike other doc types.
  + No point in rendering the whole document in writer to a single tile,
    since that's completely unrealistic and impractical (it takes
    forever for large docs and artificially spikes the memory).
* Rendering starts at the current part and not the first.
  + This gives the spreadsheet of interest priority (if saved as visible).
* The tile size is now more realistic as we use the same dimensions
  as the Online client does.
* When rendering tiles at scale, we use the same dimensions as the
  Online client rather than splitting the width by 4, for realism.
* Rendering of tiles is done rows-first, rather than columns-first,
  which is similar to what the Online client does, which is more
  cache friendly, therefore more realistic.
* Enabled compiling of tilebench when GTK3 is disabled, which
  was erroneous, since tilebench doesn't have any dependency on GTK.
  + Now it's possible to compile with local Cairo/Pixman libs.

Reviewed-on: https://gerrit.libreoffice.org/44936
Reviewed-by: Jan Holesovsky <kendy@collabora.com>
Tested-by: Jan Holesovsky <kendy@collabora.com>
(cherry picked from commit 7db98521548de9eab955ee25a5aacaaef42df323)

Change-Id: I6ad2e97f39572778dd7d0c12d14550841c1d6963
Reviewed-on: https://gerrit.libreoffice.org/46984
Tested-by: Jenkins <ci@libreoffice.org>
Reviewed-by: Ashod Nakashian <ashnakash@gmail.com>
diff --git a/Repository.mk b/Repository.mk
index 29a6dbc..78ef396 100644
--- a/Repository.mk
+++ b/Repository.mk
@@ -70,7 +70,7 @@ $(eval $(call gb_Helper_register_executables,NONE, \
        svptest \
        svpclient \
        pixelctl ) \
	$(if $(and $(ENABLE_GTK3), $(filter LINUX %BSD SOLARIS,$(OS))), tilebench) \
	$(if $(filter LINUX %BSD SOLARIS,$(OS)), tilebench) \
	$(if $(filter LINUX MACOSX SOLARIS WNT %BSD,$(OS)),icontest \
	    outdevgrind) \
	vcldemo \
diff --git a/libreofficekit/Executable_tilebench.mk b/libreofficekit/Executable_tilebench.mk
index 4da51d3..9d8b2a1 100644
--- a/libreofficekit/Executable_tilebench.mk
+++ b/libreofficekit/Executable_tilebench.mk
@@ -15,7 +15,6 @@ $(eval $(call gb_Executable_set_include,tilebench,\
))

$(eval $(call gb_Executable_use_libraries,tilebench,\
    libreofficekitgtk \
	sal \
))

diff --git a/libreofficekit/Module_libreofficekit.mk b/libreofficekit/Module_libreofficekit.mk
index c74c068..2d7519c 100644
--- a/libreofficekit/Module_libreofficekit.mk
+++ b/libreofficekit/Module_libreofficekit.mk
@@ -24,10 +24,13 @@ ifneq ($(ENABLE_GTK3),)
$(eval $(call gb_Module_add_targets,libreofficekit,\
    Library_libreofficekitgtk \
    Executable_gtktiledviewer \
))
endif # ($(ENABLE_GTK3),)

$(eval $(call gb_Module_add_targets,libreofficekit,\
    Executable_tilebench \
    Package_selectionhandles \
))
endif # ($(ENABLE_GTK3),)

endif

diff --git a/libreofficekit/qa/tilebench/tilebench.cxx b/libreofficekit/qa/tilebench/tilebench.cxx
index 6865232..ed8d44c 100644
--- a/libreofficekit/qa/tilebench/tilebench.cxx
+++ b/libreofficekit/qa/tilebench/tilebench.cxx
@@ -10,10 +10,12 @@
#include <assert.h>
#include <stdio.h>
#include <string.h>
#include <cmath>

#include <vector>
#include <osl/time.h>

#include <LibreOfficeKit/LibreOfficeKitEnums.h>
#include <LibreOfficeKit/LibreOfficeKitInit.h>
#include <LibreOfficeKit/LibreOfficeKit.hxx>

@@ -21,7 +23,7 @@ using namespace lok;

static int help()
{
    fprintf( stderr, "Usage: tilebench <absolute-path-to-libreoffice-install> [path to document]\n" );
    fprintf( stderr, "Usage: tilebench <absolute-path-to-libreoffice-install> [path to document] [max parts|-1] [max tiles|-1]\n" );
    fprintf( stderr, "renders a selection of small tiles from the document, checksums them and times the process\n" );
    return 1;
}
@@ -36,13 +38,17 @@ static double getTimeNow()

int main( int argc, char* argv[] )
{
    static const double origin = getTimeNow();
    struct TimeRecord {
        const char *mpName;
        double mfTime;

        TimeRecord() : mpName(nullptr), mfTime(getTimeNow()) { }
        explicit TimeRecord(const char *pName) :
                       mpName(pName ), mfTime(getTimeNow()) { }
                       mpName(pName), mfTime(getTimeNow())
        {
            fprintf(stderr, "%3.3fs - %s\n", (mfTime - origin), mpName);
        }
    };
    std::vector< TimeRecord > aTimes;
    if( argc < 2 ||
@@ -55,11 +61,20 @@ int main( int argc, char* argv[] )
        return 1;
    }

    // Use realistic dimensions, similar to the Online client.
    long nTilePixelWidth = 512;
    long nTilePixelHeight = 512;
    long nTileTwipWidth = 3840;
    long nTileTwipHeight = 3840;

    aTimes.emplace_back("initialization");
    // coverity[tainted_string] - build time test tool
    Office *pOffice = lok_cpp_init(argv[1]);
    aTimes.emplace_back();

    const int max_parts = (argc > 3 ? atoi(argv[3]) : -1);
    const int max_tiles = (argc > 4 ? atoi(argv[4]) : -1);

    if (argv[2] != nullptr)
    {
        aTimes.emplace_back("load document");
@@ -67,12 +82,17 @@ int main( int argc, char* argv[] )
        aTimes.emplace_back();

        aTimes.emplace_back("getparts");
        int nParts = pDocument->getParts();
        const int nOriginalPart = pDocument->getPart();
        // Writer really has 1 part (the full doc).
        const int nTotalParts = (pDocument->getDocumentType() == LOK_DOCTYPE_TEXT ? 1 : pDocument->getParts());
        const int nParts = (max_parts < 0 ? nTotalParts : std::min(max_parts, nTotalParts));
        fprintf(stderr, "Parts to render: %d, Total Parts: %d, Max parts: %d, Max tiles: %d\n", nParts, nTotalParts, max_parts, max_tiles);
        aTimes.emplace_back();

        aTimes.emplace_back("get size of parts");
        for (int nPart = 0; nPart < nParts; nPart++)
        for (int n = 0; n < nParts; ++n)
        {
            const int nPart = (nOriginalPart + n) % nTotalParts;
            char* pName = pDocument->getPartName(nPart);
            pDocument->setPart(nPart);
            long nWidth = 0, nHeight = 0;
@@ -82,40 +102,49 @@ int main( int argc, char* argv[] )
        }
        aTimes.emplace_back();

        unsigned char pPixels[256*256*4];
        for (int nPart = 0; nPart < nParts; nPart++)
        std::vector<unsigned char> vBuffer(nTilePixelWidth * nTilePixelHeight * 4);
        unsigned char* pPixels = &vBuffer[0];

        for (int n = 0; n < nParts; ++n)
        {
            {
                char* pName = pDocument->getPartName(nPart);
                fprintf (stderr, "render '%s'\n", pName);
                free (pName);
            }
            const int nPart = (nOriginalPart + n) % nTotalParts;
            char* pName = pDocument->getPartName(nPart);
            pDocument->setPart(nPart);
            long nWidth = 0, nHeight = 0;
            pDocument->getDocumentSize(&nWidth, &nHeight);
            fprintf (stderr, "render '%s' -> %ld, %ld\n", pName, nWidth, nHeight);
            free (pName);

            { // whole document
                aTimes.emplace_back("render whole document");
                pDocument->paintTile(pPixels, 256, 256,
            if (pDocument->getDocumentType() != LOK_DOCTYPE_TEXT)
            { // whole part; meaningful only for non-writer documents.
                aTimes.emplace_back("render whole part");
                pDocument->paintTile(pPixels, nTilePixelWidth, nTilePixelHeight,
                                     0, 0, nWidth, nHeight); // not square
                aTimes.emplace_back();
            }

            { // 1:1
                aTimes.emplace_back("render sub-region at 1:1");
                // Estimate the maximum tiles based on the number of parts requested, if Writer.
                int nMaxTiles = max_tiles;
                if (pDocument->getDocumentType() == LOK_DOCTYPE_TEXT)
                    nMaxTiles = (int)ceil(max_parts * 16128. / nTilePixelHeight) * ceil((double)nWidth / nTilePixelWidth);
                int nTiles = 0;
                int nSplit = nWidth / 4;
                for (int nX = 0; nX < 4; nX++)
                for (int nY = 0; nY < nHeight - 1; nY += nTilePixelHeight)
                {
                    for (int nY = 0; nY < nHeight / nSplit; nY++)
                    for (int nX = 0; nX < nWidth - 1; nX += nTilePixelWidth)
                    {
                        int nTilePosX = nX * nSplit;
                        int nTilePosY = nY * nSplit;
                        pDocument->paintTile(pPixels, 256, 256,
                                             nTilePosX, nTilePosY, 256, 256);
                        if (nMaxTiles >= 0 && nTiles >= nMaxTiles)
                        {
                            nY = nHeight;
                            break;
                        }

                        pDocument->paintTile(pPixels, nTilePixelWidth, nTilePixelHeight,
                                             nX, nY, nTilePixelWidth, nTilePixelHeight);
                        nTiles++;
                        fprintf (stderr, "   rendered tile %d at %d, %d\n",
                                 nTiles, nTilePosX, nTilePosY);
                        fprintf (stderr, "   rendered 1:1 tile %d at %d, %d\n",
                                 nTiles, nX, nY);
                    }
                }
                aTimes.emplace_back();
@@ -123,19 +152,26 @@ int main( int argc, char* argv[] )

            { // scaled
                aTimes.emplace_back("render sub-regions at scale");
                // Estimate the maximum tiles based on the number of parts requested, if Writer.
                int nMaxTiles = max_tiles;
                if (pDocument->getDocumentType() == LOK_DOCTYPE_TEXT)
                    nMaxTiles = (int)ceil(max_parts * 16128. / nTileTwipHeight) * ceil((double)nWidth / nTileTwipWidth);
                int nTiles = 0;
                int nSplit = nWidth / 4;
                for (int nX = 0; nX < 4; nX++)
                for (int nY = 0; nY < nHeight - 1; nY += nTileTwipHeight)
                {
                    for (int nY = 0; nY < nHeight / nSplit; nY++)
                    for (int nX = 0; nX < nWidth - 1; nX += nTileTwipWidth)
                    {
                        int nTilePosX = nX * nSplit;
                        int nTilePosY = nY * nSplit;
                        pDocument->paintTile(pPixels, 256, 256,
                                             nTilePosX, nTilePosY, nSplit, nSplit);
                        if (nMaxTiles >= 0 && nTiles >= nMaxTiles)
                        {
                            nY = nHeight;
                            break;
                        }

                        pDocument->paintTile(pPixels, nTilePixelWidth, nTilePixelHeight,
                                             nX, nY, nTileTwipWidth, nTileTwipHeight);
                        nTiles++;
                        fprintf (stderr, "   rendered tile %d at %d, %d\n",
                                 nTiles, nTilePosX, nTilePosY);
                        fprintf (stderr, "   rendered scaled tile %d at %d, %d\n",
                                 nTiles, nX, nY);
                    }
                }
                aTimes.emplace_back();
@@ -153,7 +189,7 @@ int main( int argc, char* argv[] )
    fprintf (stderr, "profile run:\n");
    for (size_t i = 0; i < aTimes.size() - 1; i++)
    {
        double nDelta = aTimes[i+1].mfTime - aTimes[i].mfTime;
        const double nDelta = aTimes[i+1].mfTime - aTimes[i].mfTime;
        fprintf (stderr, "  %s - %2.4f(ms)\n", aTimes[i].mpName, nDelta * 1000.0);
        if (aTimes[i+1].mpName == nullptr)
            i++; // skip it.