blob: 960ca86747b3dec47eb3e54500b761f7c7ffc106 [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
5#include "SkCommandLineFlags.h"
6#include "SkPicture.h"
mtkleinec2ae582014-07-01 07:46:50 -07007#include "SkThreadPool.h"
commit-bot@chromium.orga90c6802014-04-30 13:20:45 +00008
9DEFINE_bool(quilt, true, "If true, draw into a quilt of small tiles and compare.");
mtklein7cdc1ee2014-07-07 10:41:04 -070010DEFINE_int32(quiltTile, 256, "Dimension of (square) quilt tile.");
mtkleinec2ae582014-07-01 07:46:50 -070011DEFINE_bool(quiltThreaded, false, "If true, draw quilt tiles with multiple threads.");
commit-bot@chromium.orga90c6802014-04-30 13:20:45 +000012
mtklein7cdc1ee2014-07-07 10:41:04 -070013static const char* kSuffixes[] = { "quilt", "quilt_skr" };
14
commit-bot@chromium.orga90c6802014-04-30 13:20:45 +000015namespace DM {
16
mtklein7cdc1ee2014-07-07 10:41:04 -070017QuiltTask::QuiltTask(const Task& parent, skiagm::GM* gm, SkBitmap reference, QuiltTask::Mode mode)
commit-bot@chromium.orga90c6802014-04-30 13:20:45 +000018 : CpuTask(parent)
mtklein7cdc1ee2014-07-07 10:41:04 -070019 , fMode(mode)
20 , fName(UnderJoin(parent.name().c_str(), kSuffixes[mode]))
commit-bot@chromium.orga90c6802014-04-30 13:20:45 +000021 , fGM(gm)
22 , fReference(reference)
23 {}
24
25static int tiles_needed(int fullDimension, int tileDimension) {
26 return (fullDimension + tileDimension - 1) / tileDimension;
27}
28
mtkleinec2ae582014-07-01 07:46:50 -070029class Tile : public SkRunnable {
30public:
mtklein7cdc1ee2014-07-07 10:41:04 -070031 Tile(int x, int y, const SkPicture& picture, SkBitmap* quilt)
32 : fX(x * FLAGS_quiltTile)
33 , fY(y * FLAGS_quiltTile)
mtkleinec2ae582014-07-01 07:46:50 -070034 , fPicture(picture)
mtklein7cdc1ee2014-07-07 10:41:04 -070035 , fQuilt(quilt) {}
mtkleinec2ae582014-07-01 07:46:50 -070036
37 virtual void run() SK_OVERRIDE {
38 SkBitmap tile;
mtklein7cdc1ee2014-07-07 10:41:04 -070039 fQuilt->extractSubset(&tile, SkIRect::MakeXYWH(fX, fY, FLAGS_quiltTile, FLAGS_quiltTile));
mtkleinec2ae582014-07-01 07:46:50 -070040 SkCanvas tileCanvas(tile);
41
mtklein7cdc1ee2014-07-07 10:41:04 -070042 tileCanvas.translate(SkIntToScalar(-fX), SkIntToScalar(-fY));
mtkleinec2ae582014-07-01 07:46:50 -070043 fPicture.draw(&tileCanvas);
44 tileCanvas.flush();
45
mtkleinec2ae582014-07-01 07:46:50 -070046 delete this;
47 }
48
49private:
50 const int fX, fY;
mtkleinec2ae582014-07-01 07:46:50 -070051 const SkPicture& fPicture;
mtklein7cdc1ee2014-07-07 10:41:04 -070052 SkBitmap* fQuilt;
mtkleinec2ae582014-07-01 07:46:50 -070053};
54
commit-bot@chromium.orga90c6802014-04-30 13:20:45 +000055void QuiltTask::draw() {
mtklein7cdc1ee2014-07-07 10:41:04 -070056 SkAutoTUnref<SkPicture> recorded(
57 RecordPicture(fGM.get(), NULL/*bbh factory*/, kSkRecord_Mode == fMode));
commit-bot@chromium.orga90c6802014-04-30 13:20:45 +000058
59 SkBitmap full;
commit-bot@chromium.org26642072014-05-15 17:33:31 +000060 AllocatePixels(fReference, &full);
commit-bot@chromium.orga90c6802014-04-30 13:20:45 +000061
mtklein7cdc1ee2014-07-07 10:41:04 -070062 int threads = 0;
63 if (kSkRecord_Mode == fMode || FLAGS_quiltThreaded) {
64 threads = SkThreadPool::kThreadPerCore;
65 }
66 SkThreadPool pool(threads);
commit-bot@chromium.orga90c6802014-04-30 13:20:45 +000067
mtkleinec2ae582014-07-01 07:46:50 -070068 for (int y = 0; y < tiles_needed(full.height(), FLAGS_quiltTile); y++) {
69 for (int x = 0; x < tiles_needed(full.width(), FLAGS_quiltTile); x++) {
70 // Deletes itself when done.
mtklein7cdc1ee2014-07-07 10:41:04 -070071 pool.add(new Tile(x, y, *recorded, &full));
commit-bot@chromium.orga90c6802014-04-30 13:20:45 +000072 }
73 }
74
mtkleinec2ae582014-07-01 07:46:50 -070075 pool.wait();
mtkleinec2ae582014-07-01 07:46:50 -070076
commit-bot@chromium.orga90c6802014-04-30 13:20:45 +000077 if (!BitmapsEqual(full, fReference)) {
78 this->fail();
79 this->spawnChild(SkNEW_ARGS(WriteTask, (*this, full)));
80 }
81}
82
83bool QuiltTask::shouldSkip() const {
84 if (fGM->getFlags() & skiagm::GM::kSkipPicture_Flag) {
85 return true;
86 }
87 if (fGM->getFlags() & skiagm::GM::kSkipTiled_Flag) {
88 return true;
89 }
90 return !FLAGS_quilt;
91}
92
93} // namespace DM