1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
19 #include <LibreOfficeKit/LibreOfficeKitEnums.h>
20 #include <LibreOfficeKit/LibreOfficeKitInit.h>
21 #include <LibreOfficeKit/LibreOfficeKit.hxx>
24 #include <vcl/svapp.hxx>
27 #include <boost/property_tree/json_parser.hpp>
31 static int help( const char *error
= nullptr )
34 fprintf (stderr
, "Error: %s\n\n", error
);
35 fprintf( stderr
, "Usage: tilebench <absolute-path-to-libreoffice-install> [path to document] [--preinit] <options>\n");
36 fprintf( stderr
, "\trenders a selection of small tiles from the document, checksums them and times the process based on options:\n" );
37 fprintf( stderr
, "\t--tile\t[max parts|-1] [max tiles|-1]\n" );
38 fprintf( stderr
, "\t--dialog\t<.uno:Command>\n" );
39 fprintf( stderr
, "\t--join\trun tile joining tests\n" );
43 static double getTimeNow()
46 osl_getSystemTime(&aValue
);
47 return static_cast<double>(aValue
.Seconds
) +
48 static_cast<double>(aValue
.Nanosec
) / (1000*1000*1000);
59 TimeRecord() : mpName(nullptr), mfTime(getTimeNow()) { }
60 explicit TimeRecord(const char *pName
) :
61 mpName(pName
), mfTime(getTimeNow())
63 fprintf(stderr
, "%3.3fs - %s\n", (mfTime
- origin
), mpName
);
69 static std::vector
< TimeRecord
> aTimes
;
71 /// Dump an array (or sub-array) of RGBA or BGRA to an RGB PPM file.
72 static void dumpTile(const char *pNameStem
,
73 const int nWidth
, const int nHeight
,
74 const int mode
, const unsigned char* pBufferU
,
75 const int nOffX
= 0, const int nOffY
= 0,
81 auto pBuffer
= reinterpret_cast<const char *>(pBufferU
);
82 static int counter
= 0;
83 std::string aName
= "/tmp/dump_tile";
85 aName
+= "_" + std::to_string(counter
);
88 std::ofstream
ofs(aName
);
90 NSArray
*paths
= NSSearchPathForDirectoriesInDomains(NSDocumentDirectory
, NSUserDomainMask
, YES
);
91 NSString
*documentsDirectory
= [paths objectAtIndex
:0];
92 NSString
*path
= [NSString stringWithFormat
:@
"%@/dump_tile_%d.ppm", documentsDirectory
, counter
];
93 std::ofstream
ofs([path UTF8String
]);
94 std::cerr
<< "---> Dumping tile\n";
102 const bool dumpText
= false;
105 fprintf(stderr
, "Stream %s - %dx%d:\n", pNameStem
, nWidth
, nHeight
);
107 for (int y
= 0; y
< nHeight
; ++y
)
109 const char* row
= pBuffer
+ (y
+ nOffY
) * nTotalWidth
* 4 + nOffX
* 4;
110 for (int x
= 0; x
< nWidth
; ++x
)
112 const char* pixel
= row
+ x
* 4;
113 if (mode
== LOK_TILEMODE_RGBA
)
115 ofs
.write(pixel
, 3); // Skip alpha
117 else if (mode
== LOK_TILEMODE_BGRA
)
119 const int alpha
= *(pixel
+ 3);
129 buf
[0] = (*(pixel
+ 2) * 255 + alpha
/ 2) / alpha
;
130 buf
[1] = (*(pixel
+ 1) * 255 + alpha
/ 2) / alpha
;
131 buf
[2] = (*(pixel
+ 0) * 255 + alpha
/ 2) / alpha
;
138 int lowResI
= (pixel
[0] + pixel
[1] + pixel
[2])/(3*16);
139 fprintf(stderr
,"%1x", lowResI
);
143 fprintf(stderr
,"\n");
148 static void testTile( Document
*pDocument
, int max_parts
,
149 int max_tiles
, bool dump
)
151 const int mode
= pDocument
->getTileMode();
153 aTimes
.emplace_back("getparts");
154 const int nOriginalPart
= (pDocument
->getDocumentType() == LOK_DOCTYPE_TEXT
? 1 : pDocument
->getPart());
155 // Writer really has 1 part (the full doc).
156 const int nTotalParts
= (pDocument
->getDocumentType() == LOK_DOCTYPE_TEXT
? 1 : pDocument
->getParts());
157 const int nParts
= (max_parts
< 0 ? nTotalParts
: std::min(max_parts
, nTotalParts
));
158 aTimes
.emplace_back();
160 aTimes
.emplace_back("get size of parts");
163 for (int n
= 0; n
< nParts
; ++n
)
165 const int nPart
= (nOriginalPart
+ n
) % nTotalParts
;
166 char* pName
= pDocument
->getPartName(nPart
);
167 pDocument
->setPart(nPart
);
168 pDocument
->getDocumentSize(&nWidth
, &nHeight
);
169 fprintf (stderr
, " '%s' -> %ld, %ld\n", pName
, nWidth
, nHeight
);
172 aTimes
.emplace_back();
174 // Use realistic dimensions, similar to the Online client.
175 long const nTilePixelWidth
= 512;
176 long const nTilePixelHeight
= 512;
177 long const nTileTwipWidth
= 3840;
178 long const nTileTwipHeight
= 3840;
180 // Estimate the maximum tiles based on the number of parts requested, if Writer.
181 if (pDocument
->getDocumentType() == LOK_DOCTYPE_TEXT
)
182 max_tiles
= static_cast<int>(ceil(max_parts
* 16128. / nTilePixelHeight
) * ceil(static_cast<double>(nWidth
) / nTilePixelWidth
));
183 fprintf(stderr
, "Parts to render: %d, Total Parts: %d, Max parts: %d, Max tiles: %d\n", nParts
, nTotalParts
, max_parts
, max_tiles
);
185 std::vector
<unsigned char> vBuffer(nTilePixelWidth
* nTilePixelHeight
* 4);
186 unsigned char* pPixels
= vBuffer
.data();
188 for (int n
= 0; n
< nParts
; ++n
)
190 const int nPart
= (nOriginalPart
+ n
) % nTotalParts
;
191 char* pName
= pDocument
->getPartName(nPart
);
192 pDocument
->setPart(nPart
);
193 pDocument
->getDocumentSize(&nWidth
, &nHeight
);
194 fprintf (stderr
, "render '%s' -> %ld, %ld\n", pName
, nWidth
, nHeight
);
197 if (dump
|| pDocument
->getDocumentType() != LOK_DOCTYPE_TEXT
)
199 // whole part; meaningful only for non-writer documents.
200 aTimes
.emplace_back("render whole part");
201 pDocument
->paintTile(pPixels
, nTilePixelWidth
, nTilePixelHeight
,
202 nWidth
/2, 2000, 1000, 1000);
203 aTimes
.emplace_back();
205 dumpTile("tile", nTilePixelWidth
, nTilePixelHeight
, mode
, pPixels
);
209 aTimes
.emplace_back("render sub-region at 1:1");
210 // Estimate the maximum tiles based on the number of parts requested, if Writer.
211 int nMaxTiles
= max_tiles
;
213 for (long nY
= 0; nY
< nHeight
- 1; nY
+= nTilePixelHeight
)
215 for (long nX
= 0; nX
< nWidth
- 1; nX
+= nTilePixelWidth
)
217 if (nMaxTiles
>= 0 && nTiles
>= nMaxTiles
)
222 pDocument
->paintTile(pPixels
, nTilePixelWidth
, nTilePixelHeight
,
223 nX
, nY
, nTilePixelWidth
, nTilePixelHeight
);
225 fprintf (stderr
, " rendered 1:1 tile %d at %ld, %ld\n",
229 aTimes
.emplace_back();
233 aTimes
.emplace_back("render sub-regions at scale");
234 int nMaxTiles
= max_tiles
;
235 if (pDocument
->getDocumentType() == LOK_DOCTYPE_TEXT
)
236 nMaxTiles
= static_cast<int>(ceil(max_parts
* 16128. / nTileTwipHeight
) * ceil(static_cast<double>(nWidth
) / nTileTwipWidth
));
238 for (long nY
= 0; nY
< nHeight
- 1; nY
+= nTileTwipHeight
)
240 for (long nX
= 0; nX
< nWidth
- 1; nX
+= nTileTwipWidth
)
242 if (nMaxTiles
>= 0 && nTiles
>= nMaxTiles
)
247 pDocument
->paintTile(pPixels
, nTilePixelWidth
, nTilePixelHeight
,
248 nX
, nY
, nTileTwipWidth
, nTileTwipHeight
);
250 fprintf (stderr
, " rendered scaled tile %d at %ld, %ld\n",
254 aTimes
.emplace_back();
259 static uint32_t fade(uint32_t col
)
261 uint8_t a
= (col
>> 24) & 0xff;
262 uint8_t b
= (col
>> 16) & 0xff;
263 uint8_t g
= (col
>> 8) & 0xff;
264 uint8_t r
= (col
>> 0) & 0xff;
265 uint8_t grey
= (r
+g
+b
)/6;
266 return (a
<<24) + (grey
<<16) + (grey
<<8) + grey
;
269 static bool sloppyEqual(uint32_t pixA
, uint32_t pixB
)
273 a
[0] = (pixA
>> 24) & 0xff;
274 a
[1] = (pixA
>> 16) & 0xff;
275 a
[2] = (pixA
>> 8) & 0xff;
276 a
[3] = (pixA
>> 0) & 0xff;
278 b
[0] = (pixB
>> 24) & 0xff;
279 b
[1] = (pixB
>> 16) & 0xff;
280 b
[2] = (pixB
>> 8) & 0xff;
281 b
[3] = (pixB
>> 0) & 0xff;
283 for (int i
= 0; i
< 4; ++i
)
287 // tolerate small differences
288 if (delta
< -4 || delta
> 4)
294 // Count and build a picture of any differences into rDiff
295 static int diffTiles( const std::vector
<unsigned char> &vBase
,
296 long nBaseRowPixelWidth
,
297 const std::vector
<unsigned char> &vCompare
,
298 long nCompareRowPixelWidth
,
299 long nTilePixelHeight
,
300 long nPosX
, long nPosY
,
301 std::vector
<unsigned char> &rDiff
)
304 const uint32_t *pBase
= reinterpret_cast<const uint32_t *>(vBase
.data());
305 const uint32_t *pCompare
= reinterpret_cast<const uint32_t *>(vCompare
.data());
306 uint32_t *pDiff
= reinterpret_cast<uint32_t *>(rDiff
.data());
307 long left
= 0, mid
= nCompareRowPixelWidth
, right
= nCompareRowPixelWidth
*2;
308 for (long y
= 0; y
< nTilePixelHeight
; ++y
)
310 long nBaseOffset
= nBaseRowPixelWidth
* (y
+ nPosY
) + nPosX
* nCompareRowPixelWidth
;
311 long nCompareOffset
= nCompareRowPixelWidth
* y
;
312 long nDiffRowStart
= nCompareOffset
* 3;
313 for (long x
= 0; x
< nCompareRowPixelWidth
; ++x
)
315 pDiff
[nDiffRowStart
+ left
+ x
] = pBase
[nBaseOffset
+ x
];
316 pDiff
[nDiffRowStart
+ mid
+ x
] = pCompare
[nCompareOffset
+ x
];
317 pDiff
[nDiffRowStart
+ right
+ x
] = fade(pBase
[nBaseOffset
+ x
]);
318 if (!sloppyEqual(pBase
[nBaseOffset
+ x
], pCompare
[nCompareOffset
+ x
]))
320 pDiff
[nDiffRowStart
+ right
+ x
] = 0xffff00ff;
322 fprintf (stderr
, "First mismatching pixel at %ld (pixels) into row %ld\n", x
, y
);
330 static std::vector
<unsigned char> paintTile( Document
*pDocument
,
332 long const nTilePixelWidth
,
333 long const nTilePixelHeight
,
334 long const nTileTwipWidth
,
335 long const nTileTwipHeight
)
337 // long e = 0; // tweak if we suspect an overlap / visibility issue.
338 // pDocument->setClientVisibleArea( nX - e, nY - e, nTileTwipWidth + e, nTileTwipHeight + e );
339 std::vector
<unsigned char> vData( nTilePixelWidth
* nTilePixelHeight
* 4 );
340 pDocument
->paintTile( vData
.data(), nTilePixelWidth
, nTilePixelHeight
,
341 nX
, nY
, nTileTwipWidth
, nTileTwipHeight
);
345 static int testJoinsAt( Document
*pDocument
, long nX
, long nY
,
346 long const nTilePixelSize
,
347 long const nTileTwipSize
)
349 const int mode
= pDocument
->getTileMode();
351 long const nTilePixelWidth
= nTilePixelSize
;
352 long const nTilePixelHeight
= nTilePixelSize
;
353 long const nTileTwipWidth
= nTileTwipSize
;
354 long const nTileTwipHeight
= nTileTwipSize
;
356 long initPosX
= nX
* nTileTwipWidth
, initPosY
= nY
* nTileTwipHeight
;
358 // Calc has to do significant work on changing zoom ...
359 pDocument
->setClientZoom( nTilePixelWidth
, nTilePixelHeight
,
360 nTileTwipWidth
, nTileTwipHeight
);
362 // Unfortunately without getting this nothing renders ...
363 std::stringstream aForceHeaders
;
364 aForceHeaders
<< ".uno:ViewRowColumnHeaders?x=" << initPosX
<< "&y=" << initPosY
<<
365 "&width=" << (nTileTwipWidth
* 2) << "&height=" << (nTileTwipHeight
* 2);
366 std::string cmd
= aForceHeaders
.str();
367 char* pJSON
= pDocument
->getCommandValues(cmd
.c_str());
368 fprintf(stderr
, "command: '%s' values '%s'\n", cmd
.c_str(), pJSON
);
371 // Get a base image 4x the size
372 std::vector
<unsigned char> vBase(
373 paintTile(pDocument
, initPosX
, initPosY
,
374 nTilePixelWidth
* 2, nTilePixelHeight
* 2,
375 nTileTwipWidth
* 2, nTileTwipHeight
* 2));
387 int nDifferences
= 0;
388 // Compare each of the 4x tiles with a sub-tile of the larger image
389 for( auto &rPos
: aCompare
)
391 std::vector
<unsigned char> vCompare(
393 initPosX
+ rPos
.X
* nTileTwipWidth
,
394 initPosY
+ rPos
.Y
* nTileTwipHeight
,
395 nTilePixelWidth
, nTilePixelHeight
,
396 nTileTwipWidth
, nTileTwipHeight
));
398 std::vector
<unsigned char> vDiff( nTilePixelWidth
* 3 * nTilePixelHeight
* 4 );
399 int nDiffs
= diffTiles( vBase
, nTilePixelWidth
* 2,
400 vCompare
, nTilePixelWidth
,
402 rPos
.X
, rPos
.Y
* nTilePixelHeight
,
406 fprintf( stderr
, " %d differences in sub-tile pixel mismatch at %ld, %ld at offset %ld, %ld (twips) size %ld\n",
407 nDiffs
, rPos
.X
, rPos
.Y
, initPosX
, initPosY
,
409 dumpTile("_base", nTilePixelWidth
* 2, nTilePixelHeight
* 2,
411 /* dumpTile("_sub", nTilePixelWidth, nTilePixelHeight,
413 rPos.X*nTilePixelWidth, rPos.Y*nTilePixelHeight,
414 nTilePixelWidth * 2);
415 dumpTile("_compare", nTilePixelWidth, nTilePixelHeight,
416 mode, vCompare.data());*/
417 dumpTile("_diff", nTilePixelWidth
* 3, nTilePixelHeight
, mode
, vDiff
.data());
419 nDifferences
+= nDiffs
;
425 // Check that our tiles join nicely ...
426 static int testJoin( Document
*pDocument
)
428 // Ignore parts - just the first for now ...
429 long nWidth
= 0, nHeight
= 0;
430 pDocument
->getDocumentSize(&nWidth
, &nHeight
);
431 fprintf (stderr
, "Width is %ld, %ld (twips)\n", nWidth
, nHeight
);
433 // Use realistic dimensions, similar to the Online client.
434 long const nTilePixelSize
= 256;
435 long const nTileTwipSize
= 3840;
444 std::stringstream results
;
446 for( auto z
: fZooms
)
449 long nDifferences
= 0;
450 for( long y
= 0; y
< 8; ++y
)
452 for( long x
= 0; x
< 8; ++x
)
454 int nDiffs
= testJoinsAt( pDocument
, x
, y
, nTilePixelSize
, nTileTwipSize
* z
);
457 nDifferences
+= nDiffs
;
461 results
<< "\tZoom " << z
<< " bad tiles: " << nBad
<< " with " << nDifferences
<< " mismatching pixels\n";
466 fprintf( stderr
, "Failed %ld joins\n", nFails
);
468 fprintf( stderr
, "All joins compared correctly\n" );
470 fprintf(stderr
, "%s\n", results
.str().c_str());
475 static std::atomic
<bool> bDialogRendered(false);
476 static std::atomic
<int> nDialogId(-1);
478 static void kitCallback(int nType
, const char* pPayload
, void* pData
)
480 Document
*pDocument
= static_cast<Document
*>(pData
);
482 if (nType
!= LOK_CALLBACK_WINDOW
)
485 std::stringstream
aStream(pPayload
);
486 boost::property_tree::ptree aRoot
;
487 boost::property_tree::read_json(aStream
, aRoot
);
488 nDialogId
= aRoot
.get
<unsigned>("id");
489 const std::string aAction
= aRoot
.get
<std::string
>("action");
491 if (aAction
!= "created")
494 const std::string aType
= aRoot
.get
<std::string
>("type");
495 const std::string aSize
= aRoot
.get
<std::string
>("size");
496 int nWidth
= atoi(aSize
.c_str());
498 const char *pComma
= strstr(aSize
.c_str(), ", ");
500 nHeight
= atoi(pComma
+ 2);
501 std::cerr
<< "Size " << aSize
<< " is " << nWidth
<< ", " << nHeight
<< "\n";
503 if (aType
!= "dialog")
506 aTimes
.emplace_back(); // complete wait for dialog
508 unsigned char *pBuffer
= new unsigned char[nWidth
* nHeight
* 4];
510 aTimes
.emplace_back("render dialog");
511 pDocument
->paintWindow(nDialogId
, pBuffer
, 0, 0, nWidth
, nHeight
);
512 dumpTile("dialog", nWidth
, nHeight
, pDocument
->getTileMode(), pBuffer
);
513 aTimes
.emplace_back();
517 bDialogRendered
= true;
520 static void testDialog( Document
*pDocument
, const char *uno_cmd
)
522 int view
= pDocument
->createView();
523 pDocument
->setView(view
);
524 pDocument
->registerCallback(kitCallback
, pDocument
);
526 aTimes
.emplace_back("open dialog");
527 pDocument
->postUnoCommand(uno_cmd
, nullptr, true);
528 aTimes
.emplace_back();
530 aTimes
.emplace_back("wait for dialog");
531 while (!bDialogRendered
)
536 aTimes
.emplace_back("post close dialog");
537 pDocument
->postWindow(nDialogId
, LOK_WINDOW_CLOSE
);
538 aTimes
.emplace_back();
540 pDocument
->destroyView(view
);
543 static void documentCallback(const int type
, const char* p
, void*)
545 std::cerr
<< "Document callback " << type
<< ": " << (p
? p
: "(null)") << "\n";
548 // Avoid excessive dbgutil churn.
549 static void ignoreCallback(const int /*type*/, const char* /*p*/, void* /*data*/)
553 int main( int argc
, char* argv
[] )
556 origin
= getTimeNow();
559 // avoid X oddness etc.
563 ( argc
> 1 && ( !strcmp( argv
[1], "--help" ) || !strcmp( argv
[1], "-h" ) ) ) )
566 if ( argv
[1][0] != '/' )
568 fprintf(stderr
, "Absolute path required to libreoffice install\n");
572 const char *doc_url
= argv
[arg
++];
573 const char *mode
= argv
[arg
++];
575 bool pre_init
= false;
576 if (!strcmp(mode
, "--preinit"))
582 std::string
user_url("file:///");
583 user_url
.append(argv
[1]);
584 user_url
.append("../user");
588 aTimes
.emplace_back("pre-initialization");
589 setenv("LOK_ALLOWLIST_LANGUAGES", "en_US", 0);
590 // coverity[tainted_string] - build time test tool
591 lok_preinit(argv
[1], user_url
.c_str());
592 aTimes
.emplace_back();
594 const char *install_path
= argv
[1];
595 const char *user_profile
= user_url
.c_str();
597 const char *install_path
= nullptr;
598 const char *user_profile
= nullptr;
599 const char *doc_url
= strdup([[[[[NSBundle mainBundle
] bundleURL
] absoluteString
] stringByAppendingString
:@
"/test.odt"] UTF8String
]);
600 const char *mode
= "--tile";
603 aTimes
.emplace_back("initialization");
604 // coverity[tainted_string] - build time test tool
605 std::unique_ptr
<Office
> pOffice( lok_cpp_init(install_path
, user_profile
) );
606 if (pOffice
== nullptr)
608 fprintf(stderr
, "Failed to initialize Office from %s\n", argv
[1]);
611 aTimes
.emplace_back();
612 pOffice
->registerCallback(ignoreCallback
, nullptr);
614 std::unique_ptr
<Document
> pDocument
;
616 pOffice
->setOptionalFeatures(LOK_FEATURE_NO_TILED_ANNOTATIONS
);
618 aTimes
.emplace_back("load document");
619 if (doc_url
!= nullptr)
620 pDocument
.reset(pOffice
->documentLoad(doc_url
));
621 aTimes
.emplace_back();
625 pDocument
->initializeForRendering("{\".uno:Author\":{\"type\":\"string\",\"value\":\"Local Host #0\"}}");
626 pDocument
->registerCallback(documentCallback
, nullptr);
627 if (!strcmp(mode
, "--tile"))
629 const int max_parts
= (argc
> arg
? atoi(argv
[arg
++]) : -1);
630 int max_tiles
= (argc
> arg
? atoi(argv
[arg
++]) : -1);
631 const bool dump
= true;
633 // coverity[tainted_data] - we trust the contents of this variable
634 testTile (pDocument
.get(), max_parts
, max_tiles
, dump
);
636 else if (!strcmp(mode
, "--join"))
638 return testJoin (pDocument
.get());
640 else if (!strcmp (mode
, "--dialog"))
642 const char *uno_cmd
= argc
> arg
? argv
[arg
++] : nullptr;
645 switch (pDocument
->getDocumentType())
647 case LOK_DOCTYPE_SPREADSHEET
:
648 uno_cmd
= ".uno:FormatCellDialog";
650 case LOK_DOCTYPE_TEXT
:
651 case LOK_DOCTYPE_PRESENTATION
:
652 case LOK_DOCTYPE_DRAWING
:
653 case LOK_DOCTYPE_OTHER
:
654 return help("missing argument to --dialog and no default");
657 testDialog (pDocument
.get(), uno_cmd
);
659 return help ("unknown parameter");
665 aTimes
.emplace_back("destroy document");
667 aTimes
.emplace_back();
672 fprintf (stderr
, "profile run:\n");
673 for (size_t i
= 0; i
< aTimes
.size() - 1; i
++)
675 const double nDelta
= aTimes
[i
+1].mfTime
- aTimes
[i
].mfTime
;
676 fprintf (stderr
, " %s - %2.4f(ms)\n", aTimes
[i
].mpName
, nDelta
* 1000.0);
677 if (aTimes
[i
+1].mpName
== nullptr)
681 fprintf (stderr
, "Total: %2.4f(s)\n", nTotal
);
685 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */