<commit_before>/*
 * Copyright 2012 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include "BenchTimer.h"
#include "SamplePipeControllers.h"
#include "SkBitmap.h"
#include "SkCanvas.h"
#include "SkGPipe.h"
#include "SkOSFile.h"
#include "SkPicture.h"
#include "SkStream.h"
#include "SkTArray.h"
#include "picture_utils.h"

const int DEFAULT_REPEATS = 100;
const int DEFAULT_TILE_WIDTH = 256;
const int DEFAULT_TILE_HEIGHT = 256;

struct Options;
static void run_simple_benchmark(SkPicture* picture, const Options&);

struct Options {
    int fRepeats;
    void (*fBenchmark) (SkPicture*, const Options& options);
    int fTileWidth;
    int fTileHeight;
    double fTileWidthPercentage;
    double fTileHeightPercentage;

    Options() : fRepeats(DEFAULT_REPEATS), fBenchmark(run_simple_benchmark),
    fTileWidth(DEFAULT_TILE_WIDTH), fTileHeight(DEFAULT_TILE_HEIGHT),
    fTileWidthPercentage(0), fTileHeightPercentage(0){}
};

static void usage(const char* argv0) {
    SkDebugf("SkPicture benchmarking tool\n");
    SkDebugf("\n"
"Usage: \n"
"     %s <inputDir>...\n"
"     [--repeat] [--tile width height]"
, argv0);
    SkDebugf("\n\n");
    SkDebugf(
"     inputDir:  A list of directories and files to use as input.\n"
"                    Files are expected to have the .skp extension.\n\n");
    SkDebugf(
"     --pipe : "
"Set to use piping."
" Default is to not use piping.\n");
    SkDebugf(
"     --repeat : "
"Set the number of times to repeat each test."
" Default is %i.\n", DEFAULT_REPEATS);
    SkDebugf(
"     --tile width[%] height[%]: "
"Set to use the tiling size and specify the dimensions of each tile.\n"
"                                     Default is to not use tiling\n");
    SkDebugf(
"     --unflatten: "
"Set to do a picture unflattening benchmark. Default is not to do this.\n");
}

static void run_simple_benchmark(SkPicture* picture, const Options& options) {
    SkBitmap bitmap;
    sk_tools::setup_bitmap(&bitmap, picture->width(), picture->height());

    SkCanvas canvas(bitmap);

    // We throw this away to remove first time effects (such as paging in this
    // program)
    canvas.drawPicture(*picture);

    BenchTimer timer = BenchTimer(NULL);
    timer.start();
    for (int i = 0; i < options.fRepeats; ++i) {
        canvas.drawPicture(*picture);
    }
    timer.end();

    printf("simple: msecs = %6.2f\n", timer.fWall / options.fRepeats);
}

struct TileInfo {
    SkBitmap* fBitmap;
    SkCanvas* fCanvas;
};

static void clip_tile(SkPicture* picture, const TileInfo& tile) {
    SkRect clip = SkRect::MakeWH(SkIntToScalar(picture->width()), 
                                 SkIntToScalar(picture->height()));
    tile.fCanvas->clipRect(clip);
}

static void setup_single_tile(SkPicture* picture, const SkBitmap& bitmap,
                              const Options& options, SkTArray<TileInfo>* tiles,
                              int tile_x_start, int tile_y_start) {
    TileInfo& tile = tiles->push_back();
    tile.fBitmap = new SkBitmap();
    SkIRect rect = SkIRect::MakeXYWH(tile_x_start, tile_y_start,
                                     options.fTileWidth, options.fTileHeight);
    bitmap.extractSubset(tile.fBitmap, rect);
    tile.fCanvas = new SkCanvas(*(tile.fBitmap));
    tile.fCanvas->translate(SkIntToScalar(-tile_x_start), 
                            SkIntToScalar(-tile_y_start));

    clip_tile(picture, tile);
}

static void setup_tiles(SkPicture* picture, const SkBitmap& bitmap,
                        const Options& options, SkTArray<TileInfo>* tiles) {
    for (int tile_y_start = 0; tile_y_start < picture->height();
         tile_y_start += options.fTileHeight) {
        for (int tile_x_start = 0; tile_x_start < picture->width();
             tile_x_start += options.fTileWidth) {
            setup_single_tile(picture, bitmap, options, tiles, tile_x_start,
                              tile_y_start);
        }
    }

}

static void run_tile_benchmark(SkPicture* picture, const Options& options) {
    SkBitmap bitmap;
    sk_tools::setup_bitmap(&bitmap, picture->width(), picture->height());

    SkTArray<TileInfo> tiles;
    setup_tiles(picture, bitmap, options, &tiles);

    // We throw this away to remove first time effects (such as paging in this
    // program)
    for (int j = 0; j < tiles.count(); ++j) {
        tiles[j].fCanvas->drawPicture(*picture);
    }

    BenchTimer timer = BenchTimer(NULL);
    timer.start();
    for (int i = 0; i < options.fRepeats; ++i) {
        for (int j = 0; j < tiles.count(); ++j) {
            tiles[j].fCanvas->drawPicture(*picture);
        }
    }
    timer.end();

    for (int i = 0; i < tiles.count(); ++i) {<commit_msg>Benchmark tiling now better reflects how it is done in Chrome.<commit_after>/*
 * Copyright 2012 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include "BenchTimer.h"
#include "SamplePipeControllers.h"
#include "SkBitmap.h"
#include "SkCanvas.h"
#include "SkGPipe.h"
#include "SkOSFile.h"
#include "SkPicture.h"
#include "SkStream.h"
#include "SkTArray.h"
#include "picture_utils.h"

const int DEFAULT_REPEATS = 100;
const int DEFAULT_TILE_WIDTH = 256;
const int DEFAULT_TILE_HEIGHT = 256;

struct Options;
static void run_simple_benchmark(SkPicture* picture, const Options&);

struct Options {
    int fRepeats;
    void (*fBenchmark) (SkPicture*, const Options& options);
    int fTileWidth;
    int fTileHeight;
    double fTileWidthPercentage;
    double fTileHeightPercentage;

    Options() : fRepeats(DEFAULT_REPEATS), fBenchmark(run_simple_benchmark),
    fTileWidth(DEFAULT_TILE_WIDTH), fTileHeight(DEFAULT_TILE_HEIGHT),
    fTileWidthPercentage(0), fTileHeightPercentage(0){}
};

static void usage(const char* argv0) {
    SkDebugf("SkPicture benchmarking tool\n");
    SkDebugf("\n"
"Usage: \n"
"     %s <inputDir>...\n"
"     [--repeat] [--tile width height]"
, argv0);
    SkDebugf("\n\n");
    SkDebugf(
"     inputDir:  A list of directories and files to use as input.\n"
"                    Files are expected to have the .skp extension.\n\n");
    SkDebugf(
"     --pipe : "
"Set to use piping."
" Default is to not use piping.\n");
    SkDebugf(
"     --repeat : "
"Set the number of times to repeat each test."
" Default is %i.\n", DEFAULT_REPEATS);
    SkDebugf(
"     --tile width[%] height[%]: "
"Set to use the tiling size and specify the dimensions of each tile.\n"
"                                     Default is to not use tiling\n");
    SkDebugf(
"     --unflatten: "
"Set to do a picture unflattening benchmark. Default is not to do this.\n");
}

static void run_simple_benchmark(SkPicture* picture, const Options& options) {
    SkBitmap bitmap;
    sk_tools::setup_bitmap(&bitmap, picture->width(), picture->height());

    SkCanvas canvas(bitmap);

    // We throw this away to remove first time effects (such as paging in this
    // program)
    canvas.drawPicture(*picture);

    BenchTimer timer = BenchTimer(NULL);
    timer.start();
    for (int i = 0; i < options.fRepeats; ++i) {
        canvas.drawPicture(*picture);
    }
    timer.end();

    printf("simple: msecs = %6.2f\n", timer.fWall / options.fRepeats);
}

struct TileInfo {
    SkBitmap* fBitmap;
    SkCanvas* fCanvas;
};

static void clip_tile(SkPicture* picture, const TileInfo& tile) {
    SkRect clip = SkRect::MakeWH(SkIntToScalar(picture->width()),
                                 SkIntToScalar(picture->height()));
    tile.fCanvas->clipRect(clip);
}

static void setup_single_tile(SkPicture* picture, const Options& options,
                              SkTArray<TileInfo>* tiles, int tile_x_start, int tile_y_start) {
    TileInfo& tile = tiles->push_back();

    tile.fBitmap = new SkBitmap();
    sk_tools::setup_bitmap(tile.fBitmap, options.fTileWidth, options.fTileHeight);

    tile.fCanvas = new SkCanvas(*(tile.fBitmap));
    tile.fCanvas->translate(SkIntToScalar(-tile_x_start), SkIntToScalar(-tile_y_start));
    clip_tile(picture, tile);
}

static void setup_tiles(SkPicture* picture, const Options& options, SkTArray<TileInfo>* tiles) {
    for (int tile_y_start = 0; tile_y_start < picture->height();
         tile_y_start += options.fTileHeight) {
        for (int tile_x_start = 0; tile_x_start < picture->width();
             tile_x_start += options.fTileWidth) {
            setup_single_tile(picture, options, tiles, tile_x_start, tile_y_start);
        }
    }

}

static void run_tile_benchmark(SkPicture* picture, const Options& options) {
    SkTArray<TileInfo> tiles;
    setup_tiles(picture, options, &tiles);

    // We throw this away to remove first time effects (such as paging in this
    // program)
    for (int j = 0; j < tiles.count(); ++j) {
        tiles[j].fCanvas->drawPicture(*picture);
    }

    BenchTimer timer = BenchTimer(NULL);
    timer.start();
    for (int i = 0; i < options.fRepeats; ++i) {
        for (int j = 0; j < tiles.count(); ++j) {
            tiles[j].fCanvas->drawPicture(*picture);
        }
    }
    timer.end();

    for (int i = 0; i < tiles.count(); ++i) {
{'fraction_non_alphanumeric': 0.12607793908961903, 'fraction_numerical': 0.0056762362187534115, 'mean_word_length': 3.006121556624399, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2243006', 'n_tokens_mistral': 3049, 'n_tokens_neox': 2948, 'n_words': 788}