blob: 6ffe360ea800a1c91c3e1bebce53098e55c7fb9f [file] [log] [blame]
commit-bot@chromium.orga90c6802014-04-30 13:20:45 +00001#include "DMQuiltTask.h"
2#include "DMUtil.h"
3#include "DMWriteTask.h"
4
mtkleine4636aa2014-07-09 13:10:58 -07005#include "SkBBHFactory.h"
commit-bot@chromium.orga90c6802014-04-30 13:20:45 +00006#include "SkCommandLineFlags.h"
7#include "SkPicture.h"
mtklein406654b2014-09-03 15:34:37 -07008#include "SkTaskGroup.h"
commit-bot@chromium.orga90c6802014-04-30 13:20:45 +00009
mtkleine4636aa2014-07-09 13:10:58 -070010DEFINE_bool(quilt, true, "If true, draw GM via a picture into a quilt of small tiles and compare.");
mtklein7cdc1ee2014-07-07 10:41:04 -070011DEFINE_int32(quiltTile, 256, "Dimension of (square) quilt tile.");
commit-bot@chromium.orga90c6802014-04-30 13:20:45 +000012
13namespace DM {
14
mtklein5ad6ee12014-08-11 08:08:43 -070015static SkString suffix(QuiltTask::Backend backend, QuiltTask::BBH bbh) {
16 static const char* kBackends[] = { "default", "skrecord" };
mtklein2a65a232014-08-26 14:07:04 -070017 static const char* kBBHs[] = { "nobbh", "rtree", "tilegrid" };
mtklein5ad6ee12014-08-11 08:08:43 -070018 return SkStringPrintf("%s-%s", kBackends[backend], kBBHs[bbh]);
19}
20
21QuiltTask::QuiltTask(const Task& parent, skiagm::GM* gm, SkBitmap reference,
22 QuiltTask::BBH bbh, QuiltTask::Backend backend)
commit-bot@chromium.orga90c6802014-04-30 13:20:45 +000023 : CpuTask(parent)
mtklein5ad6ee12014-08-11 08:08:43 -070024 , fBBH(bbh)
25 , fBackend(backend)
26 , fName(UnderJoin(parent.name().c_str(), suffix(backend, bbh).c_str()))
commit-bot@chromium.orga90c6802014-04-30 13:20:45 +000027 , fGM(gm)
28 , fReference(reference)
29 {}
30
31static int tiles_needed(int fullDimension, int tileDimension) {
32 return (fullDimension + tileDimension - 1) / tileDimension;
33}
34
mtkleinec2ae582014-07-01 07:46:50 -070035class Tile : public SkRunnable {
36public:
mtklein7cdc1ee2014-07-07 10:41:04 -070037 Tile(int x, int y, const SkPicture& picture, SkBitmap* quilt)
38 : fX(x * FLAGS_quiltTile)
39 , fY(y * FLAGS_quiltTile)
mtkleinec2ae582014-07-01 07:46:50 -070040 , fPicture(picture)
mtklein7cdc1ee2014-07-07 10:41:04 -070041 , fQuilt(quilt) {}
mtkleinec2ae582014-07-01 07:46:50 -070042
43 virtual void run() SK_OVERRIDE {
44 SkBitmap tile;
mtklein7cdc1ee2014-07-07 10:41:04 -070045 fQuilt->extractSubset(&tile, SkIRect::MakeXYWH(fX, fY, FLAGS_quiltTile, FLAGS_quiltTile));
mtkleinec2ae582014-07-01 07:46:50 -070046 SkCanvas tileCanvas(tile);
47
mtklein7cdc1ee2014-07-07 10:41:04 -070048 tileCanvas.translate(SkIntToScalar(-fX), SkIntToScalar(-fY));
robertphillipsc5ba71d2014-09-04 08:42:50 -070049 fPicture.playback(&tileCanvas);
mtkleinec2ae582014-07-01 07:46:50 -070050 tileCanvas.flush();
51
mtkleinec2ae582014-07-01 07:46:50 -070052 delete this;
53 }
54
55private:
56 const int fX, fY;
mtkleinec2ae582014-07-01 07:46:50 -070057 const SkPicture& fPicture;
mtklein7cdc1ee2014-07-07 10:41:04 -070058 SkBitmap* fQuilt;
mtkleinec2ae582014-07-01 07:46:50 -070059};
60
commit-bot@chromium.orga90c6802014-04-30 13:20:45 +000061void QuiltTask::draw() {
mtkleine4636aa2014-07-09 13:10:58 -070062 SkAutoTDelete<SkBBHFactory> factory;
mtklein5ad6ee12014-08-11 08:08:43 -070063 switch (fBBH) {
64 case kNone_BBH: break;
65 case kRTree_BBH:
mtkleine4636aa2014-07-09 13:10:58 -070066 factory.reset(SkNEW(SkRTreeFactory));
67 break;
mtklein5ad6ee12014-08-11 08:08:43 -070068 case kTileGrid_BBH: {
mtkleine4636aa2014-07-09 13:10:58 -070069 const SkTileGridFactory::TileGridInfo tiles = {
70 { FLAGS_quiltTile, FLAGS_quiltTile },
71 /*overlap: */{0, 0},
72 /*offset: */{0, 0},
73 };
74 factory.reset(SkNEW_ARGS(SkTileGridFactory, (tiles)));
75 break;
76 }
mtkleine4636aa2014-07-09 13:10:58 -070077 }
78
79 // A couple GMs draw wrong when using a bounding box hierarchy.
80 // This almost certainly means we have a bug to fix, but for now
81 // just draw without a bounding box hierarchy.
82 if (fGM->getFlags() & skiagm::GM::kNoBBH_Flag) {
83 factory.reset(NULL);
84 }
85
86 SkAutoTUnref<const SkPicture> recorded(
mtklein5ad6ee12014-08-11 08:08:43 -070087 RecordPicture(fGM.get(), factory.get(), kSkRecord_Backend == fBackend));
commit-bot@chromium.orga90c6802014-04-30 13:20:45 +000088
89 SkBitmap full;
commit-bot@chromium.org26642072014-05-15 17:33:31 +000090 AllocatePixels(fReference, &full);
commit-bot@chromium.orga90c6802014-04-30 13:20:45 +000091
mtkleine4636aa2014-07-09 13:10:58 -070092 if (fGM->getFlags() & skiagm::GM::kSkipTiled_Flag) {
93 // Some GMs don't draw exactly the same when tiled. Draw them in one go.
94 SkCanvas canvas(full);
robertphillipsc5ba71d2014-09-04 08:42:50 -070095 recorded->playback(&canvas);
mtkleine4636aa2014-07-09 13:10:58 -070096 canvas.flush();
97 } else {
98 // Draw tiles in parallel into the same bitmap, simulating aggressive impl-side painting.
mtklein406654b2014-09-03 15:34:37 -070099 SkTaskGroup tg;
mtkleine4636aa2014-07-09 13:10:58 -0700100 for (int y = 0; y < tiles_needed(full.height(), FLAGS_quiltTile); y++) {
101 for (int x = 0; x < tiles_needed(full.width(), FLAGS_quiltTile); x++) {
102 // Deletes itself when done.
mtklein406654b2014-09-03 15:34:37 -0700103 tg.add(new Tile(x, y, *recorded, &full));
mtkleine4636aa2014-07-09 13:10:58 -0700104 }
commit-bot@chromium.orga90c6802014-04-30 13:20:45 +0000105 }
106 }
107
commit-bot@chromium.orga90c6802014-04-30 13:20:45 +0000108 if (!BitmapsEqual(full, fReference)) {
109 this->fail();
110 this->spawnChild(SkNEW_ARGS(WriteTask, (*this, full)));
111 }
112}
113
114bool QuiltTask::shouldSkip() const {
115 if (fGM->getFlags() & skiagm::GM::kSkipPicture_Flag) {
116 return true;
117 }
commit-bot@chromium.orga90c6802014-04-30 13:20:45 +0000118 return !FLAGS_quilt;
119}
120
121} // namespace DM