aboutsummaryrefslogtreecommitdiffhomepage
path: root/dm/DMQuiltTask.cpp
blob: b0f36c08700000579bb94c86f949160c8329b76b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include "DMQuiltTask.h"
#include "DMUtil.h"
#include "DMWriteTask.h"

#include "SkCommandLineFlags.h"
#include "SkPicture.h"
#include "SkThreadPool.h"

DEFINE_bool(quilt, true, "If true, draw into a quilt of small tiles and compare.");
DEFINE_int32(quiltTile, 256, "Dimension of (square) quilt tile.");
DEFINE_bool(quiltThreaded, true, "If true, draw quilt tiles with multiple threads.");

static const char* kSuffixes[] = { "quilt", "quilt_skr" };

namespace DM {

QuiltTask::QuiltTask(const Task& parent, skiagm::GM* gm, SkBitmap reference, QuiltTask::Mode mode)
    : CpuTask(parent)
    , fMode(mode)
    , fName(UnderJoin(parent.name().c_str(), kSuffixes[mode]))
    , fGM(gm)
    , fReference(reference)
    {}

static int tiles_needed(int fullDimension, int tileDimension) {
    return (fullDimension + tileDimension - 1) / tileDimension;
}

class Tile : public SkRunnable {
public:
    Tile(int x, int y, const SkPicture& picture, SkBitmap* quilt)
        : fX(x * FLAGS_quiltTile)
        , fY(y * FLAGS_quiltTile)
        , fPicture(picture)
        , fQuilt(quilt) {}

    virtual void run() SK_OVERRIDE {
        SkBitmap tile;
        fQuilt->extractSubset(&tile, SkIRect::MakeXYWH(fX, fY, FLAGS_quiltTile, FLAGS_quiltTile));
        SkCanvas tileCanvas(tile);

        tileCanvas.translate(SkIntToScalar(-fX), SkIntToScalar(-fY));
        fPicture.draw(&tileCanvas);
        tileCanvas.flush();

        delete this;
    }

private:
    const int fX, fY;
    const SkPicture& fPicture;
    SkBitmap* fQuilt;
};

void QuiltTask::draw() {
    SkAutoTUnref<SkPicture> recorded(
            RecordPicture(fGM.get(), NULL/*bbh factory*/, kSkRecord_Mode == fMode));

    SkBitmap full;
    AllocatePixels(fReference, &full);

    int threads = 0;
    if (kSkRecord_Mode == fMode || FLAGS_quiltThreaded) {
        threads = SkThreadPool::kThreadPerCore;
    }
    SkThreadPool pool(threads);

    for (int y = 0; y < tiles_needed(full.height(), FLAGS_quiltTile); y++) {
        for (int x = 0; x < tiles_needed(full.width(), FLAGS_quiltTile); x++) {
            // Deletes itself when done.
            pool.add(new Tile(x, y, *recorded, &full));
        }
    }

    pool.wait();

    if (!BitmapsEqual(full, fReference)) {
        this->fail();
        this->spawnChild(SkNEW_ARGS(WriteTask, (*this, full)));
    }
}

bool QuiltTask::shouldSkip() const {
    if (fGM->getFlags() & skiagm::GM::kSkipPicture_Flag) {
        return true;
    }
    if (fGM->getFlags() & skiagm::GM::kSkipTiled_Flag) {
        return true;
    }
    return !FLAGS_quilt;
}

}  // namespace DM