commit-bot@chromium.org | a90c680 | 2014-04-30 13:20:45 +0000 | [diff] [blame] | 1 | #include "DMQuiltTask.h" |
| 2 | #include "DMUtil.h" |
| 3 | #include "DMWriteTask.h" |
| 4 | |
| 5 | #include "SkCommandLineFlags.h" |
| 6 | #include "SkPicture.h" |
mtklein | ec2ae58 | 2014-07-01 07:46:50 -0700 | [diff] [blame] | 7 | #include "SkThreadPool.h" |
commit-bot@chromium.org | a90c680 | 2014-04-30 13:20:45 +0000 | [diff] [blame] | 8 | |
| 9 | DEFINE_bool(quilt, true, "If true, draw into a quilt of small tiles and compare."); |
mtklein | 7cdc1ee | 2014-07-07 10:41:04 -0700 | [diff] [blame^] | 10 | DEFINE_int32(quiltTile, 256, "Dimension of (square) quilt tile."); |
mtklein | ec2ae58 | 2014-07-01 07:46:50 -0700 | [diff] [blame] | 11 | DEFINE_bool(quiltThreaded, false, "If true, draw quilt tiles with multiple threads."); |
commit-bot@chromium.org | a90c680 | 2014-04-30 13:20:45 +0000 | [diff] [blame] | 12 | |
mtklein | 7cdc1ee | 2014-07-07 10:41:04 -0700 | [diff] [blame^] | 13 | static const char* kSuffixes[] = { "quilt", "quilt_skr" }; |
| 14 | |
commit-bot@chromium.org | a90c680 | 2014-04-30 13:20:45 +0000 | [diff] [blame] | 15 | namespace DM { |
| 16 | |
mtklein | 7cdc1ee | 2014-07-07 10:41:04 -0700 | [diff] [blame^] | 17 | QuiltTask::QuiltTask(const Task& parent, skiagm::GM* gm, SkBitmap reference, QuiltTask::Mode mode) |
commit-bot@chromium.org | a90c680 | 2014-04-30 13:20:45 +0000 | [diff] [blame] | 18 | : CpuTask(parent) |
mtklein | 7cdc1ee | 2014-07-07 10:41:04 -0700 | [diff] [blame^] | 19 | , fMode(mode) |
| 20 | , fName(UnderJoin(parent.name().c_str(), kSuffixes[mode])) |
commit-bot@chromium.org | a90c680 | 2014-04-30 13:20:45 +0000 | [diff] [blame] | 21 | , fGM(gm) |
| 22 | , fReference(reference) |
| 23 | {} |
| 24 | |
| 25 | static int tiles_needed(int fullDimension, int tileDimension) { |
| 26 | return (fullDimension + tileDimension - 1) / tileDimension; |
| 27 | } |
| 28 | |
mtklein | ec2ae58 | 2014-07-01 07:46:50 -0700 | [diff] [blame] | 29 | class Tile : public SkRunnable { |
| 30 | public: |
mtklein | 7cdc1ee | 2014-07-07 10:41:04 -0700 | [diff] [blame^] | 31 | Tile(int x, int y, const SkPicture& picture, SkBitmap* quilt) |
| 32 | : fX(x * FLAGS_quiltTile) |
| 33 | , fY(y * FLAGS_quiltTile) |
mtklein | ec2ae58 | 2014-07-01 07:46:50 -0700 | [diff] [blame] | 34 | , fPicture(picture) |
mtklein | 7cdc1ee | 2014-07-07 10:41:04 -0700 | [diff] [blame^] | 35 | , fQuilt(quilt) {} |
mtklein | ec2ae58 | 2014-07-01 07:46:50 -0700 | [diff] [blame] | 36 | |
| 37 | virtual void run() SK_OVERRIDE { |
| 38 | SkBitmap tile; |
mtklein | 7cdc1ee | 2014-07-07 10:41:04 -0700 | [diff] [blame^] | 39 | fQuilt->extractSubset(&tile, SkIRect::MakeXYWH(fX, fY, FLAGS_quiltTile, FLAGS_quiltTile)); |
mtklein | ec2ae58 | 2014-07-01 07:46:50 -0700 | [diff] [blame] | 40 | SkCanvas tileCanvas(tile); |
| 41 | |
mtklein | 7cdc1ee | 2014-07-07 10:41:04 -0700 | [diff] [blame^] | 42 | tileCanvas.translate(SkIntToScalar(-fX), SkIntToScalar(-fY)); |
mtklein | ec2ae58 | 2014-07-01 07:46:50 -0700 | [diff] [blame] | 43 | fPicture.draw(&tileCanvas); |
| 44 | tileCanvas.flush(); |
| 45 | |
mtklein | ec2ae58 | 2014-07-01 07:46:50 -0700 | [diff] [blame] | 46 | delete this; |
| 47 | } |
| 48 | |
| 49 | private: |
| 50 | const int fX, fY; |
mtklein | ec2ae58 | 2014-07-01 07:46:50 -0700 | [diff] [blame] | 51 | const SkPicture& fPicture; |
mtklein | 7cdc1ee | 2014-07-07 10:41:04 -0700 | [diff] [blame^] | 52 | SkBitmap* fQuilt; |
mtklein | ec2ae58 | 2014-07-01 07:46:50 -0700 | [diff] [blame] | 53 | }; |
| 54 | |
commit-bot@chromium.org | a90c680 | 2014-04-30 13:20:45 +0000 | [diff] [blame] | 55 | void QuiltTask::draw() { |
mtklein | 7cdc1ee | 2014-07-07 10:41:04 -0700 | [diff] [blame^] | 56 | SkAutoTUnref<SkPicture> recorded( |
| 57 | RecordPicture(fGM.get(), NULL/*bbh factory*/, kSkRecord_Mode == fMode)); |
commit-bot@chromium.org | a90c680 | 2014-04-30 13:20:45 +0000 | [diff] [blame] | 58 | |
| 59 | SkBitmap full; |
commit-bot@chromium.org | 2664207 | 2014-05-15 17:33:31 +0000 | [diff] [blame] | 60 | AllocatePixels(fReference, &full); |
commit-bot@chromium.org | a90c680 | 2014-04-30 13:20:45 +0000 | [diff] [blame] | 61 | |
mtklein | 7cdc1ee | 2014-07-07 10:41:04 -0700 | [diff] [blame^] | 62 | int threads = 0; |
| 63 | if (kSkRecord_Mode == fMode || FLAGS_quiltThreaded) { |
| 64 | threads = SkThreadPool::kThreadPerCore; |
| 65 | } |
| 66 | SkThreadPool pool(threads); |
commit-bot@chromium.org | a90c680 | 2014-04-30 13:20:45 +0000 | [diff] [blame] | 67 | |
mtklein | ec2ae58 | 2014-07-01 07:46:50 -0700 | [diff] [blame] | 68 | 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. |
mtklein | 7cdc1ee | 2014-07-07 10:41:04 -0700 | [diff] [blame^] | 71 | pool.add(new Tile(x, y, *recorded, &full)); |
commit-bot@chromium.org | a90c680 | 2014-04-30 13:20:45 +0000 | [diff] [blame] | 72 | } |
| 73 | } |
| 74 | |
mtklein | ec2ae58 | 2014-07-01 07:46:50 -0700 | [diff] [blame] | 75 | pool.wait(); |
mtklein | ec2ae58 | 2014-07-01 07:46:50 -0700 | [diff] [blame] | 76 | |
commit-bot@chromium.org | a90c680 | 2014-04-30 13:20:45 +0000 | [diff] [blame] | 77 | if (!BitmapsEqual(full, fReference)) { |
| 78 | this->fail(); |
| 79 | this->spawnChild(SkNEW_ARGS(WriteTask, (*this, full))); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | bool 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 |