More hacking on the tilebench part of the UnitTest app

On iOS, don't attempt to write the tile dump ppm file to /tmp, but use
the app-specific directory so that it can be copied for inspection
using iTunes. (Of course, even better would be to simply paint it to
the app's view. Later)

Change-Id: I8dd60d04adc61de6594099f5c358a9b6220522da
Reviewed-on: https://gerrit.libreoffice.org/61255
Reviewed-by: Tor Lillqvist <tml@collabora.com>
Tested-by: Tor Lillqvist <tml@collabora.com>
diff --git a/ios/UnitTest/UnitTest/Info.plist b/ios/UnitTest/UnitTest/Info.plist
index 16be3b6..e6a294e 100644
--- a/ios/UnitTest/UnitTest/Info.plist
+++ b/ios/UnitTest/UnitTest/Info.plist
@@ -2,6 +2,8 @@
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
	<key>UIFileSharingEnabled</key>
	<true/>
	<key>CFBundleDevelopmentRegion</key>
	<string>$(DEVELOPMENT_LANGUAGE)</string>
	<key>CFBundleExecutable</key>
diff --git a/ios/UnitTest/UnitTest/ViewController.mm b/ios/UnitTest/UnitTest/ViewController.mm
index 0b25baf6..a955812 100644
--- a/ios/UnitTest/UnitTest/ViewController.mm
+++ b/ios/UnitTest/UnitTest/ViewController.mm
@@ -29,9 +29,35 @@ extern "C" {
}

#include <premac.h>
#import <CoreGraphics/CoreGraphics.h>
#import "ViewController.h"
#include <postmac.h>

// This is from online's Mobile app (as it is called at the moment);
// should of course be factored out to some common place. Here in
// core?

static thread_local CGContextRef cgc = nullptr;

static unsigned char *lo_ios_app_get_cgcontext_for_buffer(unsigned char *buffer, int width, int height)
{
    assert(cgc == nullptr);

    cgc = CGBitmapContextCreate(buffer, width, height, 8, width*4, CGColorSpaceCreateDeviceRGB(), kCGImageAlphaNoneSkipFirst);

    CGContextTranslateCTM(cgc, 0, height);
    CGContextScaleCTM(cgc, 1, -1);

    return (unsigned char*)cgc;
}

static void lo_ios_app_release_cgcontext_for_buffer()
{
    assert(cgc != nullptr);
    CGContextRelease(cgc);
    cgc = nullptr;
}

@interface ViewController ()

@end
diff --git a/libreofficekit/qa/tilebench/tilebench.cxx b/libreofficekit/qa/tilebench/tilebench.cxx
index d4148df..ee325c4 100644
--- a/libreofficekit/qa/tilebench/tilebench.cxx
+++ b/libreofficekit/qa/tilebench/tilebench.cxx
@@ -64,7 +64,15 @@ static std::vector< TimeRecord > aTimes;
static void dumpTile(const int nWidth, const int nHeight, const int mode, const unsigned char* pBufferU)
{
    auto pBuffer = reinterpret_cast<const char *>(pBufferU);
#ifndef IOS
    std::ofstream ofs("/tmp/dump_tile.ppm");
#else
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    static int counter = 0;
    NSString *path = [NSString stringWithFormat:@"%@/dump_tile_%d.ppm", documentsDirectory, counter++];
    std::ofstream ofs([path UTF8String]);
#endif
    ofs << "P6\n"
        << nWidth << " "
        << nHeight << "\n"
@@ -101,6 +109,7 @@ static void dumpTile(const int nWidth, const int nHeight, const int mode, const 
            }
        }
    }
    ofs.close();
}

static void testTile( Document *pDocument, int max_parts,
@@ -156,8 +165,15 @@ static void testTile( Document *pDocument, int max_parts,
        {
            // whole part; meaningful only for non-writer documents.
            aTimes.emplace_back("render whole part");
#ifndef IOS
            pDocument->paintTile(pPixels, nTilePixelWidth, nTilePixelHeight,
                                 nWidth/2, 2000, 1000, 1000); // not square
#else
            pDocument->paintTile(lo_ios_app_get_cgcontext_for_buffer(pPixels, nTilePixelWidth, nTilePixelHeight),
                                 nTilePixelWidth, nTilePixelHeight,
                                 nWidth/2, 2000, 1000, 1000); // not square
            lo_ios_app_release_cgcontext_for_buffer();
#endif
            aTimes.emplace_back();
            if (dump)
                dumpTile(nTilePixelWidth, nTilePixelHeight, mode, pPixels);
@@ -177,9 +193,15 @@ static void testTile( Document *pDocument, int max_parts,
                        nY = nHeight;
                        break;
                    }

#ifndef IOS
                    pDocument->paintTile(pPixels, nTilePixelWidth, nTilePixelHeight,
                                         nX, nY, nTilePixelWidth, nTilePixelHeight);
#else
                    pDocument->paintTile(lo_ios_app_get_cgcontext_for_buffer(pPixels, nTilePixelWidth, nTilePixelHeight),
                                         nTilePixelWidth, nTilePixelHeight,
                                         nX, nY, nTilePixelWidth, nTilePixelHeight);
                    lo_ios_app_release_cgcontext_for_buffer();
#endif
                    nTiles++;
                    fprintf (stderr, "   rendered 1:1 tile %d at %d, %d\n",
                             nTiles, nX, nY);
@@ -203,9 +225,15 @@ static void testTile( Document *pDocument, int max_parts,
                        nY = nHeight;
                        break;
                    }

#ifndef IOS
                    pDocument->paintTile(pPixels, nTilePixelWidth, nTilePixelHeight,
                                         nX, nY, nTileTwipWidth, nTileTwipHeight);
#else
                    pDocument->paintTile(lo_ios_app_get_cgcontext_for_buffer(pPixels, nTilePixelWidth, nTilePixelHeight),
                                         nTilePixelWidth, nTilePixelHeight,
                                         nX, nY, nTileTwipWidth, nTileTwipHeight);
                    lo_ios_app_release_cgcontext_for_buffer();
#endif
                    nTiles++;
                    fprintf (stderr, "   rendered scaled tile %d at %d, %d\n",
                             nTiles, nX, nY);