1#include "DMQuiltTask.h"
2#include "DMUtil.h"
3#include "DMWriteTask.h"
4
5#include "SkCommandLineFlags.h"
6#include "SkPicture.h"
7
8DEFINE_bool(quilt, true, "If true, draw into a quilt of small tiles and compare.");
9DEFINE_int32(quiltTile, 16, "Dimension of (square) quilt tile.");
10
11namespace DM {
12
13QuiltTask::QuiltTask(const Task& parent, skiagm::GM* gm, SkBitmap reference)
14    : CpuTask(parent)
15    , fName(UnderJoin(parent.name().c_str(), "quilt"))
16    , fGM(gm)
17    , fReference(reference)
18    {}
19
20static int tiles_needed(int fullDimension, int tileDimension) {
21    return (fullDimension + tileDimension - 1) / tileDimension;
22}
23
24void QuiltTask::draw() {
25    SkAutoTUnref<SkPicture> recorded(RecordPicture(fGM.get()));
26
27    SkBitmap full;
28    AllocatePixels(fReference, &full);
29    SkCanvas fullCanvas(full);
30
31    SkBitmap tile;
32    tile.allocPixels(SkImageInfo::Make(FLAGS_quiltTile, FLAGS_quiltTile,
33                                       fReference.colorType(), kPremul_SkAlphaType));
34    SkCanvas tileCanvas(tile);
35
36    for (int y = 0; y < tiles_needed(full.height(), tile.height()); y++) {
37        for (int x = 0; x < tiles_needed(full.width(), tile.width()); x++) {
38            SkAutoCanvasRestore ar(&tileCanvas, true/*also save now*/);
39
40            const SkScalar xOffset = SkIntToScalar(x * tile.width()),
41                           yOffset = SkIntToScalar(y * tile.height());
42            SkMatrix matrix = tileCanvas.getTotalMatrix();
43            matrix.postTranslate(-xOffset, -yOffset);
44            tileCanvas.setMatrix(matrix);
45
46            recorded->draw(&tileCanvas);
47            tileCanvas.flush();
48            fullCanvas.drawBitmap(tile, xOffset, yOffset, NULL);
49        }
50    }
51
52    if (!BitmapsEqual(full, fReference)) {
53        this->fail();
54        this->spawnChild(SkNEW_ARGS(WriteTask, (*this, full)));
55    }
56}
57
58bool QuiltTask::shouldSkip() const {
59    if (fGM->getFlags() & skiagm::GM::kSkipPicture_Flag) {
60        return true;
61    }
62    if (fGM->getFlags() & skiagm::GM::kSkipTiled_Flag) {
63        return true;
64    }
65    return !FLAGS_quilt;
66}
67
68}  // namespace DM
69