aboutsummaryrefslogtreecommitdiffhomepage
path: root/dm/DMTileGridTask.cpp
blob: cd3c7b79bec9de6beb7d5754a8cf1ce4b2e6037e (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
#include "DMTileGridTask.h"
#include "DMWriteTask.h"
#include "DMUtil.h"

#include "SkCommandLineFlags.h"
#include "SkPicture.h"
#include "SkTileGridPicture.h"

// TODO(mtklein): Tile grid tests are currently failing.  (Skia issue 1198).  When fixed, -> true.
DEFINE_bool(tileGrid, false, "If true, run picture replay tests with a tile grid.");

namespace DM {

TileGridTask::TileGridTask(const Task& parent, skiagm::GM* gm, SkBitmap reference, SkISize tileSize)
    : Task(parent)
    , fName(UnderJoin(parent.name().c_str(), "tilegrid"))
    , fGM(gm)
    , fReference(reference)
    , fTileSize(tileSize)
    {}

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

void TileGridTask::draw() {
    const SkTileGridPicture::TileGridInfo info = {
        fTileSize,
        SkISize::Make(0,0),   // Overlap between adjacent tiles.
        SkIPoint::Make(0,0),  // Offset.
    };
    const SkISize size = fGM->getISize();
    SkTileGridPicture recorded(size.width(), size.height(), info);
    RecordPicture(fGM.get(), &recorded, SkPicture::kUsePathBoundsForClip_RecordingFlag);

    SkBitmap full;
    SetupBitmap(fReference.config(), fGM.get(), &full);
    SkCanvas fullCanvas(full);

    SkBitmap tile;
    tile.setConfig(fReference.config(), fTileSize.width(), fTileSize.height());
    tile.allocPixels();
    SkCanvas tileCanvas(tile);

    SkPaint paint;
    paint.setXfermodeMode(SkXfermode::kSrc_Mode);

    for (int y = 0; y < tiles_needed(full.height(), tile.height()); y++) {
        for (int x = 0; x < tiles_needed(full.width(), tile.width()); x++) {
            SkAutoCanvasRestore ar(&tileCanvas, true/*also save now*/);

            const SkScalar xOffset = SkIntToScalar(x * tile.width()),
                           yOffset = SkIntToScalar(y * tile.height());
            SkMatrix matrix = tileCanvas.getTotalMatrix();
            matrix.postTranslate(-xOffset, -yOffset);
            tileCanvas.setMatrix(matrix);

            recorded.draw(&tileCanvas);
            tileCanvas.flush();
            fullCanvas.drawBitmap(tile, xOffset, yOffset, &paint);
        }
    }

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

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

}  // namespace DM